扫雷器代码C++
Minesweeper Code C++
我在为扫雷舰编写代码时遇到了问题,当你点击空格时,它会向外级联,并导致一个数字环。我有一个数字数组和一个炸弹数组。在数字数组中,零表示那里什么都没有。我首先检查点击的点是否是炸弹,如果不是,然后检查是否有数字,然后else语句必须导致数字的级联和环。基本上,我在点击框的上、下、左、右循环的同时浏览四个,直到找到一个数字。从那里开始,我有八个for循环,按照x和y的递增和递减顺序,沿着每一条线,直到找到一个数字。一旦找到一个数字,我就试图回到原点,但要检查到达那里的路径的边界,将坐标值增加一,将坐标数值减少一。由于某些原因,我无法使代码正常工作。下面是我while循环的一个例子。"checkx"answers"checky"是数组的坐标。"reveaxposition"answers"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;
}
您的代码真的很难阅读,我不知道xx或xxx是什么意思或a=xx*20;
我曾经做过一次扫雷,我很确定有一种更容易的方法来检查空白区域。对象是使用递归。让我给你举个例子。
void checkNumber(x,y) {
revealNumber(x,y); // write the number
if(Number[x,y] == 0){ // if it is blank space, check all 8 square around
if(x>0)
checkNumber(x-1,y); // check left
if(x<maxX)
checkNumber(x+1,y); // check right
// repeat for up,down and diag
}
}
这将给你一个循环,显示所有空格和最接近的数字。希望它能帮助你。
)
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值