-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lazily_solving_a_maze_seeking_all_paths.php
90 lines (76 loc) · 2.06 KB
/
Lazily_solving_a_maze_seeking_all_paths.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php declare(strict_types=1);
namespace Stratadox\PuzzleSolver\Test;
use PHPUnit\Framework\TestCase;
use Stratadox\PuzzleSolver\Find;
use Stratadox\PuzzleSolver\Puzzle\Maze\MazeFactory;
use Stratadox\PuzzleSolver\Puzzle\Maze\MazePuzzle;
use Stratadox\PuzzleSolver\PuzzleSolver;
use Stratadox\PuzzleSolver\UniversalSolver;
use function assert;
/**
* @testdox Lazily solving a maze, seeking all paths
*/
class Lazily_solving_a_maze_seeking_all_paths extends TestCase
{
/** @var PuzzleSolver */
private $solver;
/** @var MazeFactory */
private $newMaze;
protected function setUp(): void
{
$this->solver = UniversalSolver::aimingTo(Find::allLooplessSolutions())->select();
$this->newMaze = MazeFactory::make();
}
/** @test */
function lazily_solving_a_simple_maze()
{
$maze = $this->newMaze->fromString('
#################
#H X#
#################
');
$solutions = $this->solver->solve($maze);
self::assertCount(1, $solutions);
self::assertCount(14, $solutions[0]->moves());
$solutionState = $solutions[0]->state();
assert($solutionState instanceof MazePuzzle);
$hero = $solutionState->hero();
self::assertEquals(2, $hero->y());
self::assertEquals(15, $hero->x());
}
/** @test */
function lazily_solving_a_maze_with_two_solutions()
{
$maze = $this->newMaze->fromString('
####
#H #
# X#
####
');
$solutions = $this->solver->solve($maze);
self::assertCount(2, $solutions, (string) $solutions);
foreach ($solutions as $solution) {
self::assertEquals(2, $solution->cost());
}
}
/**
* @test
* @group thorough
*/
function lazily_solving_a_maze_with_three_loopless_solutions()
{
$maze = $this->newMaze->fromString('
#########
#H #
# ##### #
# #X #
# # #####
# #
# ##### #
# #
#########
');
$solutions = $this->solver->solve($maze);
self::assertCount(3, $solutions, (string) $solutions);
}
}