Reputation: 516
What would be a good maze generation algorithm where the developer can choose entrance and exit points arbitrarily (both of them should be located on the edges, of course)? A piece of pseudocode or an implementation in any language would be most helpful.
Upvotes: 0
Views: 3716
Reputation: 121
For an implementation that generates a random maze from any planar graph, check out this web app. For a slightly different take on it, and a general description of dual graphs, I've also put together a presentation.
Note, the notion of an entry or exit is something I have not addressed at all. But, given a well defined maze, you can always place the entry and exit points at any location. (Granted, some may lead to trivial traversals as the solution.) In any maze, you should always be able to travel between any two points or 'rooms'.
Finding an entry and exit point that results in an interesting or sufficiently complex solution given a particular maze is likely just a matter of ensuring a solution with a sufficient length. If you'd like to pre-specify an entry an exit point for an arbitrary graph, that can also be done pretty simply.
If you have a graph and want to add a start or end point, all you need to do is add a 'room' to the perimeter. This room has to be reachable to the rest of the maze, and it can only be reached by removing the wall between it and the rest of the maze. The idea is shown in the following, with the start and end rooms as the triangles at the top left and bottom right:
.
A random maze from the graph would be as follows:
Upvotes: 0
Reputation: 50017
There are many algorithms for generating mazes: with a little work I expect you can change them to allow the starting and ending points to be specified.
The best reference I'm aware of for mazes is Mazes For Programmers by Jamis Buck. Jamis blogs about many things software-related, and among his posts you can find quite a few on maze algorithms. The following is a recap of what's on his blog:
Best of luck.
Upvotes: 3
Reputation: 514
While not exactly an algorithm, i describe here some steps in creating a maze. Each of these steps will need to be turned into code, but i think they are a good starting point.
step 0: Initially, all the squares in the matrix have all their borders marked as walls.
Later on, some of these walls will be cut, such as to create paths.
In the following image, every wall that will be intersected by a path will be cut;
all the walls that are not cut by any path will be kept.
step 1: We start the following way: create the most simple path between the entry and the exit points; this path will be made of only one horizontal and one vertical line.
Next, because this path is too simple, we try to make it more complicated (more twisted, having more turns); we do this by gradually replacing segments of the existing path, with three sides of a rectangle (which, together with the removed segment, would have represented an entire rectangle); we repeat this step as many times as necessary, only taking care that the path will not intersect itself.
step 2: Now we have a relatively complicated path that connects the entry and exit points in the maze. However, there are still cells in the matrix will walls surrounding them on all 4 sides. I have looked for some examples of mazes, and i have noticed that every cell in the maze is potentially accessible from the entry point, even though it is a dead end. (to put it in another way, all the paths in the maze form a tree).
What we need to do now is to connect all the remaining cells in the maze to the existing path. As a convention, i will call 'main path' the path that we have just created from the entry to the exit points; all the other paths that branch out of it will be named 'secondary paths'. So how do we create secondary paths? By picking one random point in the large areas of the maze that are not traversed by the main path, and another random point on the main path. Than link these points in a similar way that we did with the entry and exit points: link them with the most simple path possible, later make this path more complicated - by replacing a segment with the 3 segments that would make the complement of a rectangle. We won't need to repeat this step many times because the secondary paths don't need to be more complicated than the main path.
step 3: Now we have created several secondary paths , by linking some random points in the maze with the main path. There are still some areas that are inaccessible, but they are small. So for these cells, we can just link them with a straight line to the nearest point in either the main path or a secondary path, and that's it. All the squares in the maze are accessible. All we need to do now is to remove any wall that is intersected by a path, and keep all the other walls.
Upvotes: 2