Reputation: 11
I am trying to create a simple maze, that will be based on a grid, 5x9 (for the iphone 5) The maze will be made with simple white squares(path) and black squares (the walls). How is it possible to generate a code that will make the path totally random, but will always be passable. Thanks in advance.
Upvotes: 0
Views: 1030
Reputation: 21
Consider an NxN grid of cells, each of which initially has a wall between it and its four neighbouring cells. For each cell (x, y), maintain a variable up[x,y] that is true if there is wall separating (x, y) and (x, y + 1). Maintain analogous variables left[x, y], down[x,y], and right[x,y] for the corresponding walls.
Note that if there is a wall above (x, y) then up[x,y] = down[x,y+1] = true. Construct the maze by knocking down some of the walls as follows:
Source: http://algs4.cs.princeton.edu/41undirected/
A Swift implementation can be found at: https://github.com/lucaslouca/swift-mazes
Upvotes: 1