当前位置:   article > 正文

C/C++实现扫雷小游戏_c++ 扫雷思路

c++ 扫雷思路

C/C++实现扫雷

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

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

  • 我们先梳理思路

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

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

  • 设计者棋盘如下:

    在这里插入图片描述

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

***初始化棋盘***
  • 1
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
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
注意红框外面的是行列坐标
  • 1
  • 3.看到设计者棋盘跟玩家棋盘,我们应该知晓 棋盘是10*10的格子。外面还有行列坐标,所以我们的行和列应该设置为ROWS:11,COLS:11。
***打印棋盘***
  • 1
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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 4.初始化之后设计者棋盘布雷,1表示雷。
***随机在设计者棋盘上布置雷,这里我布雷的10个雷***
  • 1
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--;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 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;
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 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';
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
//头文件展示
  • 1
#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();
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
// 主函数展示
  • 1
#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);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
// 下面是功能函数展示   功能函数+主函数+头文件即可开始游戏
  • 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/90835
推荐阅读
相关标签
  

闽ICP备14008679号