C++实现扫雷游戏

头文件Mine_Sweep.h

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <Windows.h>
using namespace std;

typedef pair<int, int> P;
const int M = 10;
const int N = 10;

void InitGame(int A[M][N]);//初始化游戏
void Map(int A[M][N], int Static[M][N]);//地图
void Result(int A[M][N]);//游戏结果
int IfSafety(int A[M][N],int x,int y);//周围是否有地雷
void Operate(int A[M][N],int Static[M][N], int x, int y);   //操作产生的影响
void Welcome();    //欢迎界面
void GameOver();   //结束界面
void gotoxy(int x, int y);         //指针位置
void color(int c);       //改变颜色

实现部分

#include"Mine_Sweep.h"

void InitGame(int A[M][N])
{
	//从右开始顺时针->右(1,0)右上(1,1)上(0,1)左上(-1,1)左(-1,0)左下(-1,-1)下(0,-1)右下(1,-1)
	int Sign[8][2] = { {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1} };
	int count = 0;//产生M个不同随机数
	int Random;   //随机数1-100
	int x, y;     //横纵坐标
	srand(unsigned(time(NULL)));
	//清空
	for (int i = 0; i < M; i++)
		for (int j = 0; j < N; j++)
			A[i][j] = 0;
	//把M个雷安放好
	while (count < M)
	{
		Random = rand() % 100 + 1;
		x = Random / 10;     //转换为X
		y = Random - 10 * x; //转换为Y
		if (A[x][y] == 0)       //地雷设为-1,无地雷设为0,1,2...
		{
			A[x][y] = -1;
			count++;
		}
	}
	//构建一个虚拟的地图即增加外围[M][N]->[M+2][N+2]目的是为了让边界和内部的处理方式一样
	int Virtual[M + 2][N + 2] = { 0 };
	for (int i = 0; i < M; i++)
	{
		for (int j = 0; j < N; j++)
		{
			Virtual[i + 1][j + 1] = A[i][j];//所有元素向下向右移动1格
		}
	}
	//计算每个地方的地雷分布情况
	for (int i = 1; i <= M; i++)
	{
		for (int j = 1; j <= N; j++)
		{
			int flag = 0;
			//如果是地雷
			if (Virtual[i][j] == -1)
				continue;     //跳过
			for (int k = 0; k < 8; k++)
			{
				if (Virtual[i + Sign[k][0]][j + Sign[k][1]]==-1)
					flag++;
			}
			A[i-1][j-1] = flag;
		}
	}
}

void Map(int A[M][N],int Static[M][N])
{
	system("cls");
	Welcome();
	//地雷用※表示,数字用123...,未翻开用■ Static[M][N]用于储存状态0表示未翻开,1表示翻开
	for (int i = 0; i < M; i++)
	{
		cout  << "\t\t\t\t\t"<<" ";
		for (int j = 0; j < N; j++)
		{
			if (Static[i][j] == 0)
				cout << "■";
			else
			{
				if (A[i][j] == -1)
				{
					cout << "※";
					
				}
				else
				{
					if (IfSafety(A, i, j))
						cout << "□";
					else
						cout <<" " <<A[i][j];
				}
			}
		}
		cout << endl;
	}
}

void Operate(int A[M][N], int Static[M][N],int x,int y)//贪心算法
{
	queue<P> que;//队列
	//从右开始顺时针->右(1,0)右上(1,1)上(0,1)左上(-1,1)左(-1,0)左下(-1,-1)下(0,-1)右下(1,-1)
	int Sign[8][2] = { {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1} };
	if (A[x][y] == -1)  //如果翻到地雷,游戏结束
	{
		Result(A);   //显示结果
		GameOver();           //游戏结束
		exit(0);
	}
	Static[x][y] = 1;//翻开
	que.push(P(x, y));

	while (que.size()) {
		P p = que.front();
		que.pop();
		if(!IfSafety(A, p.first, p.second))
			continue;    //如果周围有炸弹,继续遍历队列
		
		for (int i = 0; i < 8; i++) {
			int nx = p.first + Sign[i][0];
			int ny = p.second + Sign[i][1];
			if (0 <= nx && nx < M && 0 <= ny && ny < N && Static[nx][ny] == 0) {
				que.push(P(nx, ny));
				Static[nx][ny] = 1;//翻开
			}
		}
	}
}
void Result(int A[M][N])
{
	for (int i = 0; i < M; i++) {
		cout << "\t\t\t\t\t";
		for (int j = 0; j < N; j++)
			cout << A[i][j]<<" ";
		cout << endl;
	}
	
}

int IfSafety(int A[M][N],int x, int y)
{
	//从右开始顺时针->右(1,0)右上(1,1)上(0,1)左上(-1,1)左(-1,0)左下(-1,-1)下(0,-1)右下(1,-1)
	int Sign[8][2] = { {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1} };
	int Virtual[M + 2][N + 2] = { 0 };
	x = x + 1;//虚拟坐标
	y = y + 1;
	for (int i = 0; i < M; i++)
	{
		for (int j = 0; j < N; j++)
		{
			Virtual[i + 1][j + 1] = A[i][j];//所有元素向下向右移动1格
		}
	}
	//计算每个地方的地雷分布情况
	for (int k = 0; k < 8; k++)
	{
		if (Virtual[x + Sign[k][0]][y + Sign[k][1]] == -1)
			return 0;
	}
	return 1;
}

void Welcome()
{
	cout << "\t\t\t\t\t\t扫雷游戏"<<endl<<endl;
	cout << "\t\t\t\t===========================================" << endl << endl;
	gotoxy(40, 3);
	for (int i = 0; i < M; i++)
	{
		//画x轴坐标
		cout << " " << i + 1;
	}
	
	for (int j = 0; j < M; j++)
	{
		//画Y轴坐标
		gotoxy(38, 4+j);
		cout << j + 1<<endl;
	}
	gotoxy(0, 4);
}

void GameOver()
{
	cout << "\t\t\t\t游戏结束,你输了" << endl;
	getchar();
	getchar();
}

void gotoxy(int x, int y)
{
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void color(int c)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
}

主函数

#include"Mine_Sweep.h"

int main()
{
	int A[M][N], x, y, Static[M][N] = { 0 };
	color(13);
	InitGame(A);
	cout << "\n\n\n\t\t游戏说明:" << "你需要在不点错雷的情况下尽可能快的将所有的雷都标记出来\n";
	cout << "\t\t你点出了一个数字,是1,就说明它周围的8的格子里有1个雷,是2就有两个雷,是3就有三个雷···" << endl;
	cout<< "\t\t但如果你标记错了雷,那就会boom!游戏结束。"<<endl;
	cout << "\t\t请先输入横坐标,再输入纵坐标 eg: 1 2" << endl;
	cout << "\t\t回车键开始,祝你好运!" << endl;
	getchar();
	color(15);
	while (1)
	{
		int flag = 1;
		Map(A, Static);
		cout << "\t\t\t\t请输入要翻开的坐标:";
		cin >> x >> y;
		while (x <= 0 || x > M || y <= 0 || y > N)
		{
			cout << "\t\t\t\t超出范围,请重新输入要翻开的坐标:" ;
			cin >> x >> y;
		}
		Operate(A, Static, x-1, y-1);
	}
	return 0;
}

运行效果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Who_Am_I.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值