コード例 #1
0
ファイル: MazeSolver.cs プロジェクト: yonimn2000/maze
        public void SolveStep()
        {
            CurrentCell.IsVisited = true; //Step 2.1.4
            List <MazeCell> neighboringCells = GetUnvisitedNeighboringCellsList(CurrentCell, 1);

            if (neighboringCells.Count > 0)                                                  //Step 2.1
            {
                MazeCell chosenCell = neighboringCells[Random.Next(neighboringCells.Count)]; //Step 2.1.1
                CellsStack.Push(CurrentCell);                                                //Step 2.1.2
                CurrentCell = chosenCell;                                                    //Step 2.1.4
            }
            else if (CellsStack.Count > 0)                                                   //Step 2.2
            {
                CurrentCell = CellsStack.Pop();                                              //Step 2.2.1 and 2.2.2
            }
            else if (CellsStack.Count == 0)
            {
                throw new Exception("No solution.");
            }
            if (CurrentCell.X == Size.Width - 2 && CurrentCell.Y == Size.Height - 2)
            {
                Path.Add(new Point(CurrentCell.X, CurrentCell.Y));
                foreach (MazeCell cell in CellsStack)
                {
                    Path.Add(new Point(cell.X, cell.Y));
                }
                IsDone = true;
            }
        }
コード例 #2
0
ファイル: MazeGenerator.cs プロジェクト: yonimn2000/maze
        public void GenerateStep()
        {
            CurrentCell.IsVisited = true; //Step 2.1.4
            List <MazeCell> neighboringCells = GetUnvisitedNeighboringCellsList(CurrentCell, 2);

            if (neighboringCells.Count > 0)                                                  //Step 2.1
            {
                MazeCell chosenCell = neighboringCells[Random.Next(neighboringCells.Count)]; //Step 2.1.1
                CellsStack.Push(CurrentCell);                                                //Step 2.1.2
                RemoveWallBetween2Cells(CurrentCell, chosenCell);                            //Step 2.1.3
                CurrentCell = chosenCell;                                                    //Step 2.1.4
            }
            else if (CellsStack.Count > 0)                                                   //Step 2.2
            {
                CurrentCell = CellsStack.Pop();                                              //Step 2.2.1 and 2.2.2
            }
            else
            {
                IsDone = true;
            }

            void RemoveWallBetween2Cells(MazeCell currentCell, MazeCell chosenCell)
            {
                if (currentCell.X - chosenCell.X == -2)
                {
                    Cells[currentCell.Y, currentCell.X + 1].IsWall = false;
                }
                else if (currentCell.X - chosenCell.X == 2)
                {
                    Cells[currentCell.Y, currentCell.X - 1].IsWall = false;
                }
                else if (currentCell.Y - chosenCell.Y == -2)
                {
                    Cells[currentCell.Y + 1, currentCell.X].IsWall = false;
                }
                else if (currentCell.Y - chosenCell.Y == 2)
                {
                    Cells[currentCell.Y - 1, currentCell.X].IsWall = false;
                }
            }
        }