What is minesweeper
The explanation based on Baidu Encyclopedia is as follows:
Minesweeper is a popular mini-puzzle game released in 1992. The goal of the game is to find out all non-thunder grid according to the number of clicking grid in the shortest time, while avoiding stepping on thunder, stepping on a thunder is completely lost.
First, the idea of implementation
Maps of mine clearance vary in size, as does the number of mines laid. The bigger the map, the more thunder the harder. In order to adjust and change the parameters of minesweeper, the game parameters are defined by macro definition. With multiple files, game programs are better implemented and clearer. Because of the need to bury mines and determine the number of mines on the map, it is difficult to achieve if only one map is used. Here, two identical maps are used. One is used to lay mines, and the other is used to display a map and select the number of mines in the location. However, since the method of judging the number of lightning around the middle and the most edge area is not the same, so in the process of realizing the code, add two lines on the map on the basis of display. Subtract the top and bottom lines and the left and right lines, leaving the remaining area for play. Explanation of ray calculation method:
When judging the number of lightning near a location, it is necessary to judge the situation of eight locations, because the location near the edge is missing several locations, so in order to unify the calculation method, simple code process, sacrifice some memory space. By adding a row or column of mine-free areas up, down, left, and right, the area used for the game will be calculated the same regardless of the area, thus requiring only one method of calculation.
2. Header files
Tip: Change the number of minesweeper rows and bombs by changing the size of the macro definition number. You can also change the style of the board icon
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#define ROW 10 / / the number of rows
#define COL 10 / / the number of columns
#define START The '*' // Position style
#define MINE 35 // The number of thunder
extern void game(a);
Copy the code
#include<windows.h>
#include<stdlib.h>
These two header files are library functions needed to take time as a random number
Main function file
Menu functions
void Menu(a)// Game menu
{
printf("+--------------------+\n");
printf("| | minesweeping game \ n");
printf("| 1.PLAY 2.EXIT |\n");
printf("+--------------------+\n");
printf("Please enter your choice #");
}
Copy the code
2. The main function
int main(void)
{
Menu();
int x = 1;
while (x)
{
scanf("%d", &x);
switch (x)
{
case 1:
game();// Start the game
Menu();
break;
case 2:
x = 0;// End the loop
break;
default:
printf("Your input is wrong, please retype #");
break; }}printf("bye~bye~\n");
system("pause");
return 0;
}
Copy the code
Game files
1. Map initialization
memset(show_board, START, sizeof(show_board));// Initialize an array of elements with sizeof() using START
memset(mine_board, '0'.sizeof(mine_board));
Copy the code
Or use two nested loops to assign each element of a two-dimensional array
void Init_Board(char arr[][COL],char x)
{
int row = 0;
int col = 0;
for(; row < ROW; row++) {for(; col < COL; col++) { arr[row][col] = x; }}}Copy the code
2. Show the map
Display map function
static void Show_Board(char arr[][COL+2])// Displays the minesweeper map function
{
system("cls");/ / clear screen
printf("");
for (int i = 1; i < COL+1; i++) {printf(" %2d", i);
}
Line();
for (int i = 1; i < ROW+1; i++) {printf("%-2d |", i );// Left integer format control
for (int j = 1; j < COL+1; j++) {printf(" %c |", arr[i][j]); } Line(); }}Copy the code
A subfunction of the output spacer to reduce duplicate code
static void Line(a)// The spacing line between the lines
{
printf("\n ");
for (int i = 0; i < COL; i++) {printf("--");
}
printf("\n");
}
Copy the code
3. Mine burying function
Because the random number is used to bury mines on the map, it is inevitable that there will be repeated values. When assigning values to random coordinates, it is necessary to determine whether there has been lightning. It takes no thunder to mine, and one less to mine.
static void Set_Board(char arr[][COL+2])// The mine burying function uses '1 'to indicate the mine, and '0' to indicate the absence of mine
{
int row = 0;
int col = 0;
int x = MINE;
while (x)
{
row = rand() % ROW + 1;
col = rand() % COL + 1;
if(arr[row][col] ! ='1')// Determine that there is no lightning to reduce the number of cycles
{
arr[row][col] = '1'; x--; }}}Copy the code
4. Determine the number of mines near the location
Because of the use of character array, in the number of calculation, you can skillfully use the ASCII code table for calculation and the final display of the number.
static char Judge_Board(char arr[][COL+2].int row, int col)// Determine the number of thunder here
{
return arr[row - 1][col - 1] + arr[row - 1][col] + arr[row - 1][col + 1] + \
arr[row][col + 1] + arr[row + 1][col + 1] + arr[row + 1][col] + \
arr[row + 1][col - 1] + arr[row][col - 1] - 7*'0';// Because the type is a character, use ASCII conversion
}
Copy the code
Subtracting the ASCII value of ‘0’ from the eight positions leaves only one ASCII value of ‘0’. And for each mine, the ASCII value is incremented by one, and the corresponding character is incremented by one.
5. Main game functions
There are four aspects to discuss when making game progress judgments
- Whether the player’s input is valid
- Judge whether lightning strikes
- After not stepping on thunder, judge the number of lightning around
- Judge victory
void game(a)
{
srand((unsigned long)time(NULL));// A random number seed
char show_board[ROW+2][COL+2];// A two-dimensional array for display
char mine_board[ROW+2][COL+2];// A two-dimensional array for burying mines
memset(show_board, START, sizeof(show_board));// Initialize an array of elements with sizeof() using START
memset(mine_board, '0'.sizeof(mine_board));
Set_Board(mine_board);/ / buried land mines
Show_Board(show_board);// Display the map
int x = ROW*COL-MINE;// Calculate the remaining positions except for mines
int row = 0;
int col = 0;
while (x)
{
printf("Please enter the selected location
#"
,y>);
scanf("%d %d", &row, &col);
if (row <= 0 || row > ROW || col <= 0 || col > COL)// Verify that the position coordinates are valid
{
printf("Input error,");
continue;// Skip the rest of the loop
}
if(show_board[row][col] ! = START)// Determine whether to repeat the input
{
printf("Repeat input,");
continue;
}
if (mine_board[row][col] == '1')// Check if you stepped on a lightning bolt
{
Show_Board(mine_board);// After stepping on a thunder, display the thunder map
printf("You lose! \n");
break;
}
show_board[row][col] = Judge_Board(mine_board, row, col);// Record the number of mines around the location
Show_Board(show_board);// Displays the number of mines around the location
x--;// The number of valid games is reduced by one
}
if (x == 0)
{
printf("Congratulations, you succeeded! \n"); }}Copy the code
Five, code operation display
1. Start screen
2. Start the game
3. Game over
All procedures displayed, welcome your valuable suggestions
6. The original code of the game
Need the original code of children’s shoes click here c language to achieve minesweeper game original code