Completed
Push — master ( c94f97...665cde )
by Thijs
03:59
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\engine;
4
5
use Closure;
6
use lucidtaz\minimax\game\Decision;
7
8
class DecisionWithScore
9
{
10
    /**
11
     * @var Decision
12
     */
13
    public $decision = null;
14
15
    /**
16
     * @var float
17
     */
18
    public $score;
19
20
    /**
21
     * @var integer How deep in the execution tree this result was found. Higher
22
     * means earlier. This is to prefer earlier solutions to later solutions
23
     * with the same score.
24
     */
25
    public $age;
26
27 10
    public function isBetterThan(DecisionWithScore $other): bool
28
    {
29 10
        if (abs($this->score - $other->score) < 0.1) {
30 10
            return $this->age > $other->age;
31
        }
32 8
        return $this->score > $other->score;
33
    }
34
35 12
    public static function getBestComparator(): Closure
36
    {
37
        return function (DecisionWithScore $a, DecisionWithScore $b) {
38 10
            return $a->isBetterThan($b) ? $a : $b;
39 12
        };
40
    }
41
42
    public static function getWorstComparator(): Closure
43
    {
44 11
        return function (DecisionWithScore $a, DecisionWithScore $b) {
45 10
            return $b->isBetterThan($a) ? $a : $b;
46 11
        };
47
    }
48
}
49