扫雷游戏(简单易懂)
该扫雷游戏简单易懂易玩,与五子棋做法像似,甚至比五子棋还简单有兴趣可以试试!
首先建立俩个棋盘,第一棋盘为游戏棋盘,第二棋盘用来赋值雷的位置和雷旁边的数字
根据用户所选的位置,第二棋盘把这位置周围的数字赋值给第一棋盘,从而让第一棋盘构成雷被隐藏的效果,如果所选的位置有雷,则游戏结束
用“*”来表示未扫描的位置,用”#“来表示雷
如果扫描完无(”*“),只剩下雷被覆盖的”*“,则玩家成功扫完所有的雷
头文件#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<ctime>
void buildboard();//建立棋盘
void displayer();//打印棋盘
void buildboard();//初始化棋盘
void game();//运行游戏
int addmath(int temprow, int tempcol);//按照周围的雷数,增加数字
void add2(int temprow, int tempcol);//按照周围的雷数,在第二棋盘打印数字
void passboard(int getrow, int getcol);//根据所扫描的位置,在这位置的周围把第二棋盘的数字覆盖到第一棋盘
int iswin();//判断是否赢建立棋盘效果
函数文件
#include"扫雷.h"
#define row 12
#define col 12//10x10的棋盘
#define bomb 12//雷的数量
string board;//第一棋盘
string board2;//第二棋盘
void buildboard()//初始化棋盘
{
for (int i = 0; i < row; i++)//
{
for (int k = 0; k < col; k++)
{
board = " * ";
board2 = " ";
}
}
}
void displayer()//打印棋盘
{
for (int i = 1; i < row - 1; i++)
{
cout << "|";
cout << "---";
}
cout << "|";
cout << endl;
for (int i = 1; i < row-1; i++)
{
for (int k = 1; k < col-1; k++)
{
cout << "|";
cout << board;
}
cout << "|";
cout << endl;
cout << "|";
for (int k = 1; k < col - 1; k++)
{
cout << "---";
cout << "|";
}
cout << endl;
}
cout << endl;
}
void game()
{
buildboard();
srand((unsigned int)time(NULL));
for (int i = 1; i <= bomb; i++)
{
while (true)
{
int temprow = rand() % 10 + 1;//1~10
int tempcol = rand() % 10 + 1;
if (board2 == " ")
{
board2 = " # ";
add2( temprow,tempcol);
break;
}
}
}
int getrow, getcol;
while (true)
{
displayer();
cout << "请输入扫描的位置(先输入行数再输入列数):";
cin >> getrow >> getcol;
if (getrow >= 1 && getrow <= 10 && getcol <= 10 && getcol >= 1)
{
if (board2 == " # ")
{
for (int i = 1; i < row - 1; i++)
{
for (int k = 1; k < col; k++)
{
if (board2 == " # ")
{
board = " # ";
}
}
}
cout << "第" << getrow << "行" << "第" << getcol << "列有雷!!!" << endl;
displayer();
cout << "您已触雷,游戏结束!" << endl;
break;
}
else
{
passboard(getrow, getcol);
if (iswin())
{
cout << "恭喜你赢了!" << endl;
break;
}
}
}
else
{
cout << "您已越界,请重新输入!" << endl;
}
}
}
int addmath(int temprow, int tempcol)
{
if ( board2 == " ")
{
board2 = " 1 ";
return 0;
}
if (board2 == " 1 ")
{
board2 = " 2 ";
return 0;
}
if (board2 == " 2 ")
{
board2 = " 3 ";
return 0;
}
if (board2 == " 3 ")
{
board2 = " 4 ";
return 0;
}
if (board2 == " 4 ")
{
board2 = " 5 ";
return 0;
}
if (board2 == " 5 ")
{
board2 = " 6 ";
return 0;
}
if (board == " 6 ")
{
board = " 7 ";
return 0;
}
if (board == " 7 ")
{
board = " 8 ";
return 0;
}
}
int iswin()
{
for (int i = 1; i < row - 1; i++)
{
for (int k = 1; k < col - 1; k++)
{
if (board2 == " # ")
{
continue;
}
if (board == " * ")
{
return 0;
}
}
}
return 1;
}
void add2(int temprow, int tempcol)
{
for (int i = tempcol - 1; i <= tempcol + 1; i++)
{
addmath(temprow - 1, i);
addmath(temprow + 1, i);
}
addmath(temprow, tempcol - 1);
addmath(temprow, tempcol + 1);
}
void passboard(int getrow, int getcol)
{
board = board2;
for (int i = getcol - 1; i <= getcol + 1; i++)
{
if (board2 != " # ")
{
board = board2;
}
if (board2 != " # ")
{
board = board2;
}
}
if (board2 != " # ")
{
board = board2;
}
if (board2 != " # ")
{
board = board2;
}
}主函数文件
#include"扫雷.h"
#include<iostream>
using namespace std;
#include<string>
int main()
{
cout << "**************<<扫雷游戏>>**************" << endl;
cout << "**************1.进入游戏****************" << endl;
cout << "**************2.退出游戏****************" << endl;
int select;
cout << "请输入数字进行操作:";
cin >> select;
if (select >= -1000 && select <= 1000)
{
while (true)
{
switch (select)
{
case 1:
{
game();
cout << "1.再玩一次"<<endl;
cout << "2.退出游戏"<<endl;
break;
}
case 2:
{
cout << "欢迎下次使用!"<<endl;
return 0;
break;
}
default:
cout << "输入错误,请重新输入!"<<endl;
}
cout << "请输入数字进行操作:";
cin >> select;
}
}
else
{
cout << "输入错误,请退出重试!";
return 0;
}
return 0;
}运行效果
https://blog.51cto.com/u_15403035/4773367
页:
[1]