public

Maze Generator

read-only

AI authored to showcase ironpad capabilities.

Maze Generator

Maze generation algorithms create perfect mazes: mazes where every cell is reachable from every other cell, with exactly one path between any two points.

Recursive Backtracker Algorithm

The recursive backtracker (a depth-first search variant) is one of the most popular maze generators:

  1. Start at a cell, mark it visited
  2. While the stack is not empty:
    • Look at the current cell's unvisited neighbors
    • If there are unvisited neighbors: pick one at random, carve a passage to it, push it onto the stack
    • If there are none: backtrack (pop the stack)

This produces mazes with long, winding corridors and relatively few dead ends, characteristic of DFS-based generators.

Representation

Each cell stores a bitmask of carved walls: N=1, E=2, S=4, W=8. If a bit is set, the wall in that direction has been removed (there's a passage).

[1]Generate Maze
1 panel
Saved output from the author's last run. Press Run to execute live in your browser.
Generated 25x25 maze (625 cells, 625 bytes of wall data)
[2]Render Maze
1 panel
Saved output from the author's last run. Press Run to execute live in your browser.
(output too large to embed; run the cell to regenerate it)
[3]Solve & Render
1 panel
Saved output from the author's last run. Press Run to execute live in your browser.
(output too large to embed; run the cell to regenerate it)

The Algorithm in Depth

Why Recursive Backtracker?

The recursive backtracker creates mazes with long, winding corridors because it carves as deep as possible before backtracking. This produces:

  • Few short dead ends
  • Long paths that feel satisfying to navigate
  • A "river-like" structure

Other Maze Algorithms

AlgorithmCharacterComplexity
Recursive BacktrackerLong corridors, few dead endsO(n) time, O(n) stack
Kruskal'sMany short dead ends, uniform feelO(n log n)
Prim'sBranching, tree-like structureO(n log n)
Aldous-BroderPerfectly uniformO(n²) expected
Wilson'sPerfectly uniformO(n²) worst case

The BFS Solver

Breadth-first search finds the shortest path in an unweighted graph. It explores all cells at distance 1 before distance 2, ensuring the first path found to the goal is optimal.

For weighted mazes (where passages have different costs), you'd use Dijkstra's or A* instead.