扫雷展开时的递归求助
展开的递归:
void open(char copy[ROWS][COLS], char show[ROWS][COLS], int rows, int cols, int x, int y)
{
int ret = Open_condition(copy, x, y);
if (ret == 0)
{
show[x][y] = ' ';
if (x != 0 || x != rows - 1 || y != 0 || y != rows - 1)
{
int i = 0;
int num = 0;
for (i = x - 1; i <= x + 1; i++)
{
int j = 0;
for (j = y - 1; j <= y + 1; j++)
{
if(show[i][j]=='*')
open(copy, show, ROWS, COLS, x, y);
}
}
}
}
}
获取雷的个数:
int Open_condition(char copy[ROWS][COLS],int x, int y)
{
int i = 0;
int j = 0;
int num = 0;
for (i = x-1; i <= x + 1; i++)
{
for (j = y-1 ; j <= y + 1; j++)
{
if (copy[i][j] == '1')
{
num++;
}
}
}
return num;
}
int player_move(char copy[ROWS][COLS], char show[ROW][COLS], int rows, int cols)
{
int x = 0;
int y = 0;
int ret = 0;
while (1)
{
scanf("%d%d", &x, &y);
if (2 <= x && x <= 10 && 2 <= y && y <= 10)
{
if (show[x - 1][y - 1] == '*')
{
if (copy[x - 1][y - 1] == '1')
{
printf("死");
return 0;
break;
}
int ret = Open_condition(copy, x - 1, y - 1);
if (ret != 0)
{
show[x - 1][y - 1] = ret + '0';
return 1;
break;
}
if (ret == 0)
{
open(copy, show, rows, cols, x - 1, y - 1);
return 1;
break;
}
}
else
{
printf("重复排查,请重新输入\n");
}
}
else
{
printf("坐标违法,请重新输入\n");
}
}
}
现在踩到雷提示失败这一趴没问题。
排查雷周围的坐标显示有多少雷也没问题。
就是如果排查位置没雷,要展开出现问题-是递归出错了还是哪出错了?