路径生成器未按预期/预期工作

时间:2017-08-26 03:27:47

标签: c#

正如您从屏幕截图中看到的那样,错误发生的时候errors 我的代码应该在那些时刻观察白色块上方的点,检查它们的任何一侧是否有任何其他空的部分,如果不是,圆圈区域应该是空的,而不是块。

namespace ConsoleApp1
{
class Program
{
    static void Main(string[] args)
    {
        //initiate lists (1 list for object), (2 list for occupied nearby)
        int[] list1 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list2 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list3 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list4 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list5 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list6 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list7 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list8 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list9 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list10 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list11 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list12 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list13 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list14 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list15 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list16 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[][] lists = new int[][] { list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11, list12, list13, list14, list15, list16 };

        int[] list1a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list2a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list3a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list4a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list5a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list6a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list7a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list8a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list9a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list10a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list11a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list12a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list13a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list14a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list15a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[] list16a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int[][] listsa = new int[][] { list1a, list2a, list3a, list4a, list5a, list6a, list7a, list8a, list9a, list10a, list11a, list12a, list13a, list14a, list15a, list16a };

        //assign walls
        Random random = new Random();
        //full
        // open = 2, unnasigned = 0, block = 1
        for (int i = 0; i <= 15; i++)
        {
            for (int j = 0; j <= 15; j++)
            {
                // % chance of block spawning
                int r = 2;
                int p = random.Next(1, 11);
                if (p > 5) { r = 1; }
                else if (p < 6) { r = 2; }
                //do we have any surrounding open* blocks? (above,sides) *(not unassigned, OPEN)
                int[] openspaces = new int[3] { 0, 0, 0 }; //for recording which spaces are open
                var open = "no"; //assumed to be no
                //check space to sides and above, but not below as always unassigned
                try { if (lists[j][i - 1] == 2) { open = "yes"; openspaces[0] = 1; } } catch (IndexOutOfRangeException) { }  //above
                try { if (lists[j + 1][i] == 2) { open = "yes"; openspaces[1] = 1; } } catch (IndexOutOfRangeException) { }  //right
                try { if (lists[j - 1][i] == 2) { open = "yes"; openspaces[2] = 1; } } catch (IndexOutOfRangeException) { }  //left
                if (open == "no")
                {
                    lists[j][i] = r; //  50/50 chance as completely singular square
                }
                else if (open == "yes")
                {   // if the spaces are open, do they have any other open or undecided blocks near them (under, sides)
                    int[] otheropen = new int[3] { 0, 0, 0 }; //for recording which nearby open block have other nearby open spaces
                    if (openspaces[0] == 1) //above
                    {
                        //check space to sides, but not above as not relavent, and below would be checking itself, and none are invalid
                        try { if ((lists[j + 1][i - 1] == 0 || lists[j + 1][i - 1] == 2) && listsa[j + 1][i - 1] != 1)  { otheropen[0] = 1; } } catch (IndexOutOfRangeException) { }
                        try { if ((lists[j - 1][i - 1] == 0 || lists[j - 1][i - 1] == 2) && listsa[j - 1][i - 1] != 1) { otheropen[0] = 1; } } catch (IndexOutOfRangeException) { }
                    }
                    else if (openspaces[0] == 0) { otheropen[0] = 2; } // if the block is not open it is ignored

                    if (openspaces[1] == 1) //right
                    {
                        //check space to right and below, but not above as not relavent, and left would be checking itself, and none are invalid
                        try { if ((lists[j + 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j + 1][i + 1] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
                        try { if ((lists[j + 2][i] == 0 || lists[j + 1][i] == 2) && listsa[j + 2][i] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
                    }
                    else if (openspaces[1] == 0) { otheropen[1] = 2; } // if the block is not open it is ignored

                    if (openspaces[2] == 1) //left
                    {
                        //check space to left and below, but not above as not relavent, and right would be checking itself, and none are invalid
                        try { if ((lists[j - 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j - 1][i + 1] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
                        try { if ((lists[j - 2][i] == 0 || lists[j - 1][i] == 2) && listsa[j - 2][i] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
                    }
                    else if (openspaces[2] == 0) { otheropen[2] = 2; } // if the block is not open it is ignored

                    if (otheropen[0] == 0 || otheropen[1] == 0 || otheropen[2] == 0) { lists[j][i] = 2; } //sets self as open, as a nearby open block has no other open blocks
                    else if (otheropen[0] != 0 && otheropen[1] != 0 && otheropen[2] != 0)
                            //yes all the near open block have a possible other route
                        {
                        lists[j][i] = r; // 50/50 as this block will not create a dead end
                        if (r == 1)//if original block closed then nearby open blocks will become invalid for next check, i.e. considered closed
                            {
                            try { if (otheropen[0] == 1) { listsa[j][i - 1] = 1; } } catch (IndexOutOfRangeException) { }//top
                            try { if (otheropen[1] == 1) { listsa[j + 1][i] = 1; } } catch (IndexOutOfRangeException) { }//right
                            try { if (otheropen[2] == 1) { listsa[j + 1][i] = 1; } } catch (IndexOutOfRangeException) { }//left
                        } 
                        }
                }

            }
        }

        for (int j = 0; j <= 15; j++)
        {
            Console.WriteLine();
            for (int i = 0; i <= 15; i++)
            {
                if (lists[j][i] == 1) { Console.Write("██"); }
                if (lists[j][i] == 2) { Console.Write("░░"); }

            }
        }

    }
}

我无法弄清楚这个错误发生的原因,可能是由于尝试检查。我知道这是一个很大的代码转储,可能很难从我发布的内容中解释出来,而且我确信我可以更有效地做事,因为我是C#的新手,但任何帮助会是不可思议的。

我真的希望每个广场都可以进入或成为一堵墙,但是你可以看到墙壁在整个区域穿过。

编辑:流程探索:

所以它遍历一个大型数组。首先,它看看是否有任何&#34;开放块&#34; (值2)在2d阵列中的当前特定值的上方和侧面。如果没有随机决定该块是否打开&#34;或者&#34;填充&#34; (分别为2或1)。

如果有一个开放块,它将检查围绕块(侧面和下面)的每个块,以查看是否有任何其他开放(2)块可以充当路径。如果有,那么它会承认并再次随机选择。如果其中一个块没有周围的开放块,则当前值应该变为开放块。

如果随机选择是“填充”。在后一个进程之后阻塞,然后它会将near块分配为不再有效,再次对同一个进程开放,这样当另一个块正在考虑是否可以将其用作路径时它知道它不能。

1 个答案:

答案 0 :(得分:0)

好吧,在重新格式化代码以使其更容易后,我发现了我认为是错误的错误(至少我不能在这些更改后重现错误。(下面的代码没有重新格式化以使您更容易在你的代码中找到)

在本节中

if (openspaces[1] == 1) //right       
    {
    //check space to right and below, but not above as not relavent, and left would be checking itself, and none are invalid
    try { if ((lists[j + 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j + 1][i + 1] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
    try { if ((lists[j + 2][i] == 0 || lists[j + 1][i] == 2) && listsa[j + 2][i] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
}
else if (openspaces[1] == 0) { otheropen[1] = 2; } // if the block is not open it is ignored

if (openspaces[2] == 1) //left
{
    //check space to left and below, but not above as not relavent, and right would be checking itself, and none are invalid
    try { if ((lists[j - 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j - 1][i + 1] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
    try { if ((lists[j - 2][i] == 0 || lists[j - 1][i] == 2) && listsa[j - 2][i] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
}

如果你看一下它所说的try { if ...,你会注意到你或声明中的第二个条件与第一个条件不同。它们应该是一样的。您似乎成了复制粘贴错误的受害者。

正如@Tipx建议重新格式化代码会使这更容易找到。由于您是C#的新手,请从头开始学习使您的代码可读。你以后会感谢自己。

请注意,您的代码仍允许将路径块与主路径分开(将随机种子设置为0并查看右上角),但您不应该像您一样拥有更多的单个方块。