C++实现扫雷程序开发

所属分类: 软件编程 / C 语言 阅读数: 102
收藏 0 赞 0 分享

C++程序开发实现扫雷游戏,供大家参考,具体内容如下

//扫雷的类的定义

#pragma once

class Game{
public:
 //开始游戏
 void play();
 //退出游戏
 int quit();
 //游戏规则
 void rule();

private:
 //踩雷次数,作为失败条件
 int error = 0;
 //分数
 int score = 0;
 //最高分记录
 int Rocord[5] = { 0,0,0,0,0 };
 //地图
 int map[40][40];

 //地图的大小Size*Size
 int Size = 10;

 //容错
 int fault_tolerant = 10;

 //困难程度
 int _difficulty=1;

 //初始化
 void reset();

 //画地图
 void drawGrid();

 //查看格子的结果
 void Cheak();

 //判断是否游戏结束
 int isWin();

 //导入最高分记录
 void get_Rocord();

 //导出最高分记录
 void put_Rocord();

 //选择难度
 int Selection_difficulty();

 //加载界面
 void loading();
};

然后是对类的函数的定义

//对Game类的成员函数的定义

#include "扫雷.h"
#include<Windows.h>
#include<iostream>
#include<fstream>
#include<time.h>
#include <conio.h>


#pragma warning(disable:4996) //这一行是为了能在 Visual Studio 2017内使用getch()函数

//定义最高分记录的存储地址
#define RocordPath "D:\\VS/扫雷最高分.txt"

using namespace std;

#define none  "█"


//定义5种情况,有雷和无雷,查看后的三种结果
enum players { Boom, None, Boom1, None1, Show1 };

//定义三种游戏难度
enum _Difficulty{Easy,General,Difficulty,Purgatory};

int D_size[4][2] = { {10,10} ,{15,8},{20,5},{30,3} };


//游戏规则的描述
void Game::rule() {
 loading();
 //清屏
 system("cls");
 cout << "\n\n\n\n";
 cout << "游戏规则:\n\n";
 cout << "1.当查看点为雷时,会显示“*”,并且将扣10分" << endl;
 cout << "2.当差看点不为雷且周围均无雷将显示周围所以格为“ ”(周围指的是相邻的8个格子)" << endl;
 cout << "3.当查看点不为雷,且周围存在雷时,将显示周围雷数" << endl;
 cout << "4.当踩到最大容错个数雷时游戏将失败" << endl;
 cout << "5.当不存在未查阅的非雷格时游戏胜利" << endl;
 cout << "\n\n\n\t\t\t\t30秒后自动退出该界面!";

 Sleep(30000);


}

//退出游戏
int Game::quit() {


 system("cls");
 //定义控制台屏幕初始坐标
 COORD c = { 40, 13 };
 //设置控制台光标位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "游戏结束!!!" << endl;

 Sleep(1000);

 loading();

 return 0;


}


//游戏模式
void Game::play() {
 //导入最高分记录
 get_Rocord();

 while (true) {
 //选择游戏难度
 _difficulty=Selection_difficulty();
 //默认游戏一直进行
 int res = 1;
 //初始化
 reset();
 //
 drawGrid();
 while (true) {

  //查看点
  Cheak();
  //
  drawGrid();


  if (!isWin()) {
  
  if (score > Rocord[_difficulty])Rocord[_difficulty] = score;
  put_Rocord();
  char s;
  cout << "是否再来一局?是(y|Y)/否(n|N)" << endl;
  cin >> s;
  if (s == 'y' || s == 'Y')res = 1;
  else res = 0;
  break;
  }
  
 }
 if (!res)break;
 
 }


}


//更新(初始化)
void Game::reset() {
 //数据初始化
 score = 0;
 error = 0;
 //棋盘初始化
 srand(time(NULL));
 for (int i = 0; i < Size; i++) {
 for (int j = 0; j < Size; j++) {
  int t = rand() % 2;
  if (t==1)map[j][i] = Boom;
  else map[j][i] = None;
  //cout << t<< " ";
 }
 //cout << endl;
 }
 
 
}


//画地图
void Game::drawGrid() {

 //清屏
 system("cls");
 //定义控制台屏幕初始坐标
 COORD c = { 0, 2 };
 //设置控制台光标位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 //棋局初始状态
 for (int i = 0; i <= Size; i++) {
 if (i < 10)
  cout << i << " ";
 else cout << i;
 for (int j = 0; j < Size; j++) {
  if (i == 0) {
  if (j < 9)
   cout << j + 1 << " ";
  else cout << j + 1;
  }
  else cout << none;
 }
 cout << endl;
 }

 for (int y = 0; y < Size; y++) {
 for (int x = 0; x < Size; x++) {
  if (map[x][y] == Boom1|| map[x][y] == None1) {
  //光标位置坐标
  COORD c = { x * 2 + 2, 3 + y };
  //设置控制台光标位置
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle函数获得句柄
  string o;
  if (map[x][y] == Boom1) o = "* ";
  if (map[x][y] == None1) o = " ";
  cout << o;
  }

  if (map[x][y] == Show1) {
  int cnt = 0;
  for (int i = x - 1; i <= x + 1; i++) {
   for (int j = y - 1; j <= y + 1; j++) {
   if (i >= 0 && i < Size && j >= 0 && j < Size) {
    if (map[i][j] == Boom || map[i][j] == Boom1)cnt++;
   }
   }
  }
  //光标位置坐标
  COORD c = { x*2+2, 3 + y };
  //设置控制台光标位置
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle函数获得句柄
  cout << cnt << " ";

  }
 }
 }
 c.Y = Size+3;
 //设置控制台光标位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "当前分数是:"<<score<<"\n最高纪录是:"<<Rocord[_difficulty]<<"\n请输入查看格的坐标" << endl;
 

}

//查看点结果
void Game::Cheak() {
 int x = 0, y = 0;

 cin >> x >> y;
 x -= 1, y -= 1;
 while(map[x][y] == Boom1 || map[x][y] == None1 || map[x][y] == Show1 || x < 0 || x >= Size || y < 0 || y >= Size) {
 //定义控制台屏幕初始坐标
 COORD c = { 0, 2 };
 c.Y = Size+6;
 //设置控制台光标位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "该格以检查过或不在棋盘内,请重新输入" << endl;
 cin >> x >> y;
 x -= 1, y -= 1;
 
 }

 

 if (map[x][y] == Boom) {
 map[x][y] = Boom1;
 score -= 10;
 error++;
 }
 
 else {
 score += 10;
 int cnt = 0; 
 for (int i = x - 1; i <= x + 1; i++) {
  for (int j = y - 1; j <= y + 1; j++) {
  if (i >= 0 && i < Size && j >= 0 && j < Size) {
   if (map[i][j] == Boom || map[i][j] == Boom1)cnt++;
  }
  }
 }
 if (cnt == 0) {
  for (int i = x - 1; i <= x + 1; i++) {
  for (int j = y - 1; j <= y + 1; j++) {
   if (i >= 0 && i < Size && j >= 0 && j < Size) {
   map[i][j] = None1;
   }
  }
  }
 }
 else map[x][y] = Show1;
 }
 
}

//判断是否游戏结束
int Game::isWin() {
 int cnt = 0;
 for (int i = 0; i < Size; i++) {
 for (int j = 0; j < Size; j++) {
  if (map[i][j] == None)cnt++;
 }
 }

 if (cnt == 0) {
 system("cls");
 //定义控制台屏幕初始坐标
 COORD c = { 50, 15 };
 //设置控制台光标位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "You Win!!!" << endl;
 return 0;
 }
 else if (error >= fault_tolerant) {
 system("cls");
 //定义控制台屏幕初始坐标
 COORD c = { 50, 15 };
 //设置控制台光标位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "You Loss!!!" << endl;
 return 0;
 }
 else return 1;

}

//导入最高分记录
void Game::get_Rocord() {

 ifstream fin(RocordPath, ios::in);
 for (int i = 0; i < 5; i++) {
 fin >> Rocord[i];
 }
}

//导出最高分记录
void Game::put_Rocord() {

 ofstream fout(RocordPath, ios::out);
 for(int i=0;i<5;i++)
 fout << Rocord[i] << endl;
}

//选择难度
int Game::Selection_difficulty() {
 //清屏
 system("cls");
 //定义控制台屏幕初始坐标
 COORD c = { 0, 6 };
 //设置控制台光标位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 
 cout << "\t\t\t\t\t\t1.简单  (10*10格 10容错)\n\n" << endl;
 cout << "\t\t\t\t\t\t2.一般  (15*15格 8容错)\n\n" << endl;
 cout << "\t\t\t\t\t\t3.困难  (20*20格 5容错)\n\n" << endl;
 cout << "\t\t\t\t\t\t4.炼狱  (30*30格 3容错)\n\n" << endl;
 cout << "\t\t\t\t\t\t5.自定义\n\n" << endl; 
 cout << "\t\t\t\t\t\t请选择游戏难度:";
 int t = 1;
 cin >> t;
 while (t < 1 || t>5) {
 COORD c = { 0, 21 };
 //设置控制台光标位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "\t\t\t\t\t\t输入错误请重新输入:" << endl;;
 cin >> t;
 }
 switch (t) {
 case 1:Size = D_size[Easy][0], fault_tolerant = D_size[Easy][1]; break;
 case 2:Size = D_size[General][0], fault_tolerant = D_size[General][1]; break;
 case 3:Size = D_size[Difficulty][0], fault_tolerant = D_size[Difficulty][1]; break;
 case 4:Size = D_size[Purgatory][0], fault_tolerant = D_size[Purgatory][1]; break;
 case 5: {
 //清屏
 system("cls");
 cout << "\n\n\n\n\n\t\t\t\t请输入地图尺码和最多踩雷失败数    (尺码在10-30,容错在10以内)";
  cout << "\t\t\t\t\t\t\t\t\t尺码:";
  cin >> Size;
  cout << "\n\t\t\t\t\t容错:";
  cin >> fault_tolerant;
 }break;
 }
 loading();
 return t;

}


void Game::loading() {

 COORD c = { 50,15 };
 //设置控制台光标位置
 int t = 6;
 while (t--) {
 system("cls");
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 if(t%3==0)
  cout << "loading..." << endl;
 if (t % 3 == 1)
  cout << "loading.." << endl;
 if (t % 3 == 2)
  cout << "loading." << endl;
 Sleep(500);
 }


}

最后就是主函数部分

//扫雷游戏的主函数

#include<iostream>
#include<Windows.h>
#include"扫雷.h"
using namespace std;
int main() {
 Game game;
 while (true) {
 int t, g = 1;
 system("cls");
 //定义控制台屏幕初始坐标
 COORD c = { 30, 10 };
 //设置控制台光标位置
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
 cout << "欢迎来到 扫雷!!!\n\n\n\n\n\n";
 cout << "\t\t\t\t\t1.开始游戏\n\n\n\t\t\t\t\t2.阅读规则\n\n\n\t\t\t\t\t3.退出" << endl;
 cin >> t;
 switch (t) {
 case 1:game.play(); break;
 case 2:game.rule(); break;
 case 3:g=game.quit(); break;
 }
 if (g == 0)break;
 }
 return 0;
 
}

这是第一次写博客 也是第一次独立完成项目,有不足的地方,希望各位大牛指教。

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

更多精彩内容其他人还在看

用标准c++实现string与各种类型之间的转换

这个类在头文件中定义, < sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本
收藏 0 赞 0 分享

C++如何通过ostringstream实现任意类型转string

再使用整型转string的时候感觉有点棘手,因为itoa不是标准C里面的,而且即便是有itoa,其他类型转string不是很方便。后来去网上找了一下,发现有一个好方法
收藏 0 赞 0 分享

C/C++指针小结

要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占据的内存区
收藏 0 赞 0 分享

C++ 类的静态成员深入解析

在C++中类的静态成员变量和静态成员函数是个容易出错的地方,本文先通过几个例子来总结静态成员变量和成员函数使用规则,再给出一个实例来加深印象
收藏 0 赞 0 分享

C++类的静态成员初始化详细讲解

通常静态数据成员在类声明中声明,在包含类方法的文件中初始化.初始化时使用作用域操作符来指出静态成员所属的类.但如果静态成员是整型或是枚举型const,则可以在类声明中初始化
收藏 0 赞 0 分享

C++类静态成员与类静态成员函数详解

静态成员不可在类体内进行赋值,因为它是被所有该类的对象所共享的。你在一个对象里给它赋值,其他对象里的该成员也会发生变化。为了避免混乱,所以不可在类体内进行赋值
收藏 0 赞 0 分享

C++中的friend友元函数详细解析

友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类。友元函数的特点是能够访问类中的私有成员的非成员函数。友元函数从语法上看,它与普通函数一样,即在定义上和调用上与普通函数一样
收藏 0 赞 0 分享

static全局变量与普通的全局变量的区别详细解析

以下是对static全局变量与普通的全局变量的区别进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助
收藏 0 赞 0 分享

C++ explicit关键字的应用方法详细讲解

C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢?下面就让我们一起来看看这方面的知识吧
收藏 0 赞 0 分享

教你5分钟轻松搞定内存字节对齐

随便google一下,人家就可以跟你解释的,一大堆的道理,我们没怎么多时间,讨论为何要对齐.直入主题,怎么判断内存对齐规则,sizeof的结果怎么来的,请牢记以下3条原则
收藏 0 赞 0 分享
查看更多