Christian Ernst Rysgaard
Senior System Developer Consultant
Software Research Department, SimCorp A/S
Created June 19, 2000
Click to get the source files for this article
This small program is used for generating a labyrinth with exactly one solution. It was developed in Visual Basic and can be reused as you may wish. If the algorithm and program actually works it was developed by Christian Ernst Rysgaard, Software Research Department, SimCorp A/S, Denmark in June 2000 for the Danish MSDN developer labyrinth contest summer 2000. Otherwise I have no clue about who made it or even why.
The source files also includes a solution using the same algorithm written in clean C with a commandline interface. However speedier it does not beat the VB version in readability. All maze operations are done using standard clib string functions - a rather cunning and speedy solution but it obviously obscures any attempt to grasp the mere outline of the algorithm.
Share and Enjoy!
The user interface is quite simple:
The popup menu contains these functions:
The labyrinth is shown with the following legends:
|
![]() |
Please notice:
The program has been tested on the following systems:
The program uses the following algorithm to generate a labyrinth.
The following table briefly shows the steps involved in four iterations of the above algorithm (X = Wall, ? = Possible position, + = Selected position)
| 1 | 2 | 3 | 4 | 5 | 6 |
XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXXXXXX XXX?XXX XXXXXXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXXXXXX XXX XXX XXXXXXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXXXXXX XXX XXX XXXXXXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX?XXX XX? ?XX XXX?XXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX?XXX XX? ?XX XXX+XXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX?XXX XX? ?XX XXX XXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX?XXX XXX XXX XXX XXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX?XXX XX? ?XX XX? ?XX XXX?XXX XXXXXXX |
XXXXXXX XXXXXXX XXX?XXX XX? ?XX XX? ?XX XXX+XXX XXXXXXX |
||
XXXXXXX XXXXXXX XXX?XXX XX? ?XX XX? ?XX XXX XXX XXXXXXX |
XXXXXXX XXXXXXX XXX?XXX XX? ?XX XXX XXX XXX XXX XXXXXXX |
XXXXXXX XXXXXXX XXX?XXX XX? ?XX XX? ?XX XX? ?XX XXX?XXX |
XXXXXXX XXXXXXX XXX?XXX XX+ ?XX XX? ?XX XX? ?XX XXX?XXX |
||
XXXXXXX XXXXXXX XXX?XXX XX ?XX XX? ?XX XX? ?XX XXX?XXX |
XXXXXXX XXXXXXX XXXXXXX XX ?XX XXX ?XX XX? ?XX XXX?XXX |
XXXXXXX XXXXXXX XX??XXX X? ?XX XXX ?XX XX? ?XX XXX?XXX |
XXXXXXX XXXXXXX XX??XXX X? ?XX XXX ?XX XX? +XX XXX?XXX |
As seen from the above algorithm, the two important parts are: the list of possible positions and position validation.
Think of it as a list of positions within the labyrinth that might be used as a path. Once a possible position is turned into a path in the labyrinth some of the surrounding possible positions might become invalid. That's why the algorithm after selecting a random one from the list, removes all those invalidated by the new position and then adds all extra positions created by the new position. By constantly maintaining the list of possible positions instead of rebuilding it for every iteration, the program adds extra speed to the generation.
The program uses the following algorithm to obtain all the possible positions :
Each of the cells in steps 3 to 6 that are nonblank and complies with the rules specified will be added as possible positions.
The following example displays the validation of positions from a specific position: (X=Wall, Space=Path, +=Current position, ?=Possible position):
| Current | Up | Right | Left | Down | Result |
XXXXXXX XXXXXXX XXXXXXX XXX +XX XXX XXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXXXXXX XXX +XX XXX XXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXXXXXX XXX +XX XXX XXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXXXXXX XXX +XX XXX XXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXXXXXX XXX +XX XXX XXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXXX?XX XXX +?X XXX XXX XXXXXXX XXXXXXX |
XXXXXXX XXXX X XXX+X X XXX X X XXX X XXXXXXX XXXXXXX |
XXXXXXX XXXX X XXX+X X XXX X X XXX X XXXXXXX XXXXXXX |
XXXXXXX XXXX X XXX+X X XXX X X XXX X XXXXXXX XXXXXXX |
XXXXXXX XXXX X XXX+X X XXX X X XXX X XXXXXXX XXXXXXX |
XXXXXXX XXXX X XXX+X X XXX X X XXX X XXXXXXX XXXXXXX |
XXXXXXX XXXX X XX?+X X XXX X X XXX X XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX+XXX XX XX XXXXXXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX+XXX XX XX XXXXXXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX+XXX XX XX XXXXXXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX+XXX XX XX XXXXXXX XXXXXXX XXXXXXX |
XXXXXXX XXXXXXX XXX+XXX XX XX XXXXXXX XXXXXXX XXXXXXX |
XXXXXXX XXX?XXX XXX+XXX XX XX XXXXXXX XXXXXXX XXXXXXX |
(The red colour is used to mark the area of interest.)
Seen from the above examples it is easy to deduce that new possible positions will only pop up in the cases where the red area is completely filled with walls (X's)
During documentation and test I have discovered that the cell validation function misses some possible cells on the boundary of the labyrinth. Cells starting horizontally from a clean cell on the vertical bound (and vice versa) will be disregarded only based on their coordinates. The solution is obviously to correct the error but this will severely obscure the readability of the function (ok, I admit it - I couldn't locate the damn bug)!
Send feedback on this article.