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;
}