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

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

Download source code

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

This library uses Wilson's algorithm to create a 3D (cuboid) maze of a specified width, height, and depth.
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.


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)
G_maze_width, G_maze_height, G_maze_depth (globals)

ADDITIONAL FUNCTIONS:-
SaveMaze - Saves the maze to a binary file. See the function's definition for the structure of the saved data.
LoadMaze - Loads a saved maze.
DrawMaze - Draws the maze with white lines representing horizontal connections, red squares representing upward connections, and blue squares representing downward 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 * depth) set in 'SetupMaze' must be larger than the value set for 'start_limit' in 'GenerateMaze'.
Note that 0, 0, 0 in the array is at the left-bottom-back position in the maze. Looking at the maze face-on the front is the side nearest the observer. This means that the 3D world Z axis will need to be inverted when translating into 3D space (the front will be Z+ and the back will be Z- in 3D world space).

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

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

Comments

virtlands2015
Your 3D maze code is fascinating...

Here are 4 sample screenshots taken (out of a possible 32 levels deep) ::



Red squares show upward maze connections.
Blue dots show downward maze connections.
White lines are horizontalish.


videz2015
Looks interesting and it works now.


Code Archives Forum