How the mazes are generated

Updated 13 November 2026

Every maze in Infinite Maze is created in the browser in a few milliseconds. There is no level file and no server call: the game carves a new grid the moment you begin. The algorithm behind it is iterative randomized depth-first search, also known as recursive backtracking.

The idea

Start with a grid where every cell is surrounded by walls. Pick a cell, mark it visited, and push it onto a stack. Then repeat: look at the unvisited neighbours of the cell on top of the stack, choose one at random, knock down the wall between them, mark the new cell visited and push it. If there are no unvisited neighbours, pop the stack. When the stack is empty, every cell has been visited exactly once.

Because each step carves exactly one passage and no cell is visited twice, the result is a spanning tree: a connected graph with no loops. In maze terms that is a perfect maze — exactly one route between any two cells, and therefore a guaranteed path from the entry to the exit.

Why iterative, not recursive

The textbook version calls itself for each cell. On a 24×24 grid that is 576 nested calls, which is survivable but wasteful, and recursion depth grows with the grid. The game keeps an explicit typed array as the stack instead: constant memory, no call-stack risk, and predictable timing on phones.

What it means for gameplay

Why the layout differs every run

Neighbours are chosen with Math.random(), so the sequence of carves changes each time. The rules guarantee quality — connectivity, one path, solvability — while the randomness guarantees variety. That combination is what makes an endless mode possible without hand-authored levels.

Want to see it in action? Play a level and press R a few times to regenerate the same size grid.