最新扫雷代码C++Word文档下载推荐.docx
《最新扫雷代码C++Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《最新扫雷代码C++Word文档下载推荐.docx(16页珍藏版)》请在冰豆网上搜索。
voidflashshow();
intcheck();
//checkwhetherignitethebomb,ifnot,checkarrond
//return1meanswin;
2meanslose;
0meansnothing
~view();
private:
inthight,width,numbomb;
intnhight,nwidth;
intmain()
boolquit=true;
while(quit)
std:
:
cout<
<
"
----------------------------------------------------------------"
endl;
-welcome-"
--"
-w:
ups:
downa:
leftd:
rightp:
pointb:
begine:
enterq:
qiut-"
-byuilfromscut-"
chargets=cin.get();
if(gets=='
q'
)
quit=false;
continue;
}
elseif(gets!
='
b'
system("
cls"
);
intw,h,b;
pleaseenterthewith:
cin>
>
w;
if(w<
1)
w=10;
pleaseenterthehight:
h;
if(h<
h=10;
pleaseenterthenumberofbomb:
b;
if(b<
b=10;
viewviews(h,w,b);
clock_tstart=clock();
clock_tflash=start;
bool**forcheck=newbool*[h];
for(intf=0;
f<
h;
f++)
forcheck[f]=newbool[w];
for(intr=0;
r<
w;
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;
s'
if(views.nhight+1<
views.gethight())//&
views.qipan[views.nhight+1][views.nwidth].state%10==0)
views.nhight++;
a'
if(views.nwidth-1>
=0)//&
views.qipan[views.nhight][views.nwidth-1].state%10==0)
views.nwidth--;
d'
if(views.nwidth+1<
views.getwidth())//&
views.qipan[views.nhight][views.nwidth+1].state%10==0)
views.nwidth++;
p'
end=views.check();
r'
restart=false;
//weneedtoimprovehere
e'
end=views.check(forcheck,views.nhight,views.nwidth);
default:
//themaincircle,whichgettheimport
if(end==1)
views.falseshow();
endl<
youarelose"
usetime:
(double)((clock()-start)/clocks_per_sec)<
tryagain?
y/n"
;
boolrestarts=false;
while(cin&
!
restarts)
chara;
a;
if(a=='
n'
return0;
elseif(a=='
y'
restarts=true;
}//needtoimprove.
elseif(end==2)
youarewin!
}//needtoimprove
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>
forcheck[h-1][w-1])
//hereshowtheview.qipan[][]herehaveproblem
check(forcheck,h-1,w-1);
=0&
forcheck[h-1][w])
check(forcheck,h-1,w);
w+1<
width&
forcheck[h-1][w+1])
check(forcheck,h-1,w+1);
if(w-1>
forcheck[h][w-1])
check(forcheck,h,w-1);
if(w+1<
forcheck[h][w+1])
check(forcheck,h,w+1);
if(h+1<
hight&
w-1>
forcheck[h+1][w-1])
check(forcheck,h+1,w-1);
forcheck[h+1][w])
check(forcheck,h+1,w);
forcheck[h+1][w+1])
check(forcheck,h+1,w+1);
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])<
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++;
gethight()
returnhight;
getwidth()
returnwidth;
getnumbomb()
returnnumbomb;
voidview:
show()
//qipan[nhight][nwidth].state=9;
for(intn=0;
n<
=(width)*4+2;
n++)
'
-'
for(inth=0;
=hight-1;
|'
for(intw=0;
w<
=width-1;
w++)
inti=0;
if(qipan[h][w].state==0)
¡
ö
"
elseif(0<
qipan[h][w].state&
qipan[h][w].state<
9)
qipan[h][w].state<
//needtoadvanced
elseif(qipan[h][w].state==9)
¤
elseif(qipan[h][w].state==10)
p"
elseif(qipan[h][w].state==11)
õ
assert(0);
for(intk=0;
k<
k++)
hight:
nhight+1<
width:
nwidth+1<
~view()
deleteqipan[n];
deleteqipan;
bombnear(inth,intw)
intbombs=0;
=0)
if(qipan[h-1][w-1].havebomb)
bombs++;
if(qipan[h-1][w].havebomb)
width)
if(qipan[h-1][w+1].havebomb)
if(w-1>
if(qipan[h][w-1].havebomb)
if(qipan[h][w+1].havebomb)
hight&
if(qipan[h+1][w-1].havebomb)
hight)
if(qipan[h+1][w].havebomb)
if(qipan[h+1][w+1].havebomb)
returnbombs;
falseshow()
è
ò
Á
flashshow()
hight;
width;
if(h==nhight&
w==nwidth)
ð
elseif(qipan[h][w].state==0)
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;