Code archives/Algorithms/Create 2D maze in an array

This code has been declared by its author to be Public Domain code.

Download source code

Create 2D maze in an array by Zethrax2015
*** Blitz3D Code ***

This library uses Wilson's algorithm to create a 2D maze of a specified width and height.
The maze will be fully connected (no isolated sections or unconnected cells) and will not contain any loops.
The maze is stored in the 'A_maze' array with data for the maze cells stored in 'T_maze_cell' custom type objects.

Note that in the demo you can change the value of the 'G_demo_mode' global to alter how the maze building process is displayed. The following settings are available:-

* 'G_demo_mode = 2' shows the maze being created dynamically with the loop-erased random walk path displayed (the purple line wiggling about). This mode will take a while to build the maze as it needs to be slowed down enough for people to get an idea of how the process works.

* 'G_demo_mode = 1' shows the maze being created without the walk path displayed. The display is updated after each finished path is added to the maze, so it's a lot quicker than mode 2 but still intended to be slow enough to see what's going on.

* 'G_demo_mode = 0' just creates the maze and then displays it. Use this mode if you just want to see the finished maze.


DEMO CODE NOTES:-
Change the value of the 'G_demo_mode' global in the demo code to use different demo modes.
- 0 = No dynamic demo. 1 = Draw maze but not walkpath. 2 = Draw maze and walkpath.
The 'DoRandomWalk' function includes lines required to draw the maze for the demo. These lines are marked with '; **** DEMO CODE ****'. Remove or comment out these lines for non-demo code.
Note that in the demo the lines are the maze path, not the space between the lines.


EXTERNAL LIBRARY FUNCTIONS:-
SetupMaze - Creates a blank maze and initializes it. Call this before calling 'GenerateMaze'.
GenerateMaze - Generates the maze structure (the connections between cells). Call this after calling 'SetupMaze'.
DestroyMaze - Destroys the maze to free up the memory used by it.

INTERNAL LIBRARY FUNCTIONS:-
DoRandomWalk - Called by 'GenerateMaze' to create the paths for the maze.
ConnectMazeCells - Called by 'DoRandomWalk' to create connections between maze cells.

REQUIRES:-
T_maze_cell (type)
A_maze (array)
C_TOP, C_RIGHT, C_BOTTOM, C_LEFT (constants)

ADDITIONAL FUNCTIONS:-
SaveMaze - Saves the maze to a text file. See the function's definition for the structure of the saved data.
LoadMaze - Loads a saved maze.
InputMaze - Creates the maze using the specified' string.
HexToDec - Accepts a hexadecimal string and converts and returns it as a decimal integer. Used by the load/save functions.
DrawMaze - Draws the maze with white lines representing horizontal connections.
CountEstablishedMazeCells - Counts the number of cells that have been added to the established maze structure. Just used for debugging, but I've left it in as it shows how to iterate the maze structure.

NOTES:-
The maze is a connected graph so there will be a path between any two cells in the maze. For the beginning and end of the maze just pick any two cells.
The number of cells in the array (width * height) set in 'SetupMaze' must be larger than the value set for 'start_limit' in 'GenerateMaze'.

REFERENCES:-
Wilson’s algorithm
http://bl.ocks.org/mbostock/11357811
http://www.pixieland.org.uk/2014/wilsons-algorithm/
https://eventuallyalmosteverywhere.wordpress.com/tag/wilsons-algorithm/
http://en.wikipedia.org/wiki/Loop-erased_random_walk

Minimum spanning tree (with decent definitions and summary) (BlitzMax code)
http://www.blitzbasic.com/codearcs/codearcs.php?code=2870

Some interesting summaries of maze algorithms
http://www.astrolog.org/labyrnth/algrithm.htm

The code can be found at: http://www.blitzbasic.com/codearcs/codearcs.php?code=3178

Comments

videz2015
This looks interesting Zethrax. But I don't see any maze if I set your demo to 2. in options 0 it shows up though..


Zethrax2015
'G_demo_mode = 2' shows the maze being created dynamically with the loop-erased random walk path displayed (the purple line wiggling about). This mode will take a while to build the maze as it needs to be slowed down enough for people to get an idea of how the process works.

'G_demo_mode = 1' shows the maze being created without the walk path displayed. The display is updated after each finished path is added to the maze, so it's a lot quicker than mode 2 but still intended to be slow enough to see what's going on.

'G_demo_mode = 0' just creates the maze and then displays it. Use this mode if you just want to see the finished maze.

I'll add this explanation to the summary.


Code Archives Forum