<>前言

<>一、周围雷数量为0，连续自动翻棋子

void Findmines(char mine[Rows][Cols], char show[Rows][Cols], int x, int y) {
if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) { return ; } if
(show[x][y] != '*') return; if (show[x][y] == '*') { int ret = minenum(mine, x,
y); show[x][y] = ret + '0'; } if (show[x][y] == '0') { Findmines(mine, show, x
- 1, y - 1); Findmines(mine, show, x - 1, y); Findmines(mine, show, x - 1, y +
1); Findmines(mine, show, x, y - 1); Findmines(mine, show, x, y + 1);
Findmines(mine, show, x + 1, y - 1); Findmines(mine, show, x + 1, y);
Findmines(mine, show, x + 1, y + 1); }
<>二、标记雷和取消标记

void concel(char show[Rows][Cols], int x, int y) { if (x == 0 || y == 0 || x
== Rows - 1 || y == Cols - 1) { printf("坐标非法，请重新输入：\n"); return; } if
(show[x][y] != '!') return; if (show[x][y] == '!') { show[x][y] = '*';
Initshow(show, Row, Col); } } void add(char show[Rows][Cols], int x, int y) {
if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {
printf("坐标非法，请重新输入：\n"); return; } if (show[x][y] != '*') return; if
(show[x][y] == '*') { show[x][y] = '!'; Initshow(show, Row, Col);; } }
3.总代码如下

game.c
#include "game.h" void Mine(char board[Rows][Cols], int rows, int cols, char
set) { int i = 0, j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols;
j++) { board[i][j] = set; } } } void Initshow(char show[Rows][Cols], int row,
int col) { int i = 0, j = 0; printf("--------欢迎来到扫雷游戏--------\n"); for (i = 0;
i <= col; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) {
printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", show[i][j]); }
printf("\n"); } printf("--------欢迎来到扫雷游戏--------\n"); } void Mineset(char
mine[Rows][Cols], int row, int col) { int n = mines; while (n) { int x = rand()
% row + 1; int y = rand() % col + 1; if (x <= row && x >= 1 && y <= col && y >=
1) { mine[x][y] = '1'; n--; } } } void concel(char show[Rows][Cols], int x, int
y) { if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {
printf("坐标非法，请重新输入：\n"); return; } if (show[x][y] != '!') return; if
(show[x][y] == '!') { show[x][y] = '*'; Initshow(show, Row, Col); } } void
add(char show[Rows][Cols], int x, int y) { if (x == 0 || y == 0 || x == Rows -
1 || y == Cols - 1) { printf("坐标非法，请重新输入：\n"); return; } if (show[x][y] != '*')
return; if (show[x][y] == '*') { show[x][y] = '!'; Initshow(show, Row, Col);; }
} int minenum(char mine[Rows][Cols], int x, int y) { return mine[x - 1][y - 1]
+ mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] +
mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0'; } void
Checkmines(char show[Rows][Cols],int* x, int* y) { int i = 0, j = 0; for (i =
0; i < Row; i++) { for (j = 0; j < Col; j++) { if (show[i][j] != '*') x++; if
(show[i][j] == '!') y++; } } } void Findmines(char mine[Rows][Cols], char
show[Rows][Cols], int x, int y) { if (x == 0 || y == 0 || x == Rows - 1 || y ==
Cols - 1) { return ; } if (show[x][y] != '*') return; if (show[x][y] == '*') {
int ret = minenum(mine, x, y); show[x][y] = ret + '0'; } if (show[x][y] == '0')
{ Findmines(mine, show, x - 1, y - 1); Findmines(mine, show, x - 1, y);
Findmines(mine, show, x - 1, y + 1); Findmines(mine, show, x, y - 1);
Findmines(mine, show, x, y + 1); Findmines(mine, show, x + 1, y - 1);
Findmines(mine, show, x + 1, y); Findmines(mine, show, x + 1, y + 1); } } void
****\n"); printf("**** 3.标记雷 ****\n"); printf("**** 4.取消标记 ****\n");
printf("**** 5.继续游戏 ****\n"); printf("***************************\n"); } void
Findmine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col) { int
rets = 0,qi=0; while (1) { int x = 0, y = 0; printf("请输入坐标>\n"); scanf("%d%d",
&x, &y); if (x <= row && x >= 1 && y <= col && y >= 1) { Findmines(mine, show,
x, y); Initshow(show, Row, Col); if (mine[x][y] == '1') {
printf("肯定是失误了，你怎么可能会输？只能再来一局了\n"); show[x][y] = '1'; Initshow(mine, Row, Col);
return; } } else { printf("输入坐标非法，请重新输入：\n"); } Checkmines(show,&rets, &qi); if
(rets == Row * Col - mines || qi == mines) { printf("恭喜你，简简单单赢了，小意思哦\n");
return; } while (1) { menu1(); int g = 0; printf("请输入>\n"); scanf("%d", &g); if
(g == 3) { int x = 0, y = 0; printf("请输入坐标：\n"); scanf("%d%d", &x, &y);
add(show, x, y); } else if (g == 4) { int x = 0, y = 0; printf("请输入坐标：\n");
scanf("%d%d", &x, &y); concel(show, x, y); } else if (g == 5) break; else goto
again; } } again:return; }
<>text.c
#include "game.h" void menu() { printf("***************************\n");
printf("**** 1.玩游戏 ****\n"); printf("**** 0.退出游戏 ****\n");
printf("***************************\n"); } void game() { char mine[Rows][Cols]
= { 0 }; char show[Rows][Cols] = { 0 }; Mine(mine, Rows, Cols, '0'); Mine(show,
Rows, Cols, '*'); Initshow(show, Row, Col); Mineset(mine, Row, Col);
//Initshow(mine, Row, Col); //Mineset(mine, Row, Col); Findmine(mine, show,
Row, Col); } int main() { int input = 0; srand((unsigned int)time(NULL)); do {
menu(); printf("请输入>\n"); scanf("%d", &input); switch (input) { case 1:game();
break; case 0:printf("退出游戏\n"); break; default: break; } } while (input);
return 0; }

game.h
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h>
#define mines 10 #define Row 9 #define Col 9 #define Rows Row+2 #define Cols
Col+2 void Mine(char board[Rows][Cols], int rows , int cols, char set); void
Initshow(char show[Rows][Cols], int row, int col); void Mineset(char
mine[Rows][Cols], int row, int col); void Findmine(char mine[Rows][Cols], char
show[Rows][Cols], int row, int col); void Findmines(char mine[Rows][Cols], char
show[Rows][Cols], int y, int x); void Check(char show[Rows][Cols],int* x, int*
y); void add(char show[Rows][Cols], int x, int y); void concel(char
show[Rows][Cols], int x, int y);

GitHub

Gitee