首页 > 编程语言 > C++扫雷游戏的简单制作
2020
09-24

C++扫雷游戏的简单制作

本文实例为大家分享了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<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;
}

更多精彩游戏小代码,请点击《游戏专题》阅读

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧