Recursive Maze Generation
by Paolo on Dec.14, 2008, under Experiments
Recursion is probably one of the most powerful logic tools that a programmer can use. It solves many problems that are simply too complex for humans to be able to solve on their own. From quickly sorting millions or records of information to swift path-finding techniques. In the example above, I created a recursive map generator to create a maze, which can be used for random map generation in many games.
The logic is as follows:
- Pick a direction to travel.
- Check if the direction to travel is legal (not out of bounds and if it has not been visited.)
- If true, then go that direction.
- If false, then pick another direction.
- If all directions are invalid moves, backtrack a step.
- Check to see if we have not backtracked to the beginning.
- If true, then do step 2.
- If false, then there are no more legal moves. End recursion.