private void createMazeWithDFSBacktracking() { _grid = new Grid(SizeX, SizeZ); Random rand = new Random(); // Set up stack for DFS. var cellStack = new Stack<CellWithPosition>(); // Need to track number of unvisited cells for DFS. Decrement each time we visit a new cell. int numOfUnvisitedCells = SizeX * SizeZ; // Pick a random initial cell as currentCell and mark as visited. var randX = rand.Next(0, SizeX); var randZ = rand.Next(0, SizeZ); var currentCell = new CellWithPosition { Cell = Matrix[randX, randZ], XPos = randX, ZPos = randZ }; currentCell.Cell.IsInMaze = true; --numOfUnvisitedCells; while (numOfUnvisitedCells != 0) { // Choose randomly one of the unvisited neighbours, or check for the absence of any. var randomUnvisitedNeighborOfCurrent = chooseRandomUnvisitedNeighbor(currentCell.XPos, currentCell.ZPos); // Check if the current cell has any neighbours which have not been visited. if (randomUnvisitedNeighborOfCurrent.CellWithPosition.Cell != null) { // Push current cell on to the stack. cellStack.Push(currentCell); // Remove the wall between the current cell and the chosen cell. _grid.BreakWall(currentCell.XPos, currentCell.ZPos, randomUnvisitedNeighborOfCurrent.RelativeLocation); // Make the chosen cell the current cell and mark it as visited. currentCell = randomUnvisitedNeighborOfCurrent.CellWithPosition; currentCell.Cell.IsInMaze = true; --numOfUnvisitedCells; } else if (cellStack.Count != 0) { // Pop a cell off stack and make it the current cell. currentCell = cellStack.Pop(); } } }
public void BreakWalls_MatchPattern() { // Arrange var knownGoodMatrix = new Cell[3, 3]; Cell SWCell = new Cell { HasNorthWall = false, HasSouthWall = true, HasEastWall = false, HasWestWall = true }; Cell SCell = new Cell { HasNorthWall = false, HasSouthWall = true, HasEastWall = false, HasWestWall = false }; Cell SECell = new Cell { HasNorthWall = false, HasSouthWall = true, HasEastWall = true, HasWestWall = false }; Cell WCell = new Cell { HasNorthWall = false, HasSouthWall = false, HasEastWall = false, HasWestWall = true }; Cell CenterCell = new Cell { HasNorthWall = false, HasSouthWall = false, HasEastWall = false, HasWestWall = false }; Cell ECell = new Cell { HasNorthWall = false, HasSouthWall = false, HasEastWall = true, HasWestWall = false }; Cell NWCell = new Cell { HasNorthWall = true, HasSouthWall = false, HasEastWall = false, HasWestWall = true }; Cell NCell = new Cell { HasNorthWall = true, HasSouthWall = false, HasEastWall = false, HasWestWall = false }; Cell NECell = new Cell { HasNorthWall = true, HasSouthWall = false, HasEastWall = true, HasWestWall = false }; knownGoodMatrix[0, 0] = SWCell; knownGoodMatrix[1, 0] = SCell; knownGoodMatrix[2, 0] = SECell; knownGoodMatrix[0, 1] = WCell; knownGoodMatrix[1, 1] = CenterCell; knownGoodMatrix[2, 1] = ECell; knownGoodMatrix[0, 2] = NWCell; knownGoodMatrix[1, 2] = NCell; knownGoodMatrix[2, 2] = NECell; var target = new Grid(3, 3); // Act target.BreakWall(0,0,GridDirection.East); target.BreakWall(0, 0, GridDirection.North); target.BreakWall(0, 1, GridDirection.East); target.BreakWall(0, 1, GridDirection.North); target.BreakWall(0, 2, GridDirection.East); target.BreakWall(2,0, GridDirection.West); target.BreakWall(2, 0, GridDirection.North); target.BreakWall(2, 1, GridDirection.West); target.BreakWall(2, 1, GridDirection.North); target.BreakWall(2, 2, GridDirection.West); target.BreakWall(1, 2, GridDirection.South); target.BreakWall(1, 0, GridDirection.North); // Assert for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { Assert.AreEqual(knownGoodMatrix[i, j], target.Matrix[i, j], $"Cell at [{i},{j}] doesn't match."); } } }