选择多维数组中的所有相邻值
我正在制作一款类似泡泡破坏者的游戏。我的代码使用两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否满足您的确切选择逻辑,但不是递归解决方案:
如果尚未选择位置,则进行额外检查对于防止无限递归非常重要
Not sure this fullfills your exact selecting logic but isn't recursion the solution:
The extra check if the position isn't selected already is important to prevent endless recursion
没关系,我自己解决了。我检查了所选圆圈的数组,这有效。
Nevermind, I solved it myself. I checked against the array of selected circles, this worked.