Effect display

Required settings

We need two 11*11 Two dimensional array of . Because if you only set 9*9, Then the edge of the matrix , Corners will be limited .

Specific module code

The necessary header files and some function declarations to realize the minesweeping game (game.h file )
// Required header file #include <stdio.h> #include <stdlib.h> #include <time.h> // Required macro definitions
#define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define COUNT
10 // Functions to be used ( incomplete ) void InitBoard(char board[ROWS][COLS], int rows, int cols,
char set);// Function declaration for initializing array void PrintBoard(char board[ROWS][COLS], int row, int
col);// Print function declaration of array void Set_Mine(char mine[ROWS][COLS], int row, int
col);// Declaration of buried function void Find_Mine(char mine[ROWS][COLS],char show[ROWS][COLS], int
row, int col);// Demining function declaration
  Main function of minesweeping game (test.c file )
void game() { // Define two arrays , One for practical operation , One for display char mine[ROWS][COLS] = { 0 }; char
show[ROWS][COLS] = { 0 }; // Initialize array InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*'); // Print PrintBoard(show, ROW, COL); // Mine the operational array
Set_Mine(mine, ROW, COL); //PrintBoard(mine, ROW, COL); // Demining the operational array , Player games
Find_Mine(mine,show, ROW, COL); } void menu() { printf("\n"); printf("\n");
printf("\n"); printf("***************************************\n");
printf("******1.PLAY 0.EXIT**********\n");
printf("***************************************\n"); printf("\n");
printf("\n"); printf("\n"); } int main() { srand((unsigned int)time(NULL)); int
input = 0; do { menu();// Easy Menu printf(" Please select :>"); scanf("%d", &input); switch
(input) { case 1: game();// Game body logic function break; case 0: printf(" Exit the game \n"); break;
default: break; } } while (input); return 0; }
Specific function of minesweeping game (game.c file )
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i =
0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) {
board[i][j] = set; } } } // Print array void PrintBoard(char board[ROWS][COLS], int
row, int col) { int i = 0; int j = 0; // Display coordinate sequence for (j = 0; j <= col; j++) {
printf("%d ", j); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ",
i);// Display coordinate row order for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); }
printf("\n"); } } // Mine burying void Set_Mine(char mine[ROWS][COLS], int row, int col) {
int x = 0; int y = 0; int count = 10; while (count) { x = rand() % row+1; y =
rand() % col+1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } void
Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) {
// Those that do not meet the display conditions will exit recursion if (x == 0 || y == 0 || x == ROWS - 1 || y == COLS - 1)
return; if (show[x][y] != '*') return; // Recursive implementation of expansion function int count = Num_Mine(mine, x,
y); if (count > 0) { show[x][y] = count+'0'; } else if (count == 0) {
show[x][y] = '0'; Expand(mine, show, x-1, y); Expand(mine, show, x-1, y-1);
Expand(mine, show, x, y-1); Expand(mine, show, x+1, y-1); Expand(mine, show,
x+1, y); Expand(mine, show, x+1, y+1); Expand(mine, show, x, y+1); Expand(mine,
show, x-1, y+1); } } // Number of mines around the target coordinate int Num_Mine(char mine[ROWS][COLS], int x,
int y) { return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x +
1][y + 1] + mine[x + 1][y] + mine[x + 1][y - 1] + mine[x][y + 1] + mine[x -
1][y + 1] - 8 * '0'; } // Demining function ( Player game mode ) void Find_Mine(char mine[ROWS][COLS],char
show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0; while
(win<row*col-COUNT) { printf(" Please enter coordinates :>"); scanf("%d%d", &x, &y); if (x >= 1 && x
<= 9 && y >= 1 && y <= 9) { if (mine[x][y] == '1') { printf(" You were killed \n");
PrintBoard(mine, ROW, COL); break; } else if (mine[x][y] == '0') { Expand(mine,
show, x, y);// Recursive expansion PrintBoard(show, ROW, COL); win++; } } else {
printf(" Illegal coordinates , Please re-enter \n"); break; } } if (win == row * col - COUNT) {
printf(" congratulations , Clearance game \n"); } }
  problem analysis

Question 1 : Why is the code for counting the number of mines around the target coordinates written like this ?
int Num_Mine(char mine[ROWS][COLS], int x, int y) { return mine[x - 1][y] +
mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y + 1] + mine[x + 1][y] +
mine[x + 1][y - 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0'; }
answer : We can analyze it first ASCLL Code table .

 

Can see characters 1 And characters 0 The difference between 1, Our code is to put the surrounding 8 Character addition of coordinates , subtract 8 Characters 0, We can get the exact number of thunder .

Question 2 : How recursion is implemented ?

answer : The core idea of recursion is very simple , Just like when we write code , If our destination coordinates are around 8 Coordinates without thunder , So what the destination coordinates print out is 0. Since it is 0, Then we can expand outward ( Because around 8 Coordinates without thunder ), Until it extends around a certain coordinate 8 Until there is thunder in the coordinates .

  When we choose (8,5) This coordinate , Because there is no thunder around , So we have to expand outward .

 

Let's assume that we extend this 0( In fact, every one next to the destination coordinate 0 Will expand ), Can see , Can only continue to the upper right corner 0 extend . We have written this rule into the code .

Technology