#include<stdio.h> #define Row a #define Col b #define Mine c void Board(int board[Row][Col],int mark[Row][Col]) { int i,j,k=0; do{ for(i=0;i<Row;i++) { for(j=0;j<Col;j++) { scanf("%c",&board[i][j]); mark[i][j]="."; if(board[i][j]=="") k++; } printf("\n"); } }while(k!=Mine); } void Number(int x,int y,int board[Row][Col],int mark[Row][Col]) { int i,j,n=0; for(i=x-1;i<=x+1;i++) { for(j=y-1;j<=y+1;i++) { if(board[i][j]=="") n++; } } mark[x][y]=n; } void Print(int mark[Row][Col]) { int i,j; for(i=0;i<Row;i++) { for(j=0;j<Col;j++) { printf("%c",mark[i][j]); } } } void Operation(int o,int x,int y,int win,int board[Row][Col],int mark[Row][Col],int mine) { switch(o) { case 1:Number(x,y,board[Row][Col],mark[Row][Col]); while(mark[x][y]==0){int i,j; for(i=x-1;i<=x+1;i++) { for(j=y-1;j<=y+1;j++) { Number(i,j,board[Row][Col],mark[Row][Col]); win=win+1; } } } break; case 2:if(mark[x][y]=="."||mark[x][y]=="?"){mark[x][y]="!";mine=mine-1;} break; case 3:if(mark[x][y]=="."){mark[x][y]="?";}if(mark[x][y]=="!"){mark[x][y]="?";mine=mine+1;} break; case 4:if(mark[x][y]=="?"){mark[x][y]=".";}if(mark[x][y]=="!"){mark[x][y]=".";mine=mine+1;} break; case 9: if(mark[x][y]=="."||mark[x][y]=="?"||mark[x][y]=="!") break; int i,j,m=0,v=0; for(i=x-1;i<=x+1;i++) { for(j=y-1;j<=y+1;i++) { if(mark[i][j]=="!") m++; if(mark[i][j]=="."&&board[i][j]=="") v++; } } if(m==mark[x][y]&&v!=0) { printf("Hit mine, you lose"); break; } if(m==mark[x][y]&&v==0) { for(i=x-1;i<=x+1;i++) { for(j=y-1;j<=y+1;i++) { if(mark[i][j]==".") { Number(i,j,board[Row][Col],mark[Row][Col]); win=win+1; } } } } } } int main() { int win=0; int step=0; int t,a,b,c; scanf("%d",&t); switch(t) { case 1:a=9,b=9,c=10; break; case 2:a=16,b=16,c=40; break; case 3:a=16,b=30,c=99; break; case 4:scanf("%d %d %d",&a,&b,&c); break; } printf("\n"); int mine=c; int board[Row][Col],mark[Row][Col]; Board(board[Row][Col]); int o,x,y; while(win<ab-c) { scanf("%d %d %d",&o,&x,&y); step=step+1; if(o==1&&board[x][y]=="") { printf("Hit mine, you lose"); break; } if(o!=1&&o!=2&&o!=3&&o!=4&&o!=9) { win=win+1; } if(x<1||x>Row||y<1||y>Col) { win=win+1; } else { Operation(o,x,y); } printf("Game in progress"); printf("操作次数:%d 剩余雷数:%d",step,mine); Print(mark[Row][Col]); } while(win==ab-c) { printf("Game over, you win"); } int i,j; for(i=0;i<Row,i++) { for(j=0;j<Col;j++) { if(board[i][j]=="*") mark[i][j]=board[i][j]; } } Print(mark[Row][Col]); return 0; } 用c语言Debug
时间: 2023-06-20 21:09:04 浏览: 222
这段代码存在很多问题,首先,在宏定义时应该使用 #define a 9 而不是 #define Row a,因为宏定义是对字符串进行替换。其次,在 Board 函数中,应该使用 getchar() 函数读取输入的字符,而不是 scanf("%c", &board[i][j]),因为 scanf 会忽略空格和换行符,导致读取错误。另外,在 Number 函数中,for 循环内应该是 j++ 而不是 i++,否则会导致死循环。还有在 Operation 函数中,case 9 的 for 循环内,j 的循环条件应该是 j<=y+1 而不是 j<=y+1。最后,在 main 函数中,调用 Board 和 Print 函数时不需要传递数组的下标,应该直接传递数组名即可。
以下是修改后的代码:
```
#include <stdio.h>
#define a 9
#define b 9
#define c 10
void Board(int board[a][b], int mark[a][b]) {
int i, j, k = 0;
do {
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
board[i][j] = getchar();
mark[i][j] = ".";
if (board[i][j] == '\n') {
board[i][j] = getchar();
}
if (board[i][j] == '*') k++;
}
}
} while (k != c);
}
void Number(int x, int y, int board[a][b], int mark[a][b]) {
int i, j, n = 0;
for (i = x - 1; i <= x + 1; i++) {
for (j = y - 1; j <= y + 1; j++) {
if (board[i][j] == '*' || i < 0 || i >= a || j < 0 || j >= b) {
continue;
}
if (board[i][j] == '.') {
Number(i, j, board, mark);
} else {
n++;
}
}
}
mark[x][y] = n + '0';
}
void Print(int mark[a][b]) {
int i, j;
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
printf("%c", mark[i][j]);
}
printf("\n");
}
}
void Operation(int o, int x, int y, int *win, int board[a][b], int mark[a][b], int *mine) {
switch (o) {
case 1:
if (board[x][y] == '*') {
printf("Hit mine, you lose");
*win = 1;
} else {
Number(x, y, board, mark);
while (mark[x][y] == '0') {
int i, j;
for (i = x - 1; i <= x + 1; i++) {
for (j = y - 1; j <= y + 1; j++) {
if (board[i][j] == '*' || i < 0 || i >= a || j < 0 || j >= b || mark[i][j] != '.') {
continue;
}
Number(i, j, board, mark);
}
}
}
(*win)++;
}
break;
case 2:
if (mark[x][y] == '.' || mark[x][y] == '?') {
mark[x][y] = '!';
(*mine)--;
}
break;
case 3:
if (mark[x][y] == '.') {
mark[x][y] = '?';
} else if (mark[x][y] == '!') {
mark[x][y] = '?';
(*mine)++;
}
break;
case 4:
if (mark[x][y] == '?') {
mark[x][y] = '.';
} else if (mark[x][y] == '!') {
mark[x][y] = '.';
(*mine)++;
}
break;
case 9:
if (mark[x][y] == '.' || mark[x][y] == '?' || mark[x][y] == '!') {
break;
}
int i, j, m = 0, v = 0;
for (i = x - 1; i <= x + 1; i++) {
for (j = y - 1; j <= y + 1; j++) {
if (board[i][j] == '*' && mark[i][j] == '!') {
m++;
}
if (board[i][j] == '.' && mark[i][j] == '.') {
v++;
}
}
}
if (m == mark[x][y] - '0' && v != 0) {
for (i = x - 1; i <= x + 1; i++) {
for (j = y - 1; j <= y + 1; j++) {
if (board[i][j] == '.' && mark[i][j] == '.') {
Operation(1, i, j, win, board, mark, mine);
}
}
}
}
break;
}
}
int main() {
int win = 0;
int step = 0;
int t, mine = c;
scanf("%d", &t);
switch (t) {
case 1:
break;
case 2:
a = b = 16;
c = 40;
break;
case 3:
a = 16;
b = 30;
c = 99;
break;
case 4:
scanf("%d %d %d", &a, &b, &c);
break;
}
int board[a][b], mark[a][b];
Board(board, mark);
int o, x, y;
while (win == 0 && mine > 0) {
scanf("%d %d %d", &o, &x, &y);
if (x < 0 || x >= a || y < 0 || y >= b) {
continue;
}
if (o == 1 && board[x][y] == '*') {
printf("Hit mine, you lose");
mark[x][y] = '*';
Print(mark);
break;
}
Operation(o, x, y, &win, board, mark, &mine);
printf("Game in progress\n");
printf("操作次数:%d 剩余雷数:%d\n", ++step, mine);
Print(mark);
}
if (mine == 0) {
printf("Game over, you win");
}
return 0;
}
```
阅读全文