Examples of this article are shared with you C Language to achieve the specific code of minesweeping game , For your reference , The details are as follows :

first , Create a text.c file :

Write main function : 
int main() { test(); return 0; }
definition test() function , Start implementation
void test() { printf(" The Minesweeper game !\n"); int input = 0; srand((unsigned
int)time(NULL)); do { menu(); printf(" Please select :"); scanf("%d", &input); switch
(input) { case 1: game(); break; case 0: printf(" Exit the game !\n"); break; default:
printf(" Selection error ! Please reselect !"); break; } } while (input); }
realization menu() function , Make a simple game start interface :
// The Minesweeper game #include "game.h" void menu() { printf("*************************\n");
printf("*** 1. play 0. No play ***\n"); printf("*************************\n"); }
make game() function , The implementation of the whole minesweeping game is defined in this :
void game() { // Ray's information //1. Information about the arranged thunder char mine[ROWS][COLS] = { 0 };//11*11
//2. Check the information of mine char show[ROWS][COLS] = { 0 }; // initialization InitBoard(mine, ROWS, COLS,
'0'); InitBoard(show, ROWS, COLS, '*'); // Print chessboard //DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL); // Lay thunder SetMine(mine, ROW, COL); DisplayBoard(mine,
ROW, COL); // mine clearance FindMine(mine, show, ROW, COL); }
subsequently , Create a header file game.h:

  Call the library function in the header file and declare the function to be implemented. , Then other files call the header file , This allows library functions to avoid repeated calls :
#pragma once #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2
#define EASY_COUNT 78 #include<stdio.h> #include<stdlib.h> #include<time.h>
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); void
DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char
mine[ROWS][COLS], int row, int col); void FindMine(char mine[ROWS][COLS], char
show[ROWS][COLS], int row, int col);
Create a game.c file , For function implementation :
#include "game.h" 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; } } } void DisplayBoard(char board[ROWS][COLS],
int row, int col) { int i = 0; int j = 0; // Print column number 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 ", board[i][j]); } printf("\n"); }
} void SetMine(char mine[ROWS][COLS], int row, int col) { int count =
EASY_COUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1;
if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } //0 1 2 3x //1 1 1 1
//2 1 1 1 //3 1 1 1 //y int get_mine_count(char mine[ROWS][COLS],int x,int y) {
return mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x -
1][y] + mine[x + 1][y] + mine[x - 1][y + 1] + mine[x][y + 1] + mine[x + 1][y +
1] - 8 * '0'; } void FindMine(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-
EASY_COUNT) { printf(" Please enter coordinates :"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row
&& y >= 1 && y <= col) { // Legal coordinates //1. Step on thunder if (mine[x][y] == '1') {
printf(" unfortunately , You were killed !\n"); DisplayBoard(mine, ROW, COL); break; } else { //2. I didn't step on it
int count = get_mine_count(mine, x, y); show[x][y] = count + '0';
DisplayBoard(show, row, col); win++; } } else { printf(" Input error , Please re-enter :"); } } if
(win == row*col - EASY_COUNT) { printf(" congratulations , Successful mine clearance !\n"); DisplayBoard(mine, ROW,
COL); } }
Code run :

  

matters needing attention :

1. All function headers are declared in the library , Other files only need to call the header file , Avoid duplicate definitions ;

 

2. Definition of values such as chessboard , Best use #define Defined in header file , Reuse of other functions , This is convenient for later modification :

3. be careful , When defining the chessboard , If you want to play 9*9 Mine clearance , Just define one 11*11 Chessboard (ROWS and COLS), This facilitates dealing with boundary problems when writing , The following figure is an example :

The real place to play chess is in the inner box .

That's all for today's minesweeping game , Thank you for watching , Thank you !

Move, you knock the code 666 My little hand , Give me a compliment ! Thank you very much !

If you have questions, you can also leave a message in the comment area !

 

Technology