コード例 #1
0
        private void CarveMaze(Maze maze, Point2D current, Random rnd)
        {
            if (maze.hasWallAt(current))
            {
                MazeWall wall = new MazeWall(current);
                maze.removeWall(wall);
            }

            List <Point2D> possiblePoints = GetPossibleNextPoints(maze, current);

            Console.WriteLine("Possible Next Moves: " + possiblePoints.Count);

            if (possiblePoints.Count > 0)
            {
                int     randomIndex = rnd.Next(possiblePoints.Count);
                Point2D nextPoint   = possiblePoints[randomIndex];

                CarveMaze(maze, nextPoint, rnd);
            }
        }
コード例 #2
0
        public Maze GenerateRandomMaze(int w, int h, Random rnd, int maxIterations = 1000)
        {
            Maze maze = new Maze(w, h);

            FillMaze(maze);

            int     randomY1 = (rnd.Next((h - 1) / 2) * 2) + 1;
            Point2D start    = new Point2D(0, randomY1);

            CarveMaze(maze, start, rnd);

            List <Point2D> possibleBranches = GetPossibleNextBranches(maze);
            int            iterations       = 0;

            while (possibleBranches.Count > 0 & iterations < maxIterations)
            {
                iterations++;
                Console.WriteLine("Iterations: " + iterations);
                int randomIndex = rnd.Next(possibleBranches.Count);
                CarveMaze(maze, possibleBranches[randomIndex], rnd);

                possibleBranches = GetPossibleNextBranches(maze);
                Console.WriteLine("Possible Next Branches: " + possibleBranches.Count);
            }

            List <Point2D> possibleExits = GetPossibleExits(maze);

            if (possibleExits.Count == 0)
            {
                Console.WriteLine("Skipping maze exit because we have no possible nearby walls.");
            }
            else
            {
                int randomExitIndex = rnd.Next(possibleExits.Count);
                maze.removeWall(new MazeWall(possibleExits[randomExitIndex]));
            }

            return(maze);
        }