Skip to content

Python program to generate mazes and solve them using backtracking. Uses pygame to visualize backtracking algorithm in progress.

Notifications You must be signed in to change notification settings

jurijw/Maze-Generator-Solver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maze Generator and Solver

Description

Python program to generate mazes and solve them using a recursive backtracking approach. Uses pygame to visualize backtracking algorithm in progress.

Inspired by u/enguzelharf's reddit post

Notes

Two seperate python files are contained.

maze_generation.py will generate a random n * m maze. It also contains a print_board(grid) function, which displays the maze in the commandline, which can be useful for debugging.

The main file, visualization.py contains several functions to display the maze, solve the maze, and display the algorithm's solving process in real-time. It will not run without maze_generation.py as this is required to generate the maze!

During the visualization, cells are marked depending on their state.

  • White if unvisited
  • Blue if visited
  • Light green if part of current path
  • Dark green if starting node
  • Red if terminal node

n and m - the number of cells in a row and column, respectively, are set to a default of 50 each. In order to allow for this, as well as even bigger mazes, python's maximum recursion limit has been exceeded using sys.setrecursionlimit().

A delay between each step can be introduced by setting delay to the desired amount of delay in miliseconds.

Lastly, pygame may crash if you do not let the algorithm find its way to the end. Just force quit it in this case. This is done so as to not have to check for events in pygame.event.get() in every step of the algorithm.

Example Images

Algorithm in Process

Algorithm In Progress

Terminated Algorithm

Terminated algorithm

About

Python program to generate mazes and solve them using backtracking. Uses pygame to visualize backtracking algorithm in progress.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages