扫雷代码C++.docx
- 文档编号:2926379
- 上传时间:2022-11-16
- 格式:DOCX
- 页数:17
- 大小:16.64KB
扫雷代码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); } } return
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 扫雷 代码 C+