本文实例为大家分享了C++实现扫雷游戏的具体代码,供大家参考,具体内容如下
#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#include #include #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 = 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<<" 扫雷"<
更多精彩游戏小代码,请点击《游戏专题》阅读
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。