C++小游戏扫雷——如何写出一个简易的扫雷
其实很简单,但是这段代码不知道是否有BUG,有的话可以提出,谢谢大家。
BFS就可以了。
#include "all.h"//copy我的函数库,在上面
using namespace std;
const int flg_xzy[9][2]={{0,0},{0,1},{0,-1},{1,0},{1,1},{1,-1},{-1,0},{-1,1},{-1,-1}};
int ground=0,mp[105][105],n=9,m=9,Set_number=10,number,Ending=0;
//string s1="mode con cols=",s2=" lines=";
bool vis_xzy[105][105],vis[105][105],se[105][105];
int ran(int x){return x<=0?0:(rand()<<16|rand()<<1|(rand()&1))%x;}
void mp_add(int x,int y,int s){
mp[x][y]=-1;
for(int k=1;k<9;k++)
if(flg_xzy[k][0]+x>=0&&flg_xzy[k][0]+x<n&&flg_xzy[k][1]+y>=0&&flg_xzy[k][1]+y<m)
if(mp[flg_xzy[k][0]+x][flg_xzy[k][1]+y]^-1) mp[flg_xzy[k][0]+x][flg_xzy[k][1]+y]+=s;
}
void mak_xzy(int X,int Y,int num){
memset(mp,0,sizeof(mp));
memset(vis_xzy,0,sizeof(vis_xzy));
for(int i=0;i<9;i++)
if(flg_xzy[i][0]+X>=0&&flg_xzy[i][0]+X<n&&flg_xzy[i][1]+Y>=0&&flg_xzy[i][1]+Y<m)
vis_xzy[flg_xzy[i][0]+X][flg_xzy[i][1]+Y]=1;
while(num--){
int x=rand()%n,y=rand()%m;
while(mp[x][y]||vis_xzy[x][y]) x=rand()%n,y=rand()%m;
mp_add(x,y,1);
}
}
void DFS_xzy(int x,int y,bool C){
vis_xzy[x][y]=1;se[x][y]=1;
if(vis[x][y]||mp[x][y]==-1) Ending=1;
if(mp[x][y]&&C) return;
for (int i=1;i<9;i++){
int xx=x+flg_xzy[i][0],yy=y+flg_xzy[i][1];
if (xx<0||yy<0||xx>=n||yy>=m||vis_xzy[xx][yy]||vis[xx][yy]) continue;
DFS_xzy(xx,yy,1);
}
}
void change_mp(){
setcolor(ground+12);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(se[i][j]==1){GOTO(i,j*2);if(!mp[i][j]) printf(" ");else printf("%d ",mp[i][j]);}
}
void Lost(){
cls();
setcolor(ground+12);
printf("Lost\n");
Sleep(1500);
}
void Win(){
cls();
setcolor(ground+12);
printf("Win\n");
Sleep(1500);
}
int Start(){
string s;
// s=s1+ChangeNumToStr(m)+s2+ChangeNumToStr(n);
// system(s.c_str());
setcolor(ground+14);cls();
for(int i=1;i<=n;i++){for(int j=1;j<=m;j++) printf("▇");printf("\n");}
setcolor(ground+15);
GOTO(n,0);printf(" ");
GOTO(n,0);printf("%d",number);
int x=0,y=0;GOTO(x,2*y);setcolor(ground+10);printf("▇");
bool t=0;int Js=number;
while(1){
char ch=getch();
if(ch<0){
ch=getch();
GOTO(x,y*2);
if(se[x][y]){
setcolor(ground+12);
if(mp[x][y]) printf("%d ",mp[x][y]);else printf(" ");
}else if(vis[x][y]) setcolor(ground+15),printf("▇");
else setcolor(ground+14),printf("▇");
if(ch==75&&y>0) y--;else
if(ch==77&&y<m-1) y++;else
if(ch==72&&x>0) x--;else
if(ch==80&&x<n-1) x++;
GOTO(x,y*2);
if(se[x][y]){
setcolor(160+12);
if(mp[x][y]) printf("%d ",mp[x][y]);else printf(" ");
}else setcolor(ground+10),printf("▇");
}else{
GOTO(x,y*2);
if(se[x][y]){
setcolor(ground+12);
if(mp[x][y]) printf("%d ",mp[x][y]);else printf(" ");
}else if(vis[x][y]) setcolor(ground+15),printf("▇");
else setcolor(ground+14),printf("▇");
if((ch=='W'||ch=='w')&&x>0) x--;else
if((ch=='S'||ch=='s')&&x<n-1) x++;else
if((ch=='A'||ch=='a')&&y>0) y--;else
if((ch=='D'||ch=='d')&&y<m-1) y++;else
if(ch==' '){
if(!t) t=1,mak_xzy(x,y,Set_number);
if(mp[x][y]==-1){Lost();return 0;}
else{
memset(vis_xzy,0,sizeof(vis_xzy));
DFS_xzy(x,y,0);change_mp();
if(Ending){Lost();return 0;}
}
}
if(ch=='P'||ch=='p'){
GOTO(x,y*2);vis[x][y]=1-vis[x][y];
if(mp[x][y]==-1&&vis[x][y]==1) number--;
if(mp[x][y]==-1&&vis[x][y]==0) number++;
if(vis[x][y]==1) Js--;
if(vis[x][y]==0) Js++;
if(Js<0) vis[x][y]=1-vis[x][y],Js++;
setcolor(ground+15);
GOTO(n,0);printf(" ");
GOTO(n,0);printf("%d",Js);
if(number==0){Win();return 0;}
}
GOTO(x,y*2);
if(se[x][y]){
setcolor(160+12);
if(mp[x][y]) printf("%d ",mp[x][y]);else printf(" ");
}else setcolor(ground+10),printf("▇");
}
}
}
void Set(){
cls();setcolor(ground+15);
printf("选择难度\n");
printf("0.返回\n");
printf("1.初级\n");
printf("2.中级\n");
printf("3.高级\n");
printf("4.自定义\n");
char ch=getch();
while(ch<'0'||ch>'4') ch=getch();
if(ch=='0') return;
if(ch=='1') n=9,m=9,Set_number=10;else
if(ch=='2') n=16,m=16,Set_number=40;else
if(ch=='3') n=16,m=30,Set_number=99;
if(ch^'4') return;
int nn=0;
while(nn<5||nn>25){
printf("行数(范围5~25):");
cin>>nn;
if(nn<5||nn>25) printf("\n重复");
}
n=nn;
int mm=0;
while(mm<5||mm>30){
printf("列数(范围5~30):");
cin>>mm;
if(mm<5||mm>30) printf("\n重复");
}
m=mm;
int tt=0;
while(tt<=0||tt>25){
printf("雷的个数(范围1~%d):",n*m);
cin>>tt;
if(tt<=0||tt>25) printf("\n重复");
}
Set_number=tt;
}
int main(){
system("title ");
char ch;bsrand();
while(1){
setcolor(ground+12);
cls();memset(se,0,sizeof(se));number=Set_number;Ending=0;
memset(vis,0,sizeof(vis));
printf(" 主菜单\n");
printf(" 0.退出\n");
printf(" 1.开始游戏\n");
printf(" 2.选择难度\n");
printf(" 空格点击,P标记\n");
ch=getch();
if(ch=='0') retr();else
if(ch=='1') Start();else
if(ch=='2') Set();
}
return 0;
}