我正在尝试实现Prim迷宫生成算法:
我的问题是我的迷宫没有完成,也没有遍历所有的单元格。有时只遍历了几个单元格,几乎所有的单元格都已经完成了。我相信我缺少了一些东西,但是无法弄清楚缺少什么。
请帮忙看一下下面部分遍历的迷宫图片。
- 开始时,建立一个全是墙的网格。
- 选择一个单元格,将其标记为迷宫的一部分。将单元格的墙添加到墙列表中。
- 当列表中有墙时:
- 从列表中随机选择一面墙。如果另一侧的单元格还没有在迷宫中:
- 将墙变成通道,并将另一侧的单元格标记为迷宫的一部分。
- 将相邻单元格的墙添加到墙列表中。
- 如果另一侧的单元格已经在迷宫中,则从列表中移除该墙。
- 从列表中随机选择一面墙。如果另一侧的单元格还没有在迷宫中:
Cell[][] maze
迷宫是由单元格组成的矩阵。每个单元格都有左、右、上、下四面墙。前沿的墙壁被标记为
boolean frontier
,但不是实现的一部分,因为我希望保持迷宫的边框。public Cell[][] prim(){
List<Wall> walls = new ArrayList<Wall>();
//Pick a cell, mark it as part of the maze
int initialCellI = rnd(sizeX)-1;
int initialCellJ = rnd(sizeY)-1;
Cell randomCell = maze[initialCellI][initialCellJ];
randomCell.setPartOftheMaze(true);
//Add the walls of the cell to the wall list.
if ((randomCell.getLeft() != null) && (!randomCell.getLeft().isFrontier()))
walls.add(randomCell.getLeft());
if ((randomCell.getRight() != null) && (!randomCell.getRight().isFrontier()))
walls.add(randomCell.getRight());
if ((randomCell.getButtom() != null) && (!randomCell.getButtom().isFrontier()))
walls.add(randomCell.getButtom());
if ((randomCell.getUp() != null) && (!randomCell.getUp().isFrontier()))
walls.add(randomCell.getUp());
//While there are walls in the list:
while (!walls.isEmpty()){
//Pick a random wall from the list.
Wall randomWall = randomElement(walls);
//pick the cell opposite to this wall.
Cell opositeSideCell = getNeightbourCell(randomWall, maze);
if (opositeSideCell.isPartOftheMaze()){
//If the cell on the opposite side already was in the maze, remove the wall from the list.
walls.remove(randomWall);
}
else{
// Make the wall a passage and mark the cell on the opposite side as part of the maze.
this.removeWall(randomWall, maze);
opositeSideCell.setPartOftheMaze(true);
//Add the walls of the cell to the wall list.
if ((opositeSideCell.getLeft() != null) && (!opositeSideCell.getLeft().isFrontier()))
walls.add(opositeSideCell.getLeft());
if ((opositeSideCell.getRight() != null) && (!opositeSideCell.getRight().isFrontier()))
walls.add(opositeSideCell.getRight());
if ((opositeSideCell.getButtom() != null) && (!opositeSideCell.getButtom().isFrontier()))
walls.add(opositeSideCell.getButtom());
if ((opositeSideCell.getUp() != null) && (!opositeSideCell.getUp().isFrontier()))
walls.add(opositeSideCell.getUp());
}
}
return maze;
}
我的问题是我的迷宫没有完成,也没有遍历所有的单元格。有时只遍历了几个单元格,几乎所有的单元格都已经完成了。我相信我缺少了一些东西,但是无法弄清楚缺少什么。
请帮忙看一下下面部分遍历的迷宫图片。
![enter image description here](https://istack.dev59.com/N5Vwe.gif)