Maze Generator And Solver
Program Purpose: Generates a maze that then solves itself
Language: Python and Pygame
Algorithm: Randomized DFS / Floodfill / Recursive Backtracking
Wikipedia Algorithm Pseudocode
Screen.Recording.2021-06-04.at.11.57.55.PM.mov
How the program works
Generation
- Maintain a matrix that keeps track of visited cells
- Maintain a stack that keeps track of cells on the current path
- Start at the top left cell
- Randomly choose one of the available neighbor cells
- If there are none, go to previous cell in the stack
- Color visited cells blue, current cell blue, and remove the white wall
- Will eventually return to the top left cell and guarantees a maze
Solving
- Maintain a matrix that keeps track of visited cells
- Maintain a stack that keeps track of cells on the current path
- Start at the top left cell
- Randomly choose one of the available neighbor cells without a wall
- If there are none, go to previous cell in the stack
- Color visited cells green and current cell red
- Will eventually reach the bottom right cell, at which point the program restarts
Code Overview
- A matrix of cell classes: each cell instance maintains properties such as a wall array, visited / current booleans, and methods to draw to the screen
- Simulation Function
- Generation - Find next cell, check if it is inbounds and unvisited, remove the wall, move to that cell
- Solving - Find next cell, check if it is inbounds and unvisited, move to that cell
Install as executable file
Open the terminal / command line
pip install pyinstaller
Put maze.py
inside of a folder called main
(or whatever name you want)
Open up a terminal / command line inside the folder
pyinstaller --onefile -w maze.py
Go to dist
and select main.exe