資源描述:
《c語(yǔ)言實(shí)現(xiàn)掃雷程序模擬.doc》由會(huì)員上傳分享,免費(fèi)在線(xiàn)閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫(kù)。
1、本人寫(xiě)的掃雷程序,由于時(shí)間緊迫,寫(xiě)的比較粗糙,但經(jīng)過(guò)c編譯可運(yùn)行,下面是源代碼:/*name:kaituozhe001saolei1125.cpp*/#include#include#include#defineN9intmine[N][N],game[N][N];introw=0,col=0,a,b;voidmain(){voidputmine(intn1);intexpand(introw,intcol);intRemain();voidmyprint
2、f1();voidmyprintf2();intaroundmine(introw,intcol);/*提示說(shuō)明*/printf("-------------------------------------------------------------------*您接下來(lái)要玩一個(gè)%d*%d的掃雷游戲。**請(qǐng)按提示操作(否則會(huì)出意外的),**在這個(gè)游戲中“*”代表地雷,“#”代表未打開(kāi)的盒子。**當(dāng)您將全部非雷盒子打開(kāi)后,您就贏了!**謝謝參與游戲!》》開(kāi)拓者制作bo《《*-----
3、--------------------------------------------------------------",N,N);intn1,i=0,j=0;/*初始化雷區(qū)*/for(i=0;i>",N,N,N*N);scanf("%d",&n1);printf("下面是所布雷陣>>");putmine(n1
4、);myprintf1();for(i=0;i>");myprintf1();while(Remain()!=n1){printf("請(qǐng)輸入掃雷坐標(biāo),以“,”分開(kāi)>>");scanf("%d,%d",&row,&col);for(i=0;i5、1)a=i,b=j;}if(mine[a][b]==42)gotoend;elseexpand(a,b);myprintf2();}printf("小樣,你贏了!有本事再來(lái)一次!");end:if(Remain()!=n1)printf("杯具,你踩到雷了,所以你lose了!");}/*布雷*/voidputmine(intn1){intminenumber();inti=0,j=0;srand(time(NULL));for(i=0;minenumber()6、()%N,col=rand()%N;mine[row][col]=42;}}/*打開(kāi)相關(guān)不是地雷的數(shù)組元素*/intexpand(introw,intcol){intaroundmine(introw,intcol);intinminearea(introw,intcol);inti=0,j=0;if(mine[row][col]=='*')return0;if(aroundmine(row,col)!=0){game[row][col]=aroundmine(row,col)+48;return0;}gam
7、e[row][col]='0';for(i=row-1;i<=row+1;i++)for(j=col-1;j<=col+1;j++){if(!inminearea(i,j))continue;if(game[i][j]!='#')continue;game[i][j]=aroundmine(i,j)+48;if(aroundmine(i,j)==0)expand(i,j);}}/*獲得周?chē)乩讉€(gè)數(shù)*/intaroundmine(introw,intcol){intinminearea(introw,intco
8、l);intaround=0;inti=0,j=0;for(i=row-1;i<=row+1;i++)for(j=col-1;j<=col+1;j++){if(!inminearea(i,j))continue;if(mine[i][j]==42)around++;}returnaround;}/*是否在雷區(qū)*/intinminearea(introw,intcol){if(row>=0&&row<