I'm basing a maze generator program on the Prim's algorithm:
This algorithm is a randomized version of Prim's 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.
(from Wikipedia)
I understand the algorithm already, I'm just stuck on this part: "Knowing if the neighbour cell forms part of the maze or not" (this means, Getting the neighbour cell first) As the cells are actually nodes of a tree (the maze, a bi-dimensional array of Cells) and the walls are the edges between those nodes, I thought it would be necessary to identify each wall with a pair of points (x,y). How do I know if two Cells are connected by a wall? (remember that each cell has 4 walls)
I thought about using the equals() function. I'm just asking for a pseudo-code or your best explanation that would make the things easier.
My Wall class has three attributes: bool isWall(determines if it is a wall or a passage between cells); int x; int y (identifiers).
If you think that I would need more attributes I'll be pleased to know. I know there's an easy way, I'm just stuck ;) Thanks for your time!
Lets see what we can define:
Cell[][] maze; // prepopulate with cells in a rectangular fashion.
// Populate adjacent cells with walls.
{
maze = new Cell[m][n];
for (i = 0 .. m) {
for (j = 0 .. n) {
cell = maze[i][j] = new Cell(m, n);
// fix top wall
if (i == 0) { // first row
cell.wall[0] = new Wall();
cell.wall[0].setIsEdge();
} else {
cell.wall[0] = maze[i-1][j].wall[2]; // my up is last row's down
}
// fix bottom wall
cell.wall[2] = new Wall();
if (i == m-1) { // last row
cell.wall[2].setIsEdge();
}
// fix left wall
if (j == 0) { // leftmost column
cell.wall[3] = new Wall();
cell.wall[3].setIsEdge();
} else {
cell.wall[3] = maze[i][j-1].wall[1]; // my left is last col's right
}
// fix right wall
cell.wall[1] = new Wall();
if (j == n-1) { // rightmost column
cell.wall[1].setIsEdge();
}
}
}
}
List walls = new List();
class Cell {
Wall[] walls = new Wall[4]; // 0 is up, 1 is right, 2 is down, 3 is left (count clockwise)
boolean isInMaze = false;
int x;
int y;
Cell(col, row) {
x = col;
y = row;
}
}
class Wall {
boolean isOpen = false; // open walls are passages
boolean isEdge = false; // edges have nothing on the other side.
Cell[] cells = new Cell[2];
}
Cell aCell = maze[randomx][randomy]; // make sure x,y in bounds
initializeCellInMaze(aCell, null);
while (!walls.isEmpty()) {
aWall = walls.get(randomWall); // in range
if (!(aWall.cell[0].isInMaze && aWall.cell[1].isInMaze)) {
// opposite cell NOT in maze
wall.setIsOpen();
Cell otherCell;
if (wall.cell[0].isInMaze) {
otherCell = wall.cell[1];
} else {
otherCell = wall.cell[0];
}
initializeCellInMaze(otherCell, aWall);
}
walls.remove(aWall); // or use index
}
initializeCellInMaze(cell, wallToSkip) {
cell.setIsInMaze();
for (i = 0 .. 3) if (cell.wall[i] != wallToSkip) walls.add(cell.wall[i]);
}
I can't comment to add to the discussion so ill just reply with an answer. Basically Lee Meandor has the right idea.

This is the basic structure of the cell to wall to cell relation.
So a cell has a north south west and east wall.
A wall is between two adjacent cells connecting them.
Class Cell{
Wall North;
Wall South;
Wall East;
Wall West;
}
Class Wall{
// You can store the cells that are connected to the wall but it's not necessary.
Cell 1;
Cell 2;
bool isUP;
}
The important thing to keep in mind is for the Cells to be pointing to the correct walls.
That's some important logic work :).
If you need help with that just drop a comment.
Well I spent all the afternoon coding the maze generator, basing on your help. Now I'm getting this kind of mazes (sorry for the big screen-shot I don't have a pic editor here.

I think the problem is how i'm returning the neighbour cell. Maybe not. This is how I'm returning the neighbour cell, I did it the @progenhard way, the Wall has 2 cells, cell1 and cell2, if cell1 is occupied, return cell2 and vice-versa:
public Cell getNeighbourCell(Wall wall, Cell cell) {
if (wall.getCell1().equals(cell)) {
return wall.getCell2();
} else {
return wall.getCell1();
}
}
Thanks again!
来源:https://stackoverflow.com/questions/18242689/prims-algorithm-for-generating-a-maze-getting-the-neighbour-cell