Prim's algorithm for generating a maze: Getting the neighbour cell
I'm basing a maze generator program on the Prim's algorithm:
Blockquote
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. _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" 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!
 
No comments:
Post a Comment