Skip to content

Commit 818c59f

Browse files
authored
Merge pull request #365 from realpython/python-maze-solver
Maze Solver SbSp: Materials
2 parents 5ce44d1 + 405d655 commit 818c59f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+4004
-0
lines changed

python-maze-solver/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Build a Maze Solver in Python Using Graphs
2+
3+
This is the code for the Real Python tutorial [Build a Maze Solver in Python Using Graphs](https://realpython.com/python-maze-solver/).
4+
5+
## Installation
6+
7+
The code requires Python 3.10 or later and depends on the NetworkX library. You can install the project into an active virtual environment by issuing the following command from the project root folder:
8+
9+
```sh
10+
(venv) $ python -m pip install .
11+
```
12+
13+
For development, consider installing the code in editable mode:
14+
15+
```sh
16+
(venv) $ python -m pip install --editable .
17+
```
18+
19+
## Running
20+
21+
To run the maze solver, use the `solve` command and provide the path to a maze file:
22+
23+
```sh
24+
$ solve /path/to/labyrinth.maze
25+
```
26+
27+
Alternatively, you can use the more explicit yet equivalent command:
28+
29+
```sh
30+
$ python -m maze_solver /path/to/labyrinth.maze
31+
```
32+
33+
If the command succeeds, then you should see either a rendered solution in your default web browser or a relevant error message in the console.
28 Bytes
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# fmt: off
2+
# flake8: noqa
3+
from pathlib import Path
4+
5+
from maze_solver.models.border import Border
6+
from maze_solver.models.maze import Maze
7+
from maze_solver.models.role import Role
8+
from maze_solver.models.square import Square
9+
10+
11+
def main() -> None:
12+
build_maze().dump(Path(__file__).with_suffix(".maze"))
13+
14+
15+
def build_maze() -> Maze:
16+
return Maze(
17+
squares=(
18+
Square(0, 0, 0, Border.EMPTY, Role.EXTERIOR),
19+
Square(1, 0, 1, Border.TOP | Border.LEFT),
20+
Square(2, 0, 2, Border.TOP | Border.BOTTOM),
21+
Square(3, 0, 3, Border.TOP | Border.RIGHT),
22+
Square(4, 0, 4, Border.BOTTOM | Border.LEFT, Role.EXTERIOR),
23+
Square(5, 1, 0, Border.TOP | Border.BOTTOM | Border.RIGHT, Role.EXIT),
24+
Square(6, 1, 1, Border.LEFT | Border.RIGHT),
25+
Square(7, 1, 2, Border.TOP | Border.BOTTOM | Border.LEFT | Border.RIGHT, Role.WALL),
26+
Square(8, 1, 3, Border.LEFT),
27+
Square(9, 1, 4, Border.TOP | Border.BOTTOM, Role.ENTRANCE),
28+
Square(10, 2, 0, Border.TOP | Border.RIGHT, Role.EXTERIOR),
29+
Square(11, 2, 1, Border.BOTTOM | Border.LEFT),
30+
Square(12, 2, 2, Border.TOP | Border.BOTTOM),
31+
Square(13, 2, 3, Border.BOTTOM | Border.RIGHT),
32+
Square(14, 2, 4, Border.TOP | Border.LEFT, Role.EXTERIOR),
33+
)
34+
)
35+
36+
37+
if __name__ == '__main__':
38+
main()
909 Bytes
Binary file not shown.

python-maze-solver/mazes/labyrinth.py

Lines changed: 919 additions & 0 deletions
Large diffs are not rendered by default.
25 Bytes
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# fmt: off
2+
# flake8: noqa
3+
from pathlib import Path
4+
5+
from maze_solver.models.border import Border
6+
from maze_solver.models.maze import Maze
7+
from maze_solver.models.role import Role
8+
from maze_solver.models.square import Square
9+
10+
11+
def main() -> None:
12+
build_maze().dump(Path(__file__).with_suffix(".maze"))
13+
14+
15+
def build_maze() -> Maze:
16+
return Maze(
17+
squares=(
18+
Square(0, 0, 0, Border.TOP | Border.LEFT),
19+
Square(1, 0, 1, Border.TOP | Border.RIGHT),
20+
Square(2, 0, 2, Border.LEFT | Border.RIGHT, role=Role.EXIT),
21+
Square(3, 0, 3, Border.TOP | Border.LEFT | Border.RIGHT),
22+
Square(4, 1, 0, Border.BOTTOM | Border.LEFT | Border.RIGHT),
23+
Square(5, 1, 1, Border.LEFT | Border.RIGHT),
24+
Square(6, 1, 2, Border.BOTTOM | Border.LEFT),
25+
Square(7, 1, 3, Border.RIGHT),
26+
Square(8, 2, 0, Border.TOP | Border.LEFT, role=Role.ENTRANCE),
27+
Square(9, 2, 1, Border.BOTTOM),
28+
Square(10, 2, 2, Border.TOP | Border.BOTTOM),
29+
Square(11, 2, 3, Border.BOTTOM | Border.RIGHT),
30+
)
31+
)
32+
33+
34+
if __name__ == '__main__':
35+
main()
336 Bytes
Binary file not shown.

python-maze-solver/mazes/pacman.py

Lines changed: 346 additions & 0 deletions
Large diffs are not rendered by default.
336 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)