-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEagerly_solving_a_maze.php
More file actions
118 lines (97 loc) · 2.88 KB
/
Copy pathEagerly_solving_a_maze.php
File metadata and controls
118 lines (97 loc) · 2.88 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php declare(strict_types=1);
namespace Stratadox\PuzzleSolver\Test;
use PHPUnit\Framework\TestCase;
use Stratadox\PuzzleSolver\Find;
use Stratadox\PuzzleSolver\PuzzleSolver;
use Stratadox\PuzzleSolver\NoSolution;
use Stratadox\PuzzleSolver\Puzzle\Maze\MazeFactory;
use Stratadox\PuzzleSolver\Puzzle\Maze\MazePuzzle;
use Stratadox\PuzzleSolver\UniversalSolver;
use function assert;
/**
* @testdox Eagerly solving a maze
*/
class Eagerly_solving_a_maze extends TestCase
{
/** @var PuzzleSolver */
private $solver;
/** @var MazeFactory */
private $newMaze;
protected function setUp(): void
{
$this->solver = UniversalSolver::aimingTo(Find::aBestSolution())->select();
$this->newMaze = MazeFactory::make();
}
/** @test */
function eagerly_solving_a_simple_maze()
{
$maze = $this->newMaze->fromString('
###################
# H X #
###################
');
$solution = $this->solver->solve($maze)[0];
self::assertCount(14, $solution->moves());
$solutionState = $solution->state();
assert($solutionState instanceof MazePuzzle);
$hero = $solutionState->hero();
self::assertEquals(2, $hero->y());
self::assertEquals(16, $hero->x());
}
/** @test */
function eagerly_solving_a_maze_with_obstacles()
{
$maze = $this->newMaze->fromString('
###################
# H # #
# # X #
###################
');
$solution = $this->solver->solve($maze)[0];
self::assertCount(17, $solution->moves());
$solutionState = $solution->state();
assert($solutionState instanceof MazePuzzle);
$hero = $solutionState->hero();
self::assertEquals(3, $hero->y());
self::assertEquals(16, $hero->x());
}
/** @test */
function eagerly_solving_a_maze_with_multiple_obstacles()
{
$maze = $this->newMaze->fromString('
###################
# H # #X #
# ###### # ###### #
# # #
###################
');
$solution = $this->solver->solve($maze)[0];
self::assertCount(28, $solution->moves());
$solutionState = $solution->state();
assert($solutionState instanceof MazePuzzle);
$hero = $solutionState->hero();
self::assertEquals(2, $hero->y());
self::assertEquals(14, $hero->x());
}
/** @test */
function not_eagerly_solving_unsolvable_puzzles()
{
$maze = $this->newMaze->fromString('
###################
# H # X #
###################
');
$this->expectException(NoSolution::class);
$this->solver->solve($maze);
}
/** @test */
function unsolved_mazes_are_not_solved()
{
$maze = $this->newMaze->fromString('
###################
# H X #
###################
');
self::assertFalse($maze->isSolved());
}
}