design requirement :

1. You won't be killed by thunder for the first time .

2. If it's not thunder, the number of thunder around will be displayed .

3. There's no thunder around , And then it unfolds .

Realization idea :

1. First, we need to design two mine arrays , One is the mine array used by the designer to store the data of minefield layout , The other is the minefield used by players to clear mines

2. Mine laying : We need to design one A*A(A Represents the size of the minefield , Design according to the designer's needs ) The thunder array , Cloth has X(X Represents the number of mines , Design according to the needs of designers and players ) A thunder , Then we can use two-dimensional array to realize ray array , Use character ‘1’ It means there is thunder , character ‘0’ No thunder ( If you use numbers 0 and 1 Indicates whether there is thunder , Then there will be ambiguity about whether the number represents thunder or the number of thunder , So this is not the way ), So the ray matrix is initialized as a character ‘0’, And then use %c To print . Another question is whether we are going to design the mine array into a A*A What's wrong , The answer is No , If you're counting the number of mines around a coordinate , It's going to traverse the surrounding environment A-1 Coordinates , If it's designed to be A*A The thunder array , So there's no space around the outermost coordinates 8 It's a place , So we should design the mine array as (A+2)*(A+2), But mine laying and mine sweeping are only in the middle A*A In progress , In this way, the problem can be solved .

                 

 3. mine clearance : Just like bray , We also need to design the mine array as (A+2)*(A+2); Because this thunder array is a player's thunder array , In order to increase the mystery , It should be initialized as a character ‘ * ’, Or other characters .

                    

 4. Judge whether you win or lose : Characters in danglei matrix ‘ * ’ also 10 Or the number of demining has reached A*A-X Win the first time .

Specific function implementation :

1. Enter the menu : Choose whether to play the game
void mune() { printf("****************************\n"); printf("***** 1. play
*****\n"); printf("***** 0. exit *****\n");
printf("****************************\n"); }
2. Enter the game , Initialize two mine arrays .
void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set) // Initialize mine array {
int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) {
arr[i][j] = set; } } }
3. Lay out mines in a mine array
void SetMine(char mine1[ROWS][COLS], int row, int col) // Set the distribution of mine array { int count =
EASY_COUNT; while (count) { int x = rand()%row+1; int y = rand()%col+1; if
(mine1[x][y] == '0') { mine1[x][y] = '1'; count--; } } }
4. Print ray array
void DisplayBoard(char mine1[ROWS][COLS], int row, int col) // Print ray array { int i =
0; int j = 0; for (i = 0; i <= row; i++) { printf("%d ", i); } printf("\n");
for (i = 1; i <= row; i++) { printf("%d ", i); for (j = 1; j <= col; j++) {
printf("%c ", mine1[i][j]); } printf("\n"); } }
5. If the coordinate is not ray , Count the number of mines around
int GetMineCount(char mine1[ROWS][COLS], int x, int y) // Count the number of Mines { return
mine1[x - 1][y] + mine1[x - 1][y - 1] + mine1[x][y - 1] + mine1[x + 1][y - 1] +
mine1[x + 1][y] + mine1[x + 1][y + 1] + mine1[x][y + 1] + mine1[x - 1][y + 1] -
8 * '0'; }
6. Avoid being killed by thunder for the first time
void MoveMine(char mine1[ROWS][COLS], char mine2[ROWS][COLS], int row, int
col)// Avoid being killed by thunder for the first time { int x = 0; int y = 0; char ch = 0; int count = 0; int ret =
1; printf(" Mine sweeping by inputting coordinates :>"); while (1) { scanf("%d%d", &x, &y); if (x >= 1 && x <=
row&&y >= 1 && y <= col) { if (mine1[x][y] == '1') { mine1[x][y] = '0'; char ch
= GetMineCount(mine1,x, y); mine2[x][y] = ch + '0'; OpenMine(mine1,mine2, x,
y); while (ret) { int x = rand() % row + 1; int y = rand() % col + 1; if
(mine1[x][y] == '0') { mine1[x][y] = '1'; ret--; break; } } break; } if
(mine1[x][y] == '0') { char ch = GetMineCount(mine1, x, y); mine2[x][y] = ch +
'0'; OpenMine(mine1,mine2, x, y); break; } } else { printf(" Coordinate error , Please re-enter !\n"); }
} }
7. There's no thunder around the coordinates , open
void OpenMine(char mine1[ROWS][COLS], char mine2[ROWS][COLS], int x, int
y)// There's no thunder around the coordinates , It can be expanded { if (mine1[x - 1][y - 1] == '0') { mine2[x - 1][y - 1] =
GetMineCount(mine1,x - 1, y - 1) + '0';// Display the number of mines around the coordinate } if (mine1[x - 1][y] ==
'0') { mine2[x - 1][y] = GetMineCount(mine1, x - 1, y) + '0';// Display the number of mines around the coordinate } if
(mine1[x - 1][y + 1] == '0') { mine2[x - 1][y + 1] = GetMineCount(mine1, x - 1,
y + 1) + '0';// Display the number of mines around the coordinate } if (mine1[x][y - 1] == '0') { mine2[x][y - 1] =
GetMineCount(mine1, x, y - 1) + '0';// Display the number of mines around the coordinate } if (mine1[x][y + 1] == '0') {
mine2[x][y + 1] = GetMineCount(mine1, x, y + 1) + '0';// Display the number of mines around the coordinate } if (mine1[x
+ 1][y - 1] == '0') { mine2[x + 1][y - 1] = GetMineCount(mine1, x + 1, y - 1) +
'0';// Display the number of mines around the coordinate } if (mine1[x + 1][y] == '0') { mine2[x + 1][y] =
GetMineCount(mine1, x + 1, y) + '0';// Display the number of mines around the coordinate } if (mine1[x + 1][y + 1] ==
'0') { mine2[x + 1][y + 1] = GetMineCount(mine1, x + 1, y + 1) +
'0';// Display the number of mines around the coordinate } }
8. Mine sweeping and winning judgment
void FindMine(char mine1[ROWS][COLS], char mine2[ROWS][COLS], int row, int
col) // mine clearance { int x = 0; int y = 0; int win = 0; while (win<row*col-EASY_COUNT) {
printf(" Please enter the coordinates to be checked :>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row&&y >= 1 &&
y <= col) { if (mine1[x][y] == '1') { printf(" unfortunately , You were killed in the blast !\n");
DisplayBoard(mine1, row, col); break; } else { int count = GetMineCount(mine1,
x, y); mine2[x][y] = count + '0'; DisplayBoard(mine2, row, col); } } else {
printf(" Illegal coordinates , Please re-enter !\n"); } } if (win == row*col - EASY_COUNT) {
printf(" congratulations , Successful demining !\n"); } }
Attach the source code :

game.h( Header file )
#include<stdio.h> #include<stdlib.h> #include<time.h> #define EASY_COUNT 10
// Set the number of mines #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 void
InitBoard(char arr[ROWS][COLS], int rows, int cols, char set); // Initialize mine array void
SetMine(char mine1[ROWS][COLS], int row, int col);// Setting and distribution of Mines void
DisplayBoard(char mine1[ROWS][COLS], int row, int col);// Print ray array void
MoveMine(char mine1[ROWS][COLS], char mine2[ROWS][COLS], int row, int
col);// Avoid being killed by thunder for the first time void OpenMine(char mine1[ROWS][COLS], char mine2[ROWS][COLS],
int row, int col);// There's no thunder around the coordinates , Implementation deployment void FindMine(char mine1[ROWS][COLS], char
mine2[ROWS][COLS], int row, int col);// mine clearance
game.c( Function implementation )
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void InitBoard(char
arr[ROWS][COLS], int rows, int cols,char set) // Initialize mine array { int i = 0; int j = 0;
for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { arr[i][j] = set; } }
} void SetMine(char mine1[ROWS][COLS], int row, int col) // Set the distribution of mine array { int count =
EASY_COUNT; while (count) { int x = rand()%row+1; int y = rand()%col+1; if
(mine1[x][y] == '0') { mine1[x][y] = '1'; count--; } } } void DisplayBoard(char
mine1[ROWS][COLS], int row, int col) // Print ray array { int i = 0; int j = 0; for (i = 0;
i <= row; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) {
printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", mine1[i][j]); }
printf("\n"); } } int GetMineCount(char mine1[ROWS][COLS], int x, int y)
// Count the number of Mines { return mine1[x - 1][y] + mine1[x - 1][y - 1] + mine1[x][y - 1] +
mine1[x + 1][y - 1] + mine1[x + 1][y] + mine1[x + 1][y + 1] + mine1[x][y + 1] +
mine1[x - 1][y + 1] - 8 * '0'; } void MoveMine(char mine1[ROWS][COLS], char
mine2[ROWS][COLS], int row, int col)// Avoid being killed by thunder for the first time { int x = 0; int y = 0; char ch
= 0; int count = 0; int ret = 1; printf(" Mine sweeping by inputting coordinates :>"); while (1) { scanf("%d%d",
&x, &y); if (x >= 1 && x <= row&&y >= 1 && y <= col) { if (mine1[x][y] == '1')
{ mine1[x][y] = '0'; char ch = GetMineCount(mine1,x, y); mine2[x][y] = ch +
'0'; OpenMine(mine1,mine2, x, y); while (ret) { int x = rand() % row + 1; int y
= rand() % col + 1; if (mine1[x][y] == '0') { mine1[x][y] = '1'; ret--; break;
} } break; } if (mine1[x][y] == '0') { char ch = GetMineCount(mine1, x, y);
mine2[x][y] = ch + '0'; OpenMine(mine1,mine2, x, y); break; } } else {
printf(" Coordinate error , Please re-enter !\n"); } } } void OpenMine(char mine1[ROWS][COLS], char
mine2[ROWS][COLS], int x, int y)// There's no thunder around the coordinates , It can be expanded { if (mine1[x - 1][y - 1] ==
'0') { mine2[x - 1][y - 1] = GetMineCount(mine1,x - 1, y - 1) + '0';// Display the number of mines around the coordinate
} if (mine1[x - 1][y] == '0') { mine2[x - 1][y] = GetMineCount(mine1, x - 1, y)
+ '0';// Display the number of mines around the coordinate } if (mine1[x - 1][y + 1] == '0') { mine2[x - 1][y + 1] =
GetMineCount(mine1, x - 1, y + 1) + '0';// Display the number of mines around the coordinate } if (mine1[x][y - 1] ==
'0') { mine2[x][y - 1] = GetMineCount(mine1, x, y - 1) + '0';// Display the number of mines around the coordinate } if
(mine1[x][y + 1] == '0') { mine2[x][y + 1] = GetMineCount(mine1, x, y + 1) +
'0';// Display the number of mines around the coordinate } if (mine1[x + 1][y - 1] == '0') { mine2[x + 1][y - 1] =
GetMineCount(mine1, x + 1, y - 1) + '0';// Display the number of mines around the coordinate } if (mine1[x + 1][y] ==
'0') { mine2[x + 1][y] = GetMineCount(mine1, x + 1, y) + '0';// Display the number of mines around the coordinate } if
(mine1[x + 1][y + 1] == '0') { mine2[x + 1][y + 1] = GetMineCount(mine1, x + 1,
y + 1) + '0';// Display the number of mines around the coordinate } } void FindMine(char mine1[ROWS][COLS], char
mine2[ROWS][COLS], int row, int col) // mine clearance { int x = 0; int y = 0; int win = 0;
while (win<row*col-EASY_COUNT) { printf(" Please enter the coordinates to be checked :>"); scanf("%d%d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col) { if (mine1[x][y] == '1') {
printf(" unfortunately , You were killed in the blast !\n"); DisplayBoard(mine1, row, col); break; } else { int
count = GetMineCount(mine1, x, y); mine2[x][y] = count + '0';
DisplayBoard(mine2, row, col); } } else { printf(" Illegal coordinates , Please re-enter !\n"); } } if (win
== row*col - EASY_COUNT) { printf(" congratulations , Successful demining !\n"); } }
test.c( test )
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void mune() {
printf("****************************\n"); printf("***** 1. play *****\n");
printf("***** 0. exit *****\n"); printf("****************************\n"); }
void game() { char mine1[ROWS][COLS] = { 0 }; // Mine array char mine2[ROWS][COLS] = {
0 }; // Display array // initialization InitBoard(mine1, ROWS, COLS, '0'); // Initializing a ray array InitBoard(mine2,
ROWS, COLS, '*'); // Initialize presentation array //1. Set up a mine SetMine(mine1, ROW, COL);// Set the distribution of mine array
//DisplayBoard(mine1, ROW, COL);// Print ray array DisplayBoard(mine2, ROW, COL);// Print display array
//2. mine clearance MoveMine(mine1, mine2, ROW, COL);// Avoid being killed by thunder for the first time //DisplayBoard(mine1, ROW,
COL); // Print ray array DisplayBoard(mine2, ROW, COL); // Print display array
FindMine(mine1,mine2,ROW,COL);// mine clearance } void test() { int inupt = 0;
srand((unsigned int)time(NULL)); do { mune(); printf(" Please select :>"); scanf("%d",
&inupt); switch (inupt) { case 1: game(); break; case 0: printf(" Quit the game .\n");
break; default: printf(" Input error , Please select again .\n"); break; } } while (inupt); } int main()
{ test(); return 0; }
Results display :

Choose whether to enter the game or not :

  Enter the game , Start minesweeping

Enter coordinates , And show the number of mines around , And unfold . 

  Sweep to thunder , The game failed

  summary : Before writing any program, the first thing to do is to analyze its logical thinking , Make the logic clear , The next step is to use syntax to achieve the goal , And we need to pay attention to the details , Wrong. It's hard to find a detail after debugging for a long time , We should also pay attention to the optimization of time and space ; Be careful in the process of writing , Pay attention to parameter transfer and function call , You also need to pay attention to the use of brackets , Ensure readability .

Technology