C++實現(xiàn)掃雷程序開發(fā)
更新時間:2020年07月21日 15:04:22 作者:qq_46058158
這篇文章主要為大家詳細介紹了C++實現(xiàn)掃雷程序開發(fā),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
C++程序開發(fā)實現(xiàn)掃雷游戲,供大家參考,具體內(nèi)容如下
//掃雷的類的定義 #pragma once class Game{ public: //開始游戲 void play(); //退出游戲 int quit(); //游戲規(guī)則 void rule(); private: //踩雷次數(shù),作為失敗條件 int error = 0; //分數(shù) int score = 0; //最高分記錄 int Rocord[5] = { 0,0,0,0,0 }; //地圖 int map[40][40]; //地圖的大小Size*Size int Size = 10; //容錯 int fault_tolerant = 10; //困難程度 int _difficulty=1; //初始化 void reset(); //畫地圖 void drawGrid(); //查看格子的結(jié)果 void Cheak(); //判斷是否游戲結(jié)束 int isWin(); //導(dǎo)入最高分記錄 void get_Rocord(); //導(dǎo)出最高分記錄 void put_Rocord(); //選擇難度 int Selection_difficulty(); //加載界面 void loading(); };
然后是對類的函數(shù)的定義
//對Game類的成員函數(shù)的定義 #include "掃雷.h" #include<Windows.h> #include<iostream> #include<fstream> #include<time.h> #include <conio.h> #pragma warning(disable:4996) //這一行是為了能在 Visual Studio 2017內(nèi)使用getch()函數(shù) //定義最高分記錄的存儲地址 #define RocordPath "D:\\VS/掃雷最高分.txt" using namespace std; #define none "█" //定義5種情況,有雷和無雷,查看后的三種結(jié)果 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} }; //游戲規(guī)則的描述 void Game::rule() { loading(); //清屏 system("cls"); cout << "\n\n\n\n"; cout << "游戲規(guī)則:\n\n"; cout << "1.當查看點為雷時,會顯示“*”,并且將扣10分" << endl; cout << "2.當差看點不為雷且周圍均無雷將顯示周圍所以格為“ ”(周圍指的是相鄰的8個格子)" << endl; cout << "3.當查看點不為雷,且周圍存在雷時,將顯示周圍雷數(shù)" << endl; cout << "4.當踩到最大容錯個數(shù)雷時游戲?qū)⑹? << endl; cout << "5.當不存在未查閱的非雷格時游戲勝利" << endl; cout << "\n\n\n\t\t\t\t30秒后自動退出該界面!"; Sleep(30000); } //退出游戲 int Game::quit() { system("cls"); //定義控制臺屏幕初始坐標 COORD c = { 40, 13 }; //設(shè)置控制臺光標位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "游戲結(jié)束!!!" << endl; Sleep(1000); loading(); return 0; } //游戲模式 void Game::play() { //導(dǎo)入最高分記錄 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() { //數(shù)據(jù)初始化 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 }; //設(shè)置控制臺光標位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); //棋局初始狀態(tài) 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 }; //設(shè)置控制臺光標位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle函數(shù)獲得句柄 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, 3 + y }; //設(shè)置控制臺光標位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle函數(shù)獲得句柄 cout << cnt << " "; } } } c.Y = Size+3; //設(shè)置控制臺光標位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "當前分數(shù)是:"<<score<<"\n最高紀錄是:"<<Rocord[_difficulty]<<"\n請輸入查看格的坐標" << endl; } //查看點結(jié)果 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; //設(shè)置控制臺光標位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "該格以檢查過或不在棋盤內(nèi),請重新輸入" << 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; } } //判斷是否游戲結(jié)束 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 }; //設(shè)置控制臺光標位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "You Win!!!" << endl; return 0; } else if (error >= fault_tolerant) { system("cls"); //定義控制臺屏幕初始坐標 COORD c = { 50, 15 }; //設(shè)置控制臺光標位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "You Loss!!!" << endl; return 0; } else return 1; } //導(dǎo)入最高分記錄 void Game::get_Rocord() { ifstream fin(RocordPath, ios::in); for (int i = 0; i < 5; i++) { fin >> Rocord[i]; } } //導(dǎo)出最高分記錄 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 }; //設(shè)置控制臺光標位置 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 }; //設(shè)置控制臺光標位置 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請輸入地圖尺碼和最多踩雷失敗數(shù) (尺碼在10-30,容錯在10以內(nèi))"; 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 }; //設(shè)置控制臺光標位置 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); } }
最后就是主函數(shù)部分
//掃雷游戲的主函數(shù) #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 }; //設(shè)置控制臺光標位置 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.閱讀規(guī)則\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; }
這是第一次寫博客 也是第一次獨立完成項目,有不足的地方,希望各位大牛指教。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
分享一下8年C++面向?qū)ο笤O(shè)計的經(jīng)驗體會
關(guān)于C++程序設(shè)計的書藉非常多,本章不講C++的語法,只講一些小小的編程道理。如果我能早幾年明白這些小道理,就可以大大改善數(shù)十萬行程序的質(zhì)量了2017-07-07C/C++: Inline function, calloc 對比 malloc
以下是對c/c++中的malloc函數(shù)與calloc函數(shù)的區(qū)別以及它們之間的聯(lián)系進行了介紹,需要的朋友可以過來參考下2016-07-07在C++17中實現(xiàn)無鎖數(shù)據(jù)結(jié)構(gòu)的方法詳解
在探索?C++17?中的無鎖數(shù)據(jù)結(jié)構(gòu)之前,我們首先需要理解無鎖編程的基本概念及其在現(xiàn)代軟件開發(fā)中的重要性,在這個章節(jié)中,我們將深入探討無鎖編程的概念,以及它如何滿足人類對于更高效、更可靠軟件的本能需求,文中通過代碼示例介紹的非常詳細,感興趣的朋友可以參考下2023-12-12