简体   繁体   English

C# 停止递归

[英]C# Stopping recursion

So I have made a maze where '#' are walls and '.'所以我做了一个迷宫,其中 '#' 是墙壁和 '.' are available coordinates to move to.是可移动到的坐标。 I'm attempting to stop the recursion when it finds its way outside of the maze.当递归找到迷宫外时,我试图停止它。 So when either (x,y) is 12. This may be a simple fix.所以当 (x,y) 为 12 时。这可能是一个简单的修复。

This is my maze...这是我的迷宫...

char[,] maze1 =
            // USE THIS
        { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
        { '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
        { '#', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
        { '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' },
        { '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };

Here is my method...这是我的方法...

private void mazeTraversal(int currentX, int currentY, char[,] maze)
    {
        /*************** ATTEMPT 1 ******************/

        // Checks if coordinates are inside the maze
        if (currentX <= 11 && currentY <= 11)
        {
            // Implement maze traversal recursive call
            //PrintOriginalMaze(maze, currentX, currentY);

            // If the coordinate is a '.' , continue(This is the base case)
            if ('.' == maze[currentX, currentY])
            {
                // Changes location to a X so you cant go back.
                maze[currentX, currentY] = 'X';

                // Implement maze traversal recursive call
                PrintOriginalMaze(maze, currentX, currentY);

                mazeTraversal(currentX, currentY + 1, maze);
                // maze[currentX, currentY] = 'X';

                mazeTraversal(currentX - 1, currentY, maze);
                //maze[currentX, currentY] = 'X';

                mazeTraversal(currentX + 1, currentY, maze);
                // maze[currentX, currentY] = 'X';

                mazeTraversal(currentX, currentY - 1, maze);
                //maze[currentX, currentY] = 'X';

            }

            if ('#' == maze[currentX, currentY])
            {
                Console.WriteLine("Hit a wall");
                Console.WriteLine();
            }
        }
    }

You really should put a "exit" node in the labyrinth.你真的应该在迷宫中放置一个“出口”节点。 At [4][11] I think.在 [4][11] 我想。 Then it would be a simple check if you found the exit(node).然后,如果您找到出口(节点),这将是一个简单的检查。

What you have here is the age old problem of gameboards: "How do I deal with stuff just outside the playfield?"你在这里遇到的是游戏板的古老问题:“我如何处理游戏场外的东西?” and "What do I do if I am at a border and want to check all neighbouring cells?"和“如果我在边境并想检查所有相邻的小区,我该怎么办?”

For the most part, you avoided that problem.在大多数情况下,你避免了这个问题。 You have a "border" of walls.你有一个墙的“边界”。 Putting a non-enterable border around the gameboard is one of the solutions.在游戏板周围放置不可进入的边框是解决方案之一。 The other would be dealing with all those Index checks.另一个将处理所有这些索引检查。 But personally I prefer accounting for a border.但我个人更喜欢考虑边界。

If i understand your question, and you are trying to determine if the current x and y are outside the bounds of your maze ( array )如果我理解你的问题,你正试图确定当前的xy在外面的迷宫的边界array

You could use Array.GetUpperBound(Int32)你可以使用Array.GetUpperBound(Int32)

Gets the index of the last element of the specified dimension in the array.获取数组中指定维度的最后一个元素的索引。

var maxX = maze1.GetUpperBound(0);
var maxY = maze1.GetUpperBound(1);

if (currentX > maxX || currentX < 0 || currentY > maxY || currentY < 0)
  return;

Stopping the recursion is just a matter of exiting the method consistently (on some state) to unwrap the call, ie using a return when your current xy is outside the bounds .停止递归只是一致地(在某种状态下)退出方法以展开调用,即在当前xy超出边界时使用return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM