在9x9的雷盘上进行扫雷,若是雷,被炸死,游戏结束;若不是雷,继续进行排雷,直到把所有的雷都排除,获得胜利。
void menu(){
printf("#######扫雷#######\n");
printf("######1.play######\n");
printf("######0.exit######\n");
printf("##################\n");
}
void InitBoard(char board[ROWS][COLS], int row, int col, char set){
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
board[i][j] = set;
}
}
}
#define MINENUM 10
void SetMine(char board[ROWS][COLS], int row, int col){
int x = 0;
int y = 0;
int num = MINENUM;
while (num){
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == '0'){
board[x][y] = '1';
num--;
}
}
}
int mine_nums(char board[ROWS][COLS], int x, int y){
return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1]
+ board[x][y - 1] + board[x][y + 1]
+ board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1]
- 8 * '0';
}
void FindMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col){
int x = 0;
int y = 0;
int win = 0; //记录排查成功的坐标个数
while (win < row*col-MINENUM){
printf("请输入坐标-> ");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col){
if (board1[x][y] == '1'){
printf("排雷失败,你被炸死了!\n");
ShowBoard(board1, ROWS, COLS);
break;
}
else{
int num = mine_nums(board1, x, y);
board2[x][y] = num + '0';
win++;
ShowBoard(board2, ROWS, COLS);
}
}
else{
printf("输入坐标不合法,请重新输入...\n");
}
}
if (win == row*col - MINENUM){
printf("扫雷成功!!!\n");
}
}
假设在该9x9的雷盘上有80个雷点,然后我们使用该例子进行测试,结果如下: