Make writing a habit together! This is the second day of my participation in the “Gold Digging Day New Plan Β· April More text challenge”. Click here for more details.

πŸš€ write in front πŸš€

  • πŸ”Ž hello everyone, I am ze ε₯€, I hope that after you read, you can help, not enough please correct! Learn and communicate together πŸ”Ž
  • 🎁 welcome everyone β†’ like πŸ‘ + collect ⭐️ + leave a message
  • βœ‰οΈ we don’t play the stage we choose, the play is not the script we choose πŸ“©

β…  preface

Minesweeper game, we must have played it. Endless words can also try to play a play, so to write minesweeper game of this small game will have a good idea. So this blog will introduce how to achieve the specific steps of minesweeper.

Minesweeper link πŸ‘‰ Minesweeper web edition

β…± Modular programming

Before realizing the logic of chess again, let’s say what is modular programming πŸ€”

Traditional programming

: All functions are placed in main.c, if the use of more modules, there will be a lot of code in a file, which is not conducive to the organization and management of the code, but also affects the programmer’s thinking.

Modular programming

: Put the code for each module in a separate.c file. The.h file provides declarations of external callable functions. Other.c files that want to use the code in the file simply need #include “xxx. h”. The use of modular programming can greatly improve code readability, maintainability, portability and so on.

Traditional programming

: All functions are placed in main.c, if the use of more modules, there will be a lot of code in a file, which is not conducive to the organization and management of the code, but also affects the programmer’s thinking.

Modular programming

: Put the code for each module in a separate.c file. The.h file provides declarations of external callable functions. Other.c files that want to use the code in the file simply need #include “xxx. h”. Using modular programming can greatly improve code readability, maintainability, portability and so on!

In summary: when you have more code, you can use modular programming to complete the program 😜

β…² Game thinking and logic

  1. Create a menu interface function to exit or enter the game.
  2. Store the nova that arranges thunder well and store the information that checks out thunder.
  3. First, perform thunder’s initialization board.
  4. Then, print out ray’s initialization board. Note: be sure to initialize the checkerboard before printing it.
  5. Next, lay out Ray’s message.
  6. Finally, enter the coordinates of the mine clearance.
  7. Check if the coordinate out is mine, the place of mine is character (1), the place of mine is character (0).
  8. Input coordinates when a total of β‘£ situations: “I’m sorry, you were killed!” Ning has entered the coordinates here, please re-enter! , “ning input coordinate range error! Re-enter”, “Congratulations, mine clearance success! Excellent!”
  9. Then, go back to step 1, whether to enter the game and exit the game.

β…³ Implement the game steps/process

​

β‘  Create the color function

πŸ–Š create color function color()

The corresponding value of the foreground color ↓

0= Black 8= gray 1= Blue 9= Light blue Hexadecimal 2= Green 10= light green A 3= lake blue 11= light green B 4= Red 12= light red C 5= purple 13= lavender D 6= yellow 14= light yellow E 7= white 15= bright white FCopy the code

Color () Creates the color function using the following ↓

void color(short x)	
{
	if (x >= 0 && x <= 15)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);	 
	else
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
Copy the code

It’s important to remember that the benefits of using color functions are nothing more than making your application look nice and sharp.

STD_OUTPUT_HANDLE requires the header file **#include< windows.h >** to be used.

β‘‘ Menu Interface

The menu interface function is really like one of our interfaces, like the menu of the game interface, the menu of the restaurant. Same idea. This is a library function that we can just reference directly. The following is the example code ↓

void menu() { color(0); / / Black Black system (" CLS "); // clear the screen. Color (10); Printf (" | -- -- -- -- -- -- -- -- -- -- - | | minesweeping game -- -- -- -- -- -- -- -- -- -- - | \ n "); printf("|********************************|\n"); Printf (" | painted painted u u u u u u u u u u u u u u | \ n "); Printf (" painted painted painted | 1 to 0. Quit u | \ n "); Printf (" | painted painted u u u u u u u u u u u u u u | \ n "); Printf (" | 0 = not ray -- -- -- -- -- -- -- -- -- -- 1 = it is ray | \ n "); printf("|--------------------------------|\n"); }Copy the code

Note β†’ A system(” CLS “) is used here; A clear screen effect is achieved, and only with this addition can you make all the interfaces in CMD black. Because we also add color(0) to the clear instruction; This is black.

β‘’ Realize multi-line and multi-column mine clearance

#define ROW 9
#define COL 9
Copy the code

The benefits of using the #define macro here:

  1. Convenient procedures to modify, do not need to modify the entire program, only to modify the macro definition.
  2. Improve the efficiency of the program, more convenient modularization.
  3. On the basis of checkers, the effect of checkers can be achieved by simply changing the value of the macro definition.

Change **#define** to 10. Change **#define to 10.

The minesweeper board with 10 rows and 10 columns is shown below

πŸ‘‡

​

But ten rows and ten columns is the case, of course, this problem is also very easy to solve. And you can see how to solve this problem. β˜†βŒ’(* ^ -γ‚œ)!!

β‘£ Realize multiple mines

#define Thunder 10
Copy the code

I mentioned the #define up here, but I’m not going to mention it.

Modify the number of thunder also only need to put this number (10) to modify the number of changes to modify, of course, please rational modify, you can not 9*9 chessboard, to a lot of thunder, that also how to play hahaha 😀.

For example, change it to 40 mines, and see what it looks like below

πŸ‘‡

​

β‘€ Checkerboard initialization

Print the checkerboard, essentially print the contents of an array. As shown below πŸ‘‡

void Initialization(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; }}}Copy the code

A char set is the character passed from the argument to the parameter.

Rows of real parameter group names can be omitted, but columns cannot.

β‘₯ Checkerboard printing

Print the checkerboard, essentially printing the contents of an array, where the contents are the characters **’0’**. As shown below πŸ‘‡

void Print_board(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; color(7); Printf (" ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ \ n "); for (i = 0; i <= row; i++) { if (i == 0) { printf("%d|", i); } else { printf("%d|", i); }} printf (" \ n / | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ "); printf("\n"); for (i = 1; i <= row; i++) { printf("%d|", i); for (j = 1; j <= col; j++) { printf("%c|", board[i][j]); } printf("\n"); } color(6); Printf ("\n----- minesweepers ------\n"); }Copy the code

Print a rendering of the checkerboard, as shown below πŸ‘‡

​

⑦ Lay out thunder information

After printing the board, lay out the thunder.

Void Lay_thunder(char Findout[ROWS][COLS], int row, int col) {void Lay_thunder(char Findout[ROWS], int col); while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (Findout[x][y] = '0') { Findout[x][y] = '1'; count--; }}}Copy the code

There’s another thing that’s used here: random numbers.

In actual development, we often need a certain range of random numbers, too large or too small does not meet the requirements, then, how to generate a certain range of random numbers? We can use the method of taking modulus:

int a = rand() % 10; // Generate a random number from 0 to 9. Note that 10 is divisibleCopy the code

If the upper and lower limits are to be specified:

int a = rand() % 51 + 13; // Generate random numbers ranging from 13 to 63Copy the code

Rand ()%51 = 0 and rand()%51 = 0 and rand()%51 = 50

Use the time() function in the <time.h> header to get the current time(accurate to the second), as follows:

srand((unsigned)time(NULL));
Copy the code

🍁 note: this is only executed once in the program!

⑧ players input thunder implementation steps

The player input coordinates here define a static local variable when the player inputs chess, when the code executes. When playing the game will remind once, the input of the first coordinate remember empty one grid! There is only one entry per game, so static local variables are used to ensure that the last value is not destroyed.

Check if the coordinate is not mined, the character ‘1’ is placed, the character ‘0’ is not placed.

There are several cases to judge the validity of coordinate input:

  1. I’m sorry you were blown up!
  2. Ning has entered the coordinates here, please re-enter!
  3. Ning entered wrong range of coordinates! Re-enter.
  4. Congratulations, demining success! Excellent!
void Check(char Layouts[ROWS][COLS], char Findout[ROWS][COLS], int row, int col) { //1. Enter the coordinates of lightning detection //2. Check whether the coordinates are mined. The character '1' is used for laying mines, and the character '0' is used for not laying mines. int x, y; int win = 0; while (win<row*col - Thunder) { static int j = 1; While (j) {color(8); printf("--------------------------\n"); Printf (" enter the first coordinate and remember to leave a space! \n"); printf("--------------------------\n"); j--; break; } color(11); printf("---------------\n"); Printf (" Please enter coordinates >:"); Scanf ("%d %d", &x, &y); printf("---------------\n"); / / judge the legitimacy of the coordinates of the if (x > = 1 && x < = row && y > = 1 && y < = {col) if (Layouts [x] [y] = = '1') {printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n ");  Printf (" | it's a pity that you have been killed! |\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Print_board(Layouts, ROW, COL); Sleep(5000); break; } if (Findout[x][y] == '0') { color(6); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | ning has been here input coordinates, please input again! |\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); } if (Findout[x][y] == '1') { color(6); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | ning has been here input coordinates, please input again! |\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); } else {Count = Statistics(Layouts, x,y); Findout[x][y] = Count + '0'; Print_board(Findout, row, col); win++; }} else {printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | ning to enter the coordinates of the error range! To re-enter | \ n "); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); }} the if (win = = row * col - Thunder) {printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | congratulations, demining success! Excellent! |\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Print_board(Findout, ROW, COL); }}Copy the code

⑨ Check how many mines are around X and Y

static int Statistics(char Layouts[ROWS][COLS], int x, int y)
{
	return Layouts[x-1][y-1]+
		Layouts[x][y-1] +
		Layouts[x+1][y-1]+
		Layouts[x-1][y]+
		Layouts[x+1][y]+
		Layouts[x-1][y+1]+
		Layouts[x][y+1]+
		Layouts[x+1][y+1] - 8*'0';
}
Copy the code

🍁 Note: when modifying a function with a static local variable, make the function visible only in its own source file.

β…΄ Result Demonstration

A demo of the results of being “killed” by lightning.

​

β…΅ modular code implementation

A. test b. test C. test D. test

Test the logic of the game.

#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include "game.h" void menu() {color(0); / / Black Black system (" CLS "); // clear the screen. Color (10); Printf (" | -- -- -- -- -- -- -- -- -- -- - | | minesweeping game -- -- -- -- -- -- -- -- -- -- - | \ n "); printf("|********************************|\n"); Printf (" | painted painted u u u u u u u u u u u u u u | \ n "); Printf (" painted painted painted | 1 to 0. Quit u | \ n "); Printf (" | painted painted u u u u u u u u u u u u u u | \ n "); Printf (" | 0 = not ray -- -- -- -- -- -- -- -- -- -- 1 = it is ray | \ n "); printf("|--------------------------------|\n"); } void game() { printf(" ---------\n"); printf("|PLAY GAME|\n"); printf(" ---------\n"); char Layouts[ROWS][COLS] = { 0 }; Char Findout[ROWS][COLS] = {0}; // Initialize (Layouts, ROWS, COLS, '0'); // Initialize (Layouts, ROWS, COLS, '0'); //mine Initialization(Findout, ROWS, COLS, 'x'); //show // print /*Print_board(Layouts, ROW, COL); */ Print_board(Findout, ROW, COL); // Lay_thunder(Layouts, ROW, COL); /*Print_board(Findout, ROW, COL); */ // Check(Layouts,Findout,ROW,COL); } void test() { int input = 0; srand((unsigned)time(NULL)); do { menu(); color(5); printf("\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); printf("|Please enter the interface number|:"); scanf("%d", &input); switch (input) { case 1: game(); break; Case 0: printf (" | ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | | to exit the game \ n "); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); break; default: printf("\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | because you input error penalty you can't play 5 s (> _ >) | \ n "); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Sleep(5000); } } while (input); } int main(void) { test(); return 0; }Copy the code

(2), game.h

Function declarations included in the game, inclusion of symbol declaration headers, and macro definitions.

#define COL 9 #define COLS COL+2 #define COLS COL+2 Void Initialization(char board[ROWS][COLS], int ROWS, int COLS, char set); Void Print_board(char board[ROWS][COLS], int row, int col); Void Lay_thunder(char Findout[ROWS][COLS], int row, int col); Void Check(char Layouts[ROWS][COLS], char Findout[ROWS][COLS], int row, int col);Copy the code

(c) game

Game and related function implementation.

#include "game.h" #include<stdio.h> void color(short x) {if (x >= 0 && x <= 15) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); } void Initialization(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 Print_board(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; color(7); Printf (" ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ \ n "); for (i = 0; i <= row; i++) { if (i == 0) { printf("%d|", i); } else { printf("%d|", i); }} printf (" \ n / | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ "); printf("\n"); for (i = 1; i <= row; i++) { printf("%d|", i); for (j = 1; j <= col; j++) { printf("%c|", board[i][j]); } printf("\n"); } color(6); Printf ("\n----- minesweepers ------\n"); } void Lay_thunder(char Findout[ROWS][COLS], int row, int col) {} void Lay_thunder(char Findout[ROWS][COLS], int col) { while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (Findout[x][y] = '0') { Findout[x][y] = '1'; count--; }}} // Static local variables are used to modify functions so that the function is visible only in its own source file. static int Statistics(char Layouts[ROWS][COLS], int x, int y) { return Layouts[x-1][y-1]+ Layouts[x][y-1] + Layouts[x+1][y-1]+ Layouts[x-1][y]+ Layouts[x+1][y]+ Layouts[x-1][y+1]+ Layouts[x][y+1]+ Layouts[x+1][y+1] - 8*'0'; } void Check(char Layouts[ROWS][COLS], char Findout[ROWS][COLS], int row, int col) { //1. Enter the coordinates of lightning detection //2. Check whether the coordinates are mined. The character '1' is used for laying mines, and the character '0' is used for not laying mines. int x, y; int win = 0; while (win<row*col - Thunder) { static int j = 1; While (j) {color(8); printf("--------------------------\n"); Printf (" enter the first coordinate and remember to leave a space! \n"); printf("--------------------------\n"); j--; break; } color(11); printf("---------------\n"); Printf (" Please enter coordinates >:"); Scanf ("%d %d", &x, &y); printf("---------------\n"); / / judge the legitimacy of the coordinates of the if (x > = 1 && x < = row && y > = 1 && y < = {col) if (Layouts [x] [y] = = '1') {printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n ");  Printf (" | it's a pity that you have been killed! |\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Print_board(Layouts, ROW, COL); Sleep(5000); break; } if (Findout[x][y] == '0') { color(6); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | ning has been here input coordinates, please input again! |\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); } if (Findout[x][y] == '1') { color(6); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | ning has been here input coordinates, please input again! |\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); } else {Count = Statistics(Layouts, x,y); Findout[x][y] = Count + '0'; Print_board(Findout, row, col); win++; }} else {printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | ning to enter the coordinates of the error range! To re-enter | \ n "); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); }} the if (win = = row * col - Thunder) {printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Printf (" | congratulations, demining success! Excellent! |\n"); Printf (" | ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ | \ n "); Print_board(Findout, ROW, COL); }}Copy the code

Ok, so that’s the end of the minesweeper game, I don’t know if you’ve learned. For beginners it is Μ€.Μ« ‒́)✧ because it helps to develop programming thinking and logic as well as understanding code

​

Don’t forget πŸ‘(β•Ή General Dungeons β•Ή)