How AnyTool Carves a Guaranteed-Solvable Maze
Generation runs entirely in the browser and always produces a perfect maze — a spanning tree of the grid in which exactly one path connects any two cells, with no loops and no isolated regions. Two algorithms are offered, both fed crypto-secure randomness from the Web Crypto API (crypto.getRandomValues via rejection sampling, never Math.random). The recursive backtracker is a randomized depth-first search implemented with an explicit stack (so it never overflows the call stack on large grids): from the current cell it shuffles the four directions, carves into the first unvisited neighbour and pushes it, backtracking when a cell has no unvisited neighbours — producing long, winding corridors with relatively few junctions. Randomized Prim’s instead grows the maze outward from a random seed by repeatedly picking a random frontier wall between an in-maze cell and an outside cell and carving through it, yielding a bushier maze with many short branches. Because both build a single spanning tree, the start (top-left) and finish (bottom-right) are always connected by exactly one route, which a breadth-first search then traces as the solution.
- Every maze is a perfect maze — a spanning tree with exactly one path between any two cells
- Recursive backtracker = iterative randomized DFS (explicit stack, safe on huge grids)
- Randomized Prim’s grows from a seed into random frontier cells for a bushier look
- All randomness from the Web Crypto API (crypto.getRandomValues), never Math.random
- Guaranteed solvable; BFS finds the unique start-to-finish path; nothing is uploaded
