public Wall(Cell A, Cell B) { this.A = A; this.B = B; }
public Cell(int x, int y) { up = down = left = right = previous = null; inMaze = false; position = new Vector2(x, y); }
private void AddWallsToList(Cell cell) { // Add left wall if (cell.left == null) { if (cell.position.X != 0) wallList.Add(new Wall(cell, maze[(int)cell.position.X - 1, (int)cell.position.Y])); else wallList.Add(new Wall(cell, null)); } // Add up wall if (cell.up == null) { if (cell.position.Y != 0) wallList.Add(new Wall(cell, maze[(int)cell.position.X, (int)cell.position.Y - 1])); else wallList.Add(new Wall(cell, null)); } // Add right wall if (cell.right == null) { if (cell.position.X != size - 1) wallList.Add(new Wall(cell, maze[(int)cell.position.X + 1, (int)cell.position.Y])); else wallList.Add(new Wall(cell, null)); } // Add down wall if (cell.down == null) { if (cell.position.Y != size - 1) wallList.Add(new Wall(cell, maze[(int)cell.position.X, (int)cell.position.Y + 1])); else wallList.Add(new Wall(cell, null)); } }
private void FindShortestPath() { Queue<Cell> searchQueue = new Queue<Cell>(); shortestPath = new List<Cell>(); Cell curCell = new Cell(0,0); Vector2 endPosition = new Vector2(size - 1, size - 1); maze[0, 0].visited = true; searchQueue.Enqueue(maze[0, 0]); while (searchQueue.Count != 0) { curCell = searchQueue.Dequeue(); // This cell is the end if (curCell.position.Equals(endPosition)) { break; } // Enqueue all the successors of this node if (curCell.up != null) { if (!curCell.up.visited) { curCell.up.visited = true; curCell.up.previous = curCell; searchQueue.Enqueue(curCell.up); } } if (curCell.left != null) { if (!curCell.left.visited) { curCell.left.visited = true; curCell.left.previous = curCell; searchQueue.Enqueue(curCell.left); } } if (curCell.down != null) { if (!curCell.down.visited) { curCell.down.visited = true; curCell.down.previous = curCell; searchQueue.Enqueue(curCell.down); } } if (curCell.right != null) { if (!curCell.right.visited) { curCell.right.visited = true; curCell.right.previous = curCell; searchQueue.Enqueue(curCell.right); } } } // After we find the end, reconstruct the path from the end back to the beginning // (curCell will currently be the end) while (curCell != null) { shortestPath.Insert(0, curCell); if (curCell.previous != null) curCell.previous.next = curCell; curCell = curCell.previous; } }
public Maze(int size) { maze = new Cell[size, size]; wallList = new List<Wall>(); this.size = size; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { maze[i, j] = new Cell(i, j); } } r = new Random(); GenerateMaze(); SetCellTypes(); FindShortestPath(); }