选择多维数组中的所有相邻值

发布于 2024-11-26 07:13:24 字数 1276 浏览 0 评论 0原文

我正在制作一款类似泡泡破坏者的游戏。我的代码使用两个 2D 数组,一个包含颜色索引 (1 - 6) 来表示彩色圆圈,另一个指示该圆圈是否已被选中(1 或 0)。我可以成功选择一个圆圈,第二个数组中的正确值发生变化,并且这会正确反映在屏幕上。

这是选择一个圆和四个相邻圆的方法。我传入用户在网格上选择的 X 和 Y 坐标。我将该位置设置为选定(在 SelectedCircles 数组中从 0 到 1。检查任何边是否有相同颜色的圆圈,如果有,则将该圆圈也更改为选定。

   private void SelectSurroundingCircles(int xPosition, int yPosition)
    {
        SelectedCircles[yPosition, xPosition] = 1;
        int colorKey = Circles[yPosition, xPosition];

        int increment = 1;
        for (int i = 0; i < Nickles.Length; i++)
        {
            if (Circles[yPosition - increment, xPosition] == colorKey)
                SelectedCircles[yPosition - increment, xPosition] = 1; // TOP

            if (Circles[yPosition + increment, xPosition] == colorKey)
                SelectedCircles[yPosition + increment, xPosition] = 1; // BOTTOM

            if (Circles[yPosition, xPosition + increment] == colorKey)
                SelectedCircles[yPosition, xPosition + increment] = 1; // RIGHT

            if (Circles[yPosition, xPosition - increment] == colorKey)
                SelectedCircles[yPosition, xPosition - increment] = 1; // LEFT
        }
    }

我想要实现的是所有圆圈基本上,您首先会像上面那样查看相邻的圆圈,然后查看它们的相邻圆圈,依此类推...我尝试了各种其他方法,但不知何故我无法弄清楚。希望有人可以帮助我,我必须忽略谢谢

I'm building a bubble breaker-kinda game. My code uses two 2D arrays, one containing color indexes (1 - 6) to represent colored circles, and one indicating whether the circle has been selected (1 or 0). I can succesfully select a circle, the right value in the second array changes and this is reflected correctly on screen.

This is the method that selects one circle and four adjacent circles. I pass in the X and Y coordinates that the user has selected on the grid. I set that position to selected (from 0 to 1 in the SelectedCircles array. Check whether any of the sides has a circle with the same color, if so, change that circle to selected too.

   private void SelectSurroundingCircles(int xPosition, int yPosition)
    {
        SelectedCircles[yPosition, xPosition] = 1;
        int colorKey = Circles[yPosition, xPosition];

        int increment = 1;
        for (int i = 0; i < Nickles.Length; i++)
        {
            if (Circles[yPosition - increment, xPosition] == colorKey)
                SelectedCircles[yPosition - increment, xPosition] = 1; // TOP

            if (Circles[yPosition + increment, xPosition] == colorKey)
                SelectedCircles[yPosition + increment, xPosition] = 1; // BOTTOM

            if (Circles[yPosition, xPosition + increment] == colorKey)
                SelectedCircles[yPosition, xPosition + increment] = 1; // RIGHT

            if (Circles[yPosition, xPosition - increment] == colorKey)
                SelectedCircles[yPosition, xPosition - increment] = 1; // LEFT
        }
    }

What I want to achieve is that all circles of the same color that are next to each other get selected. Basically you first look at the circles adjacent as above, look at their adjacent circles, and so on... I tried various other things but somehow I couldn't figure it out. Hopefully someone can help me, I must be overlooking something.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天暗了我发光 2024-12-03 07:13:24

不确定这是否满足您的确切选择逻辑,但不是递归解决方案:

if ( SelectedCircles[yPosition - increment, xPosition] != 1 && Circles[yPosition - increment, xPosition] == colorKey) {
   SelectSurroundingCircles(xPosition, yPosition - increment)
}
//... same for other 3 directions

如果尚未选择位置,则进行额外检查对于防止无限递归非常重要

Not sure this fullfills your exact selecting logic but isn't recursion the solution:

if ( SelectedCircles[yPosition - increment, xPosition] != 1 && Circles[yPosition - increment, xPosition] == colorKey) {
   SelectSurroundingCircles(xPosition, yPosition - increment)
}
//... same for other 3 directions

The extra check if the position isn't selected already is important to prevent endless recursion

有深☉意 2024-12-03 07:13:24

没关系,我自己解决了。我检查了所选圆圈的数组,这有效。

            for (int y = 0; y < SelectedCircles.GetLength(0); y++)
            {
                for (int x = 0; x < SelectedCircles.GetLength(1); x++)
                {
                    if (SelectedCircles[y, x] == 1)
                    {
                        if (y - 1 >= 0 && SelectedCircles[y - 1, x] != 1 && Circles[y - 1, x] == colorKey)
                            SelectedCircles[y - 1, x] = 1; // TOP

                        if (y + 1 <= 9 && SelectedCircles[y + 1, x] != 1 && Circles[y + 1, x] == colorKey)
                            SelectedCircles[y + 1, x] = 1; // BOTTOM

                        if (x + 1 <= 9 && SelectedCircles[y, x + 1] != 1 && Circles[y, x + 1] == colorKey)
                            SelectedCircles[y, x + 1] = 1; // RIGHT

                        if (x - 1 >= 0 && SelectedCircles[y, x - 1] != 1 && Circles[y, x - 1] == colorKey)
                            SelectedCircles[y, x - 1] = 1; // LEFT
                    }
                }
            }
        }

Nevermind, I solved it myself. I checked against the array of selected circles, this worked.

            for (int y = 0; y < SelectedCircles.GetLength(0); y++)
            {
                for (int x = 0; x < SelectedCircles.GetLength(1); x++)
                {
                    if (SelectedCircles[y, x] == 1)
                    {
                        if (y - 1 >= 0 && SelectedCircles[y - 1, x] != 1 && Circles[y - 1, x] == colorKey)
                            SelectedCircles[y - 1, x] = 1; // TOP

                        if (y + 1 <= 9 && SelectedCircles[y + 1, x] != 1 && Circles[y + 1, x] == colorKey)
                            SelectedCircles[y + 1, x] = 1; // BOTTOM

                        if (x + 1 <= 9 && SelectedCircles[y, x + 1] != 1 && Circles[y, x + 1] == colorKey)
                            SelectedCircles[y, x + 1] = 1; // RIGHT

                        if (x - 1 >= 0 && SelectedCircles[y, x - 1] != 1 && Circles[y, x - 1] == colorKey)
                            SelectedCircles[y, x - 1] = 1; // LEFT
                    }
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文