-
Notifications
You must be signed in to change notification settings - Fork 2
/
Eagerly_solving_a_maze_depth_first.php
113 lines (94 loc) · 3.01 KB
/
Eagerly_solving_a_maze_depth_first.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php declare(strict_types=1);
namespace Stratadox\PuzzleSolver\Test;
use PHPUnit\Framework\TestCase;
use Stratadox\PuzzleSolver\PuzzleSolver;
use Stratadox\PuzzleSolver\NoSolution;
use Stratadox\PuzzleSolver\Puzzle\Maze\MazeFactory;
use Stratadox\PuzzleSolver\Puzzle\Maze\MazePuzzle;
use Stratadox\PuzzleSolver\SearchStrategy\DepthFirstStrategyFactory;
use Stratadox\PuzzleSolver\SearchStrategy\VisitedNodeSkipperFactory;
use Stratadox\PuzzleSolver\Solver\EagerSolver;
use function assert;
use function count;
/**
* @testdox Eagerly solving a maze, depth first
*/
class Eagerly_solving_a_maze_depth_first extends TestCase
{
/** @var PuzzleSolver */
private $solver;
/** @var MazeFactory */
private $newMaze;
protected function setUp(): void
{
$this->solver = EagerSolver::using(
VisitedNodeSkipperFactory::using(
DepthFirstStrategyFactory::make()
)
);
$this->newMaze = MazeFactory::make();
}
/** @test */
function eagerly_solving_a_simple_maze_depth_first()
{
$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_depth_first()
{
$maze = $this->newMaze->fromString('
###################
# H # #
# # X #
###################
');
$solution = $this->solver->solve($maze)[0];
// Depth-first searches are not very efficient for this type of puzzle
self::assertGreaterThan(17, count($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_depth_first()
{
$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_depth_first()
{
$maze = $this->newMaze->fromString('
###################
# H # X #
###################
');
$this->expectException(NoSolution::class);
$this->solver->solve($maze);
}
}