#include<iostream>
using namespace std;
char map[9][9];//总格子数。
char malei[11][11];//外扩一圈。
char show[11][11];//显示函数
char ta[9]={'0','1','2','3','4','5','6','7','8'};//雷个数数组。
//雷的状态。“0”表示空格子,“@”表示雷。
int leinum=10;//雷的总数
int a=0,p=1;
int m,n;//横纵坐标;
void first()//初始化
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
map[i][j]='0';
}
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
malei[i][j]='1';
}
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++){
show[i][j]='*';
}
}
for(int i=1;i<10;i++){
for(int j=1;j<10;j++)
cout<<show[i][j];
cout<<endl;
}
}
void auto_lei()//自动布雷
{
do{
int x=rand()%9;
int y=rand()%9;
if(map[x][y]!='@'){
map[x][y]='@';
a++;
}
}while(a!=leinum);
for(int i=0;i<9;i++){
for (int j=0;j<9;j++){
malei[i+1][j+1]=map[i][j];
}
}
}
void num()//计算雷数
{
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
if(malei[i][j]!='@'){
int t=0;
for(int q=i-1;q<=i+1;q++)
for(int w=j-1;w<=j+1;w++)
if(malei[q][w]=='@')
t++;
malei[i][j]=ta[t];
}
}
}
for(int i=1;i<10;i++){
for (int j=1;j<10;j++){
map[i-1][j-1]=malei[i][j];
}
}
}
void showlei()//显示雷区地图。
{
num();
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cout<<map[i][j];
}
cout<<endl;
}
}
void showresult()//显示结果
{
for(int i=1;i<10;i++){
for(int j=1;j<10;j++)
cout<<show[i][j];
cout<<endl;
}
}
void output(int m,int n)//翻开
{
if(map[m][n]=='@'){
cout<<"游戏失败"<<endl;
showlei();
p=0;
}
if(map[m][n]!='0'&&map[m][n]!='@')
show[m+1][n+1]=map[m][n];
if(map[m][n]=='0'){
show[m+1][n+1]=map[m][n];
for(int i=m;i<=m+2;i++){
for(int j=n;j<=n+2;j++){
if(show[i][j]=='*'){
show[i][j]=malei[i][j];
if((show[i][j]=='0')&&(i!='m'&&j!='n'))
output(i-1,j-1);
}
}
}
}
}
int main()
{
first();
auto_lei();
num();
while(p){
cout<<"请输入你要翻开的坐标"<<endl;
cin>>m>>n;
output(m,n);
if(p!=0)
showresult();
}
}
c++扫雷控制台版
最新推荐文章于 2023-08-27 16:15:15 发布