public Cell FindAdjacent(Cell currNode, Cell.s_Directions dir) { int targetRow = currNode.m_RowIdx + Cell.s_DirectionMapping[(int)dir, 0]; int targetCol = currNode.m_ColIdx + Cell.s_DirectionMapping[(int)dir, 1]; if (targetRow < 0 || targetRow >= m_RowCount || targetCol < 0 || targetCol >= m_ColCount) { return(null); } return(m_Cells[targetRow, targetCol]); }
public void GenerateMaze(Grid grid) { List <Cell> mySet = new List <Cell>(); Dictionary <Cell, Cell> visitedSet = new Dictionary <Cell, Cell>(); s_Random = new System.Random(System.Guid.NewGuid().GetHashCode()); int row = s_Random.Next(0, grid.m_RowCount); int col = s_Random.Next(0, grid.m_ColCount); mySet.Add(grid.m_Cells[row, col]); Cell.s_Directions[] randomPickDir = new Cell.s_Directions[4] { Cell.s_Directions.N, Cell.s_Directions.E, Cell.s_Directions.W, Cell.s_Directions.S }; while (mySet.Count > 0) { Cell currCell = GetNextCell(mySet); Shuffle(randomPickDir, 0); bool findNextNode = false; foreach (var dir in randomPickDir) { var randomNeighbor = grid.FindAdjacent(currCell, dir); if (randomNeighbor != null && !visitedSet.ContainsKey(randomNeighbor)) { currCell.Link(randomNeighbor); mySet.Add(randomNeighbor); visitedSet.Add(randomNeighbor, randomNeighbor); findNextNode = true; } } if (!findNextNode) { mySet.Remove(currCell); } } }