开发者

Making a 3D maze in Java

Goal


I am making a program which generates a 3D maze and am having a bit of trouble with the creation algorithm. For ease of interaction, it will be a rectangular prism with one entrance and one exit.

Algorithm


The problem is the actual coding of the algorithm: I figured the best way to go with this is make a class called MazeBlock, which has six boolean states (up, down, left, right, in, out) which开发者_StackOverflow中文版 signify in which direction the maze can go next. using a 3D array of MazeBlocks, I want to fill the maze, each iteration of filling checking the blocks to the left, right, above, below, in front of, and behind it to see if there is any opening to that side to which to attach.

I already have one that will make the edges, placing random open slots toward the interior of the maze. All I have trouble with is the actual interior, ensuring that the maze has one entrance, one exit, and one solution to traverse it (I once solved a "difficult" 3D maze in a popup book by going only a few steps opposite the intended direction.

Question


As I siad, I think I have the basic idea for the algorithm, but I don't know how to code it. Can someone come up with a Java algorithm for this that accomplishes the task relatively quickly?

The solution must not use external libraries.


There are many maze generation algorithms that work quite well here, most of which are based on creating some sort of spanning tree in a graph of the 3D grid.

As an example, let's suppose that we have a 2D grid of cells (which I can actually render using ASCII art!) that looks like this:

*---*---*---*
|   |   |   |
*---*---*---*
|   |   |   |
*---*---*---*
|   |   |   |
*---*---*---*

We can think of this as a graph where each cell is a vertex and each of the connections between the cells is an edge. Our goal is to find some tree that connects all of the nodes. If we do this, then all cells will be reachable from one another (because a tree is connected), but there are no loops (because a tree is a minimally-connected graph). There are many different trees we could use; for example, here's one tree:

*---*---*---*
|   |   |   |
*   *   *   *
|   |   |   |
*   *   *   *
|   |   |   |
*   *   *   *

And here's another:

*   *---*   *
|   |   |   |
*---*   *   *
        |   |
*---*---*---*
    |       |
*---*   *---*

If you are looking for some sort of tree connecting the maze cells, one option would be to use a depth-first search over the graph, randomly ordering the edges that need to be visited. This strategy is a well-known maze generation algorithm and produces long, twisty mazes full of dead-ends and confusing branchings.

Another approach that's commonly used to create mazes is to reducing it to the problem of finding a minimum spanning tree of a graph. In particular, suppose you create a graph where every cell is a node with links to each of its neighbors. Choose random weights for each of the edges, and then construct a minimum spanning tree for the graph. This tree has no cycles and there's a unique path from each node to each other node, meaning that the maze has a unique solution. Moreover, the algorithm is very efficient - in a 3D cube of size n x n x n, then you have O(n3) nodes, O(n3) edges, and you can find the MST in O(n3 lg n) time using either Prim's algorithm or Kruskal's algorithm. These also produce high-quality mazes, though their properties are very different from the mazes created using the randomized depth-first search.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜