C#我的迷宫生成器似乎无法处理具有三个开口侧的块

问题描述

我正在尝试用C#创建一个迷宫生成器。即使这些单元格沿新路径前进,也不会将它们设置为CLOSED_TOP,CLOSED_BottOM,CLOSED_LEFT,CLOSED_RIGHT或OPEN。

我正在实现一种DFS算法来生成迷宫:

  1. 选择网格中的任何随机单元格(在此实现中,我们使用左上角。)
  2. 找到一个尚未被访问的随机相邻单元格。
  3. 如果找到一个,则在当前单元格和相邻单元格之间剥离墙。
  4. 如果找不到,请返回上一个单元格。
  5. 为网格中的每个单元重复步骤2和3(或步骤2和4)。

当我返回到先前的单元格(单元格A)并继续到尚未访问过的新的相邻单元格(单元格B)时,就会出现问题。我知道我的代码会访问新的单元格(单元格B)并继续沿该路径运行,但是它从未将前一个单元格(单元格A)标记为具有三个开放边。

我不知道我的逻辑出了什么问题。我知道整个网格都已填充。我在做什么错了?

using System;
using System.Collections.Generic;
using System.Text;

namespace MazeGenerator
{
    class MapGenerator
    {
        private Random random = new Random();

        public const int CLOSED = -1;

        public const int OPEN_TOP = 0;
        public const int OPEN_LEFT = 1;
        public const int OPEN_RIGHT = 2;
        public const int OPEN_BottOM = 3;

        public const int OPEN_TOP_LEFT = 4;
        public const int OPEN_TOP_RIGHT = 5;
        public const int OPEN_TOP_BottOM = 6;
        public const int OPEN_LEFT_RIGHT = 7;
        public const int OPEN_LEFT_BottOM = 8;
        public const int OPEN_RIGHT_BottOM = 9;

        public const int CLOSED_TOP = 10;
        public const int CLOSED_LEFT = 11;
        public const int CLOSED_RIGHT = 12;
        public const int CLOSED_BottOM = 13;

        public const int OPEN = 14;

        public int[] StartingPoint { get; }
        public int[] EndingPoint { get; }

        public int NumCols { get; set; }
        public int NumRows { get; set; }

        public int[,] MapGrid { get; }

        public MapGenerator(int numRows,int numCols)
        {
            NumCols = numCols;
            NumRows = numRows;

            MapGrid = new int[numRows,numCols];
            for (int i = 0; i < numRows; i++)
            {
                for (int j = 0; j < numCols; j++)
                {
                    MapGrid[i,j] = CLOSED;
                }
            }

            StartingPoint = new int[] { 0,0 };
            EndingPoint = new int[] { numRows - 1,numCols - 1 };

            GodownPath(0,0);
        }

        private void GodownPath(int row,int col)
        {
            int cellState = MapGrid[row,col];

            while (NumberUnvisitedneighbors(row,col) > 0)
            {
                // go down this path
                int nextCellWall = random.Next(0,4);

                if (nextCellWall == 0)
                {
                    // go up
                    if (row > 0 && MapGrid[row - 1,col] == CLOSED)
                    {
                        switch (cellState)
                        {
                            case CLOSED:
                                MapGrid[row,col] = OPEN_TOP;
                                break;

                            case OPEN_LEFT:
                                MapGrid[row,col] = OPEN_TOP_LEFT;
                                break;
                            case OPEN_RIGHT:
                                MapGrid[row,col] = OPEN_TOP_RIGHT;
                                break;
                            case OPEN_BottOM:
                                MapGrid[row,col] = OPEN_TOP_BottOM;
                                break;

                            case OPEN_LEFT_RIGHT:
                                MapGrid[row,col] = CLOSED_BottOM;
                                break;
                            case OPEN_LEFT_BottOM:
                                MapGrid[row,col] = CLOSED_RIGHT;
                                break;
                            case OPEN_RIGHT_BottOM:
                                MapGrid[row,col] = CLOSED_LEFT;
                                break;

                            case CLOSED_TOP:
                                MapGrid[row,col] = OPEN;
                                break;

                            default:
                                MapGrid[row,col] = OPEN;
                                break;
                        }

                        MapGrid[row - 1,col] = OPEN_BottOM;
                        GodownPath(row - 1,col);
                    }
                } else if (nextCellWall == 1)
                {
                    // go down
                    if (row < NumRows - 1 && MapGrid[row + 1,col] = OPEN_BottOM;
                                break;

                            case OPEN_TOP:
                                MapGrid[row,col] = OPEN_TOP_BottOM;
                                break;
                            case OPEN_LEFT:
                                MapGrid[row,col] = OPEN_LEFT_BottOM;
                                break;
                            case OPEN_RIGHT:
                                MapGrid[row,col] = OPEN_RIGHT_BottOM;
                                break;

                            case OPEN_TOP_LEFT:
                                MapGrid[row,col] = CLOSED_RIGHT;
                                break;
                            case OPEN_TOP_RIGHT:
                                MapGrid[row,col] = CLOSED_LEFT;
                                break;
                            case OPEN_LEFT_RIGHT:
                                MapGrid[row,col] = CLOSED_TOP;
                                break;

                            case CLOSED_BottOM:
                                MapGrid[row,col] = OPEN;
                                break;
                        }

                        MapGrid[row + 1,col] = OPEN_TOP;
                        GodownPath(row + 1,col);
                    }
                } else if (nextCellWall == 2)
                {
                    // go left
                    if (col > 0 && MapGrid[row,col - 1] == CLOSED)
                    {
                        switch (cellState)
                        {
                            case CLOSED:
                                MapGrid[row,col] = OPEN_LEFT;
                                break;

                            case OPEN_TOP:
                                MapGrid[row,col] = OPEN_LEFT_RIGHT;
                                break;
                            case OPEN_BottOM:
                                MapGrid[row,col] = OPEN_LEFT_BottOM;
                                break;

                            case OPEN_TOP_RIGHT:
                                MapGrid[row,col] = CLOSED_BottOM;
                                break;
                            case OPEN_TOP_BottOM:
                                MapGrid[row,col] = CLOSED_TOP;
                                break;

                            case CLOSED_LEFT:
                                MapGrid[row,col] = OPEN;
                                break;
                        }

                        MapGrid[row,col - 1] = OPEN_RIGHT;
                        GodownPath(row,col - 1);
                    }
                } else if (nextCellWall == 3)
                {
                    // go right
                    if (col < NumCols - 1 && MapGrid[row,col + 1] == CLOSED)
                    {
                        switch (cellState)
                        {
                            case CLOSED:
                                MapGrid[row,col] = OPEN_RIGHT;
                                break;

                            case OPEN_TOP:
                                MapGrid[row,col] = OPEN_TOP_RIGHT;
                                break;
                            case OPEN_LEFT:
                                MapGrid[row,col] = CLOSED_LEFT;
                                break;
                            case OPEN_LEFT_BottOM:
                                MapGrid[row,col] = CLOSED_TOP;
                                break;

                            case CLOSED_RIGHT:
                                MapGrid[row,col + 1] = OPEN_LEFT;
                        GodownPath(row,col + 1);
                    }
                }
            }
        }

        private int NumberUnvisitedneighbors(int row,int col)
        {
            int numUnvisited = 0;

            if (row > 0 && MapGrid[row - 1,col] == CLOSED) numUnvisited++;
            if (row < NumRows - 1 && MapGrid[row + 1,col] == CLOSED) numUnvisited++;

            if (col > 0 && MapGrid[row,col - 1] == CLOSED) numUnvisited++;
            if (col < NumCols - 1 && MapGrid[row,col + 1] == CLOSED) numUnvisited++;

            return numUnvisited;
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)