1 | <?php |
||
14 | class DecisionNode |
||
15 | { |
||
16 | /** |
||
17 | * @var Player The player to optimize for. |
||
18 | */ |
||
19 | private $objectivePlayer; |
||
20 | |||
21 | /** |
||
22 | * @var GameState The current GameState to base future decisions on. |
||
23 | */ |
||
24 | private $state; |
||
25 | |||
26 | /** |
||
27 | * @var int Limit on how deep we can continue to search, recursion limiter. |
||
28 | */ |
||
29 | private $depthLeft; |
||
30 | |||
31 | /** |
||
32 | * @var NodeType Whether we are a min-node or a max-node. This enables the |
||
33 | * caller to select either the most favorable or the least favorable |
||
34 | * outcome. |
||
35 | */ |
||
36 | private $type; |
||
37 | |||
38 | /** |
||
39 | * @param Player $objectivePlayer The Player to optimize for |
||
40 | * @param GameState $state Current GameState to base decisions on |
||
41 | * @param int $depthLeft Recursion limiter |
||
42 | * @param NodeType $type Signifies whether to minimize or maximize the score |
||
43 | */ |
||
44 | 13 | public function __construct(Player $objectivePlayer, GameState $state, int $depthLeft, NodeType $type) |
|
51 | |||
52 | /** |
||
53 | * Determine the ideal decision for this node |
||
54 | * This means either the best or the worst possible outcome for the |
||
55 | * objective player, based on who is actually playing. (If the objective |
||
56 | * player is currently playing, we take the best outcome, otherwise we take |
||
57 | * the worst. This reflects that the opponent also plays optimally.) |
||
58 | * @return DecisionWithScore |
||
59 | */ |
||
60 | 13 | public function decide(): DecisionWithScore |
|
79 | |||
80 | /** |
||
81 | * Formulate the resulting decision, considering we do not look any further |
||
82 | * The reason for not looking further can either be due to hitting the |
||
83 | * recursion limit or because the game has actually concluded. |
||
84 | * @return DecisionWithScore |
||
85 | */ |
||
86 | 13 | private function makeLeafResult(): DecisionWithScore |
|
93 | |||
94 | /** |
||
95 | * Apply a move and evaluate the outcome |
||
96 | * @param GameState $stateAfterMove The result of taking the move |
||
97 | * @param DecisionWithScore $bestDecisionWithScoreSoFar Best result |
||
98 | * encountered so far. TODO: Can probably be cleaned up by moving that logic |
||
99 | * to the caller. |
||
100 | * @return DecisionWithScore |
||
101 | */ |
||
102 | 12 | private function considerMove( |
|
120 | |||
121 | /** |
||
122 | * Recursively evaluate a child decision |
||
123 | * @param GameState $stateAfterMove The GameState that was created as a |
||
124 | * result of the current move. |
||
125 | * @return DecisionWithScore |
||
126 | */ |
||
127 | 12 | private function considerNextMove(GameState $stateAfterMove): DecisionWithScore |
|
138 | |||
139 | /** |
||
140 | * Take the best of the two operands |
||
141 | * The meaning of "best" is decided by the "ideal" member variable |
||
142 | * comparator |
||
143 | * @param DecisionWithScore $new |
||
144 | * @param DecisionWithScore $current |
||
145 | * @param bool $replaced Set to true if the second operand was better |
||
146 | * @return DecisionWithScore |
||
147 | */ |
||
148 | 12 | private function replaceIfBetter( |
|
170 | } |
||
171 |