AlphaBeta   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 44
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A initial() 0 4 1
A update() 0 8 3
A isPositiveRange() 0 4 1
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