1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\PuzzleSolver; |
4
|
|
|
|
5
|
|
|
use function assert; |
6
|
|
|
use const PHP_EOL; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Universal Solver |
10
|
|
|
* |
11
|
|
|
* Utility to easily describe puzzles and produce a corresponding puzzle solver. |
12
|
|
|
* |
13
|
|
|
* @author Stratadox |
14
|
|
|
*/ |
15
|
|
|
final class UniversalSolver implements SolverBuilder |
16
|
|
|
{ |
17
|
|
|
/** @var SolverFactory */ |
18
|
|
|
private $create; |
19
|
|
|
/** @var Find|null */ |
20
|
|
|
private $goal; |
21
|
|
|
/** @var bool */ |
22
|
|
|
private $weightedMoves = false; |
23
|
|
|
/** @var bool */ |
24
|
|
|
private $exhausting = false; |
25
|
|
|
/** @var Heuristic|null */ |
26
|
|
|
private $heuristic; |
27
|
|
|
/** @var string|null */ |
28
|
|
|
private $loggingFile; |
29
|
|
|
/** @var string|null */ |
30
|
|
|
private $logSeparator; |
31
|
|
|
/** @var int */ |
32
|
|
|
private $iterationInterval = 0; |
33
|
|
|
|
34
|
|
|
public function __construct(SolverFactory $solverFactory, ?Find $goal) |
35
|
|
|
{ |
36
|
|
|
$this->create = $solverFactory; |
37
|
|
|
$this->goal = $goal; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function forAPuzzle(): SolverBuilder |
41
|
|
|
{ |
42
|
|
|
return new self(new PuzzleSolverFactory(), null); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function aimingTo(Find $goal): SolverBuilder |
46
|
|
|
{ |
47
|
|
|
return new self(new PuzzleSolverFactory(), $goal); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function withGoalTo(Find $goal): SolverBuilder |
51
|
|
|
{ |
52
|
|
|
$new = clone $this; |
53
|
|
|
$new->goal = $goal; |
54
|
|
|
return $new; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function withWeightedMoves(): SolverBuilder |
58
|
|
|
{ |
59
|
|
|
$new = clone $this; |
60
|
|
|
$new->weightedMoves = true; |
61
|
|
|
return $new; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function whereMovesEventuallyRunOut(): SolverBuilder |
65
|
|
|
{ |
66
|
|
|
$new = clone $this; |
67
|
|
|
$new->exhausting = true; |
68
|
|
|
return $new; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function withHeuristic(Heuristic $heuristic): SolverBuilder |
72
|
|
|
{ |
73
|
|
|
$new = clone $this; |
74
|
|
|
$new->heuristic = $heuristic; |
75
|
|
|
return $new; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function withLogging(string $file, string $separator, int $timeout = 0): SolverBuilder |
79
|
|
|
{ |
80
|
|
|
$new = clone $this; |
81
|
|
|
$new->loggingFile = $file; |
82
|
|
|
$new->logSeparator = $separator; |
83
|
|
|
$new->iterationInterval = $timeout; |
84
|
|
|
return $new; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function select(): PuzzleSolver |
88
|
|
|
{ |
89
|
|
|
assert($this->goal); |
90
|
|
|
return $this->create->forAPuzzleWith( |
91
|
|
|
new PuzzleDescription( |
92
|
|
|
$this->goal, |
93
|
|
|
$this->weightedMoves, |
94
|
|
|
$this->exhausting, |
95
|
|
|
$this->heuristic |
96
|
|
|
), |
97
|
|
|
new SearchSettings( |
98
|
|
|
$this->loggingFile, |
99
|
|
|
$this->logSeparator ?: PHP_EOL, |
100
|
|
|
$this->iterationInterval |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|