public Maze(int rows, int cols) { Rows = rows; Columns = cols; Labrynth = new MazeCell[rows, cols]; }
/// <summary> /// Create a maze. /// </summary> public void CreateMaze() { for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { Labrynth[i, j] = new MazeCell(i, j, this); } } // now, remove walls int numconnected = 1; Labrynth[0, 0].IsConnected = true; var a = new List <MazeCell> { Labrynth[0, 0] }; // the fringe MazeCell currentCell; while (numconnected < (Rows * Columns)) { int r = Random.Range(0, a.Count); currentCell = (MazeCell)a[r]; int count = 0; if (currentCell.CanGoUp() || currentCell.CanGoDown() || currentCell.CanGoLeft() || currentCell.CanGoRight()) { // we can knock down a wall // count the number of ways we can go, and then pick a wall to knock down. if (currentCell.CanGoUp()) { count++; } if (currentCell.CanGoDown()) { count++; } if (currentCell.CanGoLeft()) { count++; } if (currentCell.CanGoRight()) { count++; } int temp = Random.Range(0, count); if (currentCell.CanGoUp()) { temp--; if (temp == -1) { currentCell.UpCell.IsConnected = true; currentCell.UpCell.Bottom = false; a.Add(currentCell.UpCell); numconnected++; } } if (currentCell.CanGoDown()) { temp--; if (temp == -1) { currentCell.DownCell.IsConnected = true; currentCell.Bottom = false; a.Add(currentCell.DownCell); numconnected++; } } if (currentCell.CanGoLeft()) { temp--; if (temp == -1) { currentCell.LeftCell.IsConnected = true; currentCell.LeftCell.Right = false; a.Add(currentCell.LeftCell); numconnected++; } } if (currentCell.CanGoRight()) { temp--; if (temp == -1) { currentCell.RightCell.IsConnected = true; currentCell.Right = false; a.Add(currentCell.RightCell); numconnected++; } } } else // remove that MazeCell { a.RemoveAt(r); } } }
/// <summary> /// Create a maze. /// </summary> public void CreateMaze() { for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { Labrynth[i, j] = new MazeCell(i, j, this); } } // now, remove walls int numconnected = 1; Labrynth[0, 0].IsConnected = true; var a = new List<MazeCell> { Labrynth[0, 0] }; // the fringe MazeCell currentCell; while (numconnected < (Rows * Columns)) { int r = Random.Range(0, a.Count); currentCell = (MazeCell)a[r]; int count = 0; if (currentCell.CanGoUp() || currentCell.CanGoDown() || currentCell.CanGoLeft() || currentCell.CanGoRight()) { // we can knock down a wall // count the number of ways we can go, and then pick a wall to knock down. if (currentCell.CanGoUp()) { count++; } if (currentCell.CanGoDown()) { count++; } if (currentCell.CanGoLeft()) { count++; } if (currentCell.CanGoRight()) { count++; } int temp = Random.Range(0, count); if (currentCell.CanGoUp()) { temp--; if (temp == -1) { currentCell.UpCell.IsConnected = true; currentCell.UpCell.Bottom = false; a.Add(currentCell.UpCell); numconnected++; } } if (currentCell.CanGoDown()) { temp--; if (temp == -1) { currentCell.DownCell.IsConnected = true; currentCell.Bottom = false; a.Add(currentCell.DownCell); numconnected++; } } if (currentCell.CanGoLeft()) { temp--; if (temp == -1) { currentCell.LeftCell.IsConnected = true; currentCell.LeftCell.Right = false; a.Add(currentCell.LeftCell); numconnected++; } } if (currentCell.CanGoRight()) { temp--; if (temp == -1) { currentCell.RightCell.IsConnected = true; currentCell.Right = false; a.Add(currentCell.RightCell); numconnected++; } } } else // remove that MazeCell { a.RemoveAt(r); } } }