1 | <?php |
||
18 | class Engine |
||
19 | { |
||
20 | private $objectivePlayer; |
||
21 | |||
22 | private $maxDepth; |
||
23 | |||
24 | /** |
||
25 | * @var ?Analytics Only available after run, contains statistics about the |
||
26 | * execution. |
||
27 | */ |
||
28 | private $analytics; |
||
29 | |||
30 | /** |
||
31 | * @param Player $objectivePlayer The player to play as |
||
32 | * @param int $maxDepth How far ahead should the engine look? |
||
33 | */ |
||
34 | 16 | public function __construct(Player $objectivePlayer, int $maxDepth = 3) |
|
39 | |||
40 | /** |
||
41 | * Evaluate possible decisions and take the best one |
||
42 | * @param GameState $state Current state of the game for which there needs |
||
43 | * to be made a decision. This implicitly means that the objective player |
||
44 | * currently must have its turn in the GameState. |
||
45 | * @return GameState The state resulting after the engine made its decision. |
||
46 | */ |
||
47 | 16 | public function decide(GameState $state): GameState |
|
48 | { |
||
49 | 16 | if (!$state->getNextPlayer()->equals($this->objectivePlayer)) { |
|
50 | 1 | throw new BadMethodCallException('It is not this players turn'); |
|
51 | } |
||
52 | 15 | if (empty($state->getPossibleMoves())) { |
|
53 | 1 | throw new RuntimeException('There are no possible moves'); |
|
54 | } |
||
55 | |||
56 | 14 | $rootNode = new DecisionNode( |
|
57 | 14 | $this->objectivePlayer, |
|
58 | 14 | $state, |
|
59 | 14 | $this->maxDepth, |
|
60 | 14 | NodeType::MAX(), |
|
61 | 14 | AlphaBeta::initial() |
|
62 | ); |
||
63 | |||
64 | 14 | $moveWithEvaluation = $rootNode->traverseGameTree(); |
|
65 | 14 | $this->analytics = $moveWithEvaluation->analytics; |
|
66 | 14 | if ($moveWithEvaluation->move === null) { |
|
67 | 1 | throw new LogicException('Could not find move even though there are moves. Is the maxdepth parameter correct?'); |
|
68 | } |
||
69 | 13 | return $moveWithEvaluation->move; |
|
70 | } |
||
71 | |||
72 | 1 | public function getAnalytics(): Analytics |
|
79 | } |
||
80 |