Let's sort out our ideas first and then write the mine sweeping code. In this way, we can write the code efficiently

1. First of all, you should have at least one interface to play the game ! The interface allows people to choose whether to play the game or exit , Or after playing the game, one is not enough to win and continue to play

2. Initialize array

3. Print chessboard

4. Number of mines placed

5. mine clearance

Implementation code of laidan

The code is as follows
void menu() { printf("**********************\n"); printf("****1.play
0.exit**\n"); printf("**********************\n"); } void test() { int input =
0; srand((unsigned int)time(NULL));// Not for the time being , This is the random number of time stamp used by the later code to mine do { menu();
printf(" Please select 》"); scanf("%d", &input); switch (input) { case 1:
printf(" Entered the game \n"); game(); break; case 0: printf(" Quit the game \n"); default:
printf(" Input error , Please re-enter \n"); } } while (input); } int main() { test(); return 0; }
Some people may find it strange , Why write so many functions , Can't I handle it with a function ?

It can be done by a function , But it's hard for you to modify the code later , And it's not easy to use your code if others use it , Therefore, writing code should abide by high cohesion and low coupling .

What is high cohesion and low coupling ? Is to make the functions of each function correspond to each other , My function corresponds to calculation , Instead of superimposing the results of calculation and print calculation . The good thing is , Easy to modify code errors , And people can just call this function directly with your code , Instead of changing around , Not as fast as you write .

When we finish the laidan, we should get the chessboard

The code is as follows
char mine[ROWS][COLS] = { 0 };// Thundering chessboard char show[ROWS][COLS] = { 0 };// Chessboard for players
I choose to use two arrays , An array is mine , One is a printed array chessboard .

Why choose two arrays ? The reason is obvious , Because one can't achieve two functions .

Then we begin to initialize the function

The code is as follows
void initboard(char board[ROWS][COLS], int rows, int cols, char set)// Initialize the chessboard of mine discharge
{ int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++)
{ board[i][j] = set; } } }
Because the two arrays are the same size , So you can print with the same function .

After initializing the array , You can start printing the chessboard

The code is as follows
void displayboard(char board[ROWS][COLS], int row, int col)// Print chessboard { int i = 0;
for (i = 0; i <= row; i++)// that 's ok { printf("%d ", i); } printf("\n"); int j = 0;
for (i = 1; i <= row; i++) { printf("%d ", i);// column for (j = 1; j <=col; j++) {
printf("%c ", board[i][j]); } printf("\n"); } }
Because it is the same function, you can use the same function to print the chessboard , But because the chess board with thunder can't be seen by the player , So printing the chessboard is just for us to test whether the code is wrong .

Number of mines placed

The code is as follows
void setmine(char mine[ROWS][COLS], int row, int col)// Place mine { int count =
easy_hard; while (count) { int x = rand() % row + 1; int y = rand() % col + 1;
if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } }
Be careful to put Ray's chessboard here. Don't make a mistake .

Now comes the final demining process

The code is as follows
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int
col)// Find ray { int x = 0; int y = 0; while (1) { printf(" Please enter >"); scanf("%d %d",
&x,&y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1')
{ printf(" unfortunately , You were killed \n"); displayboard(mine, row, col); break; } else {
system("cls"); int n = get_minr_count(mine,x,y);// Judge how many mines are nearby show[x][y] = n+'0';
displayboard(show, row, col);// Print chessboard } } else { printf(" The coordinates are incorrect. Please re-enter \n"); } } }
The questionable estimate here is show[x][y] =
n+'0'; Here it is , because n It's an integer , And we print the chessboard with %c To print , So it doesn't recognize printing integers , But let's add a character ‘0’, It knows .‘0‘ stay asslc It's worth it , Because the array was previously initialized , Also ’0’, So here we can print out how many mines are nearby

The second is  get_minr_count(mine,x,y); The implementation of

The code is as follows *8
static int get_minr_count(char mine[ROWS][COLS], int x, int y)// Judge the number of surrounding mines {
return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x][y -
1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]
- 8 * '0'; }
Minesweeper judges how many mines are nearby based on the range of Jiugong grid , So just add each number of the nine squares to minus 8*‘0‘ You can know the value of

All the codes of the main function are presented
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void game() { char
mine[ROWS][COLS] = { 0 };// Thundering chessboard char show[ROWS][COLS] = { 0 };// Chessboard for players
initboard(mine, ROWS, COLS, '0');// Initialize the chessboard of mine discharge initboard(show, ROWS, COLS,
'*');// Initialize the chessboard for players setmine(mine, ROW, COL);// Place mine //displayboard(mine, ROW,
COL);// Print chessboard displayboard(show, ROW, COL);// Print chessboard find_mine(mine,show, ROW,
COL);// Find ray } void menu() { printf("**********************\n");
printf("****1.play 0.exit**\n"); printf("**********************\n"); } void
test() { int input = 0; srand((unsigned int)time(NULL)); do { menu();
printf(" Please select 》"); scanf("%d", &input); switch (input) { case 1:
printf(" Entered the game \n"); game(); break; case 0: printf(" Quit the game \n"); default:
printf(" Input error , Please re-enter \n"); } } while (input); } int main() { test(); return 0; }
Header file function
#pragma once #include<stdio.h> #include<time.h> #include<stdlib.h>
#include<windows.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS
COL+2 #define easy_hard 10 void initboard(char board[ROWS][COLS], int rows, int
cols, char set);// Initialize chessboard void displayboard(char board[ROWS][COLS], int row, int
col);// Print chessboard void setmine(char mine[ROWS][COLS], int row,int col);// Place mine void
find_mine(char mine[ROWS][COLS], char show[ROWS][COLS],int row, int col);// Find ray
Game function
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" static int
get_minr_count(char mine[ROWS][COLS], int x, int y)// Judge the number of surrounding mines { return mine[x -
1][y] + mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y +
1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0'; } void
initboard(char board[ROWS][COLS], int rows, int cols, char set)// Initialize the chessboard of mine discharge { int
i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) {
board[i][j] = set; } } } void displayboard(char board[ROWS][COLS], int row, int
col)// Print chessboard { int i = 0; for (i = 0; i <= row; i++)// that 's ok { printf("%d ", i); }
printf("\n"); int j = 0; for (i = 1; i <= row; i++) { printf("%d ", i);// column for
(j = 1; j <=col; j++) { printf("%c ", board[i][j]); } printf("\n"); } } void
setmine(char mine[ROWS][COLS], int row, int col)// Place mine { int count = easy_hard;
while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if
(mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } void find_mine(char
mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)// Find ray { int x = 0; int
y = 0; while (1) { printf(" Please enter >"); scanf("%d %d", &x,&y); if (x >= 1 && x <=
row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf(" unfortunately , You were killed \n");
displayboard(mine, row, col); break; } else { system("cls"); int n =
get_minr_count(mine,x,y); show[x][y] = n+'0'; displayboard(show, row,
col);// Print chessboard } } else { printf(" The coordinates are incorrect. Please re-enter \n"); } } }
Finally, put some real pictures of mine sweeping you play with !

 

 

 

Technology
©2019-2020 Toolsou All rights reserved,
evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possible Experiment 4 Automated test tools - software test mysql Export data sql sentence _mysql according to sql Query statement export data Create a thread ——— Javaweb (3) Data structure experiment ( three )—— Stacks and queues TS stay vue2 Writing in the project web Front end signature plug-in _signature_pad Plug in implements electronic signature function docker Where is the image stored Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control springboot How to get reality in ip address