C++實(shí)現(xiàn)趣味掃雷游戲
本文實(shí)例為大家分享了C++實(shí)現(xiàn)趣味掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下
流程設(shè)計(jì)
1.初始化陣列。
2.輸入坐標(biāo)點(diǎn)。
3.選擇:挖掘,標(biāo)記,取消標(biāo)記,重啟,退出游戲。
如果選了挖掘,判斷坐標(biāo)點(diǎn)是地雷則游戲結(jié)束,是數(shù)字則顯示數(shù)字并回到2,是空格則顯示周圍8個(gè)元素值并直到連帶的空格顯示完了回到2;
如果選了標(biāo)記,將該點(diǎn)的元素值設(shè)為-2并回到2;
如果選了取消標(biāo)記,初始化該點(diǎn),回到2;
如果選了重啟,則初始化陣列,回到2;
如果選了退出游戲,則exit。
4.挖掘完所有非地雷點(diǎn)后,游戲勝利,選擇是否再來一局,是則回到1,否則exit
面向?qū)ο笤O(shè)計(jì)思想
創(chuàng)建一個(gè)bombsweep類,存儲幾個(gè)方法:
calculate:統(tǒng)計(jì)以(x,y)為中心周圍8個(gè)點(diǎn)的地雷數(shù)目。
game:模擬游戲過程。
print:打印陣列。
check:檢查是否滿足勝利條件。
在main函數(shù)中,在需要的時(shí)候根據(jù)bombsweep類創(chuàng)建bs對象,調(diào)用bs里面的相關(guān)方法。
程序代碼
#include <ctime> #include <cstdlib> #include <iostream> #include <cstring> using namespace std; int map[12][12]; // ??????????,????????????1 int 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] <= 9 && y+derection[j] <= 9 && x+derection[i] >= 1 && y+derection[j] >= 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 << " |"; for (int i=1; i<10; i++) cout << " " << i; cout << endl; cout << "__|__________________Y" ; cout << endl; for ( int i = 1; i < 10; i++ ) { cout << i << " |"; for ( int j = 1; j < 10; j++ ) { if(map[i][j]==-2) cout <<" B"; else if ( map[i][j] == -1 || map[i][j] == 9 ) cout << " #"; else cout << " "<< map[i][j]; } cout << "\n"; } cout << " X\n"; } bool check () { int counter = 0; for ( int i = 1; i < 10; i++ ) for ( int j = 1; j < 10; j++ ) if ( map[i][j] != -1 ) counter++; if ( counter == 10 ) return true; else return false; } }; int main () { int i, j, x, y; char ch; srand ( time ( 0 ) ); do { //????? memset ( map, -1, sizeof(map) ); for ( i = 0; i < 10; ) { x = rand()%9 + 1; y = rand()%9 + 1; if ( map[x][y] != 9 ) { map[x][y] = 9; i++; } } cout << " |"; for (i=1; i<10; i++) cout << " " << i; cout << endl; cout << "__|__________________Y" ; cout << endl; for ( i = 1; i < 10; i++ ) { cout << i << " |"; for ( j = 1; j < 10; j++ ) cout << " "<< "#"; cout << "\n"; } cout << " X\n"; cout << "Please input location x,press enter then input location y: \n"; while ( cin >> x >> y ) { cout << "Please select:1.dig, 2.sign, 3.cancel sign, 4.restart, 5.exit: \n"; cin >>type; switch(type) { case 1: { if ( map[x][y] == 9 || map[x][y]==-2) { cout << "YOU LOSE!" << endl; cout << " |"; for (i=1; i<10; i++) cout << " " << i; cout << endl; cout << "__|__________________Y"<<endl ; for ( i = 1; i < 10; i++ ) { cout << i << " |"; for ( j = 1; j < 10; j++ ) { if ( map[i][j] == 9 || map[i][j]==-2) cout << " @"; else cout << " #"; } cout << "\n"; } cout << " X\n"; exit(0); } bombsweep bs; bs.game(x,y); bs.print(x,y); cout << "Please input location x,press enter then input location y: \n"; if ( bs.check()) { cout << "YOU WIN" << endl; break; } continue; } case 2: { bombsweep bs; map[x][y]=-2; bs.print(x,y); cout << "Please input location x,press enter then input location y: \n"; continue; } case 3: { bombsweep bs; map[x][y]=-1; bs.print(x,y); cout << "Please input location x,press enter then input location y: \n"; continue; } case 4: { memset ( map, -1, sizeof(map) ); for ( i = 0; i < 10; ) { x = rand()%9 + 1; y = rand()%9 + 1; if ( map[x][y] != 9 ) { map[x][y] = 9; i++; } } cout << " |"; for (i=1; i<10; i++) cout << " " << i; cout << endl; cout << "__|__________________Y" ; cout << endl; for ( i = 1; i < 10; i++ ) { cout << i << " |"; for ( j = 1; j < 10; j++ ) cout << " "<< "#"; cout << "\n"; } cout << " X\n"; cout << "Please input location x,press enter then input location y: \n"; continue; } case 5: cout << "Game Ended\n"; exit(0); break; default: cout<< "Invalid input, try again: \n"; continue; }//end switch }//end while(cin >> x >>y) cout << "Do you want to play again?(y/n):" << endl; cin >> ch; }//end do while ( ch == 'y' ); return 0; }//end main()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
QT編寫窗口插件實(shí)現(xiàn)調(diào)用窗口的自適應(yīng)
這篇文章主要為大家詳細(xì)介紹了QT編寫窗口插件實(shí)現(xiàn)調(diào)用窗口的自適應(yīng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06使用?Visual?Studio?2022?開發(fā)?Linux?C++?應(yīng)用程序的過程詳解
Visual?Studio?2022?引入了用于?Linux?C++?開發(fā)的本機(jī)?WSL2?工具集,可以構(gòu)建和調(diào)試?Linux?C++?代碼,并提供了非常好的?Linux?文件系統(tǒng)性能、GUI?支持和完整的系統(tǒng)調(diào)用兼容性,這篇文章主要介紹了使用Visual?Studio?2022?開發(fā)?Linux?C++?應(yīng)用程序,需要的朋友可以參考下2021-11-11深度剖析C++對象池自動(dòng)回收技術(shù)實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于深度剖析C++對象池自動(dòng)回收技術(shù)實(shí)現(xiàn),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01成員初始化列表與構(gòu)造函數(shù)體中的區(qū)別詳細(xì)解析
無論是在構(gòu)造函數(shù)初始化列表中初始化成員,還是在構(gòu)造函數(shù)體中對它們賦值,最終結(jié)果是相同的。不同之處在于,使用構(gòu)造函數(shù)初始化列表的版本初始化數(shù)據(jù)成員,沒有定義初始化列表的構(gòu)造函數(shù)版本在構(gòu)造函數(shù)體中對數(shù)據(jù)成員賦值2013-09-09QT自定義QTextEdit實(shí)現(xiàn)大數(shù)據(jù)的實(shí)時(shí)刷新顯示功能實(shí)例
TextEdit是我們常用的Qt控件,用來顯示文本信息,下面這篇文章主要給大家介紹了關(guān)于QT自定義QTextEdit實(shí)現(xiàn)大數(shù)據(jù)的實(shí)時(shí)刷新顯示功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05C語言中send()函數(shù)和sendto()函數(shù)的使用方法
這篇文章主要介紹了C語言中send()函數(shù)和sendto()函數(shù)的使用方法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09數(shù)據(jù)結(jié)構(gòu) 紅黑樹的詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu) 紅黑樹的詳解的相關(guān)資料,數(shù)據(jù)結(jié)構(gòu)中的二叉樹查找,紅黑樹的講解,需要的朋友可以參考下2017-07-07