扫雷代码C++.docx
- 文档编号:5664254
- 上传时间:2022-12-30
- 格式:DOCX
- 页数:17
- 大小:16.95KB
扫雷代码C++.docx
《扫雷代码C++.docx》由会员分享,可在线阅读,更多相关《扫雷代码C++.docx(17页珍藏版)》请在冰豆网上搜索。
扫雷代码C++
#include
#include"windows.h"
#include
#include"time.h"
#include"assert.h"
#include"conio.h"
usingnamespacestd;
structeachcase
{
boolhavebomb;
intstate;
//ifstate=0,meansnothing;lessthen8andmorethen1meansthereare"state"bombnearhere;9meansyouhavefindhere;
//10meansyouhavepointhere;
};
classview
{
friendintcheck();
public:
view(intwidth,inthight,intnumofbomb);
voidshow();
eachcase**qipan;
intgethight();
intgetwidth();
intgetnumbomb();
voidfalseshow();
intbombnear(int,int);
intcheck(bool**&,int,int);
voidflashshow();
intcheck();
//checkwhetherignitethebomb,ifnot,checkarrond
//return1meanswin;2meanslose;0meansnothing
~view();
private:
inthight,width,numbomb;
public:
intnhight,nwidth;
};
intmain()
{
boolquit=true;
while(quit)
{
std:
:
cout<<"----------------------------------------------------------------"< : endl; std: : cout<<"-welcome-"< : endl; std: : cout<<"--"< : endl; std: : cout<<"--"< : endl; std: : cout<<"-w: ups: downa: leftd: rightp: pointb: begine: enterq: qiut-"< : endl; std: : cout<<"--"< : endl; std: : cout<<"--"< : endl; std: : cout<<"--"< : endl; std: : cout<<"--"< : endl; std: : cout<<"-byuilfromscut-"< : endl; std: : cout<<"----------------------------------------------------------------"< : endl; chargets=cin.get(); if(gets=='q') { quit=false; continue; } elseif(gets! ='b') { system("cls"); continue; } system("cls"); intw,h,b; cout<<"pleaseenterthewith: "< cin>>w; if(w<1) w=10; cout<<"pleaseenterthehight: "< cin>>h; if(h<1) h=10; cout<<"pleaseenterthenumberofbomb: "< cin>>b; if(b<1) b=10; viewviews(h,w,b); clock_tstart=clock(); clock_tflash=start; bool**forcheck=newbool*[h]; for(intf=0;f { forcheck[f]=newbool[w]; for(intr=0;r { forcheck[f][r]=true; } } //makeitforcheck()tocheckwhetheracasehavebeenfind intend=0;//desidewhetherstop boolrestart=true; while(cin&&! end&&restart&&quit) { if((double)((clock()-flash)/clocks_per_sec)>0.5) { views.flashshow(); sleep(300); } views.show(); charget; get=_getch(); switch(get) { case'w': if(views.nhight-1>=0)//&&views.qipan[views.nhight-1][views.nwidth].state%10==0) { views.nhight--; } break; case's': if(views.nhight+1 { views.nhight++; } break; case'a': if(views.nwidth-1>=0)//&&views.qipan[views.nhight][views.nwidth-1].state%10==0) { views.nwidth--; } break; case'd': if(views.nwidth+1 { views.nwidth++; } break; case'p': end=views.check(); break; case'r': restart=false; break; //weneedtoimprovehere case'q': quit=false; break; case'e': end=views.check(forcheck,views.nhight,views.nwidth); break; default: continue; } } views.show(); //themaincircle,whichgettheimport if(end==1) { views.falseshow(); cout< cout<<"usetime: "<<(double)((clock()-start)/clocks_per_sec)< cout<<"tryagain? y/n"; boolrestarts=false; while(cin&&! restarts) { chara; cin>>a; if(a=='n') return0; elseif(a=='y') { restarts=true; } } }//needtoimprove. elseif(end==2) { cout< "< cout<<"usetime: "<<(double)((clock()-start)/clocks_per_sec)< cout<<"tryagain? y/n"; chara; cin>>a; boolrestarts=false; while(cin&&! restarts) { chara; cin>>a; if(a=='n') return0; elseif(a=='y') { restarts=true; } } }//needtoimprove system("cls"); } return0; } intview: : check(bool**&forcheck,inth,intw) { forcheck[h][w]=false; if(qipan[h][w].havebomb) { return1; } intwcheck=bombnear(h,w); //usetodesidecontinuetocheck; if(wcheck) { qipan[h][w].state=wcheck; } else { qipan[h][w].state=9; if(h-1>=0&&w-1>=0&&forcheck[h-1][w-1]) //hereshowtheview.qipan[][]herehaveproblem { check(forcheck,h-1,w-1); } if(h-1>=0&&forcheck[h-1][w]) { check(forcheck,h-1,w); } if(h-1>=0&&w+1 { check(forcheck,h-1,w+1); } if(w-1>=0&&forcheck[h][w-1]) { check(forcheck,h,w-1); } if(w+1 { check(forcheck,h,w+1); } if(h+1 { check(forcheck,h+1,w-1); } if(h+1 { check(forcheck,h+1,w); } if(h+1 { check(forcheck,h+1,w+1); } } return0; } view: : view(inthights,intwidths,intnumofbomb) { hight=hights;//////////////// width=hights;/////////////// numbomb=numofbomb; nhight=0; nwidth=0; qipan=neweachcase*[hight]; //assert(sizeof(qipan)<0); srand(time(0)); //weuseittomakebomb intb=numofbomb; inth=0; for(;h<=hight;h++) { qipan[h]=neweachcase[width]; //assert(sizeof(qipan[h])<0); for(intw=0;w<=width;w++) { qipan[h][w].state=0; qipan[h][w].havebomb=false; } } for(intnb=0;nb<=numbomb-1;) { inthb=rand()%hight; intwb=rand()%width; if(! qipan[hb][wb].havebomb) { qipan[hb][wb].havebomb=true; nb++; } } } intview: : gethight() { returnhight; } intview: : getwidth() { returnwidth; } intview: : getnumbomb() { returnnumbomb; } voidview: : show() { system("cls"); //qipan[nhight][nwidth].state=9; for(intn=0;n<=(width)*4+2;n++) { std: : cout<<'-'; } for(inth=0;h<=hight-1;h++) { std: : cout< : endl<<'|'; for(intw=0;w<=width-1;w++) { inti=0; if(qipan[h][w].state==0) std: : cout<<"■"; elseif(0 std: : cout< elseif(qipan[h][w].state==9) std: : cout<<"·"; elseif(qipan[h][w].state==10) std: : cout<<"p"; elseif(qipan[h][w].state==11) std: : cout<<"□"; else assert(0); } std: : cout<<'|'< : endl; } for(intk=0;k<=(width)*4+2;k++) { std: : cout<<'-'; } cout<<"hight: "< "< } view: : ~view() { for(intn=0;n<=hight;n++) deleteqipan[n]; deleteqipan; } intview: : bombnear(inth,intw) { intbombs=0; if(h-1>=0&&w-1>=0) { if(qipan[h-1][w-1].havebomb) bombs++; } if(h-1>=0) { if(qipan[h-1][w].havebomb) bombs++; } if(h-1>=0&&w+1 { if(qipan[h-1][w+1].havebomb) bombs++; } if(w-1>=0) { if(qipan[h][w-1].havebomb) bombs++; } if(w+1 { if(qipan[h][w+1].havebomb) bombs++; } if(h+1 { if(qipan[h+1][w-1].havebomb) bombs++; } if(h+1 { if(qipan[h+1][w].havebomb) bombs++; } if(h+1 { if(qipan[h+1][w+1].havebomb) bombs++; } returnbombs; } voidview: : falseshow() { system("cls"); //qipan[nhight][nwidth].state=9; for(intn=0;n<=(width)*4+2;n++) { std: : cout<<'-'; } for(inth=0;h<=hight-1;h++) { std: : cout< : endl<<'|'; for(intw=0;w<=width-1;w++) { inti=0; if(qipan[h][w].state==0) { if(qipan[h][w].havebomb) { std: : cout<<"¤"; } else { std: : cout<<""; } } elseif(0 std: : cout< elseif(qipan[h][w].state==9) std: : cout<<""; elseif(qipan[h][w].state==10) { if(qipan[h][w].havebomb) std: : cout<<"◎"; else { std: : cout<<"×"; } } elseif(qipan[h][w].state==11) std: : cout<<"□"; else assert(0); } std: : cout<<'|'< : endl; } for(intk=0;k<=(width)*4+2;k++) { std: : cout<<'-'; } } voidview: : flashshow() { system("cls"); //qipan[nhight][nwidth].state=9; for(intn=0;n<=(width)*4+2;n++) { std: : cout<<'-'; } for(inth=0;h { std: : cout< : endl<<'|'; for(intw=0;w { if(h==nhight&&w==nwidth) std: : cout<<"○"; elseif(qipan[h][w].state==0) std: : cout<<"■"; elseif(0 std: : cout< elseif(qipan[h][w].state==9) std: : cout<<"·"; elseif(qipan[h][w].state==10) std: : cout<<"p"; elseif(qipan[h][w].state==11) std: : cout<<"□"; else assert(0); } std: : cout<<'|'< : endl; } for(intk=0;k<=(width)*4+2;k++) { std: : cout<<'-'; } cout<<"hight: "< "< } intview: : check() { if(qipan[nhight][nwidth].state==0) qipan[nhight][nwidth].state=10; elseif(qipan[nhight][nwidth].state==10) qipan[nhight][nwidth].state=0; intfinded=0; for(inths=0;hs<=hight;hs++) { for(intws=0;ws<=width;ws++) { if(qipan[hs][ws].state==10/*||qipan[hs][ws].state==0*/&&qipan[hs][ws].havebomb) finded++; } } //checkwherefindallbomb if(finded==numbomb)//////////////////////////////////////////////////////error/////////////////////////////////////// { return2; } return0; } WelcomeTo Download! ! ! 欢迎您的下载,资料仅供参考! ______________________________________________________________________________________________________________ -可编辑修改-
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 扫雷 代码 C+
![提示](https://static.bdocx.com/images/bang_tan.gif)