C++写扫雷(控制台版)

本人软工学生一枚,学艺不精。有不足之处请谅解 ^ _ ^
有什么问题还希望您能多批评指正,不吝指教。
蟹蟹你来看鸭~~

1.问题描述

用c++写一个扫雷小游戏,扫雷大家都玩过吧,先任意点一个方格,没有爆炸时,会出现一个数字,这个数字是以它为中心的9个格子内所有雷的个数。一般围在一堆数字中间的有可能是雷,你在你认为是雷的那里右击,就可以把它设定为雷,然后在数字区用鼠标左右键双击,可以打开非雷区,所有雷被标记后,就赢了。
今天我们写的程序需要能实现以下几个功能

(1).输入坐标打开一个格子,此格子若是雷则游戏结束,若不是则显示周围雷的个数。

(2).输入坐标为格子插上棋子和取消插旗子。

2.设计思路

(1)创建两个数组,一个是开发者数组,一个是玩家数组。生成两个界面,开发者界面显示雷和数字,玩家界面显示问号和数字。
(2)初始化两个雷阵,然后用随机数布雷。
(3)开始游戏,点到不是雷的地方将周围无雷的地方展开,如果点到雷游戏结束。

其他详细内容见代码

3.上代码

#include "pch.h"
#include <iostream>
#include <stdlib.h> 
#include<cstdlib>
#include<ctime>
using namespace std;

int shuzu1[12][12];
char show[12][12];


void wjm()
{
	cout << "  1     2     3     4     5     6     7     8     9    10   " << endl << endl;

	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[1][j] << "  |";

	}
	cout << "   1" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[2][j] << "  |";

	}
	cout << "   2" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[3][j] << "  |";
 
	}
	cout << "   3" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[4][j] << "  |";

	}
	cout << "   4" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[5][j] << "  |";

	}
	cout << "   5" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[6][j] << "  |";

	}
	cout << "   6" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[7][j] << "  |";

	}
	cout << "   7" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[8][j] << "  |";

	}
	cout << "   8" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[9][j] << "  |";

	}
	cout << "   9" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << shuzu1[10][j] << "  |";

	}
	cout << "   10" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;

}
//开发者界面
void first()//初始化
{
	for (int i = 0; i < 12; i++)
	{
		for (int j = 0; j < 12; j++)
		{
			shuzu1[i][j] = 0;//开发者数组
			
		}
	}
	for (int i = 0; i < 12; i++)
	{
		for (int j = 0; j <12; j++) 
		{
			show[i][j] = '?';//玩家数组
		}
	}
}
//初始化两个雷阵
void jm()//界面
{
	cout << "  1     2     3     4     5     6     7     8     9    10   " << endl << endl;
	
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[1][j] << "  |";

	}
	cout << "   1" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[2][j] << "  |";

	}
	cout << "   2" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[3][j] << "  |";

	}
	cout << "   3" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[4][j] << "  |";

	}
	cout << "   4" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[5][j] << "  |";

	}
	cout << "   5" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[6][j] << "  |";

	}
	cout << "   6" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[7][j] << "  |";

	}
	cout << "   7" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[8][j] << "  |";

	}
	cout << "   8" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[9][j] << "  |";

	}
	cout << "   9" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
	for (int j = 1; j < 11; j++)
	{
		cout << "  " << show[10][j] << "  |";

	}
	cout << "   10" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;

	cout << '\n' << "选项" << '\n' << "提示:输入横坐标后回车再输入纵坐标\n" << "1-点击(x,y)" << '\n' << "2-在(x,y)插旗子" << '\n' << "3-取消插旗子(x,y)" << '\n' << "4-老子不玩了" << endl;
}
//玩家界面
void bulei()
{
	srand(time(NULL));
	for (int a=0; a <10; a++)//生成10个雷
	{
		int m = rand() % 10 + 1;
		int n = rand() % 10 + 1;
		if (shuzu1[m][n] != 9)
		{
			shuzu1[m][n] = 9;
		}
	}
	
	


}
//布雷
void number()
{
	int count = 0;
	for (int x = 1; x < 11; x++)
	{
		for (int y = 1; y < 11; y++)
		{
			if (shuzu1[x][y] == 9)
			{
				if(shuzu1[x - 1][y - 1]!=9)
				shuzu1[x - 1][y-1]++;
				if(shuzu1[x - 1][y]!=9)
				shuzu1[x - 1][y]++;
				if(shuzu1[x - 1][y + 1]!=9)
				shuzu1[x - 1][y + 1]++;
				if(shuzu1[x][y - 1]!=9)
				shuzu1[x][y - 1]++;
				if (shuzu1[x][y + 1] != 9)
				shuzu1[x][y + 1]++;
				if (shuzu1[x + 1][y - 1] != 9)
				shuzu1[x + 1][y - 1]++;
				if (shuzu1[x + 1][y] != 9)
				shuzu1[x + 1][y]++;
				if (shuzu1[x + 1][y + 1] != 9)
				shuzu1[x + 1][y + 1]++;
			}
		}
	}
		
}
//生成数字
void unfold(int x,int y)
{
	if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
	{
		if (shuzu1[x][y] == 0)
		{
			show[x][y] = ' ';
			if (show[x][y + 1] == '?')
				unfold(x, y + 1);
			if (show[x][y - 1] == '?')
				unfold(x, y - 1);
			if (show[x + 1][y] == '?')
				unfold(x + 1, y);
			if (show[x - 1][y] == '?')
				unfold(x - 1, y);
			
		}
		if (shuzu1[x][y] != 0 && shuzu1[x][y] != 9)
		{
			show[x][y] = shuzu1[x][y] + '0';
		}
	}
		
}    
//无雷展开
void flag(int x, int y)
{
	show[x][y] = 'F';
	jm();
}
//插旗子
void unflag(int x, int y)
{
	if (show[x][y] == 'F')
	{
		show[x][y] = '?';
		jm();
	}
	else 
	{
		cout << "错误";
	}
}
//取消插旗子
void start(int x,int y)
{
	if (shuzu1[x][y] == 9)
	{
		cout << "你输了";
		exit(0);
	}
	if (shuzu1[x][y] != 9 && shuzu1[x][y] != 0)
	{
		show[x][y] = shuzu1[x][y]+'0';
		jm();
	}
	if (shuzu1[x][y] == 0)
	{
		unfold(x, y);
		jm();
	}

}
//展开格子
void end()
{
	int count = 0;
	for (int i = 1; i <= 10; i++)
	{
		for (int j = 1; j <= 10; j++)
		{
			if (show[i][j] == '?'||show[i][j]=='F')
			{
				count++;
			}
		}

	}
	if (count == 10)
	{
		cout << "你赢了";
		exit(0);
	}
	
	
}
//结束游戏



int main()
{
	int x = 5;
	int y = 8;
	int z;
	first();
	bulei();
	number();
	jm();
	for (;;)
	{
		cin >> z;
		switch (z)
		{
			case 1:
			{
			cin >> x >> y;
				if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
				{
					start(x, y);
				}
				else
				{
					cout << "错误"; break;
					
				}
		
			}break;
			case 2:
			{
				cin >> x >> y;
				if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
				{
					flag(x, y);
				}
				else
				{
					cout << "错误";
				}
			}break;
			case 3:
			{
				cin >> x >> y;
				if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
				{
					unflag(x, y);
				}
				else
				{
					cout << "错误";
				}
			}break;
			case 4:
			{
				exit(0);

			}break;
		}
		end();
	}

}


4.运行结果部分截图

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值