/// <summary> /// The generate. /// </summary> public void Generate() { while (this.VisitedCells < this.TotalCells) { // get a list of the neighboring cells with all walls intact var AdjacentCells = this.GetNeighborsWithWalls(this.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 var randomCell = Cell.TheRandom.Next(0, AdjacentCells.Count); var theCell = (Cell)AdjacentCells[randomCell]; this.CurrentCell.KnockDownWall(theCell); this.CellStack.Push(this.CurrentCell); // push the current cell onto the stack this.CurrentCell = theCell; // make the random neighbor the new current cell this.VisitedCells++; } else { // No cells with walls intact, pop current cell from stack this.CurrentCell = (Cell)this.CellStack.Pop(); } } }
/// <summary> /// The find adjacent wall. /// </summary> /// <param name="theCell"> /// The the cell. /// </param> /// <returns> /// The <see cref="int"/>. /// </returns> public int FindAdjacentWall(Cell theCell) { if (theCell.Row == this.Row) { if (theCell.Column < this.Column) return 0; return 2; } if (theCell.Row < this.Row) return 1; return 3; }
/// <summary> /// The initialize. /// </summary> public void Initialize() { this.Cells = new Cell[_Dimension, _Dimension]; this.TotalCells = _Dimension * _Dimension; for (var i = 0; i < _Dimension; i++) for (var j = 0; j < _Dimension; j++) { this.Cells[i, j] = new Cell(); this.Cells[i, j].Row = i; this.Cells[i, j].Column = j; } this.CurrentCell = this.Cells[0, 0]; this.VisitedCells = 1; this.CellStack.Clear(); }
/// <summary> /// The get neighbors with walls. /// </summary> /// <param name="aCell"> /// The a cell. /// </param> /// <returns> /// The <see cref="ArrayList"/>. /// </returns> private ArrayList GetNeighborsWithWalls(Cell aCell) { var Neighbors = new ArrayList(); for (var countRow = -1; countRow <= 1; countRow++) for (var countCol = -1; countCol <= 1; countCol++) { if ((aCell.Row + countRow < _Dimension) && (aCell.Column + countCol < _Dimension) && (aCell.Row + countRow >= 0) && (aCell.Column + countCol >= 0) && ((countCol == 0) || (countRow == 0)) && (countRow != countCol) ) { if (this.Cells[aCell.Row + countRow, aCell.Column + countCol].HasAllWalls()) { Neighbors.Add(this.Cells[aCell.Row + countRow, aCell.Column + countCol]); } } } return Neighbors; }
/// <summary> /// The knock down wall. /// </summary> /// <param name="theCell"> /// The the cell. /// </param> public void KnockDownWall(Cell theCell) { // find adjacent wall var theWallToKnockDown = this.FindAdjacentWall(theCell); this.Walls[theWallToKnockDown] = 0; var oppositeWall = (theWallToKnockDown + 2) % 4; theCell.Walls[oppositeWall] = 0; }