🌟 What is minesweeping
Mine clearance refers to the search and clearance of mines, mines and other explosives, which is usually carried out in the areas occupied and recovered by our own side. The purpose of mine clearance is to ensure the freedom of movement of the army and the safety of residents' lives and property, and restore normal production and living order as soon as possible. Let's try to realize this game in C language.
Wait, it's not like this minesweeping. Let's start again.
🌟 What is minesweeping
The game is played by randomly arranging 10 mines in a 9 * 9 square matrix. Players open the boxes one by one to find out all mines as the final goal of the game. If the player opens a box with mines, the game is over. The goal of the game is to find out all non thunder grids according to the numbers in the click grid in the shortest time, and avoid stepping on thunder at the same time.
Let's try to realize this game in C language.
🌟 Menu interface
⭐ Game menu
The game always needs an interface. Otherwise, I don't know what to do. Realize the menu interface through the [printf] function, and open the game to automatically jump out of 1 Start game 0 Quit the game.
void menu() { printf("****************************\n"); printf("******* 1.Start the game *******\n"); printf("******* 0.Exit the game *******\n"); printf("****************************\n"); }
⭐ Implementation options
With the menu, the next step is to select the option. Here you need to consider the case of input error.
void choice() { int num; do { menu(); printf("Please enter your options:"); scanf("%d",&num); switch (num) { case(1): game(); break; case(0): printf("Exit the game\n"); break; default: printf("Wrong input, little fool~~\n"); break; } } while(num); }
🌟 Chessboard interface
⭐ , create mine disk
Menu interface and selection are completed. The most important minesweeping interface has not been established. We need two two-dimensional arrays to store mine sweeping information. The first one is used to store mines, that is, to bury mines. The second one is used to display the minesweeping interface and display the minesweeping position of players.
In order to better calculate how many mines are around the position we want to sweep, we define the length of the array as 11.
#define ROW 9 #define COL 9 char mine[ROW+2][COL+2] = { 0 };//Storage mine char show[ROW+2][COL+2] = { 0 };//Minesweeping interface
⭐ Initialize thunder disk
It's not enough to have a chessboard. There's nothing in the chessboard. We fill the mine chessboard with 0 and the display chessboard with 0*
void Init(char board[ROWS][COLS], int row, int col, char set) { int i = 0 ; int j = 0 ; for (i = 0; i < row + 2; i++) { for (j = 0; j < col + 2; j++) { board[i][j] = set; } } } Init(mine, ROW, COL, '0'); Init(show, ROW, COL, '*');
⭐ ⒋ print thunder disk
When printing, we can output the coordinates once more, which is convenient to find the coordinates when playing
void Display(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("******The Minesweeper game*******\n"); for (i = 0; i < row + 1; i++) { printf("%d ", i); for (j = 1; j < col + 1; j++) { if (i == 0) printf("%d ", j); else printf("%c ", board[i][j]); } printf("\n"); } }
🌟 Minesweeping interface
⭐ , mine layout
After we realize the logic of players playing chess, we need to realize the logic of computer playing chess. The logic of computer is to generate two random numbers, respectively% 3, and get two numbers that meet the requirements of two-dimensional array coordinates of Sanzi.
⚡ Random number function
Here we need two random numbers. You can use the [rand()] function
The [#include < stdlib. H >] header file needs to be referenced to use the rand() function
By default, the [rand()] function is not a true random function, but a pseudo random function. When in use, it is also necessary to call the [srand()] function. The [srand()] function will set the random number seeds used by the [rand()] function, and each seed corresponds to a group of random numbers pre generated according to the algorithm;
srand((unsigned) time(NULL));// Produce seeds
void Set_Mine(char mine[ROWS][COLS], int row, int col, int count) { while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } }
🌟 mine clearance
⭐ select location
void Sweep(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int count) { int x = 0; int y = 0; int judge = 0; while (judge < row*col - count) { judge = 0; printf("Please enter the coordinates to sweep: \n"); scanf("%d %d", &x, &y); if (x < 0 || y < 0 || x > row || y > col) printf("Wrong input, little fool~~\n"); else if (show[x][y] != '*') printf("The coordinates have been swept\n"); if(judge == 0) No_mine(mine, show, x, y, row, col); if (mine[x][y] != '1') { Spread(x, y, mine, show, row, col); Display(show, row, col); } else { printf("You were killed\n"); break; } Stat_judge(show, &judge, row, col); }
⭐ Not for the first time
void No_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int i, int j, int row, int col) { if (mine[i][j] == '1') { while (1) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; break; } } mine[i][j] = '0'; } }
⭐ The number of thunder around the fall
int Count_Mine(int x, int y, char mine[ROWS][COLS]) { return mine[x - 1][y - 1] + mine[x - 1][y] + 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'; }
⭐ There is no thunder around the Luozi
void Spread(int x, int y, char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col ) { char count = Count_Mine(x, y, mine) + '0'; if (count != '0') show[x][y] = count; else { show[x][y] = ' '; if (x - 1 > 0 && show[x - 1][y] == '*') Spread(x - 1, y, mine, show, row, col); if (y - 1 > 0 && show[x][y - 1] == '*') Spread(x, y - 1, mine, show, row, col); if (y + 1 < col + 1 && show[x][y + 1] == '*') Spread(x, y + 1, mine, show, row, show); if (x + 1 < row + 1 && show[x + 1][y] == '*') Spread(x + 1, y, mine, show, row, col); } }
🌟 Judge victory
void Stat_judge(char show[ROWS][COLS], int* judge, int row, int col) { int i = 0; int j = 0; for (i = 1; i < row + 1; i++) { for (j = 1; j < col + 1; j++) { if (show[i][j] != '*') (*judge)++; } } }
🌟 Game function
void game() { srand((unsigned long)time(NULL)); //Generate random number seed char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; Init_board(mine, ROWS, COLS, '0');//Initialize thunder disk Init_board(show, ROWS, COLS, '*');//Initialize the displayed thunder disk Set_Mine(mine,ROW,COL,10); Sweep(mine,show,ROW,COL,10); }
🌟 Code details
The following is the complete code. You can try to write it yourself.
#include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 9 #define COL 9 #define ROWS ROW + 2 #define COLS ROW + 2 void game(); void Sweep(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int count); void No_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int i, int j, int row, int col); void Spread(int x, int y, char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col); void Stat_judge(char show[ROWS][COLS], int* judge, int row, int col); void menu() { printf("****************************\n"); printf("******* 1.Start the game *******\n"); printf("******* 0.Exit the game *******\n"); printf("****************************\n"); } void choice() { int num; do { menu(); printf("Please enter your options:"); scanf("%d",&num); switch (num) { case(1): game(); break; case(0): printf("Exit the game\n"); break; default: printf("Wrong input, little fool~~\n"); break; } } while(num); } void Init_board(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 Set_Mine(char mine[ROWS][COLS], int row, int col, int count) { while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } void Display(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("******The Minesweeper game*******\n"); for (i = 0; i < row + 1; i++) { printf("%d ", i); for (j = 1; j < col + 1; j++) { if (i == 0) printf("%d ", j); else printf("%c ", board[i][j]); } printf("\n"); } } void Sweep(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int count) { int x = 0; int y = 0; int judge = 0; while (judge < row*col - count) { judge = 0; printf("Please enter the coordinates to sweep: \n"); scanf("%d %d", &x, &y); if (x < 0 || y < 0 || x > row || y > col) printf("Wrong input, little fool~~\n"); else if (show[x][y] != '*') printf("This coordinate has been cleared:\n"); if(judge == 0) No_mine(mine, show, x, y, row, col); if (mine[x][y] != '1') { Spread(x, y, mine, show, row, col); Display(show, row, col); } else { printf("You were killed\n"); break; } Stat_judge(show, &judge, row, col); } if (judge == row * col - count) { printf("Congratulations on your success\n"); } } void No_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int i, int j, int row, int col) { if (mine[i][j] == '1') { while (1) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; break; } } mine[i][j] = '0'; } } int Count_Mine(int x, int y, char mine[ROWS][COLS]) { return mine[x - 1][y - 1] + mine[x - 1][y] + 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 Spread(int x, int y, char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { char count = Count_Mine(x, y, mine) + '0'; if (count != '0') show[x][y] = count; else { show[x][y] = ' '; if (x - 1 > 0 && show[x - 1][y] == '*') Spread(x - 1, y, mine, show, row, col); if (y - 1 > 0 && show[x][y - 1] == '*') Spread(x, y - 1, mine, show, row, col); if (y + 1 < col + 1 && show[x][y + 1] == '*') Spread(x, y + 1, mine, show, row, col); if (x + 1 < row + 1 && show[x + 1][y] == '*') Spread(x + 1, y, mine, show, row, col); } } void Stat_judge(char show[ROWS][COLS], int* judge, int row, int col) { int i = 0; int j = 0; for (i = 1; i < row + 1; i++) { for (j = 1; j < col + 1; j++) { if (show[i][j] != '*') (*judge)++; } } } void game() { srand((unsigned long)time(NULL)); //Generate random number seed char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; Init_board(mine, ROWS, COLS, '0');//Initialize thunder disk Init_board(show, ROWS, COLS, '*');//Initialize the displayed thunder disk Set_Mine(mine,ROW,COL,10); Sweep(mine,show,ROW,COL,10); } int main() { choice(); return 0; }
🌺 Daily golden sentence
Diet depends on the festival, reading depends on the essence, and exercise depends on the constant. Diet nourishes the stomach, read more and nourish the gall, and like sports to prolong life!
I am not talented. If there is any mistake, you are welcome to discuss it in the comment area. Please pay attention if it is helpful ➕ give the thumbs-up ➕ If I can't, I'll try again 💪💪💪