Question

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.

enter image description here

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top