AlphaBeta::update()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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