Project 28: Maze Maker Mini 🧩¶
Difficulty: 🟡 Intermediate
Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/maze_maker_mini/.
Description¶
Generate a perfect maze with recursive backtracking and save it as a crisp PNG map.
Libraries & Modules¶
numpymatplotlib.pyplotrandom
Python Concepts You'll Practice¶
2D grids, recursion / stacks, image rendering from arrays
🎛️ Parameter Variations¶
ROWS,COLS— maze sizeSEED— different layouts
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline
from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame
OUTPUT_DIR = Path("outputs") / "maze_maker_mini"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\maze_maker_mini
Step 1 — Carve the maze¶
In [3]:
import random
import numpy as np
# 🎛️ TWEAK THESE
ROWS, COLS = 21, 31 # odd numbers look best
SEED = 5
random.seed(SEED)
# 1 = wall, 0 = path
maze = np.ones((ROWS, COLS), dtype=int)
dirs = [(-2, 0), (2, 0), (0, -2), (0, 2)]
def carve(r, c):
maze[r, c] = 0
order = dirs[:]
random.shuffle(order)
for dr, dc in order:
nr, nc = r + dr, c + dc
if 1 <= nr < ROWS - 1 and 1 <= nc < COLS - 1 and maze[nr, nc] == 1:
maze[r + dr // 2, c + dc // 2] = 0
carve(nr, nc)
carve(1, 1)
maze[0, 1] = 0 # entrance
maze[ROWS - 1, COLS - 2] = 0 # exit
print("Maze carved:", maze.shape, "open cells =", int((maze == 0).sum()))
Maze carved: (21, 31) open cells = 301
Step 2 — Render the map¶
In [4]:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(9, 6))
ax.imshow(maze, cmap="gray_r", interpolation="nearest")
ax.set_xticks([])
ax.set_yticks([])
ax.set_title("Maze Maker Mini — CodeItAll")
fig.tight_layout()
out = OUTPUT_DIR / "maze.png"
fig.savefig(out, dpi=160)
plt.show()
np.savetxt(OUTPUT_DIR / "maze.txt", maze, fmt="%d")
print(f"Saved {out}")
Saved outputs\maze_maker_mini\maze.png