Guide

Procedural Maze Generation Explained

How a maze is carved so that it is always solvable, and why that guarantee matters.

A generated maze that might be unsolvable is worse than no maze at all, because the player cannot tell a hard layout from an impossible one. This guide explains the algorithm used here, why it guarantees solvability, and what that guarantee buys.

The recursive backtracker

Start with a grid where every cell is wall. Pick a starting cell and carve it open. Then repeat: from the current cell, look at cells two steps away in each direction, pick one at random that is still wall, carve both it and the cell between, and move there. If no such neighbour exists, step back to the previous cell and try again.

The 'two steps' is what keeps walls between corridors. Carving to an immediate neighbour would produce open space rather than a maze.

The algorithm terminates when it has backtracked all the way to the start with no options remaining, at which point every reachable cell has been carved.

Why it guarantees a solution

Every cell is carved by being reached from an already-carved cell, which means every carved cell has a path back to the start by construction.

Since any two carved cells both connect to the start, they connect to each other. The maze is therefore fully connected - there are no isolated pockets and no unreachable regions.

That is a much stronger guarantee than testing a generated maze afterwards and regenerating if it fails. It cannot fail, so there is nothing to test.

The shape it produces

Recursive backtracking produces long winding corridors with relatively few junctions - a 'perfect maze' with exactly one path between any two points and no loops.

That shape has consequences for play. Long corridors mean wall-following is efficient and dead ends are long, which makes a wrong turn genuinely costly. Few junctions mean the decisions are infrequent but significant.

Other algorithms produce different textures. Randomised Prim's produces shorter, bushier corridors with more junctions and shorter dead ends, which feels less punishing and less memorable. The backtracker was chosen here because a maze with a torch wants long corridors - they give you something to remember.

Placing goals in a generated maze

The goals cannot simply be placed at fixed grid coordinates, because those coordinates might be wall. Sword Maze requests a corner and then spirals outward from it until it finds an open cell.

That produces goals that are always near the intended corner and always actually reachable, which is the combination the design needs: the player can rely on 'the sigils are in the corners' without the generator having to be constrained.

Enemies are placed the same way, with an additional constraint that they must be at least 220 pixels from the player's start - so no run begins with something already on top of you.

Why not hand-author the mazes

A dozen hand-designed mazes would be better than any single generated one. They are worse as a set, for a reason specific to this genre.

A hand-authored maze is solved permanently the first time. In a game whose central skill is navigating without vision, a remembered layout removes the entire challenge - the second run is not a harder version of the first, it is a different and much easier game. Twelve layouts buys twelve runs before the same thing happens.

Generation trades peak quality for renewability. Any individual generated maze is less interesting than a designed one would be, and every run gets a maze you have not seen. For a score chase built on repetition, that is the right side of the trade.

The fixed goal positions are what stops this becoming pure randomness. The corridors are new every time and the structure of the problem is constant, so a player builds transferable skill rather than memorising anything.

The parameters that actually matter

Two numbers control how a generated maze feels, and both are easy to get wrong.

Cell size against torch radius is the first. A torch lighting four cells produces a game about local decisions; one lighting twelve produces a game about navigation with mild fog. The ratio, rather than either number alone, is what determines how much of the maze is a memory problem.

Grid dimensions against run length is the second. A maze large enough to be interesting and small enough to cross several times within the torch budget is a narrow band, and missing it in either direction produces a game that is either trivial or unfinishable regardless of skill.

Both are tuned by playtesting rather than calculated, which is worth saying because the algorithm is the part that looks technical and the parameters are the part that decides whether the game is any good.

Determinism and why it is available

The random number generator used across this site is a small deterministic algorithm that accepts a seed. Given the same seed it produces the same sequence, which means the same maze.

Nothing currently exposes that seed to the player, but the capability is deliberately present. A 'practise this exact layout' feature, or a daily maze shared between players, would be a small change rather than a rewrite.

The general principle is worth stating: using a seedable generator costs nothing over using the built-in random source and preserves options that are expensive to add later.

Key takeaways

  • The recursive backtracker carves cells two steps at a time, leaving walls between corridors.
  • Every carved cell reaches the start by construction, so the maze cannot contain unreachable regions.
  • It produces long winding corridors with few junctions - good for a torch-limited game because there is something to remember.
  • Goals are placed by requesting a corner and spiralling outward to the nearest open cell.
  • Enemies are placed at least 220 pixels from the start so no run begins under pressure.
  • The generator is seedable, which preserves the option of shareable or repeatable layouts.
  • A hand-authored maze is solved permanently on the first run, which is why generation wins as a set.
  • Cell size against torch radius and grid size against run length decide how the game feels, and both are playtested rather than calculated.

Frequently asked questions

Can a generated maze be unsolvable?

Not with this algorithm. Every cell is carved by being reached from an already-carved cell, so everything connects back to the start and therefore to everything else.

Why does the maze have such long corridors?

That is the signature of a recursive backtracker. It was chosen deliberately, because a torch-limited game wants long corridors - they give the player something to remember.

How are the sigils placed if the maze is random?

By requesting a corner and spiralling outward until an open cell is found. That keeps them near the intended corner and always reachable.

Could the same maze be played twice?

Technically yes - the generator is seeded and deterministic. Nothing currently exposes the seed, but the capability is deliberately preserved.

Do enemies ever spawn on top of me?

No. Placement requires at least 220 pixels from the player's starting position.

Why not hand-design the mazes instead?

A hand-authored maze is solved permanently the first time, which removes the entire challenge of navigating without vision. Generation trades peak quality for a maze you have not seen on every run.