private string SearchForEligibleDirectionFromCell( MazeCell inputCell, Dictionary <string, MazeCell> mazeCellsVisited ) { List <string> shuffledDirections = Functions.ShuffleStringList(MazeCell.directions); foreach (string direction in shuffledDirections) { var nCell = inputCell.GetNeighborMazeCell(direction); if (nCell != null) { string nCellPos = this.GetFormattedPosition(nCell.position_X, nCell.position_Y); var wall = inputCell.GetMazeWall(direction); if (wall == null) { Debug.Log("wall not found.."); } bool neighborCellIsEligible = ( !mazeCellsVisited.ContainsKey(nCellPos) && wall.isActive ); if (neighborCellIsEligible) { return(direction); } } } return(null); }
private Maze ApplyDepthFirstBacktrackingToMaze(Maze maze) { var backtrackMem = new Stack(); var mazeCellsVisited = new Dictionary <string, MazeCell>(); // select random cell to start and memorize MazeCell currentMazeCell = this.SelectRandomMazeCell(maze); MazeCell initialMazeCell = currentMazeCell; do { // mark current maze cell as visited string currPos = this.GetFormattedPosition( currentMazeCell.position_X, currentMazeCell.position_Y ); if (!mazeCellsVisited.ContainsKey(currPos)) { mazeCellsVisited.Add(currPos, currentMazeCell); } // check for eligible direction string eligibleDirection = this.SearchForEligibleDirectionFromCell( currentMazeCell, mazeCellsVisited ); if (eligibleDirection != null) { // per the direction, remove the wall, memorize current maze cell, // and move to neighbor maze cell var nCell = currentMazeCell.GetNeighborMazeCell(eligibleDirection); var wall = currentMazeCell.GetMazeWall(eligibleDirection); wall.isActive = false; backtrackMem.Push(currentMazeCell); currentMazeCell = nCell; } else { // backtrack one cell and try again in next loop currentMazeCell = (MazeCell)backtrackMem.Pop(); } } while(currentMazeCell != initialMazeCell); return(maze); }