您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)C++rh 制作掃雷游戲,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
本文實(shí)例為大家分享了C++實(shí)現(xiàn)掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下
#ifndef SAOLEI_H #define SAOLEI_H class Block { friend class Saoleigame; public: Block(); bool isShown(); void setnum(int); int getnum(); bool isbomb(); protected: int num; bool flag_show; int x; int y; }; class Saoleigame { public: Saoleigame(); ~Saoleigame(); void _init_(); void gameStart(); void reflash(); void check(int x, int y); void click(int x, int y); void gameOver(); private: Block juzheng[100]; bool flag; int b[10]; unsigned int score; }; #endif
以上是編寫的頭文件
#include<iostream> #include<cstdlib> #include<ctime> #include"Saolei.h" using namespace std; Saoleigame::Saoleigame() { _init_(); flag = true; score = 0; } Saoleigame::~Saoleigame() { } void Saoleigame::_init_() { srand(time(NULL)); for( int i = 0; i < 10; i++ ) { b[i] = -1; } for(int i = 0; i < 10; i ++ ) { bool temp_flag = false; do { int temp = (unsigned int)rand()%100; for( int j = 0; j < i; j ++) { if(temp == b[i]) { temp_flag = true; } } if(!temp_flag) { b[i] = temp; } }while(temp_flag); } for(int i = 0; i < 10; i++ ) { juzheng[b[i]].setnum(-1); } for( int i = 0; i < 10; i ++ ) { for( int j = 0; j < 10;j++) { juzheng[i*10+j].x = i+1; juzheng[i*10+j].y = j+1; } } for(int m = 0; m < 10; m ++ ) { for( int n = 0; n < 10 ; n++ ) { check(m + 1, n + 1); } } } void Saoleigame::check(int x, int y) { if(juzheng[(x - 1)*10 + (y - 1)].num == -1)return; int trans = (x - 1)*10 + (y - 1); int number = 0; for( int i = -1; i < 2; i ++ ) { for(int j = -1; j < 2; j ++) { if(!(x + i<= 0 && x + i >= 10 || j + y <= 0 && y + j >= 10)) { if(juzheng[(x + i - 1)*10 + (y + j - 1)].num == -1) number ++; } } } juzheng[(x - 1)*10 + (y - 1)].setnum(number); } void Saoleigame::click(int x, int y) { if(juzheng[(x- 1)*10 + (y - 1)].num == 0) { for( int i = -1; i < 2; i ++ ) { for(int j = -1; j < 2; j ++) { if(!((x + i<= 0 || x + i > 10 )|| (j + y <= 0 || y + j > 10)) && !(i == 0&& j ==0) && !juzheng[(x+i- 1)*10 + (y +j- 1)].flag_show){ juzheng[(x+i- 1)*10 + (y +j- 1)].flag_show = true; click(x + i, y + j); } } } } juzheng[(x- 1)*10 + (y - 1)].flag_show = true; return; } void Saoleigame::gameStart() { do { reflash(); int x, y; cout<<"input the position: "; cin >> x>> y; if(juzheng[(x-1)*10 + (y-1)].isbomb()) { gameOver(); return; } else { click(x , y); } }while(flag); } void Saoleigame::reflash() { system("cls"); score = 0; cout<<" 掃雷"<<endl; cout<<" 1 2 3 4 5 6 7 8 9 10"<<endl; cout<<" -------------------"<<endl; for(int i = 0; i < 100; i ++ ) { if(i%10 == 0) { if(i /10 + 1 == 10)cout<<10<<"|"; else cout<<i /10 + 1<<" |"; } if(juzheng[i].isShown()) { if(juzheng[i].isShown() && (juzheng[i].getnum())!=-1) { score ++; } if((juzheng[i].getnum())==-1)cout <<"*"<<"|"; else cout <<juzheng[i].getnum()<<"|"; } else { cout<<" |"; } if((i+1)%10 == 0)cout<<endl; } cout<<" -------------------"<<endl; cout<<"score:"<<(score*100)/95<<endl; } void Saoleigame::gameOver() { for(int i = 0 ; i < 10 ; i++ ) { juzheng[b[i]].flag_show = true; } reflash(); cout<<"Game Over"<<endl<<endl; flag = false; } Block::Block() { flag_show = false; num = 0; } bool Block::isShown() { return flag_show; } void Block::setnum(int _num) { num = _num; } int Block::getnum() { return num; } bool Block::isbomb() { return num == -1; }
關(guān)于“C++rh 制作掃雷游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。