Maze generation prim algorithm not all cells are traversed

陌路散爱 提交于 2019-12-10 10:13:01

问题


I'm trying to implement Prim maze generation algorithm:

  • Start with a grid full of walls.
  • Pick a cell, mark it as part of the maze. Add the walls of the cell to the wall list.
  • While there are walls in the list:
    • Pick a random wall from the list. If the cell on the opposite side
      isn't in the maze yet:
      • Make the wall a passage and mark the cell on the opposite side as part of the maze.
      • Add the neighboring walls of the cell to the wall list.
    • If the cell on the opposite side already was in the maze, remove the wall from the list.

Removing some implementation details my implementation looks like this:

Cell[][] maze 

is the matrix with cells. each cell has left/right/up/button walls. the frontiers walls marked as boolean frontier and are not part of implementation because I want to keep my maze framed.

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

My problem is that my maze is not completed and not all cells are traversed. Sometimes a few cells only traversed, almost all cells are done. I believe I'm missing something bur can't figure out what.

Please help.

See picture below for partial traversed maze.


回答1:


Well, I solved this issue. This pure Java issue and has nothing to do with algorithm. I compared between two walls.

public class Wall{

    Point p1;
    Point p2;
    }

    public class Point{
    int x;
    int y;
    }

If I implement Wall class equals() and hashCode() using p1 and p2. then in leftCell.rightWall will be equal to rightCell.leftWall and it was the problem.



来源:https://stackoverflow.com/questions/11459752/maze-generation-prim-algorithm-not-all-cells-are-traversed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!