Minesweeper – including blank expansion, marking function, game interface optimization – fully reproduced on the console

Table of Contents

introduce

Ideas

Involving header file index

main function

Branch function function implementation

Sum of codes—CV can be played

Introduction

I accidentally played the game Minesweeper in the past two days. I played Minesweeper owned by Microsoft. I feel that it is much better than the simple interface before.

d55dbbc3433044dab4e7deb8fe494db2.png

I feel that minesweeper can be played very interestingly if you find the rules. If you think about it carefully, minesweeper is not just a one-dimensional array, it can be realized by me. That’s when my design minesweeping journey began.

Since I wanted to take into account the flexibility of Minesweeper and the functional similarity with the original version, I still spent some time to improve these functions, as the title also mentioned, including blank expansion, marking, etc. Let’s start our explanation below!

Ideas

The main idea is to create two arrays. One is used to record the background data of mine clearance, including: the location of mines, and record the number of mines around each location. An interface used to display what the player can see – the display interface, which displays the content you want to display based on the player’s input. I drew a picture to facilitate everyone’s understanding.

f16354e5b59c4b5486f3ff8ae321ad81.png

The user enters an address, and the data from the background array is transferred to the display array and printed. This completes the detection of the grid.

If thunder is selected, the game is judged to be over and exited, thereby achieving a failure judgment.

If the total number of grids – the number of excluded non-mines = equal to the number of mines, the victory of the game is determined.

If 0 is selected, a recursive function is used to expand, thus completing the blank expansion function.

Change the displayed content from * to $ based on the incoming address to complete the marking of unknown locations.

The above is an overview of the main code functions, which will be explained in detail below.

Involving header file index

Before I explain, I want to do something that most bloggers won’t do, which is to briefly introduce the less common header files and functions used. See the code.

#include<stdio.h>
//Input and output header files, you all know this

#include<stdlib.h>
//srand generates seeds and rand generates header files for random numbers
//The random number generated by rand is related to the seed generated by srand, and the seed randomly generated by srand is related to time(NULL)

#include<time.h>
//Call function time() to generate timestamp and call Sleep() function to indicate dwell time

#include<windows.h>
//Call the system("cls"); function, which clears the screen

If you don’t understand the above content, I strongly recommend you to read the relevant content specifically. After understanding these applications, I believe it will be easier to understand my calls. So let’s start with our code content!

Main function

Put the code directly below, there are comments next to it, it is relatively easy to understand.

#define _CRT_SECURE_NO_WARNINGS
#include "mine_sweep.h"//There are too many header files and function declarations, so I created a separate header file and put them in
int main()
{
int x = 0, y = 0;//About the size of the game interface-row-column
char mine[105][105];//Backend interface
char show[105][105]; //Display interface
srand((unsigned int)time(NULL));//Randomly generate seeds
int input = 0;
int digit_m = 10;//number of mines
do {
input=menu();//Print menu
if (input == 1) {
printf("start");
printf("\\
Please enter the minesweeper board size (length:width):");
scanf("%d%d", & amp;x, & amp;y);
printf("\\
Please enter the number of mines to set:");
scanf("%d", & amp;digit_m);
Initboard(x, y, mine, '0');//Initialize the chessboard--background area
Initboard(x, y, show, '*');//Initialize the chessboard--show song
Init_mine(x, y, mine,show, digit_m);//Initialize mine
playgame(x, y, digit_m, mine, show);//Game running function
Sleep(15000); //End the interface display stay
}
else if (input == 0);
else { printf("Input error"); Sleep(1000); }
system("cls"); //Clear the screen
} while (input);//Determine whether to continue
printf("Exit game");
return 0;
}

Implementation of branch function functions

Menu function

int menu()
{
printf("************************\\
");
printf("******Welcome to Minesweeper******\\
");
printf("***** 1. Start playing *****\\
");
printf("***** *****\\
");
printf("***** 0.Exit *****\\
");
printf("************************\\
");
printf("Please select:");
int m;
scanf("%d", & amp;m);
return m;
}

There’s not much to say, just display the menu and a return value, and use the return value to determine whether to continue the game.

Initialize chessboard function

The initialization chessboard function is applicable to both the background and the display chessboard. It initializes the corresponding two-dimensional array according to the parameters passed in. See the code.

void Initboard(int x,int y,char arr[105][105],char ch)
{
for (int i = 0; i <= x + 1; i + + )
for (int j = 0; j <= y + 1; j + + )
arr[i][j] = ch;//Initialize each element of the array to ch
}

Initialize thunder function

Lei’s initialization involves randomly generating numbers and seeds. The random generation of seeds has been completed in advance in the main function. Here, just generate random numbers. However, it is recommended to pay attention to the way to generate a specific range of random numbers. See the code.

void Init_mine(int x, int y, char mine[105][105],char show[105][105], int digit)
{
int mine_x, mine_y;
for (int i = 0; i < digit; i + + ) {//Randomly generate numbers
start:;
mine_x = rand() % x + 1;
mine_y = rand() % y + 1;
if (mine[mine_x][mine_y] == '0')//Prevent two mines from being generated at the same location
mine[mine_x][mine_y] = '*';
else goto start;
}
for (int i = 1; i <= x; i + + ) {//Process the numbers around mine and put them in mine for later calls
for (int j = 1; j <= y; j + + ) {
if (mine[i][j] == '*')continue;
if (mine[i - 1][j] == '*')mine[i][j] + + ;
if (mine[i + 1][j] == '*')mine[i][j] + + ;
if (mine[i][j + 1] == '*')mine[i][j] + + ;
if (mine[i][j - 1] == '*')mine[i][j] + + ;
if (mine[i - 1][j - 1] == '*')mine[i][j] + + ;
if (mine[i + 1][j - 1] == '*')mine[i][j] + + ;
if (mine[i - 1][j + 1] == '*')mine[i][j] + + ;
if (mine[i + 1][j + 1] == '*')mine[i][j] + + ;
}
}
}

It is recommended that everyone pay attention to the processing method of background arrays, which is very helpful for writing minesweeper code and facilitates data processing.

Game running function

In fact, it’s not that complicated. It’s just the processing of two arrays. See the code.

void playgame(int x, int y, int digit_m, char mine[105][105], char show[105][105])
{
int x1, y1, judge;
int m = 0;
int* blank = & amp;m;
while (*blank < x * y - digit_m) {
system("cls");
Showboard(x, y, show);
printf("or enter the marked grid (1 row and column)\\
");
printf("Please enter the position of the grid to be detected (0 rows and columns):");
scanf("%d%d%d", & amp;judge, & amp;x1, & amp;y1);
if (mine[x1][y1] == '*' & amp; & amp;judge!=1) { *blank = 0; break; }
else if (x1<1 & amp; & amp; x1>x & amp; & amp; y1<1 & amp; & amp; y1>y) {
printf("\\
Input error, please re-enter!");
continue;
}
if (judge == 0) {
blank_unfold(x, y, x1, y1, mine, show, blank);
}
else if (show[x1][y1] != '*' & amp; & amp; judge == 1) { printf("The displayed grid cannot be marked!"); Sleep(1500); }
else if (judge == 1)show[x1][y1] = '$';
else { printf("Input error!"); Sleep(1500); }
}
system("cls");
Showboard(x, y, mine);
if (*blank) {
win_maliao();
}
else {
printf("\\
Step on thunder, the game is over!\\
");
printf("Return to the initial interface after 15 seconds.\\
");
}
}

It also involves the victory interface display, display interface function and blank expansion function, which will be explained below.

Interface display function

The display of the interface is still very important. Sometimes the beauty and comfort of a game largely determine the quality of the game. See the code.

void Showboard(int x, int y, char arr[105][105])
{
printf("%d |", 0);
for (int i = 1; i <= y; i + + )
printf(" %d |", i);
printf("\\
");
for (int i = 0; i <= y; i + + )
printf("----");
printf("\\
");
for (int i = 1; i <= x; i + + ) {
printf("%d ", i);
\t\t
for (int j = 1; j <= y; j + + ) {
if (arr[i][j] == '*' || arr[i][j] == '$')
printf("| %c ", arr[i][j]);
else printf("| %d ", arr[i][j] - '0');
}
printf("|");
printf("\\
");
for (int b = 0; b <= y; b + + )
printf("----");
printf("\\
");
}
}

Mainly use | and – symbols to optimize the interface. The final effect is like this, see the picture below

71884d0f8000439c9ad310d041cebdff.png

Blank expansion function

Blank expansion is an extended function here. If it is not implemented, it can actually be used. However, if it is implemented, it will greatly improve its playability. It is mainly used for the recursion of functions. Due to the marking function, the code here also needs to be modified. , see code

void blank_unfold(int x, int y, int x1, int y1, char mine[105][105], char show[105][105], int* blank)
{
if (x1<1 || x1>x || y1<1 || y1>y)return;//Return when the input is out of range
if (show[x1][y1] == '0' || (show[x1][y1] != '*' & amp; & amp; show[x1][y1] != ' $'))
        return; //Return when the position has been detected
else if (mine[x1][y1] != '0') {//Expand and return when the position is not blank
show[x1][y1] = mine[x1][y1];
(*blank) + + ;
return;
}
else { //Recursively expand blanks at the same time
show[x1][y1] = '0';
(*blank) + + ;
blank_unfold(x, y, x1 - 1, y1, mine, show,blank);
blank_unfold(x, y, x1 + 1, y1, mine, show,blank);
blank_unfold(x, y, x1, y1 - 1, mine, show,blank);
blank_unfold(x, y, x1, y1 + 1, mine, show,blank);
blank_unfold(x, y, x1 - 1, y1 - 1, mine, show, blank);
blank_unfold(x, y, x1 - 1, y1 + 1, mine, show, blank);
blank_unfold(x, y, x1 + 1, y1 - 1, mine, show, blank);
blank_unfold(x, y, x1 + 1, y1 + 1, mine, show, blank);
}
}

Easter egg function (victory interface display)

Skin it, make Mario when you are idle, and wholesale it for free when you win

void win_maliao()
{
printf("\\
Congratulations on the victory!\\
");
printf("Send you a Mario\\
");
printf(" ********\\
");
printf("************\\
");
printf(" ####....#.\\
");
printf(" #..###...##....\\
");
printf(" ###..........###### ### ###\\
");
printf(" ........... #...# #...#\\
");
printf(" ##*####### #.#.# #.#.#\\
");
printf(" ####*******###### #.#.# #.#.#\\
");
printf(" ...#***.****.*###.... #...# #...#\\
");
printf(" ....************##..... ### ###\\
");
printf(" ....**** *****....\\
");
printf(" #### ####\\
");
printf(" ###### ######\\
");
printf("############################################# ################\\
");
printf("#...#...#.##...#...#.##...#...#.##--- ---------------#\\
");
printf("##########################################--- ---------------#\\
");
printf("#..#....#....##..#....#....##..#....#....##### ################\\
");
printf("######################################## #--- -------#\\
");
printf("#.....#......##.....#......##....#......# #--- -------#\\
");
printf("######################################## #--- -------#\\
");
printf("#.#..#....#..##.#..#....#..##.#..#....#..# #--- -------#\\
");
printf("######################################## #### ########\\
");
printf("Return to the initial interface after 15 seconds.\\
");
}

Sum of codes—CV can be played

It’s almost over here. The above code involves a lot of header files. It will be difficult to integrate and run. I will do this work for you. I also hope that everyone can play with the minesweeper I compiled. See the code.

#include//Various header files
#include
#include
#include
#include
void Initboard(int x,int y, char arr[105][105],char ch);//Initial chessboard function
void Showboard(int x, int y, char arr[105][105]);//Print interface function
int menu();//menu initialization function
void Init_mine(int x, int y, char arr[105][105], char show[105][105], int digit);//Randomly initialize mine function
void playgame(int x,int y,int digit_m, char mine[105][105], char show[105][105]);//Game running function
void blank_unfold(int x, int y, int x1, int y1, char mine[105][105], char show[105][105],int* blank);//Blank expansion function
void win_maliao();//Victory interface display
void Initboard(int x,int y,char arr[105][105],char ch)
{
for (int i = 0; i <= x + 1; i + + )
for (int j = 0; j <= y + 1; j + + )
arr[i][j] = ch;
}
void Showboard(int x, int y, char arr[105][105])
{
printf("%d |", 0);
for (int i = 1; i <= y; i + + )
printf(" %d |", i);
printf("\\
");
for (int i = 0; i <= y; i + + )
printf("----");
printf("\\
");
for (int i = 1; i <= x; i + + ) {
printf("%d ", i);
\t\t
for (int j = 1; j <= y; j + + ) {
if (arr[i][j] == '*' || arr[i][j] == '$')
printf("| %c ", arr[i][j]);
else printf("| %d ", arr[i][j] - '0');
}
printf("|");
printf("\\
");
for (int b = 0; b <= y; b + + )
printf("----");
printf("\\
");
}
}
int menu()
{
printf("************************\\
");
printf("******Welcome to Minesweeper******\\
");
printf("***** 1. Start playing *****\\
");
printf("***** *****\\
");
printf("***** 0.Exit *****\\
");
printf("************************\\
");
printf("Please select:");
int m;
scanf("%d", & amp;m);
return m;
}
void Init_mine(int x, int y, char mine[105][105], char show[105][105], int digit)
{
int mine_x, mine_y;
for (int i = 0; i < digit; i + + ) {
start:;
mine_x = rand() % x + 1;
mine_y = rand() % y + 1;
if (mine[mine_x][mine_y] == '0')
mine[mine_x][mine_y] = '*';
else goto start;
}
for (int i = 1; i <= x; i + + ) {
for (int j = 1; j <= y; j + + ) {
if (mine[i][j] == '*')continue;
if (mine[i - 1][j] == '*')mine[i][j] + + ;
if (mine[i + 1][j] == '*')mine[i][j] + + ;
if (mine[i][j + 1] == '*')mine[i][j] + + ;
if (mine[i][j - 1] == '*')mine[i][j] + + ;
if (mine[i - 1][j - 1] == '*')mine[i][j] + + ;
if (mine[i + 1][j - 1] == '*')mine[i][j] + + ;
if (mine[i - 1][j + 1] == '*')mine[i][j] + + ;
if (mine[i + 1][j + 1] == '*')mine[i][j] + + ;
}
}
}
void playgame(int x, int y, int digit_m, char mine[105][105], char show[105][105])
{
int x1, y1, judge;
int m = 0;
int* blank = & amp;m;
while (*blank < x * y - digit_m) {
system("cls");
Showboard(x, y, show);
printf("or enter the marked grid (1 row and column)\\
");
printf("Please enter the position of the grid to be detected (0 rows and columns):");
scanf("%d%d%d", & amp;judge, & amp;x1, & amp;y1);
if (mine[x1][y1] == '*' & amp; & amp;judge!=1) { *blank = 0; break; }
else if (x1<1 & amp; & amp; x1>x & amp; & amp; y1<1 & amp; & amp; y1>y) {
printf("\\
Input error, please re-enter!");
continue;
}
if (judge == 0) {
blank_unfold(x, y, x1, y1, mine, show, blank);
}
else if (show[x1][y1] != '*' & amp; & amp; judge == 1) { printf("The displayed grid cannot be marked!"); Sleep(1500); }
else if (judge == 1)show[x1][y1] = '$';
else { printf("Input error!"); Sleep(1500); }
}
system("cls");
Showboard(x, y, mine);
if (*blank) {
win_maliao();
}
else {
printf("\\
Step on thunder, the game is over!\\
");
printf("Return to the initial interface after 15 seconds.\\
");
}
}

void blank_unfold(int x, int y, int x1, int y1, char mine[105][105], char show[105][105], int* blank)
{
if (x1<1 || x1>x || y1<1 || y1>y)return;
if (show[x1][y1] == '0' || (show[x1][y1] != '*' & amp; & amp; show[x1][y1] != ' $'))return;
else if (mine[x1][y1] != '0') {
show[x1][y1] = mine[x1][y1];
(*blank) + + ;
return;
}
else {
show[x1][y1] = '0';
(*blank) + + ;
blank_unfold(x, y, x1 - 1, y1, mine, show,blank);
blank_unfold(x, y, x1 + 1, y1, mine, show,blank);
blank_unfold(x, y, x1, y1 - 1, mine, show,blank);
blank_unfold(x, y, x1, y1 + 1, mine, show,blank);
blank_unfold(x, y, x1 - 1, y1 - 1, mine, show, blank);
blank_unfold(x, y, x1 - 1, y1 + 1, mine, show, blank);
blank_unfold(x, y, x1 + 1, y1 - 1, mine, show, blank);
blank_unfold(x, y, x1 + 1, y1 + 1, mine, show, blank);
}
}
void win_maliao()
{
printf("\\
Congratulations on the victory!\\
");
printf("Send you a Mario\\
");
printf(" ********\\
");
printf("************\\
");
printf(" ####....#.\\
");
printf(" #..###...##....\\
");
printf(" ###..........###### ### ###\\
");
printf(" ........... #...# #...#\\
");
printf(" ##*####### #.#.# #.#.#\\
");
printf(" ####*******###### #.#.# #.#.#\\
");
printf(" ...#***.****.*###.... #...# #...#\\
");
printf(" ....************##..... ### ###\\
");
printf(" ....**** *****....\\
");
printf(" #### ####\\
");
printf(" ###### ######\\
");
printf("############################################# ################\\
");
printf("#...#...#.##...#...#.##...#...#.##--- ---------------#\\
");
printf("##########################################--- ---------------#\\
");
printf("#..#....#....##..#....#....##..#....#....##### ################\\
");
printf("######################################## #--- -------#\\
");
printf("#.....#......##.....#......##....#......# #--- -------#\\
");
printf("######################################## #--- -------#\\
");
printf("#.#..#....#..##.#..#....#..##.#..#....#..# #--- -------#\\
");
printf("######################################## #### ########\\
");
printf("Return to the initial interface after 15 seconds.\\
");
}
int main()
{
int x = 0, y = 0;
char mine[105][105];//Backend interface
char show[105][105]; //Display interface
srand((unsigned int)time(NULL));
int input = 0;
int digit_m = 10;
do {
input=menu();//Print menu
if (input == 1) {
printf("start");
printf("\\
Please enter the minesweeper board size (length:width):");
scanf("%d%d", & amp;x, & amp;y);
printf("\\
Please enter the number of mines to set:");
scanf("%d", & amp;digit_m);
Initboard(x, y, mine, '0');//Initialize the chessboard--background area
Initboard(x, y, show, '*');//Initialize the chessboard--show song
Init_mine(x, y, mine,show, digit_m);//Initialize mine
playgame(x, y, digit_m, mine, show);//Game running function
Sleep(15000); //End interface display
}
else if (input == 0);
else { printf("Input error"); Sleep(1000); }
system("cls"); //Clear the screen
} while (input);//Determine whether to continue
printf("Exit game");
return 0;
}

Run screenshot

812e7802ac3d4e68b7beed9273b441ae.png

This article has been difficult to code and write. Please give me a small thumbs up before you leave! ! !

If it is helpful to you, don’t forget to add it to your favorites and follow me–Bi Xin (> – < )---?

At the same time, if there are any errors in the blogger’s article, you are welcome to mention it in the comment area. I will actively correct it. Thank you everyone for your support.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 193998 people are learning the system