A Python-based maze generator and solver that uses recursive backtracking algorithms to create perfect mazes and visualize the solving process in real-time. Built with Tkinter for graphical visualization and designed with clean, testable code following object-oriented principles.
- Procedural Maze Generation: Uses recursive backtracking (depth-first search) to create perfect mazes where exactly one path exists between any two cells
- Real-Time Visualization: Watch both the generation and solving algorithms work step-by-step with smooth animations
- Smart Pathfinding: Depth-first search solver with visual backtracking — red lines show exploration, gray lines show dead ends
- Reproducible Results: Optional seed parameter ensures the same maze is generated every time for testing and debugging
- Fully Tested: Comprehensive unit tests using Python's
unittestframework - Modern Tooling: Built with
uvfor fast dependency management and development workflow
The maze starts as a complete grid of cells with all walls intact. Using recursive backtracking:
- Start at the top-left cell and mark it as visited
- Randomly choose an unvisited neighboring cell
- Remove the wall between the current cell and chosen cell
- Recursively visit the chosen cell
- When no unvisited neighbors exist, backtrack to the previous cell
- Continue until all cells have been visited
This creates a "perfect maze" — no loops, no isolated areas, and exactly one solution path.
The solver uses depth-first search with backtracking:
- Start at the entrance (top-left, top wall removed)
- Mark current cell as visited and draw a red line
- Try each direction (left, right, up, down) where there's no wall
- Recursively explore each valid path
- If a path leads to a dead end, draw a gray "undo" line and try another direction
- Continue until reaching the exit (bottom-right, bottom wall removed)
The visual result shows red lines tracing the solution path with gray lines indicating explored dead ends.
- Python 3.10 or higher
tkinter(usually included with Python)uv(optional but recommended for package management)
-
Clone the repository:
git clone https://github.com/yourusername/MazeWalker.git cd MazeWalker -
Run with uv (recommended):
uv run main.py
Or with standard Python:
python3 main.py
# With uv
uv run python tests.py
# With standard Python
python3 tests.pymazewalker/
├── graphics.py # Core graphics primitives (Window, Point, Line, Cell)
├── maze.py # Maze generation and solving logic
├── main.py # Entry point and demo configuration
├── tests.py # Unit tests for maze functionality
├── .gitignore # Python and environment exclusions
├── pyproject.toml # Project metadata (uv)
├── .python-version # Python version pinning
└── README.md # This file
Edit main.py to customize the maze:
# Parameters: x, y, num_rows, num_cols, cell_width, cell_height, window, seed
maze = Maze(100, 60, 12, 15, 40, 40, win)
# Explanation:
# - (100, 60): Top-left corner position
# - 12 rows, 15 columns
# - 40x40 pixel cells
# - No seed = random maze each timeIn maze.py, adjust the __animate() method:
def __animate(self):
if self.__win is None:
return
self.__win.redraw()
time.sleep(0.05) # Change this value (0.01 = faster, 0.1 = slower)Use a seed for consistent results:
maze = Maze(100, 60, 12, 15, 40, 40, win, seed=42) # Same maze every time- Python 3.10+: Core programming language
- Tkinter: Built-in GUI library for canvas drawing and window management
- unittest: Standard library testing framework
- uv: Modern Python package manager (optional)
- Git/GitHub: Version control and collaboration
- Object-Oriented Design: Clean separation of concerns (Window, Cell, Maze classes)
- Testable Architecture: Window parameter is optional, allowing headless testing
- Name Mangling: Private methods use Python's
__methodconvention - Type Safety: Clear parameter naming and logical structure
- DRY Principle: Reusable components throughout
- Implement Breadth-First Search (BFS) for shortest path finding
- Add A* algorithm with heuristic optimization
- Compare algorithm performance with timing metrics
- Support for Dijkstra's algorithm visualization
- Color-coded paths by algorithm type
- Customizable color schemes (dark mode, high contrast)
- Variable animation speeds (slow backtracking, fast exploration)
- Show statistics (path length, cells visited, time elapsed)
- Add a solution path highlight after completion
- GUI controls for maze size, speed, and algorithm selection
- Buttons to generate new maze, solve, reset
- Manual play mode (user controls movement with arrow keys)
- Race mode: user vs. algorithm
- Save/load maze configurations
- Multiple maze generation algorithms (Kruskal's, Prim's, Wilson's)
- 3D maze visualization
- Larger mazes with zoom/pan functionality
- Difficulty levels (more/fewer branches)
- Maze export to image or text format
This project demonstrates:
- Recursive algorithms: Backtracking for generation and solving
- Data structures: 2D grids, visited tracking
- Graph theory: DFS traversal, perfect maze properties
- GUI programming: Event loops, canvas drawing, animations
- Software engineering: Testing, modularity, version control
- Problem decomposition: Building complexity incrementally (Fastai/Solveit approach)
MIT License - feel free to use this project for learning, teaching, or building upon!
Built as part of a programming mentorship focusing on problem-solving, algorithmic thinking, and clean code practices. Inspired by classic maze generation techniques and the joy of watching algorithms come to life visually.
Enjoy exploring the maze! 🎯