Before writing mine clearance Games , first , We should list the functions of mine clearing games we want to achieve ：
1. Displays the number of mines around the selected coordinates .
2. Make sure that the coordinates selected for the first time are not thunder .
3. Around selected coordinates 8 There are no thunder in the grid , Then expand .

second , We should understand that we need two chessboards for mine clearance ： A chessboard is used to show players , The initial interfaces are all “ * ”
（ Open lattice ）, This page is our common mine clearance page . Another chessboard is used to show the writer , There are only characters in the chessboard ‘1’ And characters ‘0’
.‘1’ For ray ,‘0’ On behalf of Fei Lei . Using characters ‘1’‘0’ To represent the thunder and non thunder is helpful for us to calculate the number of mines around the coordinates in the next step .

1. Initialize chessboard
nitboard(show_board, ROWS, COLS,'*'); //‘*’ Represents the unopened coordinates Initboard(mine_board, ROWS,
COLS, '0'); //‘0’ On behalf of Fei Lei void Initboard(char board[ROWS][COLS], int row, int col,
char set) { memset(board, set, row*col * sizeof(board[0][0])); }
2. Print chessboard
void DisPlayboard(char board[ROWS][COLS], int row, int col) { int i = 0; int j
= 0; printf(" "); for (i = 1; i <= row; i++) // Print line { printf("%d ", i); } printf(
"\n"); for (i = 1; i <= row; i++) { printf("%d ", i); // Print column for (j = 1; j <= col
; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("\n"); }
3. Mine the chessboard .（ Because every time you restart the game, the position of thunder cannot be repeated , So we choose to use random numbers to bury mines ）
void GetBoom(char board[ROWS][COLS], int row, int col) // Burying thunder { int x = 0; int y
= 0; int sum = 0; while(1) { x = rand() % row +1; // Ray put it in 1-9 Within the scope of y = rand() % col
+1; if (1) { board[x][y] = '1'; sum++; if (sum == M) // Jump out of the loop after burying the mine { break; } } }
4. Make sure you don't step on Thunder for the first time , If you step on Thunder for the first time , We will change this position to non thunder , Then move the mines here to other places where no mines have been placed .
（ There is also a special case to consider , In other words, if you win the first time, the latter function will not be executed again , So this function should be changed to a function with a return value , In this way, when calling this function, we can judge whether it is a win in one time )
int Judge_first(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int
row, int col) { int x = 0; int y = 0; int m; int n; printf(" Please enter the coordinates to be checked :"); scanf(
"%d%d", &x, &y); while (1) { if (mine_board[x][y] == '1') // If you step on Thunder for the first time , Change this coordinate to non thunder {
mine_board[x][y] = '0'; show_board[x][y] = ' '; while (1) { m = rand() % 9 + 1;
n= rand() % 9 + 1; if (m != x && n != y && mine_board[m][n] != '1') { mine_board
[m][n] = '1'; } Open(mine_board, show_board, x, y); if (Full(show_board, ROW,
COL) == 1) // After unfolding, judge whether to win once or not { printf(" Game victory \n"); DisPlayboard(mine_board, ROW, COL
); return 2; // A winning return 2 No more execution PlayerMove function break; } DisPlayboard(show_board, row,
col); break; } break; } else if (mine_board[x][y] == '0') { show_board[x][y] =
' '; Open(mine_board, show_board, x, y); if (Full(show_board, ROW, COL) == 1) {
printf(" Game victory \n"); DisPlayboard(mine_board, ROW, COL); return 2; break; }
DisPlayboard(show_board, row, col); break; } } }
5. When the number of thunder around the coordinate is 0 Time , Show the surrounding grid , And then X and Y The surrounding eight coordinates are respectively expanded in the center , When there is thunder around the coordinates, it will not be expanded recursively , And put the number of mines in this coordinate .
void Open(char mine_board[ROWS][COLS],char show_board[ROWS][COLS], int x, int y
) { int ret = 0; ret = Num(mine_board, x, y); if (ret == 0) // When the number of thunder around the coordinate is 0 Time
Open the surrounding grid And then X and Y Eight coordinates around the center of the expansion Repetition is recursive expansion { show_board[x][y] = ' '; if (x - 1 > 0 && y -
1 > 0 && show_board[x - 1][y - 1] == '*') { Open(mine_board, show_board, x - 1,
y- 1); } if (x - 1 > 0 && y > 0 && show_board[x - 1][y] == '*') { Open(
mine_board, show_board, x - 1, y); } if (x - 1 > 0 && y + 1 <= COL && show_board
[x - 1][y + 1] == '*') { Open(mine_board, show_board, x - 1, y + 1); } if (x > 0
&& y + 1 <= COL && show_board[x][y + 1] == '*') { Open(mine_board, show_board, x
, y + 1); } if (x + 1 < ROW && y + 1 <= COL && show_board[x + 1][y + 1] == '*')
{ Open(mine_board, show_board, x + 1, y + 1); } if (x + 1 <= ROW && y > 0 &&
show_board[x + 1][y] == '*') { Open(mine_board, show_board, x + 1, y); } if (x +
1 <= ROW && y - 1 > 0 && show_board[x + 1][y - 1] == '*') { Open(mine_board,
show_board, x + 1, y - 1); } if (x > 0 && y - 1 > 0 && show_board[x][y - 1] ==
'*') { Open(mine_board, show_board, x, y - 1); } } else if (ret != 0) show_board
[x][y] = Num(mine_board, x, y) + 48; }
6. Calculate the number of thunder in a circle around the coordinate .
int Num(char board[ROWS][COLS], int x, int y) { int sum = 0; //sum Is the number of mines around the coordinate sum
= board[x - 1][y - 1] + board[x][y - 1] + board[x + 1][y - 1] + board[x + 1][y]
+ board[x + 1][y + 1] + board[x][y + 1] + board[x - 1][y + 1] + board[x - 1][y]
- (8 * '0'); return sum; }
7. Judge whether the player has discharged all mines .
int Full(char board[ROWS][COLS], int row, int col) { int i; int j; int sum = 0;
//sum Is the number of chessboard that has not been opened for (i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { if (
board[i][j] == '*') sum++; } } if (sum == M) // When the number of unopened grids is thunder number return 1 return 1; }
8. Player input coordinates mine sweeping .
void PlayerMove(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int
row, int col) { int x = 0; int y = 0; int ret = 0; while (1) { if (Full(
show_board, ROW, COL) == 1) // When the chessboard is not opened and the grid is equal to the number of thunder, the player wins { printf(" Game victory \n"); DisPlayboard(
mine_board, ROW, COL); break; } printf(" Please enter coordinates :"); scanf("%d%d", &x, &y); if (x
>= 1 && x <= row && y >= 1 && y <= col) { if (mine_board[x][y] == '1')
// Players step on thunder , game over { printf(" game over \n"); DisPlayboard(mine_board, ROW, COL); break; }
ret= Num(mine_board, x, y); if (ret == 0) // When there is no thunder around , Expand the surrounding grid { Open(mine_board,
show_board, x, y); DisPlayboard(show_board, ROW, COL); } else if (ret != 0)
// Print the number of mines when there are mines around { show_board[x][y] = ret + 48; DisPlayboard(show_board, ROW, COL);
} } else printf(" Input error , Please re-enter "); } }
9. Function declaration , Macro definition , Function definition and Implementation , test .
source file ：test.c // Mainly responsible for testing the whole code
Header file ：game.h // Contains various declarations of functions , Macro definition
source file ：game.c // Contains the definition of various functions

test.c
; printf("************************************\n"); printf("******* 1.play
*******\n"); printf("******* 0.exit *******\n"); printf(
"************************************\n"); } int game() { char show_board[ROWS][
COLS] = { 0 }; char mine_board[ROWS][COLS] = { 0 }; // Initboard(show_board,
ROWS, COLS,'*'); // Initialize array Initboard(mine_board, ROWS, COLS, '0'); printf(" The game begins \n"
); DisPlayboard(show_board, ROW, COL); // Print player flash disk // GetBoom(mine_board, ROW, COL)
; // Burying thunder printf("\n\n\n"); DisPlayboard(mine_board, ROW, COL); // Print flash disk // if (
Judge_first(mine_board, show_board, ROW, COL) == 2) // If one victory , No more PlayerMove function
return 2; // PlayerMove(mine_board, show_board, ROW, COL); } void test() { int n
; menu(); do { printf(" Please enter the option button : "); scanf("%d", &n); switch (n) { case 1: game()
; break; case 0: printf(" Game exit \n"); break; default: printf(" Input error , Please re-enter \n"); break
; } }while(1); } int main() { srand((unsigned int)time(NULL)); test(); }
game.c
#include"game.h" void Initboard(char board[ROWS][COLS], int row, int col, char
set) { memset(board, set, row*col * sizeof(board[0][0])); } void DisPlayboard(
char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf(" ");
for (i = 1; i <= row; i++) // Print line { printf("%d ", i); } printf("\n"); for (i = 1;
i<= row; i++) { printf("%d ", i); // Print column for (j = 1; j <= col; j++) { printf(
"%c ", board[i][j]); } printf("\n"); } printf("\n"); } void GetBoom(char board[
ROWS][COLS], int row, int col) // Burying thunder { int x = 0; int y = 0; int sum = 0; while(1
) { x = rand() % row +1; // Ray put it in 1-9 Within the scope of y = rand() % col +1; if (1) { board[x][y]
= '1'; sum++; if (sum == M) // Burrow in the required mines and jump out of the loop { break; } } } } int Judge_first(char
mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col) { int x =
0; int y = 0; int m; int n; printf(" Please enter the coordinates to be checked :"); scanf("%d%d", &x, &y); while (
1) { if (mine_board[x][y] == '1') // If you step on Thunder for the first time , Change this coordinate to non thunder { mine_board[x][y] = '0';
show_board[x][y] = ' '; while (1) { m = rand() % 9 + 1; n = rand() % 9 + 1; if (
m!= x && n != y && mine_board[m][n] != '1') { mine_board[m][n] = '1'; } Open(
mine_board, show_board, x, y); if (Full(show_board, ROW, COL) == 1)
// After unfolding, judge whether to win once or not { printf(" Game victory \n"); DisPlayboard(mine_board, ROW, COL); return 2
; // A winning return 2 No more execution PlayerMove function break; } DisPlayboard(show_board, row, col);
break; } break; } else if (mine_board[x][y] == '0') { show_board[x][y] = ' ';
Open(mine_board, show_board, x, y); if (Full(show_board, ROW, COL) == 1) {
printf(" Game victory \n"); DisPlayboard(mine_board, ROW, COL); return 2; break; }
DisPlayboard(show_board, row, col); break; } } } void PlayerMove(char mine_board
[ROWS][COLS], char show_board[ROWS][COLS], int row, int col) { int x = 0; int y
= 0; int ret = 0; while (1) { if (Full(show_board, ROW, COL) == 1) // Chess board full of players win {
printf(" Game victory \n"); DisPlayboard(mine_board, ROW, COL); break; } printf(" Please enter coordinates :");
scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (
mine_board[x][y] == '1') { printf(" game over \n"); DisPlayboard(mine_board, ROW, COL);
break; } ret = Num(mine_board, x, y); if (ret == 0) // When there is no thunder around , Expand the surrounding grid { Open(
mine_board, show_board, x, y); DisPlayboard(show_board, ROW, COL); } else if (
ret!= 0) // Print the number of mines when there are mines around { show_board[x][y] = ret + 48; DisPlayboard(show_board,
ROW, COL); } } else printf(" Wrong input , Please re-enter "); } } void Open(char mine_board[ROWS][
COLS],char show_board[ROWS][COLS], int x, int y) { int ret = 0; ret = Num(
mine_board, x, y); if (ret == 0) // When the number of thunder around the coordinate is 0 Time Open the surrounding grid And then X and Y Eight coordinates around the center of the expansion Repetition is recursive expansion
{ show_board[x][y] = ' '; if (x - 1 > 0 && y - 1 > 0 && show_board[x - 1][y - 1]
== '*') { Open(mine_board, show_board, x - 1, y - 1); } if (x - 1 > 0 && y > 0
&& show_board[x - 1][y] == '*') { Open(mine_board, show_board, x - 1, y); } if (
x- 1 > 0 && y + 1 <= COL && show_board[x - 1][y + 1] == '*') { Open(mine_board,
show_board, x - 1, y + 1); } if (x > 0 && y + 1 <= COL && show_board[x][y + 1]
== '*') { Open(mine_board, show_board, x, y + 1); } if (x + 1 < ROW && y + 1 <=
COL&& show_board[x + 1][y + 1] == '*') { Open(mine_board, show_board, x + 1, y +
1); } if (x + 1 <= ROW && y > 0 && show_board[x + 1][y] == '*') { Open(
mine_board, show_board, x + 1, y); } if (x + 1 <= ROW && y - 1 > 0 && show_board
[x + 1][y - 1] == '*') { Open(mine_board, show_board, x + 1, y - 1); } if (x > 0
&& y - 1 > 0 && show_board[x][y - 1] == '*') { Open(mine_board, show_board, x, y
- 1); } } else if (ret != 0) show_board[x][y] = Num(mine_board, x, y) + 48; }
int Num(char board[ROWS][COLS], int x, int y) { int sum = 0; sum = board[x - 1][
y- 1] + board[x][y - 1] + board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y
+ 1] + board[x][y + 1] + board[x - 1][y + 1] + board[x - 1][y] - (8 * '0');
return sum; } int Full(char board[ROWS][COLS], int row, int col) { int i; int j;
int sum = 0; for (i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { if (board
[i][j] == '*') sum++; } } if (sum == M) return 1; }
game.h
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include
<stdlib.h> #include<time.h> #define ROW 9 #define COL 9 #define ROWS ROW + 2 #
define COLS COL + 2 #define M 10 // Number of Mines required void test(); int game(); void
Initboard(char board[ROWS][COLS], int row, int col, int set); // Initialize array void
DisPlayboard(char board[ROWS][COLS], int row, int col); // Print flash disk void GetBoom(char
board[ROWS][COLS], int row, int col); // Bomb emplacement int Judge_first(char mine_board[
ROWS][COLS], char show_board[ROWS][COLS], int row, int col); // Make sure it's not ray the first time void
PlayerMove(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row,
int col); // Player demining void Open(char mine_board[ROWS][COLS],char show_board[ROWS][
COLS], int x, int y); // Expansion function int Num(char mine_board[ROWS][COLS], int x, int y);
// Calculate the number of thunder around the coordinates int Full(char board[ROWS][COLS], int row, int col); // Judge whether the thunder is full or not

Technology
Daily Recommendation