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 | 12 | public function __construct(Player $objectivePlayer, GameState $state, int $depthLeft, NodeType $type) |
|
51 | |||
52 | /** |
||
53 | * Determine the ideal move 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 | */ |
||
59 | 12 | public function traverseGameTree(): TraversalResult |
|
84 | |||
85 | /** |
||
86 | * Formulate the evaluation result, this node being a leaf node |
||
87 | */ |
||
88 | 12 | private function makeLeafResult(): EvaluationResult |
|
95 | |||
96 | /** |
||
97 | * Recursively evaluate a child decision |
||
98 | * Apply a move and evaluate the outcome |
||
99 | * @param GameState $stateAfterMove The GameState that was created as a |
||
100 | * result of a possible move. |
||
101 | */ |
||
102 | 12 | private function getChildResult(GameState $stateAfterMove): EvaluationResult |
|
113 | |||
114 | /** |
||
115 | * Compare two evaluation results |
||
116 | * The meaning of "best" is decided by the "ideal" member variable |
||
117 | * comparator |
||
118 | */ |
||
119 | 11 | private function isIdealOver(EvaluationResult $a, EvaluationResult $b): bool |
|
127 | } |
||
128 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: