Python maze solver using Dikstra's algorithm to find a viable path through a maze. Where the maze is defined by a matrix of letters or characters, where one character represents a wall, the other the entry/exit points of the maze, and the rest are possible paths. The solver then searches for the shortest viable path based on a desired path pattern and generates a file representing the path through the maze.
For example, given the following maze:
ABAAAAAAAAAA
ACADDEACCCDA
ACCDAEADADAA
AAAAAEDDADEA
ACCDDDAAAAEA
ACAAAAADDDEA
ADDDEEACAAAA
AAAEAEACCDDA
ADEEADAAAAAA
AADAADACDDAA
ADDDADCCADEB
AAAAAAAAAAAA
Find a path using the following pattern CCC-DDD-EEE-DDD
.
- Python 3.8 or greater
- Pipenv
- Clone this repo:
git clone git@github.com:asfourco/maze_solver.git
or unpack the tarball/archive in a project directory - Navigate to the project directory
cd /path/to/maze_solver
- Initiate a python virtual environment:
pipenv shell
- Install dependencies:
pipenv install
Basic usage is:
# Basic command will print output to stdout
$ python solve.py path/to/input_file
# To write the output as well
$ python solve.py path/to/input_file --output path/to/output_file
# Help on usage
$ python solve.py --help
The following is the input format of the maze:
- The first line is the character representing the Entry and Exit of the maze.
- The second line is the character representing the Wall within the maze
- The third line is the character path pattern the solver should use
- The rest of the lines represent the maze itself
For example, to represent the above maze, the input file input.txt
would be:
B
A
CCCDDDEEEDDD
ABAAAAAAAAAA
ACADDEACCCDA
ACCDAEADADAA
AAAAAEDDADEA
ACCDDDAAAAEA
ACAAAAADDDEA
ADDDEEACAAAA
AAAEAEACCDDA
ADEEADAAAAAA
AADAADACDDAA
ADDDADCCADEB
AAAAAAAAAAAA
There a number of constraints on the structure of the maze:
- The maze cannot have the entry/exit points on the same row, i.e., neither on the same edge (e.g.,
AAABAABAAA
) or on opposites lateral sides of the maze (e.g.,BAAAAAAB
) - There is only one pair of entry/exit points to the maze
- The start of the path pattern should begin at the top of the maze.
The above maze is saved as input_examples/input_1.txt
. Running the solver will give you the following result:
$ python solve.py input_examples/input_1.txt
# output =>
Finding path in maze shape of (rows, cols):(12, 12), using path pattern: 'CCCDDDEEEDDD'
...
Path Found of length: 32
_B__________
_C_DDE______
_CCD_E______
_____E______
_CCDDD______
_C__________
_DDDEE______
_____E______
_____D______
_____D_CDD__
_____DCC_DEB
____________
If the solver did not find a path it will print out No Path Found!
Development tools we use are:
flake8
- code lintingbandit
- vulnerability scannerblack
- code formattermypy
- data typing librarypre-commit
- pre commit task runner, runs the other three tools prior to committing code to catch any issues a priori
To install pre-commit
run the following in at the root of the project directory: pre-commit install
. You can then use this tool independently of committing your changes: pre-commit run -a
(runs all tools) or a specific tool: pre-commit run flake8
.
There are example input files in the subfolder input_examples
:
input_1.txt
is the above mentioned mazeinput_2.txt
is a smaller maze to test path patternsinput_3.txt
is to test a maze with no solution; should outputNo path found