简单版的扫雷游戏

	在此扫雷源文件编写中,我采用了二维数组来做(根据需要可以在类的对象数组定义中改变N的值来改变游戏范围),同时此处定义N时,要将其定义为静态成员,不然下面的数组处将会报错(非静态成员引用必须与特定对象相对)。
#include<iostream>
#include<iomanip>        //调用库函数setw()
#include<ctime>
#include<cstdlib>         
#include<vector>
#include<stdlib.h>
using namespace std;

class Gamesl {
	static const int N = 6;
	char map[N][N];
	
	
	
public:vector<int>result;              //定义一个容器,后面用它来储存非雷区的方格编号
	int sum = 0;                        //累加分数
	int get_N() const { return N; }    //获得类中私有成员的数据
	void ditu();                     //打印刚开始的游戏地图
	void zhongleiditu();             //打印中雷后的地图
	void dilei();                     //初始化地雷函数
	int isbomb(int b);                 //判断方格是否为地雷函数
	void notbomb(int b);              //找出此方格周围所连的非雷区的函数
	int sore(int i);                  //计算得分函数
	int end();                         //游戏结束函数
	void winditu();                   //打印游戏结束地图

};

void Gamesl::ditu() {             //打印初始化地图函数(带边框)
	for (int i = 0; i < N; ++i) {           //输出每行上面的边框
		cout << "+";
		for (int i = 0; i < N; ++i)cout << "---" << "+";
		cout << endl; cout << "|";
		for (int j = 0; j < N; ++j) {
			 cout << setw(3) << i*N+j << "|";
		}
		cout << endl;
		if (i == N - 1) {                   //输出最后一行下边的边框
			cout << "+";
			for (int i = 0; i < N; ++i)cout << "---" << "+";
		}
	}
}      

void Gamesl::zhongleiditu() {             //打印中雷后的地图函数(带边框)
	cout << setw(3) << " ";
	for (int w = 0; w < N; ++w)cout << "   " << w;                 //输出上面编号的标号
	cout << endl;
	for (int i = 0; i < N; ++i) {           //输出每行上面的边框
		cout << setw(3) << " ";
		cout << "+";
		for (int i = 0; i < N; ++i)cout << "---" << "+";
		cout << endl;
		cout << setw(3) << i;
		cout << "|";
		for (int j = 0; j < N; ++j) {
			if (map[i][j] == '*')cout << setw(3) << "*" << "|";
			else cout << setw(3) << i * N + j << "|";

		}
		cout << endl;
		if (i == N - 1) {                   //输出最后一行下边的边框
			cout << setw(3) << " ";
			cout << "+";
			for (int i = 0; i < N; ++i)cout << "---" << "+";
		}
	}
}

void Gamesl::dilei() {               //初始化地雷函数   
	double m_rate = double(N * N) / 100;   
	srand((unsigned)time(NULL));                     
	for (auto& r : map) {
		for (auto& c : r) {
			double rate = static_cast<double>(rand() % 100)/100 ;        //生成0-1内的随机数
			
			if (rate <= m_rate) c = '*';                  //方格为地雷的概率为36%
			//   或者是 c = rand() % 100 < (N*N) ? '*' : ' ';
			
		}
	}

} 

int Gamesl::isbomb(int b) {                 //判断方格是否为雷函数 
	//int i = b / 10, j = b - 10 * i;    //计算出输入的方格在数组中的下标,也可以是:b/N,b%N
	
	if (map[b/N][b%N] == '*') {                 //选的方格为地雷时
		cout << "恭喜您中地雷了!" << endl;
		cout << "Game Over." << endl;
		zhongleiditu();             //打印地雷图
		
		cout << "最终得分为" << sum <<"分"<< endl;
		return 1;
		
	}
	else {                                //选的方格为非雷区时
		notbomb(b);
		return 0;
		
		
	}
}

void Gamesl::notbomb(int b) {                 //显示出次方格附近联通的非雷区,并显示出其四周的雷数的函数
	
    vector<int>vi (1,b);            //容器用来储存浏览区
	     //寻找附近的无雷区并标记其周围的雷个数
	do { 
		 b = vi.front();

		               //b方格上方的方格编号     b方格左边的方格编号         b方格下面的编号                b方格右边的方格编号
		int besides[]{ (b / N > 0) ? b -N: -1,  (b % N > 0) ? b - 1 : -1,  (b / N < N - 1) ? b +N : -1,   (b % N < N - 1) ? b + 1 : -1 };       //用一个数组来存放所选的b方格的四周的方格编号,没有对应的编号就返回-1
		for (auto &l : besides) {             //用指针来浏览数组元素
			if (l != -1 && map[l / N][l % N] != '*' && map[l / N][l % N] != '1') {            //判断四周的方格无雷的操作
				
				vi.push_back(l);                       //把周围的非雷的方格都放到一个容器里
				map[l / N][l % N] = '1';                //浏览过的方格做好标记以免下次还会再访问

			}
			
		}
		result.push_back(b);     //把处理过的方格编号放到一起(其中也就是无雷的方格编号)

		vi.erase(vi.begin());     //每执行一次循环,就把之前浏览过的方格删掉

	} while (!vi.empty()); //或者vi.size()!=0      //循环,当周围的非雷区都找出来后退出循环
	
	
	int r = 0;
	++r;
	sum = result.size()-r;
	sore(sum);            //显示当前的得分情况
	
	for (auto it = result.begin(); it != result.end(); ++it) {         //浏览非雷区并输出其周围的雷数                   
		int neibor[]{ ((*it) / N > 0) ? (*it) - N : -1,  ((*it) % N > 0) ? (*it) - 1 : -1,  ((*it) / N < N - 1) ? (*it) + N : -1,   ((*it) % N < N - 1) ? (*it) + 1 : -1 };
		int a = 0;
		for (auto& h : neibor) {
			if (h != -1 && map[h/N][h%N]=='*') {
				++a;             //计算无雷的方格四周有几个雷
			}
		}
		if (a == 0)map[(*it) / N][(*it) % N] = '0';
		if (a == 1)map[(*it) / N][(*it) % N] = '1';
		if (a == 2)map[(*it) / N][(*it) % N] = '2';
		if (a == 3)map[(*it) / N][(*it) % N] = '3';          //把这个格子周围有几个雷数赋值给这个格子
		if (a == 4)map[(*it) / N][(*it) % N] = '4';
	

	}
	cout << endl;
										
	//打印标记后的非雷区
	cout << setw(3) << " ";
	for (int w = 0; w < N; ++w)cout << "   " << w;                 //输出上面编号的标号
	cout << endl;

	for (int i = 0; i < N; ++i) {           //输出每行上面的边框
		cout << setw(3) << " ";
		cout << "+";
		for (int i = 0; i < N; ++i)cout << "---" << "+";
		cout << endl;
		cout << setw(3) << i;
		cout << "|";
		for (int j = 0; j < N; ++j) {

			if (map[i][j] == '0' || map[i][j] == '1' || map[i][j] == '2' || map[i][j] == '3' || map[i][j] == '4')
				cout << setw(3) << map[i][j] << "|";                    //打印出标记的方格
			else cout << setw(3) << "$" << "|";

		}
		cout << endl;
		if (i == N - 1) {                   //输出最后一行下边的边框
			cout << setw(3) << " ";
			cout << "+";
			for (int i = 0; i < N; ++i)cout << "---" << "+";
		}
	}
	


	

}


int Gamesl::sore(int i) {                  //得分函数
	cout << "目前的得分是:"<<i<< "分"<<endl;
	return i;
}

int Gamesl::end() {                   //游戏结束判断函数
	int s = 0;
	for (auto& i : map) {             //计算此次游戏中的非雷格数
		for (auto& j : i) {
			if (j == '*') {
				++s;
			}
		}
	}
	int number = N * N - s;
	if (sore(sum) == number) {            //当得分等于非雷格数时,游戏结束
		
		cout << "你真NICE!"<<endl;
		cout << "游戏结束"<<endl;
		cout << "最终得分为" << number << "分"<<endl;
		
		return 1;
	}
	else return 0;

}


void Gamesl::winditu() {                  //赢得游戏后的地图
	cout << endl;

	//打印标记后的非雷区
	cout << setw(3) << " ";
	for (int w = 0; w < N; ++w)cout << "   " << w;                 //输出上面编号的标号
	cout << endl;

	for (int i = 0; i < N; ++i) {           //输出每行上面的边框
		cout << setw(3) << " ";
		cout << "+";
		for (int i = 0; i < N; ++i)cout << "---" << "+";
		cout << endl;
		cout << setw(3) << i;
		cout << "|";
		for (int j = 0; j < N; ++j) {

			if (map[i][j] == '0' || map[i][j] == '1' || map[i][j] == '2' || map[i][j] == '3' || map[i][j] == '4')
				cout << setw(3) << map[i][j] << "|";                    //打印出标记的方格
			else cout << setw(3) << "*" << "|";

		}
		cout << endl;
		if (i == N - 1) {                   //输出最后一行下边的边框
			cout << setw(3) << " ";
			cout << "+";
			for (int i = 0; i < N; ++i)cout << "---" << "+";
		}
	}
}

int main() {
	
	Gamesl t; 
	t.ditu();                   //先打印地图
	
	int m;                     //输入要选择的方格
	int max = t.get_N() * t.get_N() - 1;
	cout << endl<< "请输入方格的编号(0<=m<="<<max<<"),m=";
	cin >> m;
	system("CLS");               //清掉上面打印的初始地图
	t.dilei();                 //初始化地雷
	t.isbomb(m);               //判断地雷
	


	while (1) {                            //在此处进行循环,输入接下来要查询的方格编号,当end()返回0时则找到所以非雷区
		if (t.end()) {                    //第一次执行完就判断是否结束游戏
			system("CLS");
			t.end();
			t.winditu();
			break;
		}                              
			if (t.sum == 0)break;                    //如果刚开始就猜中地雷那么直接退出循环
			int n;                                   //再次输入要选择的方格编号(此处不包括之前的非雷区的方格数)
			cout << endl << "请输入方格的编号=";
			cin >> n;

			int p = 0;
			for (auto it = t.result.begin(); it != t.result.end(); ++it) {                  //判断输入的方格编号是否为上次查找的非雷区内的方格编号
				if (n == *it) {
					++p;
				}
				
			}
			if (p == 1)continue;                        //防止重复输入相同或者已经查找过的方格编号
			system("CLS");
			if (t.isbomb(n))break;                   //当再次选中地雷就返回1,跳出循环
		
			if (t.end()) {                    //判断是否结束游戏
				system("CLS");                //结束清掉前面的过程
				t.end();                      //再次输出赢得游戏的语句和分数(因为上面的清屏把if语句中执行过的end()的语句清掉了)
				t.winditu();                  //打印出赢得游戏的地图
				break;
			}                  
				
		                      
			

		}
	
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值