欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  互联网

扫雷

程序员文章站 2022-07-09 15:25:57
#include#include#include using namespace std;int m=1;int pan[9][9]={0},pan1[9][9]={0};char a[10][10]={{‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’},{‘1’},{‘2’},{‘3’},{‘4’},{‘5’},{‘6’},{‘7’},{‘8’},{‘9’}};class Point{public:void Get_Point(){cout<&l...

#include
#include
#include
using namespace std;

int m=1;
int pan[9][9]={0},pan1[9][9]={0};
char a[10][10]={{‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’},{‘1’},{‘2’},{‘3’},{‘4’},{‘5’},{‘6’},{‘7’},{‘8’},{‘9’}};
class Point
{
public:
void Get_Point(){
cout<<“请输入坐标:”;
cin>>x>>y;
}
int Get_x(){return x;};
int Get_y(){return y;};
private:
int x,y;
};

class Map:public Point
{
public:
void draw(int x,int y);
int open(int x,int y);
void scanbomb();
int saomiao();
};

int Map::saomiao()
{
int i,j,n;
for(i=1;i<=7;i++)
{
for(j=1;j<=7;j++)
{
if(pan1[i][j]1||pan1[i][j]2)
n++;
if(n
39)
{break;}
}
if(n
39)
{ printf("\n");
printf(“恭喜你 你赢了!!!!”);
break;
}
}
if(n==39)
m=0;
return 0;
}

void Map::scanbomb()
{
int i,j;
for(i=1;i<=8;i++)
for(j=1;j<=8;j++)
{
if(pan[i][j]!=9)
{
if(i1&&j1)//计数左上角
{
if(pan[i][j+1]==9)
pan[i][j]++;
if(pan[i+1][j]9)
pan[i][j]++;
if(pan[i+1][j+1]9)
pan[i][j]++;
}
if(i
1&&j
7&&pan[1][7]!=9)//计数右上角
{
if(pan[i][j-1]==9)
pan[i][j]++;
if(pan[i+1][j]9)
pan[i][j]++;
if(pan[i+1][j-1]9)
pan[i][j]++;
}
if(i
7&&j
1&&pan[7][1]!=9)//计数左下角
{
if(pan[i][j+1]==9)
pan[i][j]++;
if(pan[i-1][j]9)
pan[i][j]++;
if(pan[i-1][j+1]9)
pan[i][j]++;
}
if(i
7&&j
7&&pan[7][7]!=9)//计数右下角
{
if(pan[i][j-1]==9)
pan[i][j]++;
if(pan[i-1][j]==9)
pan[i][j]++;
if(pan[i-1][j-1]9)
pan[i][j]++;
}
if(j
1&&i>1&&i<=7)//计数左边
{
if(pan[i-1][j]==9)
pan[i][j]++;
if(pan[i-1][j+1]==9)
pan[i][j]++;
if(pan[i][j+1]==9)
pan[i][j]++;
if(pan[i+1][j]==9)
pan[i][j]++;
if(pan[i+1][j+1]9)
pan[i][j]++;
}
if(j
7&&i>1&&i<=7)//计数右边
{
if(pan[i-1][j]==9)
pan[i][j]++;
if(pan[i-1][j-1]==9)
pan[i][j]++;
if(pan[i][j-1]==9)
pan[i][j]++;
if(pan[i+1][j]==9)
pan[i][j]++;
if(pan[i+1][j-1]9)
pan[i][j]++;
}
if(i
7&&j>1&&j<=7)//计数下边
{
if(pan[i-1][j]==9)
pan[i][j]++;
if(pan[i-1][j-1]==9)
pan[i][j]++;
if(pan[i][j-1]==9)
pan[i][j]++;
if(pan[i-1][j+1]==9)
pan[i][j]++;
if(pan[i][j+1]9)
pan[i][j]++;
}
if(i
1&&j>1&&j<=7)//计数上边
{
if(pan[i+1][j]==9)
pan[i][j]++;
if(pan[i+1][j-1]==9)
pan[i][j]++;
if(pan[i][j-1]==9)
pan[i][j]++;
if(pan[i+1][j+1]==9)
pan[i][j]++;
if(pan[i][j+1]==9)
pan[i][j]++;
}
if(i>1&&i<7&&j>1&&j<7)
{
if(pan[i-1][j-1]==9) pan[i][j]++;//左上
if(pan[i][j-1]==9) pan[i][j]++;//左
if(pan[i+1][j-1]==9) pan[i][j]++;//左下
if(pan[i-1][j]==9) pan[i][j]++;//上
if(pan[i+1][j]==9) pan[i][j]++;//下
if(pan[i-1][j+1]==9) pan[i][j]++;//右上
if(pan[i][j+1]==9) pan[i][j]++;//右
if(pan[i+1][j+1]==9) pan[i][j]++;//右
}
}
}
}

int Map::open(int x,int y)
{
if(x>=1&&y>=1)
{
int i,j;
int pan2[10][10]={0};
for(i=0;i<=8;i++)
for(j=0;j<=8;j++)
{
if(i0||i8||j0||j8)
pan[i][j]=11;
}
for(i=x-1;i<=x+1;i++)
for(j=y-1;j<=y+1;j++)
{
if(pan[x][y]==0&&pan1[x][y]==1&&pan2[x][y]==0)
{
pan2[i][j]=1;
if(x-1>=0&&y-1>=0&&pan[x-1][y-1]!=9&&pan[x-1][y-1]!=11&&pan1[x-1][y-1]==0) draw(x-1,y-1);//挖左上
if(x-1>0&&pan[x-1][y]!=9&&pan[x-1][y]!=11&&pan1[x-1][y]==0) draw(x-1,y);//挖上
if(x-1>0&&y+1<=7&&pan[x-1][y+1]!=9&&pan[x-1][y+1]!=11&&pan1[x-1][y+1]==0) draw(x-1,y+1);//挖右上
if(y-1>0&&pan[x][y-1]!=9&&pan[x][y-1]!=11&&pan1[x][y-1]==0) draw(x,y-1);//挖左
if(y+1<=7&&pan[x][y+1]!=9&&pan[x][y+1]!=11&&pan1[x][y+1]==0) draw(x,y+1);//挖右
if(x+1<=7&&y-1>0&&pan[x+1][y-1]!=9&&pan[x+1][y-1]!=11&&pan1[x+1][y-1]==0) draw(x+1,y-1);//挖左下
if(x+1<=7&&pan[x+1][y]!=9&&pan[x+1][y]!=11&&pan1[x+1][y]==0) draw(x+1,y);//挖下
if(x+1<=7&&y+1<=7&&pan[x+1][y+1]!=9&&pan[x+1][y+1]!=11&&pan1[x+1][y+1]==0) draw(x+1,y+1);//挖右下
}
return 0;
}
}
return 0;
}

void Map::draw(int x,int y)
{
if(pan[x][y]!=9&&pan[x][y]!=0&&pan1[x][y]!=2)
{
pan1[x][y]=1;
a[x][y]=pan[x][y]+‘0’;
}
if(pan[x][y]==0&&pan1[x][y]!=2)
{
pan1[x][y]=1;
a[x][y]=pan[x][y]+‘0’;
open(x,y);
}
if(pan[x][y]==9&&pan1[x][y]!=2)
{
printf(“sorry game over!!!\n游戏结束”);
m=0;
}
}

class Game
{
public:
void init();
void start();
private:
Map map;
};

void Game::start()
{
int i,j,b,n=0,k=0;
for(i=1;i<=9;i++) //打印基本界面
for(j=1;j<=9;j++)
{
a[i][j]=’’;
}
srand(time(0));
while(k<=9)
{
i=(int)(rand()%7+1);
j=(int)(rand()%7+1);
if(pan[i][j]0)
{
pan[i][j]=9;
k++;
}
}
map.scanbomb();
while(m
1)
{
for(i=0;i<=10;i++)//即时显示每次操作后的界面
for(j=0;j<10;j++)
{
if(j%100)
printf("\n");
printf("%c “,a[i][j]);
}
printf(”\n");
printf(“请输入序号:”);
scanf("%d",&b);
printf("\n");
if(b
1)//挖雷
{
map.Get_Point();
if(pan1[map.Get_x()][map.Get_y()]0&&pan1[map.Get_x()][map.Get_y()]!=2)
{
map.draw(map.Get_x(),map.Get_y());
}
}
if(b
2)//退出
m=0;
if(b==3)//标记
{
map.Get_Point();
if(pan1[map.Get_x()][map.Get_y()]0)
{
pan1[map.Get_x()][map.Get_y()]=2;
a[map.Get_x()][map.Get_y()]=’!’;
}
}
if(b
4)//取消标记
{
map.Get_Point();
if(pan1[map.Get_x()][map.Get_y()]==2)
{
pan1[map.Get_x()][map.Get_y()]=0;
a[map.Get_x()][map.Get_y()]=’
’;
}
}
map.saomiao();
}
}

void Game::init()
{
cout<<"\n****************************************************\n"<<endl;
cout<<" 欢迎进入扫雷"<<endl;
cout<<"\n****************************************************\n"<<endl;
cout<<“规则:\n输入序号1挖雷\n输入序号2退出\n输入序号3对雷进行标记\n输入序号4取消标\n”<<endl;
cout<<“玩法:\n先输入序号表明要挖雷还是要标记\n之后你需要输入一个坐标如a b(a表示行 b表示列)则指定位置会被挖开或者标记\n”<<endl;
}

int main()
{
Game game;
game.init();
game.start();
return 0;
}
//给个赞?

本文地址:https://blog.csdn.net/Liston_YDQ/article/details/107593761

相关标签: 编程