Completed
Push — master ( 73004b...66a620 )
by Thijs
02:37
created

DecisionWithScore::getWorstComparator()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace lucidtaz\minimax;
4
5
class DecisionWithScore
6
{
7
    /**
8
     * @var Decision
9
     */
10
    public $decision = null;
11
12
    /**
13
     * @var float
14
     */
15
    public $score;
16
17
    /**
18
     * @var integer How deep in the execution tree this result was found. Higher
19
     * means earlier. This is to prefer earlier solutions to later solutions
20
     * with the same score.
21
     */
22
    public $age;
23
24 9
    public function isBetterThan(DecisionWithScore $other): bool
25
    {
26 9
        if (abs($this->score - $other->score) < 0.1) {
27 9
            return $this->age > $other->age;
28
        }
29 7
        return $this->score > $other->score;
30
    }
31
32 11
    public static function getBestComparator(): \Closure
33
    {
34
        return function (DecisionWithScore $a, DecisionWithScore $b) {
35 9
            return $a->isBetterThan($b) ? $a : $b;
36 11
        };
37
    }
38
39
    public static function getWorstComparator(): \Closure
40
    {
41 10
        return function (DecisionWithScore $a, DecisionWithScore $b) {
42 9
            return $b->isBetterThan($a) ? $a : $b;
43 10
        };
44
    }
45
}
46