public void Generate() { while (VisitedCells < TotalCells) { // get a list of the neighboring cells with all walls intact ArrayList AdjacentCells = GetNeighborsWithWalls(CurrentCell); // test if a cell like this exists if (AdjacentCells.Count > 0) { // yes, choose one of them, and knock down the wall between it and the current cell int randomCell = Cell.TheRandom.Next(0, AdjacentCells.Count); Cell theCell = ((Cell)AdjacentCells[randomCell]); CurrentCell.KnockDownWall(theCell); CellStack.Push(CurrentCell); // push the current cell onto the stack CurrentCell = theCell; // make the random neighbor the new current cell VisitedCells++; } else { // No cells with walls intact, pop current cell from stack CurrentCell = (Cell)CellStack.Pop(); } } }
public void KnockDownWall(Cell theCell) { // find adjacent wall int theWallToKnockDown = FindAdjacentWall(theCell); Walls[theWallToKnockDown] = 0; int oppositeWall = (theWallToKnockDown + 2) % 4; theCell.Walls[oppositeWall] = 0; }
public int FindAdjacentWall(Cell theCell) { if (theCell.Row == Row) { if (theCell.Column < Column) return 0; else return 2; } else // columns are the same { if (theCell.Row < Row) return 1; else return 3; } }
private ArrayList GetNeighborsWithWalls(Cell aCell) { ArrayList Neighbors = new ArrayList(); int count = 0; for (int countRow = -1; countRow <= 1; countRow++) for (int countCol = -1; countCol <= 1; countCol++) { if ( (aCell.Row + countRow < kDimension) && (aCell.Column+countCol < kDimension) && (aCell.Row+countRow >= 0) && (aCell.Column+countCol >= 0) && ((countCol == 0) || (countRow == 0)) && (countRow != countCol) ) { if (Cells[aCell.Row+countRow, aCell.Column+countCol].HasAllWalls()) { Neighbors.Add( Cells[aCell.Row+countRow, aCell.Column+countCol]); } } } return Neighbors; }
public void Initialize() { Cells = new Cell[kDimension, kDimension]; TotalCells = kDimension * kDimension; for (int i = 0; i < kDimension; i++) for (int j = 0; j < kDimension; j++) { Cells[i,j] = new Cell(); Cells[i, j].Row = i; Cells[i, j].Column = j; } CurrentCell = Cells[0,0]; VisitedCells = 1; CellStack.Clear(); }