C++ implementation of mine clearance program development

  • 2020-10-23 21:08:43
  • OfStack

C++ program development to achieve minesweeper game, for your reference, the specific content is as follows


// Definition of a minesweeper class 

#pragma once

class Game{
public:
 // Start the game 
 void play();
 // Quit the game 
 int quit();
 // The rules of the game 
 void rule();

private:
 // Step on the thunder number, as the failure condition 
 int error = 0;
 // score 
 int score = 0;
 // Highest score record 
 int Rocord[5] = { 0,0,0,0,0 };
 // The map 
 int map[40][40];

 // Map size Size*Size
 int Size = 10;

 // Fault tolerance 
 int fault_tolerant = 10;

 // difficulty 
 int _difficulty=1;

 // Initialize the 
 void reset();

 // Draw a map 
 void drawGrid();

 // View the results of the grid 
 void Cheak();

 // Determine if the game is over 
 int isWin();

 // Import the highest score record 
 void get_Rocord();

 // Export the highest score record 
 void put_Rocord();

 // Select the difficulty 
 int Selection_difficulty();

 // Load the interface 
 void loading();
};

Then there is the definition of the function of the class


// right Game The definition of a member function of a class 

#include " Mine clearance .h"
#include<Windows.h>
#include<iostream>
#include<fstream>
#include<time.h>
#include <conio.h>


#pragma warning(disable:4996) // this 1 Row in order to be able to  Visual Studio 2017 use getch() function 

// Defines the storage address of the highest score record 
#define RocordPath "D:\\VS/ Highest score for mine clearance .txt"

using namespace std;

#define none  " � "


// define 5 Kind of case, have thunder and no thunder, check after 3 Kind of results 
enum players { Boom, None, Boom1, None1, Show1 };

// define 3 Game difficulty 
enum _Difficulty{Easy,General,Difficulty,Purgatory};

int D_size[4][2] = { {10,10} ,{15,8},{20,5},{30,3} };


// Description of the rules of the game 
void Game::rule() {
 loading();
 // Clear the screen 
 system("cls");
 cout << "\n\n\n\n";
 cout << " Game rules: \n\n";
 cout << "1. When the view point is thunder, it will show" * "And will be deducted 10 points " << endl;
 cout << "2. When the difference is not a mine and there are no mines around, it will show the surrounding area, so the box is"   "Around" means next to each other 8 A grid) " << endl;
 cout << "3. When the view point is not mined and there are mines around, the number of mines around is displayed " << endl;
 cout << "4. The game will fail when the maximum fault tolerance is reached " << endl;
 cout << "5. The game wins when there are no unchecked non-reggae " << endl;
 cout << "\n\n\n\t\t\t\t30 Automatically exit the interface after seconds! ";

 Sleep(30000);


}

// Quit the game 
int Game::quit() {


 system("cls");
 // Define the initial coordinates of the console screen 
 COORD c = { 40, 13 };
 // Set the console cursor position 
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << " Game over !!!" << endl;

 Sleep(1000);

 loading();

 return 0;


}


// Game mode 
void Game::play() {
 // Import the highest score record 
 get_Rocord();

 while (true) {
 // Select game Difficulty 
 _difficulty=Selection_difficulty();
 // The default game 1 Straight on 
 int res = 1;
 // Initialize the 
 reset();
 //
 drawGrid();
 while (true) {

  // See the point 
  Cheak();
  //
  drawGrid();


  if (!isWin()) {
  
  if (score > Rocord[_difficulty])Rocord[_difficulty] = score;
  put_Rocord();
  char s;
  cout << " Whether or not to 1 Bureau? is (y|Y)/ no (n|N)" << endl;
  cin >> s;
  if (s == 'y' || s == 'Y')res = 1;
  else res = 0;
  break;
  }
  
 }
 if (!res)break;
 
 }


}


// Update (initialize) 
void Game::reset() {
 // Data initialization 
 score = 0;
 error = 0;
 // Checkerboard initialization 
 srand(time(NULL));
 for (int i = 0; i < Size; i++) {
 for (int j = 0; j < Size; j++) {
  int t = rand() % 2;
  if (t==1)map[j][i] = Boom;
  else map[j][i] = None;
  //cout << t<< " ";
 }
 //cout << endl;
 }
 
 
}


// Draw a map 
void Game::drawGrid() {

 // Clear the screen 
 system("cls");
 // Define the initial coordinates of the console screen 
 COORD c = { 0, 2 };
 // Set the console cursor position 
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 // The initial state of the game 
 for (int i = 0; i <= Size; i++) {
 if (i < 10)
  cout << i << " ";
 else cout << i;
 for (int j = 0; j < Size; j++) {
  if (i == 0) {
  if (j < 9)
   cout << j + 1 << " ";
  else cout << j + 1;
  }
  else cout << none;
 }
 cout << endl;
 }

 for (int y = 0; y < Size; y++) {
 for (int x = 0; x < Size; x++) {
  if (map[x][y] == Boom1|| map[x][y] == None1) {
  // Cursor position coordinates 
  COORD c = { x * 2 + 2, 3 + y };
  // Set the console cursor position 
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle The function gets a handle 
  string o;
  if (map[x][y] == Boom1) o = "* ";
  if (map[x][y] == None1) o = " ";
  cout << o;
  }

  if (map[x][y] == Show1) {
  int cnt = 0;
  for (int i = x - 1; i <= x + 1; i++) {
   for (int j = y - 1; j <= y + 1; j++) {
   if (i >= 0 && i < Size && j >= 0 && j < Size) {
    if (map[i][j] == Boom || map[i][j] == Boom1)cnt++;
   }
   }
  }
  // Cursor position coordinates 
  COORD c = { x*2+2, 3 + y };
  // Set the console cursor position 
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle The function gets a handle 
  cout << cnt << " ";

  }
 }
 }
 c.Y = Size+3;
 // Set the console cursor position 
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << " The current score is: "<<score<<"\n The highest records are: "<<Rocord[_difficulty]<<"\n Please enter the coordinates of the view grid " << endl;
 

}

// View point results 
void Game::Cheak() {
 int x = 0, y = 0;

 cin >> x >> y;
 x -= 1, y -= 1;
 while(map[x][y] == Boom1 || map[x][y] == None1 || map[x][y] == Show1 || x < 0 || x >= Size || y < 0 || y >= Size) {
 // Define the initial coordinates of the console screen 
 COORD c = { 0, 2 };
 c.Y = Size+6;
 // Set the console cursor position 
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << " This grid is checked or not in the checkerboard , Please re-enter " << endl;
 cin >> x >> y;
 x -= 1, y -= 1;
 
 }

 

 if (map[x][y] == Boom) {
 map[x][y] = Boom1;
 score -= 10;
 error++;
 }
 
 else {
 score += 10;
 int cnt = 0; 
 for (int i = x - 1; i <= x + 1; i++) {
  for (int j = y - 1; j <= y + 1; j++) {
  if (i >= 0 && i < Size && j >= 0 && j < Size) {
   if (map[i][j] == Boom || map[i][j] == Boom1)cnt++;
  }
  }
 }
 if (cnt == 0) {
  for (int i = x - 1; i <= x + 1; i++) {
  for (int j = y - 1; j <= y + 1; j++) {
   if (i >= 0 && i < Size && j >= 0 && j < Size) {
   map[i][j] = None1;
   }
  }
  }
 }
 else map[x][y] = Show1;
 }
 
}

// Determine if the game is over 
int Game::isWin() {
 int cnt = 0;
 for (int i = 0; i < Size; i++) {
 for (int j = 0; j < Size; j++) {
  if (map[i][j] == None)cnt++;
 }
 }

 if (cnt == 0) {
 system("cls");
 // Define the initial coordinates of the console screen 
 COORD c = { 50, 15 };
 // Set the console cursor position 
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "You Win!!!" << endl;
 return 0;
 }
 else if (error >= fault_tolerant) {
 system("cls");
 // Define the initial coordinates of the console screen 
 COORD c = { 50, 15 };
 // Set the console cursor position 
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "You Loss!!!" << endl;
 return 0;
 }
 else return 1;

}

// Import the highest score record 
void Game::get_Rocord() {

 ifstream fin(RocordPath, ios::in);
 for (int i = 0; i < 5; i++) {
 fin >> Rocord[i];
 }
}

// Export the highest score record 
void Game::put_Rocord() {

 ofstream fout(RocordPath, ios::out);
 for(int i=0;i<5;i++)
 fout << Rocord[i] << endl;
}

// Select the difficulty 
int Game::Selection_difficulty() {
 // Clear the screen 
 system("cls");
 // Define the initial coordinates of the console screen 
 COORD c = { 0, 6 };
 // Set the console cursor position 
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 
 cout << "\t\t\t\t\t\t1. simple    ( 10*10 "  10 Fault tolerance) \n\n" << endl;
 cout << "\t\t\t\t\t\t2.1 a    ( 15*15 "  8 Fault tolerance) \n\n" << endl;
 cout << "\t\t\t\t\t\t3. difficult    ( 20*20 "  5 Fault tolerance) \n\n" << endl;
 cout << "\t\t\t\t\t\t4. purgatory    ( 30*30 "  3 Fault tolerance) \n\n" << endl;
 cout << "\t\t\t\t\t\t5. The custom \n\n" << endl; 
 cout << "\t\t\t\t\t\t Please select game difficulty :";
 int t = 1;
 cin >> t;
 while (t < 1 || t>5) {
 COORD c = { 0, 21 };
 // Set the console cursor position 
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "\t\t\t\t\t\t Input error please retype :" << endl;;
 cin >> t;
 }
 switch (t) {
 case 1:Size = D_size[Easy][0], fault_tolerant = D_size[Easy][1]; break;
 case 2:Size = D_size[General][0], fault_tolerant = D_size[General][1]; break;
 case 3:Size = D_size[Difficulty][0], fault_tolerant = D_size[Difficulty][1]; break;
 case 4:Size = D_size[Purgatory][0], fault_tolerant = D_size[Purgatory][1]; break;
 case 5: {
 // Clear the screen 
 system("cls");
 cout << "\n\n\n\n\n\t\t\t\t Please enter the map size and the maximum number of mine failures      (in size 10-30 Fault tolerance in 10 ) ";
  cout << "\t\t\t\t\t\t\t\t\t Size: ";
  cin >> Size;
  cout << "\n\t\t\t\t\t Tolerance: ";
  cin >> fault_tolerant;
 }break;
 }
 loading();
 return t;

}


void Game::loading() {

 COORD c = { 50,15 };
 // Set the console cursor position 
 int t = 6;
 while (t--) {
 system("cls");
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 if(t%3==0)
  cout << "loading..." << endl;
 if (t % 3 == 1)
  cout << "loading.." << endl;
 if (t % 3 == 2)
  cout << "loading." << endl;
 Sleep(500);
 }


}

And then finally the main function part


// The main function of minesweeper 

#include<iostream>
#include<Windows.h>
#include" Mine clearance .h"
using namespace std;
int main() {
 Game game;
 while (true) {
 int t, g = 1;
 system("cls");
 // Define the initial coordinates of the console screen 
 COORD c = { 30, 10 };
 // Set the console cursor position 
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << " Welcome to   Mine!! \n\n\n\n\n\n";
 cout << "\t\t\t\t\t1. Start the game \n\n\n\t\t\t\t\t2. Read the rules \n\n\n\t\t\t\t\t3. exit " << endl;
 cin >> t;
 switch (t) {
 case 1:game.play(); break;
 case 2:game.rule(); break;
 case 3:g=game.quit(); break;
 }
 if (g == 0)break;
 }
 return 0;
 
}

This is the first time for me to write a blog and also the first time for me to finish a project independently. I hope you can give me some advice on my deficiencies.


Related articles: