C#迷宫求解器图形

时间:2013-04-07 12:12:11

标签: c# solver maze

当我运行求解器时,我的迷宫求解器出现问题,它在图形变量处出现错误

 private bool solveMaze(int xPos, int yPos, bool[,] alreadySearched)
        {
            bool correctPath = false;
            bool shouldCheck = true;

            Bitmap map = (Bitmap)Mazebox.Image;
            Graphics gfx = null;
            gfx = Graphics.FromImage(map);
            Brush b = new SolidBrush(Color.CornflowerBlue);

            //out of index check
            if (xPos >= XTILES || xPos < 0 || yPos >= YTILES || yPos < 0)
                shouldCheck = false;

            if (map.GetPixel(xPos , yPos) == Color.Green)
            {
                correctPath = true;
            }
            //Search the Tile
            if (shouldCheck)
            {
                //mark tile as searched
                alreadySearched[xPos, yPos] = true;

                //Check right tile
                correctPath = correctPath || solveMaze(xPos + 1, yPos, alreadySearched);
                //Check down tile
                correctPath = correctPath || solveMaze(xPos, yPos + 1, alreadySearched);
                //check left tile
                correctPath = correctPath || solveMaze(xPos - 1, yPos, alreadySearched);
                //check up tile
                correctPath = correctPath || solveMaze(xPos, yPos - 1, alreadySearched);
            }
            //make correct path gray
            if (correctPath)
            {
                gfx.FillRectangle(b, xPos, yPos, 10, 10);
                Mazebox.Image = map;
            }

            return correctPath;
        }

我认为问题是他打开了很多然后崩溃(无穷大) 任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您永远不会使用alreadySearched字段。

你可以通过插入

来做到这一点
 shouldCheck = shouldCheck && !alreadySearched[xPos, yPos];