C++實現(xiàn)簡單掃雷小游戲
本文實例為大家分享了C++實現(xiàn)簡單掃雷小游戲的具體代碼,供大家參考,具體內(nèi)容如下
頭文件Mine_Sweep.h
#include <iostream> #include <ctime> #include <cstdlib> #include <algorithm> #include <queue> #include <Windows.h> using namespace std; typedef pair<int, int> P; const int M = 10; const int N = 10; void InitGame(int A[M][N]);//初始化游戲 void Map(int A[M][N], int Static[M][N]);//地圖 void Result(int A[M][N]);//游戲結(jié)果 int IfSafety(int A[M][N],int x,int y);//周圍是否有地雷 void Operate(int A[M][N],int Static[M][N], int x, int y); //操作產(chǎn)生的影響 void Welcome(); //歡迎界面 void GameOver(); //結(jié)束界面 void gotoxy(int x, int y); //指針位置 void color(int c); //改變顏色
實現(xiàn)部分
#include"Mine_Sweep.h" void InitGame(int A[M][N]) { //從右開始順時針->右(1,0)右上(1,1)上(0,1)左上(-1,1)左(-1,0)左下(-1,-1)下(0,-1)右下(1,-1) int Sign[8][2] = { {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1} }; int count = 0;//產(chǎn)生M個不同隨機數(shù) int Random; //隨機數(shù)1-100 int x, y; //橫縱坐標(biāo) srand(unsigned(time(NULL))); //清空 for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) A[i][j] = 0; //把M個雷安放好 while (count < M) { Random = rand() % 100 + 1; x = Random / 10; //轉(zhuǎn)換為X y = Random - 10 * x; //轉(zhuǎn)換為Y if (A[x][y] == 0) //地雷設(shè)為-1,無地雷設(shè)為0,1,2... { A[x][y] = -1; count++; } } //構(gòu)建一個虛擬的地圖即增加外圍[M][N]->[M+2][N+2]目的是為了讓邊界和內(nèi)部的處理方式一樣 int Virtual[M + 2][N + 2] = { 0 }; for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { Virtual[i + 1][j + 1] = A[i][j];//所有元素向下向右移動1格 } } //計算每個地方的地雷分布情況 for (int i = 1; i <= M; i++) { for (int j = 1; j <= N; j++) { int flag = 0; //如果是地雷 if (Virtual[i][j] == -1) continue; //跳過 for (int k = 0; k < 8; k++) { if (Virtual[i + Sign[k][0]][j + Sign[k][1]]==-1) flag++; } A[i-1][j-1] = flag; } } } void Map(int A[M][N],int Static[M][N]) { system("cls"); Welcome(); //地雷用※表示,數(shù)字用123...,未翻開用■ Static[M][N]用于儲存狀態(tài)0表示未翻開,1表示翻開 for (int i = 0; i < M; i++) { cout << "\t\t\t\t\t"<<" "; for (int j = 0; j < N; j++) { if (Static[i][j] == 0) cout << "■"; else { if (A[i][j] == -1) { cout << "※"; } else { if (IfSafety(A, i, j)) cout << "□"; else cout <<" " <<A[i][j]; } } } cout << endl; } } void Operate(int A[M][N], int Static[M][N],int x,int y)//貪心算法 { queue<P> que;//隊列 //從右開始順時針->右(1,0)右上(1,1)上(0,1)左上(-1,1)左(-1,0)左下(-1,-1)下(0,-1)右下(1,-1) int Sign[8][2] = { {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1} }; if (A[x][y] == -1) //如果翻到地雷,游戲結(jié)束 { Result(A); //顯示結(jié)果 GameOver(); //游戲結(jié)束 exit(0); } Static[x][y] = 1;//翻開 que.push(P(x, y)); while (que.size()) { P p = que.front(); que.pop(); if(!IfSafety(A, p.first, p.second)) continue; //如果周圍有炸彈,繼續(xù)遍歷隊列 for (int i = 0; i < 8; i++) { int nx = p.first + Sign[i][0]; int ny = p.second + Sign[i][1]; if (0 <= nx && nx < M && 0 <= ny && ny < N && Static[nx][ny] == 0) { que.push(P(nx, ny)); Static[nx][ny] = 1;//翻開 } } } } void Result(int A[M][N]) { for (int i = 0; i < M; i++) { cout << "\t\t\t\t\t"; for (int j = 0; j < N; j++) cout << A[i][j]<<" "; cout << endl; } } int IfSafety(int A[M][N],int x, int y) { //從右開始順時針->右(1,0)右上(1,1)上(0,1)左上(-1,1)左(-1,0)左下(-1,-1)下(0,-1)右下(1,-1) int Sign[8][2] = { {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1} }; int Virtual[M + 2][N + 2] = { 0 }; x = x + 1;//虛擬坐標(biāo) y = y + 1; for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { Virtual[i + 1][j + 1] = A[i][j];//所有元素向下向右移動1格 } } //計算每個地方的地雷分布情況 for (int k = 0; k < 8; k++) { if (Virtual[x + Sign[k][0]][y + Sign[k][1]] == -1) return 0; } return 1; } void Welcome() { cout << "\t\t\t\t\t\t掃雷游戲"<<endl<<endl; cout << "\t\t\t\t===========================================" << endl << endl; gotoxy(40, 3); for (int i = 0; i < M; i++) { //畫x軸坐標(biāo) cout << " " << i + 1; } for (int j = 0; j < M; j++) { //畫Y軸坐標(biāo) gotoxy(38, 4+j); cout << j + 1<<endl; } gotoxy(0, 4); } void GameOver() { cout << "\t\t\t\t游戲結(jié)束,你輸了" << endl; getchar(); getchar(); } void gotoxy(int x, int y) { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } void color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c); }
主函數(shù)
#include"Mine_Sweep.h" int main() { int A[M][N], x, y, Static[M][N] = { 0 }; color(13); InitGame(A); cout << "\n\n\n\t\t游戲說明:" << "你需要在不點錯雷的情況下盡可能快的將所有的雷都標(biāo)記出來\n"; cout << "\t\t你點出了一個數(shù)字,是1,就說明它周圍的8的格子里有1個雷,是2就有兩個雷,是3就有三個雷···" << endl; cout<< "\t\t但如果你標(biāo)記錯了雷,那就會boom!游戲結(jié)束。"<<endl; cout << "\t\t請先輸入橫坐標(biāo),再輸入縱坐標(biāo) eg: 1 2" << endl; cout << "\t\t回車鍵開始,祝你好運!" << endl; getchar(); color(15); while (1) { int flag = 1; Map(A, Static); cout << "\t\t\t\t請輸入要翻開的坐標(biāo):"; cin >> x >> y; while (x <= 0 || x > M || y <= 0 || y > N) { cout << "\t\t\t\t超出范圍,請重新輸入要翻開的坐標(biāo):" ; cin >> x >> y; } Operate(A, Static, x-1, y-1); } return 0; }
運行效果
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺析C++標(biāo)準(zhǔn)庫元組(tuple)源碼
這篇文章主要介紹了C++標(biāo)準(zhǔn)庫元組(tuple)源碼,介紹了什么是元組以及用法,并進行了源碼分析,需要的朋友可以參考下2015-08-08標(biāo)準(zhǔn)C++類string的Copy-On-Write技術(shù)
這里,我想從C++類或是設(shè)計模式的角度為各位揭開Copy-On-Write技術(shù)在string中實現(xiàn)的面紗,以供各位在用C++進行類庫設(shè)計時做一點參考2013-11-11Sersync+Rsync實現(xiàn)觸發(fā)式文件同步實戰(zhàn)過程
sersync是使用c++編寫,而且對linux系統(tǒng)文 件系統(tǒng)產(chǎn)生的臨時文件和重復(fù)的文件操作進行過濾。下面通過本文給大家分享Sersync+Rsync實現(xiàn)觸發(fā)式文件同步實戰(zhàn)過程,需要的朋友參考下吧2017-09-09QT使用SQLite數(shù)據(jù)庫超詳細(xì)教程(增刪改查、對大量數(shù)據(jù)快速存儲和更新)
這篇文章主要給大家介紹了關(guān)于QT使用SQLite數(shù)據(jù)庫的相關(guān)資料,其中包括增刪改查以及對大量數(shù)據(jù)快速存儲和更新,SQLite是一種嵌入式關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它是一個軟件庫,提供了一個自包含、無服務(wù)器、零配置的、事務(wù)性的SQL數(shù)據(jù)庫引擎,需要的朋友可以參考下2024-01-01詳解C++中typedef 和 #define 的區(qū)別
這篇文章主要介紹了C++中typedef 與 #define 的區(qū)別,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09