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 | * @var AlphaBeta Constraints for alpha-beta pruning |
||
40 | */ |
||
41 | private $alphaBeta; |
||
42 | |||
43 | /** |
||
44 | * @param Player $objectivePlayer The Player to optimize for |
||
45 | * @param GameState $state Current GameState to base decisions on |
||
46 | * @param int $depthLeft Recursion limiter |
||
47 | * @param NodeType $type Signifies whether to minimize or maximize the score |
||
48 | * @param AlphaBeta $alphaBeta Range of potential scores to check |
||
49 | */ |
||
50 | public function __construct(Player $objectivePlayer, GameState $state, int $depthLeft, NodeType $type, AlphaBeta $alphaBeta) |
||
58 | |||
59 | /** |
||
60 | * Determine the ideal move for this node |
||
61 | * This means either the best or the worst possible outcome for the |
||
62 | * objective player, based on who is actually playing. (If the objective |
||
63 | * player is currently playing, we take the best outcome, otherwise we take |
||
64 | * the worst. This reflects that the opponent also plays optimally.) |
||
65 | */ |
||
66 | public function traverseGameTree(): TraversalResult |
||
96 | |||
97 | /** |
||
98 | * Formulate the evaluation, this node being a leaf node |
||
99 | */ |
||
100 | private function makeLeafEvaluation(): Evaluation |
||
107 | |||
108 | /** |
||
109 | * Recursively evaluate a child decision |
||
110 | * Apply a move and evaluate the outcome |
||
111 | * @param GameState $stateAfterMove The GameState that was created as a |
||
112 | * result of a possible move. |
||
113 | */ |
||
114 | private function getChildResult(GameState $stateAfterMove): TraversalResult |
||
126 | |||
127 | /** |
||
128 | * Compare two evaluations |
||
129 | * The meaning of "best" is decided by the "ideal" member variable |
||
130 | * comparator |
||
131 | */ |
||
132 | private function isIdealOver(Evaluation $a, Evaluation $b): bool |
||
140 | } |
||
141 |
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: