What is the algorithm for generating the maze in the game Netwalk?
The source code is available at Google Code, so you can read it for yourself and find out! The maze is generated by the function generate_maze in game.c
, lines 78ff.
Update: try the demo!
Netwalk generates a maze by running a randomized version of Prim's algorithm to find a minimum spanning tree. Prim's algorithm iteratively grows a tree once branch at a time, starting from a source node (or nodes: in this case, the "server", the dark blue double-height box). At any given point in the running of the algorithm, the data structure looks something like this:
The cells coloured in green are cells at the tips of the growing branches: they still have at least one empty neighbour into which they might grow. At each step, the algorithm picks one of these green cells, and then picks one of its empty neighbours(1), and adds a branch into that neighbour. This new branch block neighbouring branches from growing in its direction. When a branch has no more empty neighbours(2), then it is removed from the list of green cells.
Eventually the green list is empty: none of the branches of the network have any empty neighbours. This means that the board is full, and every cell is connected to the server by a single path.
[I've idealised the details in a couple of places: (1) in fact the Netwalk algorithm is a bit naïve, and just picks a random direction, and if the neighbour in that direction is non-empty, it does nothing and continues to the next iteration. (2) Branches with no empty neighbours are not detected in a timely manner: they are only removed from the green list if they happen to be selected. The demo fixes these minor infelicities.]