I believe everyone has played minesweeping , It used to be in the computer room , Play minesweepers and spider cards when you're bored , Sometimes I can play for a long time . So today , Let's write a simple minesweeping game !

It's the same as the game of Gobang , Let's first list the game features it needs :
(1)void Menu(); menu
(2)void Game(); Game framework
(3)int GetRandIndex(int start, int end); Subscripts of randomly generated mines
(4)void SetMines(char mineboard[][COL], int row, int col); Mine array
(5)char ShowBoard(char mineboard[][COL], int row, int col); Minesweeping array
(6)int GetMines(char mineboard[][COL], int row, int col); Judge how many mines are around the coordinates selected by the user

next , We implement these subroutines in turn
1. menu
void Menu() { printf("#########################\n"); printf("##### 1.Play
#####\n"); printf("##### 2.Exit #####\n");
printf("#########################\n"); printf("Please select:"); }
2. Subscripts of randomly generated mines , And mine .
When generating random numbers , We used the rand() % (end - start + 1) + start;
start and end It's the range of random numbers we need .
int GetRandIndex(int start, int end) // Random generation of mine subscript { return rand() % (end - start
+ 1) + start; } void SetMines(char mineboard[][COL], int row, int col) // Mine burying {
srand((unsigned long)time(NULL)); // Random number seed int count = 0; while (count <
Mine_Num){ int x = GetRandIndex(1, 10); int y = GetRandIndex(1, 10); if
(mineboard[x][y] == '0'){ mineboard[x][y] = '1'; count++; } } }

3. Shows how many mines are around the coordinates selected by the user , We need to pay attention here , The return value of this function is integer , But what we got mineboard by "1" Character type of , We know that characters are actually ASCII The code is also an Arabic number . So to get the Arabic numerals, you need to subtract seven characters ‘0’.
int GetMines(char mineboard[][COL], int row,int col) // Shows the number of mines around { return
mineboard[row - 1][col - 1] + mineboard[row - 1][col] + mineboard[row - 1][col
+ 1] + mineboard[row][col - 1] + \ mineboard[row][col + 1] + mineboard[row +
1][col - 1] + mineboard[row + 1][col] + mineboard[row + 1][col + 1] - 7 * '0'; }
4. Show the chessboard , We need to understand that in this program , We used two arrays , One is used to bury mines , One is used to show users . And this display chessboard is used to mine and display for users .
char ShowBoard(char showboard[][COL], int row, int col) { int i = 0; int j =
0; printf(" "); for (i = 1; i <= 10; i++){ printf("%d ", i); } printf("\n");
for (i = 0; i < col-1; i++){ printf("----"); } printf("\n"); for (i = 1; i <=
row - 2; i++) { printf("%-2d|", i); for (j = 1; j <= col - 2; j++) { printf("
%c |", showboard[i][j]); } printf("\n"); int k = 1; for (k = 1; k <= col - 1;
k++) { printf("----"); } printf("\n"); } }
5. The framework of the whole game .
oid Game() { char mineboard[ROW][COL]; char showboard[ROW][COL];
memset(mineboard, '0', sizeof(mineboard)); // Clear all the mines 0 memset(showboard, '*',
sizeof(showboard)); // Set the minesweeping array to zero * SetMines(mineboard, ROW, COL); // Random setting of mine int
count = TOTAL; int x = 0; int y = 0; while (1){ ShowBoard(showboard, ROW, COL);
printf("Please select<x,y>:"); scanf("%d %d", &x, &y); if (x >= 1 && x <= ROW -
2 && y >= 1 && y <= COL - 2){ if (mineboard[x][y] == '0'){ char num =
GetMines(mineboard, x, y); showboard[x][y] = num; //Showboard(mineboard, ROW,
COL); count--; if (count <= 20){ printf(" congratulations , Avoid all minefields perfectly !\n"); break; } } else{
printf(" What a pity , You were killed in the blast !\n"); break; } } else{ printf(" Wrong input , Please select again !\n"); } } }
To avoid code redundancy , And the readability is strong , We use header file for function declaration and so on .

Function declaration mine.h
#ifndef _MINE_H_ #define _MINE_H_ #include<stdio.h> #include<windows.h>
#include<time.h> #pragma warning(disable:4996) #define ROW 12 #define COL 12
#define TOTAL 10*10 #define Mine_Num 20 void Menu(); void Game(); int
GetRandIndex(int start, int end); void SetMines(char mineboard[][COL], int row,
int col); char ShowBoard(char mineboard[][COL], int row, int col); int
GetMines(char mineboard[][COL], int row, int col); #endif
Function implementation mine.c
#include "mine.h" int GetRandIndex(int start, int end) // Random generation of mine subscript { return
rand() % (end - start + 1) + start; } void SetMines(char mineboard[][COL], int
row, int col) { srand((unsigned long)time(NULL)); int count = 0; while (count <
Mine_Num){ int x = GetRandIndex(1, 10); int y = GetRandIndex(1, 10); if
(mineboard[x][y] == '0'){ mineboard[x][y] = '1'; count++; } } } int
GetMines(char mineboard[][COL], int row,int col) // Shows the number of mines around { return
mineboard[row - 1][col - 1] + mineboard[row - 1][col] + mineboard[row - 1][col
+ 1] + mineboard[row][col - 1] + \ mineboard[row][col + 1] + mineboard[row +
1][col - 1] + mineboard[row + 1][col] + mineboard[row + 1][col + 1] - 7 * '0';
} char ShowBoard(char showboard[][COL], int row, int col) { int i = 0; int j =
0; printf(" "); for (i = 1; i <= 10; i++){ printf("%d ", i); } printf("\n");
for (i = 0; i < col-1; i++){ printf("----"); } printf("\n"); for (i = 1; i <=
row - 2; i++) { printf("%-2d|", i); for (j = 1; j <= col - 2; j++) { printf("
%c |", showboard[i][j]); } printf("\n"); int k = 1; for (k = 1; k <= col - 1;
k++) { printf("----"); } printf("\n"); } } void Game() { char
mineboard[ROW][COL]; char showboard[ROW][COL]; memset(mineboard, '0',
sizeof(mineboard)); // Clear all the mines 0 memset(showboard, '*', sizeof(showboard));
// Set the minesweeping array to zero * SetMines(mineboard, ROW, COL); int count = TOTAL; int x = 0; int y
= 0; while (1){ ShowBoard(showboard, ROW, COL); printf("Please select<x,y>:");
scanf("%d %d", &x, &y); if (x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2){
if (mineboard[x][y] == '0'){ char num = GetMines(mineboard, x, y);
showboard[x][y] = num; //Showboard(mineboard, ROW, COL); count--; if (count <=
20){ printf(" congratulations , Avoid all minefields perfectly !\n"); break; } } else{ printf(" What a pity , You were killed in the blast !\n");
break; } } else{ printf(" Wrong input , Please select again !\n"); } } }
Principal function main.c
#include "mine.h" void Menu() { printf("#########################\n");
printf("##### 1.Play #####\n"); printf("##### 2.Exit #####\n");
printf("#########################\n"); printf("Please select:"); } int main() {
int select = 0; int quit = 0; while (quit!=1){ Menu(); scanf("%d", &select);
switch (select){ case 1: Game(); break; case 2: printf("Bye-bye!\n"); quit = 1;
break; default: printf("Select error!Please select again!\n"); break; } } }
Running results :

Technology