Exploring the use of a recursive backtracker algorithm to generate a Maze.
I'm not sure why or if I'll ever need to create a maze as part of a project but it does make for an interesting code challenge!
This Wikipedia article on maze generation algorithms was a great help.
- Choose the initial cell, mark it as visited and push it to the stack
- While the stack is not empty
- Pop a cell from the stack and make it a current cell
- If the current cell has any neighbours which have not been visited
- Push the current cell to the stack
- Choose one of the unvisited neighbours
- Remove the wall between the current cell and the chosen cell
- Mark the chosen cell as visited and push it to the stack
Note: The current cell is shown in green with the cells in the stack shown in grey.
When the algorithm is backtracking, the cell is shown in pink.
One important feature of this algorithm is the use of a stack.
You can think of a stack like a pile of plates ready to be washed up - as you are given a plate you place it down on top of the previous, this is known as a ‘push’. When you go to wash one up you take it from the top of the pile, known as a ‘pop’. This is a last-in, first-out system.
In JavaScript/TypeScript, we can use an array for this structure. It helpfully has the methods push
and pop
that can be used exactly as needed.
When we visit a cell, we push it into the stack. We look at the neighbouring cells, that have not yet been visited, picking one at random, this becomes the next cell. We remove the wall between the current and the next. The next becomes our current cell. Repeat.
However, there comes a time when the current cell has no unvisited neighbours, we have no next cell to go to - we’ve reached a dead-end. This is where backtracking and the stack come into play.
We ‘pop’ an item from the stack and see if it has any unvisited neighbours. If it does, make this the current and carry on. If not, keep popping from the stack, checking for unvisited neighbours, until our stack becomes empty.
When the stack is empty we’re done!