1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\PuzzleSolver; |
4
|
|
|
|
5
|
|
|
use function assert; |
6
|
|
|
use function strtolower; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Find |
10
|
|
|
* |
11
|
|
|
* Value object that provides information on the kind of goal for the puzzle. |
12
|
|
|
* Are we looking for just one solution? All of them? Etc. |
13
|
|
|
* |
14
|
|
|
* @author Stratadox |
15
|
|
|
*/ |
16
|
|
|
final class Find |
17
|
|
|
{ |
18
|
|
|
private const THE_ONLY_SOLUTION = 0; |
19
|
|
|
private const A_BEST_SOLUTION = 1; |
20
|
|
|
private const ALL_BEST_SOLUTIONS = 2; |
21
|
|
|
private const ALL_LOOPLESS_SOLUTIONS = 3; |
22
|
|
|
private const MAP = [ |
23
|
|
|
'the only solution' => FIND::THE_ONLY_SOLUTION, |
24
|
|
|
'a best solution' => FIND::A_BEST_SOLUTION, |
25
|
|
|
'all best solutions' => FIND::ALL_BEST_SOLUTIONS, |
26
|
|
|
'all loopless solutions' => FIND::ALL_LOOPLESS_SOLUTIONS, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** @var int */ |
30
|
|
|
private $solutionType; |
31
|
|
|
|
32
|
|
|
public function __construct(int $solutionType) |
33
|
|
|
{ |
34
|
|
|
$this->solutionType = $solutionType; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function theOnlySolution(): self |
38
|
|
|
{ |
39
|
|
|
return new self(Find::THE_ONLY_SOLUTION); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function aBestSolution(): self |
43
|
|
|
{ |
44
|
|
|
return new self(Find::A_BEST_SOLUTION); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function allBestSolutions(): self |
48
|
|
|
{ |
49
|
|
|
return new self(Find::ALL_BEST_SOLUTIONS); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function allLooplessSolutions(): self |
53
|
|
|
{ |
54
|
|
|
return new self(Find::ALL_LOOPLESS_SOLUTIONS); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function fromString(string $type): self |
58
|
|
|
{ |
59
|
|
|
$type = strtolower($type); |
60
|
|
|
assert(isset(Find::MAP[$type])); |
61
|
|
|
return new self(Find::MAP[$type]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function singleSolution(): bool |
65
|
|
|
{ |
66
|
|
|
return $this->solutionType === self::THE_ONLY_SOLUTION |
67
|
|
|
|| $this->solutionType === self::A_BEST_SOLUTION; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function onlyBest(): bool |
71
|
|
|
{ |
72
|
|
|
return $this->solutionType === self::ALL_BEST_SOLUTIONS |
73
|
|
|
|| $this->solutionType === self::A_BEST_SOLUTION; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|