1. 程式人生 > 程式設計 >C++實現掃雷程式開發

C++實現掃雷程式開發

C++程式開發實現掃雷遊戲,供大家參考,具體內容如下

//掃雷的類的定義

#pragma once

class Game{
public:
 //開始遊戲
 void play();
 //退出遊戲
 int quit();
 //遊戲規則
 void rule();

private:
 //踩雷次數,作為失敗條件
 int error = 0;
 //分數
 int score = 0;
 //最高分記錄
 int Rocord[5] = { 0,0 };
 //地圖
 int map[40][40];

 //地圖的大小Size*Size
 int Size = 10;

 //容錯
 int fault_tolerant = 10;

 //困難程度
 int _difficulty=1;

 //初始化
 void reset();

 //畫地圖
 void drawGrid();

 //檢視格子的結果
 void Cheak();

 //判斷是否遊戲結束
 int isWin();

 //匯入最高分記錄
 void get_Rocord();

 //匯出最高分記錄
 void put_Rocord();

 //選擇難度
 int Selection_difficulty();

 //載入介面
 void loading();
};

然後是對類的函式的定義

//對Game類的成員函式的定義

#include "掃雷.h"
#include<Windows.h>
#include<iostream>
#include<fstream>
#include<time.h>
#include <conio.h>


#pragma warning(disable:4996) //這一行是為了能在 Visual Studio 2017內使用getch()函式

//定義最高分記錄的儲存地址
#define RocordPath "D:\\VS/掃雷最高分.txt"

using namespace std;

#define none  "█"


//定義5種情況,有雷和無雷,檢視後的三種結果
enum players { Boom,None,Boom1,None1,Show1 };

//定義三種遊戲難度
enum _Difficulty{Easy,General,Difficulty,Purgatory};

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


//遊戲規則的描述
void Game::rule() {
 loading();
 //清屏
 system("cls");
 cout << "\n\n\n\n";
 cout << "遊戲規則:\n\n";
 cout << "1.當檢視點為雷時,會顯示“*”,並且將扣10分" << endl;
 cout << "2.當差看點不為雷且周圍均無雷將顯示周圍所以格為“ ”(周圍指的是相鄰的8個格子)" << endl;
 cout << "3.當檢視點不為雷,且周圍存在雷時,將顯示周圍雷數" << endl;
 cout << "4.當踩到最大容錯個數雷時遊戲將失敗" << endl;
 cout << "5.當不存在未查閱的非雷格時遊戲勝利" << endl;
 cout << "\n\n\n\t\t\t\t30秒後自動退出該介面!";

 Sleep(30000);


}

//退出遊戲
int Game::quit() {


 system("cls");
 //定義控制檯螢幕初始座標
 COORD c = { 40,13 };
 //設定控制檯游標位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 cout << "遊戲結束!!!" << endl;

 Sleep(1000);

 loading();

 return 0;


}


//遊戲模式
void Game::play() {
 //匯入最高分記錄
 get_Rocord();

 while (true) {
 //選擇遊戲難度
 _difficulty=Selection_difficulty();
 //預設遊戲一直進行
 int res = 1;
 //初始化
 reset();
 //
 drawGrid();
 while (true) {

  //檢視點
  Cheak();
  //
  drawGrid();


  if (!isWin()) {
  
  if (score > Rocord[_difficulty])Rocord[_difficulty] = score;
  put_Rocord();
  char s;
  cout << "是否再來一局?是(y|Y)/否(n|N)" << endl;
  cin >> s;
  if (s == 'y' || s == 'Y')res = 1;
  else res = 0;
  break;
  }
  
 }
 if (!res)break;
 
 }


}


//更新(初始化)
void Game::reset() {
 //資料初始化
 score = 0;
 error = 0;
 //棋盤初始化
 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;
 }
 
 
}


//畫地圖
void Game::drawGrid() {

 //清屏
 system("cls");
 //定義控制檯螢幕初始座標
 COORD c = { 0,2 };
 //設定控制檯游標位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 //棋局初始狀態
 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) {
  //游標位置座標
  COORD c = { x * 2 + 2,3 + y };
  //設定控制檯游標位置
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);//GetStdHandle函式獲得控制代碼
  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++;
   }
   }
  }
  //游標位置座標
  COORD c = { x*2+2,c);//GetStdHandle函式獲得控制代碼
  cout << cnt << " ";

  }
 }
 }
 c.Y = Size+3;
 //設定控制檯游標位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 cout << "當前分數是:"<<score<<"\n最高紀錄是:"<<Rocord[_difficulty]<<"\n請輸入檢視格的座標" << endl;
 

}

//檢視點結果
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) {
 //定義控制檯螢幕初始座標
 COORD c = { 0,2 };
 c.Y = Size+6;
 //設定控制檯游標位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 cout << "該格以檢查過或不在棋盤內,請重新輸入" << 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;
 }
 
}

//判斷是否遊戲結束
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");
 //定義控制檯螢幕初始座標
 COORD c = { 50,15 };
 //設定控制檯游標位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 cout << "You Win!!!" << endl;
 return 0;
 }
 else if (error >= fault_tolerant) {
 system("cls");
 //定義控制檯螢幕初始座標
 COORD c = { 50,c);
 cout << "You Loss!!!" << endl;
 return 0;
 }
 else return 1;

}

//匯入最高分記錄
void Game::get_Rocord() {

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

//匯出最高分記錄
void Game::put_Rocord() {

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

//選擇難度
int Game::Selection_difficulty() {
 //清屏
 system("cls");
 //定義控制檯螢幕初始座標
 COORD c = { 0,6 };
 //設定控制檯游標位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 
 cout << "\t\t\t\t\t\t1.簡單  (10*10格 10容錯)\n\n" << endl;
 cout << "\t\t\t\t\t\t2.一般  (15*15格 8容錯)\n\n" << endl;
 cout << "\t\t\t\t\t\t3.困難  (20*20格 5容錯)\n\n" << endl;
 cout << "\t\t\t\t\t\t4.煉獄  (30*30格 3容錯)\n\n" << endl;
 cout << "\t\t\t\t\t\t5.自定義\n\n" << endl; 
 cout << "\t\t\t\t\t\t請選擇遊戲難度:";
 int t = 1;
 cin >> t;
 while (t < 1 || t>5) {
 COORD c = { 0,21 };
 //設定控制檯游標位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 cout << "\t\t\t\t\t\t輸入錯誤請重新輸入:" << 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: {
 //清屏
 system("cls");
 cout << "\n\n\n\n\n\t\t\t\t請輸入地圖尺碼和最多踩雷失敗數    (尺碼在10-30,容錯在10以內)";
  cout << "\t\t\t\t\t\t\t\t\t尺碼:";
  cin >> Size;
  cout << "\n\t\t\t\t\t容錯:";
  cin >> fault_tolerant;
 }break;
 }
 loading();
 return t;

}


void Game::loading() {

 COORD c = { 50,15 };
 //設定控制檯游標位置
 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);
 }


}

最後就是主函式部分

//掃雷遊戲的主函式

#include<iostream>
#include<Windows.h>
#include"掃雷.h"
using namespace std;
int main() {
 Game game;
 while (true) {
 int t,g = 1;
 system("cls");
 //定義控制檯螢幕初始座標
 COORD c = { 30,10 };
 //設定控制檯游標位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 cout << "歡迎來到 掃雷!!!\n\n\n\n\n\n";
 cout << "\t\t\t\t\t1.開始遊戲\n\n\n\t\t\t\t\t2.閱讀規則\n\n\n\t\t\t\t\t3.退出" << 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;
 
}

這是第一次寫部落格 也是第一次獨立完成專案,有不足的地方,希望各位大牛指教。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。