C/C++实现扫雷小游戏

C/C++实现扫雷

  • 对于扫雷小游戏来说,相信很多C/C++的初学者都希望能会这一款游戏

  • 本人是用C++写的,如果想用C语言写的小伙伴对应修改即可

  • 我们先梳理思路

  • 1.初始化棋盘,这里需要两个棋盘,玩过扫雷游戏的玩家应该知道,给你的界面是*****这样的,需要你自己去扫点,但对于设计这款游戏的人来说,是已经随机布置了多少个雷的。

  • 2.所以先初始化设计者棋盘 和 玩家棋盘

  • 设计者棋盘如下:

    在这里插入图片描述

  • 玩家棋盘如下:在这里插入图片描述

***初始化棋盘***
void Myboard::InitBoard(char board[ROWS][COLS],int row,int col,char ret)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ret;  // 初始化放*  0
		}
	}
}
注意红框外面的是行列坐标
  • 3.看到设计者棋盘跟玩家棋盘,我们应该知晓 棋盘是10*10的格子。外面还有行列坐标,所以我们的行和列应该设置为ROWS:11,COLS:11。
***打印棋盘***
void Myboard::DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		cout << i << "  ";
	}
	cout << endl;
	for (i = 1; i < row-1; i++)
	{
		cout << i << "  ";
		for (j = 1; j < col; j++)
		{
			cout << board[i][j] << "  ";
		}
		cout << endl;
	}
	cout << 10 << " ";
	for (i = 1; i < row; i++)
	{
		cout << board[10][i] << "  ";
	}
	cout << endl;
}
  • 4.初始化之后设计者棋盘布雷,1表示雷。
***随机在设计者棋盘上布置雷,这里我布雷的10个雷***
void Myboard::Set_mine(char board[ROWS][COLS])
{
	int x = 0, y = 0;
	srand((unsigned)time(NULL)); //随机数种子
	int count = Count;
	while (count)
	{
		x = rand() % 10 + 1;  // 产生1-10的随机数填入棋盘中
		y = rand() % 10 + 1;
		if (board[x][y] =='0')
		{
			board[x][y] = '1';
		}
		count--;
	}
}
  • 5.扫雷的时候第一次一般不会被炸死,所以这里应该设置第一次不被炸死的情况
void Myboard::First_safe()
{
	int x = 0;
	int y = 0;
	char ch = 0; // 显示周围雷的个数
	int set = 1;
	cout << "请输入坐标扫雷" << endl;
	while (1)
	{
		cin >> x >> y;
		if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
		{
			if (menu[x][y] == '1')  // 第一次被雷炸死
			{
				menu[x][y] = '0';
				ch = count_mine(x, y);
				show[x][y] = ch + '0'; //展示给玩家看的,ch为多少,就代表这个点周围有多少雷
				open_mine(x, y);  // 周围坐标,,那个坐标没有雷就展开那个坐标的周围
				while (set)
				{
					int x = rand() % 10 + 1;
					int y = rand() % 10 + 1;
					// 因为第一次被雷炸死了,所以就要重新随机设置没有雷的地方为雷
					if (menu[x][y] == '0')
					{
						menu[x][y] = '1';
						set--;
						break;
					}
				}break; // 跳出函数
			}
			if (menu[x][y] == '0')
			{
				ch = count_mine(x, y);
				show[x][y] = ch + '0';
				open_mine(x, y);
				break;
			}
		}
		else
		{
			cout << "输入错误,请重新输入!" << endl;
		}
	}
}
  • 6.如果扫的坐标点的周围的八个点没有雷,就统计雷的个数,判断玩家是否胜利的条件
int Myboard::count_mine(int x, int y)
{
	int count = 0;
	if (menu[x - 1][y - 1] == '1')
		count++;
	if (menu[x - 1][y] == '1')
		count++;
	if (menu[x - 1][y + 1] == '1')
		count++;
	if (menu[x][y - 1] == '1')
		count++;
	if (menu[x][y + 1] == '1')
		count++;
	if (menu[x + 1][y - 1] == '1')
		count++;
	if (menu[x + 1][y] == '1')
		count++;
	if (menu[x + 1][y + 1] == '1')
		count++;
	return count;
}
  • 7.如果扫雷周围的八个点没有雷,那么继续检测这八个点的周围是否有雷,使用递归
void Myboard::open_mine(int x,int y)
{
   if (menu[x - 1][y - 1] == '0')
   	show[x - 1][y - 1] = count_mine(x - 1, y - 1) + '0';

   if (menu[x - 1][y] == '0')
   	show[x - 1][y] = count_mine(x - 1, y) + '0';

   if (menu[x - 1][y + 1] == '0')
   	show[x - 1][y + 1] = count_mine(x - 1, y + 1) + '0';

   if (menu[x][y - 1] == '0')
   	show[x][y - 1] = count_mine(x, y - 1) + '0';

   if (menu[x][y + 1] == '0')
   	show[x][y + 1] = count_mine(x, y + 1) + '0';

   if (menu[x + 1][y - 1] == '0')
   	show[x + 1][y - 1] = count_mine(x + 1, y - 1) + '0';

   if (menu[x + 1][y] == '0')
   	show[x + 1][y] = count_mine(x + 1, y) + '0';

   if (menu[x + 1][y + 1] == '0')
   	show[x + 1][y + 1] = count_mine(x + 1, y + 1) + '0';
}
  • 8.最后进行扫雷
int Myboard::Sao_mine()
{
   int x = 0;
   int y = 0;
   char ch = 0;
   cout << "请输入坐标扫雷\n";
   cin >> x >> y;
   if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
   {
   	if (menu[x][y] == '0')  
   	{
   		ch = count_mine(x, y);
   		show[x][y] = ch + '0';
   		open_mine(x, y);  // 周围坐标,,那个坐标没有雷就展开那个坐标的周围
   		if (Count_show_mine() == Count)
   		{
   			DisplayBoard(show, ROWS, COLS);
   			cout << "玩家赢" << endl;
   			return 0;
   		}
   	}
   	else if(menu[x][y]=='1')
   	{
   		return 1;
   	}
   }
   return 0;
}
  • 8.胜利判断条件,如果最后剩余的*等于设置的雷数,则玩家胜利
int Myboard::Count_show_mine()
{
   int count = 0;
   int i = 0;
   int j = 0;
   for (i = 1; i < ROWS; i++)
   {
   	for (j = 1; j < COLS; j++)
   	{
   		if (show[i][j] == '*')
   		{
   			count++;
   		}
   	}
   }
   return count;
}
//头文件展示
#pragma once
#include<iostream>
using namespace std;
#include<ctime>
#define ROWS 11
#define COLS 11
#define Count 10  // 雷个数
class Myboard
{
public:
	char menu[ROWS][COLS]; // 设计者棋盘
	char show[ROWS][COLS]; // 玩家棋盘
	// 1.初始化棋盘
	void InitBoard(char board[ROWS][COLS], int row, int col, char ret);

	// 2.打印棋盘
	void DisplayBoard(char board[ROWS][COLS], int row, int col);

	// 3.雷个数
	void Set_mine(char board[ROWS][COLS]);

	// 4.统计坐标周围雷个数
	int count_mine(int x, int y);

	// 5.避免第一次被炸死
	void First_safe();

	// 6.如果周围的八个坐标都没有雷,则继续展开  可以使用递归
	void open_mine(int x, int y);

	// 7.扫雷
	int Sao_mine();

	// 8.判断未知区域剩余个数
	int Count_show_mine();
};
// 主函数展示
#include"game.h"
void menu()
{
	cout << "***********************" << endl;
	cout << "******   1.play   *****" << endl;
	cout << "******   0.exit   *****" << endl;
	cout << "***********************" << endl;
}
void Game()
{
	Myboard game;
	// 1.初始化玩家棋盘和设计者棋盘
	game.InitBoard(game.menu, ROWS, COLS, '0');
	game.InitBoard(game.show, ROWS, COLS, '*');
	// 2.给设计者棋盘布雷
	game.Set_mine(game.menu);
	// 3.打印设计者棋盘
	game.DisplayBoard(game.menu, ROWS, COLS);
	cout << endl;
	// 4.打印玩家棋盘
	game.DisplayBoard(game.show, ROWS, COLS);
	// 5.避免第一次被炸死
	game.First_safe();
	cout << endl;
	// 6.一步就赢的情况
	if (game.Count_show_mine() == Count)
	{
		game.DisplayBoard(game.menu, ROWS, COLS);
		cout << "玩家赢" << endl;
	}game.DisplayBoard(game.show, ROWS, COLS);

	while (1)
	{
		int count = game.Sao_mine();
		if (game.Count_show_mine() == Count)
		{
			game.DisplayBoard(game.menu, ROWS, COLS); //打印设计者棋盘
			cout << "玩家赢" << endl;
			break;
		}
		if (count)
		{
			cout << "很不幸,你被雷炸死了!" << endl;
			game.DisplayBoard(game.menu, ROWS, COLS); //打印设计者棋盘
			break;
		}game.DisplayBoard(game.show, ROWS, COLS);
	}

	//game.Set_menu(game.show);
	//game.DisplayBoard(game.show, ROWS, COLS);
}
int main()
{
	int input;
	
	do
	{
		menu();
		cout << "请选择: 1.开始游戏 0.退出游戏" << endl;
		cin >> input;
		switch (input)
		{
		case 1:Game(); break;
		case 0:cout << "退出游戏!"<<endl; exit(0); break;
		default:
			cout << "选择错误,请重新选择" << endl;
			break;
		}
	} while (1);
}

// 下面是功能函数展示   功能函数+主函数+头文件即可开始游戏
#include"game.h"
//初始化棋盘
void Myboard::InitBoard(char board[ROWS][COLS],int row,int col,char ret)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ret;  // 初始化放*  0
		}
	}
}
// 打印棋盘
void Myboard::DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		cout << i << "  ";
	}
	cout << endl;
	for (i = 1; i < row-1; i++)
	{
		cout << i << "  ";
		for (j = 1; j < col; j++)
		{
			cout << board[i][j] << "  ";
		}
		cout << endl;
	}
	cout << 10 << " ";
	for (i = 1; i < row; i++)
	{
		cout << board[10][i] << "  ";
	}
	cout << endl;
}

 //设置雷个数
void Myboard::Set_mine(char board[ROWS][COLS])
{
	int x = 0, y = 0;
	srand((unsigned)time(NULL));
	int count = Count;
	while (count)
	{
		x = rand() % 10 + 1;  // 产生1-10的随机数填入棋盘中
		y = rand() % 10 + 1;
		if (board[x][y] =='0')
		{
			board[x][y] = '1';
		}
		count--;
	}
}

// 统计当前坐标周围雷的个数
int Myboard::count_mine(int x, int y)
{
	int count = 0;
	if (menu[x - 1][y - 1] == '1')
		count++;
	if (menu[x - 1][y] == '1')
		count++;
	if (menu[x - 1][y + 1] == '1')
		count++;
	if (menu[x][y - 1] == '1')
		count++;
	if (menu[x][y + 1] == '1')
		count++;
	if (menu[x + 1][y - 1] == '1')
		count++;
	if (menu[x + 1][y] == '1')
		count++;
	if (menu[x + 1][y + 1] == '1')
		count++;
	return count;
}

// 避免第一次被炸死
void Myboard::First_safe()
{
	int x = 0;
	int y = 0;
	char ch = 0; // 显示周围雷的个数
	int set = 1;
	cout << "请输入坐标扫雷" << endl;
	while (1)
	{
		cin >> x >> y;
		if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
		{
			if (menu[x][y] == '1')  // 第一次被雷炸死
			{
				menu[x][y] = '0';
				ch = count_mine(x, y);
				show[x][y] = ch + '0';
				open_mine(x, y);  // 周围坐标,,那个坐标没有雷就展开那个坐标的周围
				while (set)
				{
					int x = rand() % 10 + 1;
					int y = rand() % 10 + 1;
					// 因为第一次被雷炸死了,所以就要重新随机设置没有雷的地方为雷
					if (menu[x][y] == '0')
					{
						menu[x][y] = '1';
						set--;
						break;
					}
				}break; // 跳出函数
			}
			if (menu[x][y] == '0')
			{
				ch = count_mine(x, y);
				show[x][y] = ch + '0';
				open_mine(x, y);
				break;
			}
		}
		else
		{
			cout << "输入错误,请重新输入!" << endl;
		}
	}
}

// 如果周围的八个坐标都没有雷,则继续展开  可以使用递归
void Myboard::open_mine(int x,int y)
{
	if (menu[x - 1][y - 1] == '0')
		show[x - 1][y - 1] = count_mine(x - 1, y - 1) + '0';

	if (menu[x - 1][y] == '0')
		show[x - 1][y] = count_mine(x - 1, y) + '0';

	if (menu[x - 1][y + 1] == '0')
		show[x - 1][y + 1] = count_mine(x - 1, y + 1) + '0';

	if (menu[x][y - 1] == '0')
		show[x][y - 1] = count_mine(x, y - 1) + '0';

	if (menu[x][y + 1] == '0')
		show[x][y + 1] = count_mine(x, y + 1) + '0';

	if (menu[x + 1][y - 1] == '0')
		show[x + 1][y - 1] = count_mine(x + 1, y - 1) + '0';

	if (menu[x + 1][y] == '0')
		show[x + 1][y] = count_mine(x + 1, y) + '0';

	if (menu[x + 1][y + 1] == '0')
		show[x + 1][y + 1] = count_mine(x + 1, y + 1) + '0';
}

// 扫雷
int Myboard::Sao_mine()
{
	int x = 0;
	int y = 0;
	char ch = 0;
	cout << "请输入坐标扫雷\n";
	cin >> x >> y;
	if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
	{
		if (menu[x][y] == '0')  
		{
			ch = count_mine(x, y);
			show[x][y] = ch + '0';
			open_mine(x, y);  // 周围坐标,,那个坐标没有雷就展开那个坐标的周围
			if (Count_show_mine() == Count)
			{
				DisplayBoard(show, ROWS, COLS);
				cout << "玩家赢" << endl;
				return 0;
			}
		}
		else if(menu[x][y]=='1')
		{
			return 1;
		}
	}
	else
	{
		cout << "输入错误,请重新输入!" << endl;
	}
	return 0;
}

// 判断剩余个数
int Myboard::Count_show_mine()
{
	int count = 0;
	int i = 0;
	int j = 0;
	for (i = 1; i < ROWS; i++)
	{
		for (j = 1; j < COLS; j++)
		{
			if (show[i][j] == '*')
			{
				count++;
			}
		}
	}
	return count;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雨轩(爵丶迹)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值