【发布时间】:2015-12-11 14:06:19
【问题描述】:
我在编写扫雷器代码时遇到问题,当您单击空白区域时,它会向外级联并产生一圈数字。我为数字设置了一个数组,为炸弹设置了一个数组。在 numbers 数组中,零表示那里什么都没有。我首先检查单击的点是否是炸弹,如果不是则是否有数字,然后 else 语句必须导致数字的级联和环。基本上,我在单击的框上、下、左和右进行四个 while 循环,直到找到一个数字。从那里我有八个 for 循环,沿着每条线以 x 和 y 的递增和递减顺序进行,直到找到一个数字。一旦找到一个数字,我就会尝试返回原点,但是通过检查我到达那里的路径的边界,将坐标值增加 1 并将坐标值减少 1。由于某种原因,我无法让我的代码正常工作。这是我的while循环的一个例子。 “checkx”和“checky”是数组的坐标。 "revealxposition" 和 "revealyposition" 是图形窗口中的坐标。
int i=0;
while(i == 0 && checkx < length && checky < length)//right
{
if(numbers[checkx][checky] != 0)
{
revealxposition = (checkx*20) + 5;
revealyposition = (checky*20) + 3;
showNumber(revealxposition, revealyposition, numbers[checkx][checky]);
xmax = checkx;
i = 1;
}
else
{
if(checkx == (length - 1))
{
i = 1;
xmax = length;
a = checkx*20;
b = checky*20;
c = a+20;
d = b+20;
line(a,b,c,d);
line(a,d,c,b);
}
else
{
a = checkx*20;
b = checky*20;
c = a+20;
d = b+20;
line(a,b,c,d);
line(a,d,c,b);
checkx++;
}
}
}
这是我的一个 for 循环的示例:
for(int xx = checkx; xx < xmax; xx++)
{
int z = 0;
int xxx = xx;
while(z == 0 && checky < length)
{
if(numbers[xx][checky] != 0)
{
revealxposition = (xx*20) + 5;
revealyposition = (checky*20) + 3;
int originalxposition = revealxposition;
int originalyposition = revealyposition;
showNumber(revealxposition, revealyposition, numbers[xx][checky]);
//reveal diagonals and back
while(bombs[xx][checky] != 1 && checky >= originaly)
{
counter++;
revealxposition = originalxposition - 20;
xx = xxx - 1;
showNumber(revealxposition, revealyposition, numbers[xx][checky]);
checky--;
revealyposition -= 20;
}
xx = xxx;
checky = originaly;
revealxposition = originalxposition;
revealyposition = originalyposition;
counter = 0;
while(bombs[xx][checky] != 1 && checky >= originaly)
{
counter++;
revealxposition = originalxposition + 20;
xx = xxx + 1;
showNumber(revealxposition, revealyposition, numbers[xx][checky]);
checky--;
revealyposition -= 20;
}
xx = xxx;
z = 1;
counter = 0;
}
else
{
a = xx*20;
b = checky*20;
c = a+20;
d = b+20;
line(a,b,c,d);
line(a,d,c,b);
checky++;
}
}
checky = originaly;
}
【问题讨论】:
标签: c++ arrays graphics minesweeper