Completed
Push — master ( 73004b...66a620 )
by Thijs
02:37
created

Engine::considerMove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2
Metric Value
dl 0
loc 19
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 12
nc 2
nop 5
crap 2
1
<?php
2
3
namespace lucidtaz\minimax;
4
5
use BadMethodCallException;
6
use RuntimeException;
7
8
class Engine
9
{
10
    private $objectivePlayer;
11
12
    private $maxDepth;
13
14 12
    public function __construct(Player $objectivePlayer, int $maxDepth = 3)
15
    {
16 12
        $this->objectivePlayer = $objectivePlayer;
17 12
        $this->maxDepth = $maxDepth;
18 12
    }
19
20
    /**
21
     * Evaluate possible decisions and take the best one
22
     */
23 12
    public function decide(GameState $state): Decision
24
    {
25 12
        if (!$state->getNextPlayer()->equals($this->objectivePlayer)) {
26 1
            throw new BadMethodCallException('It is not this players turn');
27
        }
28 11
        $topLevelNode = new DecisionPoint(
29 11
            $this->objectivePlayer,
30
            $state,
31 11
            $this->maxDepth,
32 11
            DecisionWithScore::getBestComparator()
33
        );
34 11
        $decisionWithScore = $topLevelNode->decide();
35 11
        if ($decisionWithScore->decision === null) {
36 1
            throw new RuntimeException('There are no possible moves');
37
        }
38 10
        return $decisionWithScore->decision;
39
    }
40
}
41