java生成迷宫prim_迷宫生成prim算法并非遍历所有细胞

我正在尝试实现Prim迷宫生成算法:

从一个满墙的网格开始 .

选择一个单元格,将其标记为迷宫的一部分 . 将单元格的墙添加到墙列表中 .

虽然列表中有墙:

从列表中选择一个随机墙 . 如果细胞在另一侧

尚未进入迷宫:

将墙壁作为通道,并将对面的单元格标记为迷宫的一部分 .

将单元格的相邻墙添加到墙列表中 .

如果对面的单元格已经在迷宫中,请从列表中删除墙壁 .

删除一些实现细节,我的实现如下所示:

Cell[][] maze

是带有细胞的矩阵 . 每个单元格都有左/右/上/按钮墙 . 边界墙标记为 boolean frontier ,并不是实施的一部分,因为我想保持我的迷宫框架 .

public Cell[][] prim(){

List walls = new ArrayList();

//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;

}

我的问题是我的迷宫没有完成,并没有遍历所有的细胞 . 有时只有几个细胞遍历,几乎所有细胞都已完成 . 我相信我错过了什么bur无法弄清楚是什么 .

请帮忙 .

部分遍历的迷宫见下图 .

71a749dd2835dc123b52cffc2d7be95f.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值