How to realize the interesting Minesweeper Game on C++
Most people don't understand the knowledge points of this article "How to realize fun minesweeping game in C++," so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "How to realize fun minesweeping game in C++."
process design
1. Initialize the array.
2. Enter coordinate points.
3. Choices: dig, mark, unmark, restart, exit game.
If mining is selected, the game ends if the coordinate point is a mine, the number is displayed and returns to 2 if it is a number, and the value of the surrounding 8 elements is displayed if it is a space until the associated space is displayed and returns to 2;
If the marker is selected, set the element value at that point to-2 and back to 2;
If cancel flag is selected, initialize the point and go back to 2;
If Restart is selected, initialize the array and go back to 2;
If you choose to quit the game, exit.
4. After digging all the non-mine points, the game wins, choose whether to play again, if yes, return to 1, otherwise exit
object-oriented design idea
Create a bombsweep class that stores several methods:
calculate: count the number of mines at eight points around (x,y).
Game: Simulate the game process.
print: Print an array.
check: Check whether the victory conditions are met.
In the main function, create bs objects from the bombsweep class when needed, and call the relevant methods in bs.
program code
#include #include #include #include using namespace std;int map[12][12]; // ??????????,???????????? 1int derection[3] = { 0, 1, -1 }; //???????? 8????? int type;class bombsweep{public: int calculate ( int x, int y ) { int counter = 0; for ( int i = 0; i < 3; i++ ) for ( int j = 0; j < 3; j++ ) if ( map[ x+derection[i]][ y+derection[j] ] == 9 ) counter++; // ??? (x,y)????? 8??????? return counter; } void game ( int x, int y ) { if ( calculate ( x, y ) == 0 ) { map[x][y] = 0; for ( int i = 0; i < 3; i++ ) { // ???????,????????? for ( int j = 0; j < 3; j++ ) if ( x+derection[i] = 1 && ! ( derection[i] == 0 && derection[j] == 0 ) && map[x+derection[i]][y+derection[j]] == -1 ) game( x+derection[i], y+derection[j] ); // ??????????????? 0,????????! } //????????.??????????? } else map[x][y] = calculate(x,y); } void print (int x,int y) { cout