Test Failed
Pull Request — master (#7)
by Thijs
02:13
created

AlphaBeta::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
1
<?php
2
3
namespace lucidtaz\minimax\engine;
4
5
/**
6
 * Alpha and beta pair for use in alpha/beta pruning
7
 */
8
class AlphaBeta
9
{
10
    /**
11
     * @var float
12
     */
13
    public $alpha;
14
15
    /**
16
     * @var float
17
     */
18
    public $beta;
19
20
    public function __construct(float $alpha, float $beta)
21
    {
22
        $this->alpha = $alpha;
23
        $this->beta = $beta;
24
    }
25
26
    public static function initial(): AlphaBeta
27
    {
28
        return new static(-INF, INF);
29
    }
30
31
    /**
32
     * Update the constraint with new information
33
     */
34
    public function update(Evaluation $evaluation, NodeType $nodeType)
35
    {
36
        if ($nodeType == NodeType::MAX()) {
37
            $this->alpha = max($this->alpha, $evaluation->score);
38
        } elseif ($nodeType == NodeType::MIN()) {
39
            $this->beta = min($this->beta, $evaluation->score);
40
        }
41
    }
42
43
    /**
44
     * Check whether the value ranges (alpha..inf and -inf..beta) still overlap
45
     * If not, the conclusion is that the game tree branch can be pruned.
46
     */
47
    public function isPositiveRange(): bool
48
    {
49
        return $this->alpha < $this->beta;
50
    }
51
52
    public function __toString(): string
53
    {
54
        return "($this->alpha, $this->beta)";
55
    }
56
}
57