[source code project + analysis] C language / C + + development to create a small project minesweeping game!

It has always been said that I would write a small project with hundreds of lines, so I wrote a minesweeping for the console. I didn't expect that only about 200 lines of code had been simplified. However, considering that this is my simplified code, and concentration is the essence, I sent it out for everyone to learn together. Seeing that the program can run and play, I still feel quite fulfilled ~ ha ha

Mine sweeping should belong to a popular game. Since I used the computer in junior middle school, it has been integrated into the windows system. Although it is so classic, I still want to introduce its playing method, and then consider how to realize it in the console.


 

1. The main interface of the game is a small square. Click the left button on the small square to open the small square to see what's behind.

2. Behind these small squares are hidden thunder. If you are unlucky enough to hit thunder, you will be GameOver.

If the point is not a mine, it is an open space. At this time, there are two situations:

1) click with the mouse on the eight points around the open space, then the number of mines will be displayed

2) click with the mouse that there is no thunder around the open space. At this time, all the open spaces around will be displayed. When encountering the open space with the displayed number, the number will be displayed. (if you look closely, you will find that the number will surround the open space. This is nonsense, but it is also worth thinking about why.)

3. Click the right mouse button on the small square to mark an open space as thunder. Of course, this function is only for your convenience to remember the place you determined to be thunder. (click both the left and right buttons, and click the right button to display the "mark", which is not discussed here)

4. When the number of remaining squares in the open space is as many as that of thunder, then this time should be regarded as a victory.

 

OK ~ the game flow is finished. It's time to talk about how to realize it.

1. First, we need a map. Generally, we can use a two-dimensional array to represent a map, and each element represents a small square in mine sweeping. If the corresponding element stores 0, then the location on the map is an open space, and the corresponding element stores 1, then it means that this location is a mine.

2. Print the corresponding small squares on the console according to the length and width of the two-dimensional array.

3. Then click the small boxes with the mouse. For the console, there are coordinates in the black box area. You can use some functions to capture which coordinates you clicked on the screen.

4. For the console, when printing a character, some characters occupy one position horizontally, such as ordinary alphanumeric characters, and some characters occupy two positions horizontally, such as some graphic characters: ① ② ③ ■◆ etc., which should be paid attention to when programming the console.

5. When clicking the screen, after obtaining the clicked coordinates, go to the two-dimensional array to check whether the corresponding position is mine or open space, so as to do corresponding processing.

1) if you click on Lei, you can control the end of the game

2) if you click on the open space, there are two situations

        1. If there is thunder around the open space clicked, the number of thunder will be displayed

        2. If there is no thunder around the clicked open space, then the recursive method is used to detect the surrounding points and detect all the surrounding thunder points connected with it.

 

This is the effect I achieved. Here is the code:

// saolei.cpp : Define the entry point for the console application.

//

#include "stdafx.h"

#include

#include

#include

#define Boom 10

int a[10][10] = {0};

COORD TempPos[100] ={0};

int nSign = 0;

 

 

/************************************

Function name: WriteWchar

Function function: display a string of characters on the corresponding coordinates of the console

Return value: void

Parameter: int x abscissa

Parameter: int y ordinate

Parameter: char szString [] string to display

explain:

************************************/

void WriteWchar(int x,int y,char szString[])

{

HANDLE hOut= GetStdHandle(STD_OUTPUT_HANDLE);

COORD pos = {x*2,y};

SetConsoleCursorPosition(hOut,pos);

printf("%s",szString);

}

 

 

/************************************

Function name: DrawNumber

Function function: print corresponding numeric characters on the corresponding coordinates according to the incoming numbers

Return value: void

Parameter: Cool POS position to print

Parameter: int nNumber the number to print

explain:

************************************/

void DrawNumber(COORD pos,int nNumber)

{

switch (nNumber)

{

case 1:

WriteWchar(pos.X,pos.Y,"");

break;

case 2:

WriteWchar(pos.X,pos.Y,"");

break;

case 3:

WriteWchar(pos.X,pos.Y,"");

break;

case 4:

WriteWchar(pos.X,pos.Y,"");

break;

case 5:

WriteWchar(pos.X,pos.Y,"");

break;

case 6:

WriteWchar(pos.X,pos.Y,"");

break;

case 7:

WriteWchar(pos.X,pos.Y,"");

break;

case 8:

WriteWchar(pos.X,pos.Y,"");

break;

default:

break;

}

}

 

 

/************************************

Function name: GetNumber

Function function: how many mines are there around a point

Return value: int

Parameter: COORD pos coordinates of the point to be detected

explain:

************************************/

int GetNumber(COORD pos)

{

int nCount = 0;

for(int i = pos.X-1;i<=pos.X+1;i++)

for (int j = pos.Y-1;j<=pos.Y+1;j++)

{

if (a[j][i] == Boom)

{

nCount++;

}

}

return nCount;

}

 

 

/************************************

Function name: Drawmap

Function: print map

Return value: void

explain:

************************************/

void Drawmap()

{

for (int i =0;i<10;i++)

{

for (int j =0;j<10;j++)

{

WriteWchar(j,i,"");

}

}

}

 

 

/************************************

Function name: Init

Function function: randomly generate 10 mines, and then save them to the array

Return value: void

explain:

************************************/

void Init()

{

srand(time(NULL));

for (int i =0;i<10;i++)

{

int Temp_x = rand()%10;

int Temp_y = rand()%10;

//Judge whether a mine has been generated in this place. If not, assign it as mine

if (a[Temp_x][Temp_y]!=Boom)

{

a[Temp_x][Temp_y] = Boom;

}

//If it is thunder, it means that this generation has not happened.....

else

{

i--;

}

}

Drawmap();

}

 

 

/************************************

Function name: IsClose

Function function: judge whether it is a detected point. Due to the 8-direction recursive detection, it can avoid repetition

Return value: bool

Parameter: COORD posTemp

explain:

************************************/

bool IsClose(COORD posTemp)

{

for (int i =0;i

{

if(TempPos[i].X == posTemp.X&&TempPos[i].Y == posTemp.Y)

return true;

}

return false;

}

 

 

/************************************

Function name: iskondi

Function function: judge whether a point is open space or thunder. If it is open space, other processing needs to be done

Return value: void

Parameter: Cool POS

explain:

************************************/

bool IsKongdi(COORD pos)

{

int nNumber = 0;

//1 If it's ray, return one directly false That means we're going to hang up

if (a[pos.Y][pos.X] == Boom)

{

return false;

}

//2 If it's not ray, follow up

else

{

//2.1 First judge how many mines are around

nNumber = GetNumber(pos);

if (nNumber!=0){

//If there are several mines, just print this number

DrawNumber(pos,nNumber);

return true;

}

else

{

//If there is no thunder, draw the open space first, and then spread around to detect other points

WriteWchar(pos.X,pos.Y," ");

}

}

//2.2 Point to the open space, but there is no thunder around. Continue to detect 8 points around

for(int i = -1;i<=1;i++)

for (int j = -1;j<=1;j++)

{

COORD posTemp = {pos.X+i,pos.Y+j};

//Did you cross the line

if (i==0&&j==0||posTemp.X==-1||posTemp.Y==-1||posTemp.X==10||posTemp.Y==10)

continue;

//Has this point been detected

if (IsClose(posTemp))

continue;

//If this point has not been detected, it will be added to the array, and then it will be stored in the future detection

TempPos[nSign++] =posTemp;

IsKongdi(posTemp);

}

return true;

}

 

 

/************************************

Function name: GetMousePosition

Function function: get the position of the mouse click. If it is not obtained, it returns (- 1, - 1)

Return value: COORD mouse click coordinates

explain:

************************************/

COORD GetMousePosition()

{

HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);

INPUT_RECORD stcInput = {0};

DWORD buffer;

COORD pos = {-1,-1};

ReadConsoleInput(hIn,&stcInput,1,&buffer);

if (stcInput.EventType == MOUSE_EVENT)

{

MOUSE_EVENT_RECORD stcMouseEnent = stcInput.Event.MouseEvent;

if (stcMouseEnent.dwButtonState ==FROM_LEFT_1ST_BUTTON_PRESSED )

{

pos = stcMouseEnent.dwMousePosition;

pos.X/=2;

}

}

return pos;

}

int _tmain(int argc, _TCHAR* argv[])

{

Init();

COORD pos;

//Start the game

while(1)

{

//Get mouse click position

pos = GetMousePosition();

if (pos.X!=-1)

{

//If the position of the mouse click is detected, the next cycle begins

if (IsClose(pos))

{

continue;

}

TempPos[nSign++] =pos;

bool bIskongdi = IsKongdi(pos);

//When the thunder hits, you just quit the game.

if (false ==bIskongdi)

{

system("cls");

WriteWchar(20,10,"you lose");

getchar();

break;

}

//Whether the detection has won or not, the winning condition is that the number of points not detected is equal to the number of mines

if (nSign ==90)

{

system("cls");

WriteWchar(20,10,"you win");

}

}

}

return 0;

}

 


 

The project is not very long, but the notes I wrote are fairly clear. It is estimated that everyone can understand it. I hope it can be of some help to novices. Thank you for your support!!!

Have you learned a lot of new knowledge here~

If you really want to learn programming, Xiaobian recommends me C language / C + + programming learning base [Click to enter]!

It's all about learning programming, guys. It's still simple to take you in. Learn together and cheer together~

There are many learning materials and videos, I believe you will like it!

Involving: game development, common software development, basic programming knowledge, curriculum design, hackers, etc


 

Posted by designguy79 on Thu, 19 May 2022 09:42:56 +0300