1、最新扫雷代码C+#include #include windows.h#include #include time.h#include assert.h#include conio.husing namespace std;struct eachcasebool havebomb;int state;/if state = 0,means nothing; less then 8 and more then 1 means there are state bomb near here; 9 means you have find here ;/10 means you have point h
2、ere;class viewfriend int check();public:view(int width,int hight,int numofbomb);void show();eachcase *qipan;int gethight();int getwidth();int getnumbomb();void falseshow();int bombnear(int,int);int check(bool* &,int,int);void flashshow();int check();/ check whether ignite the bomb, if not, check arr
3、ond/ return 1 means win ;2 means lose; 0 means nothingview();private:int hight,width,numbomb;public:int nhight,nwidth;int main()bool quit = true;while(quit)std:cout-std:endl;std:cout- welcome -std:endl;std:cout- -std:endl;std:cout- -std:endl;std:cout- w:up s:down a:left d :right p:point b:begin e:en
4、ter q:qiut -std:endl;std:cout- -std:endl;std:cout- -std:endl;std:cout- -std:endl;std:cout- -std:endl;std:cout- by uil from scut -std:endl;std:cout-std:endl;char gets = cin.get();if(gets = q)quit = false;continue;else if(gets != b)system(cls);continue;system(cls);int w,h,b;coutplease enter the with:w
5、;if(w1)w=10;coutplease enter the hight:h;if(h1)h=10;coutplease enter the number of bomb:b;if(b1)b=10;view views(h,w,b);clock_t start=clock();clock_t flash = start;bool* forcheck = new bool*h;for(int f = 0; f h;f+)forcheckf = new bool w;for(int r = 0;r 0.5)views.flashshow();sleep(300);views.show();ch
6、ar get;get = _getch();switch(get)case w:if(views.nhight-1=0 )/& views.qipanviews.nhight-1views.nwidth.state%10 = 0)views.nhight-;break;case s:if(views.nhight+1=0)/ & views.qipanviews.nhightviews.nwidth-1.state%10 = 0)views.nwidth-;break;case d:if(views.nwidth+1views.getwidth()/ & views.qipanviews.nh
7、ightviews.nwidth+1.state%10 = 0)views.nwidth+;break;case p:end = views.check();break;case r:restart = false;break;/ we need to improve herecase q:quit = false;break;case e:end = views.check(forcheck,views.nhight,views.nwidth);break;default:continue;views.show();/the main circle, which get the import
8、if(end = 1)views.falseshow();coutendlyou are loseendl;coutuse time:(double)(clock() - start)/clocks_per_sec)endl;couta;if(a = n)return 0;else if(a = y)restarts = true;/ need to improve.else if(end = 2)coutendlyou are win!endl;coutuse time:(double)(clock() - start)/clocks_per_sec)endl;couta;bool rest
9、arts = false;while(cin & !restarts)char a;cina;if(a = n)return 0;else if(a = y)restarts = true;/ need to improvesystem(cls);return 0;int view:check(bool* &forcheck,int h,int w)forcheckhw = false;if(qipanhw.havebomb)return 1;int wcheck = bombnear(h,w);/ use to deside continue to check;if(wcheck)qipan
10、hw.state= wcheck;elseqipanhw.state = 9;if(h-1 =0 & w-1=0 & forcheckh-1w-1)/ here show the view.qipan here have problemcheck(forcheck,h-1,w-1);if(h-1 = 0 & forcheckh-1w)check(forcheck,h-1,w);if(h-1 =0 & w+1 = 0 & forcheckhw-1)check(forcheck,h,w-1);if(w+1 width & forcheckhw+1)check(forcheck,h,w+1);if(
11、h+1 = 0 & forcheckh+1w-1)check(forcheck,h+1,w-1);if(h+1 hight & forcheckh+1w)check(forcheck,h+1,w);if(h+1 hight & w+1 width & forcheckh+1w+1)check(forcheck,h+1,w+1);return 0;view:view(int hights,int widths,int numofbomb)hight = hights;/width = hights;/numbomb = numofbomb;nhight = 0;nwidth = 0;qipan
12、= new eachcase*hight;/assert(sizeof(qipan)0);srand(time(0);/ we use it to make bombint b = numofbomb;int h = 0;for(;h=hight;h+)qipanh = new eachcasewidth;/assert(sizeof(qipanh)0);for(int w = 0; w = width ; w+)qipanhw.state = 0;qipanhw.havebomb = false;for(int nb = 0; nb = numbomb-1;)int hb=rand()%hi
13、ght;int wb=rand()%width;if(!qipanhbwb.havebomb)qipanhbwb.havebomb = true;nb+;int view:gethight()return hight;int view:getwidth()return width;int view:getnumbomb()return numbomb;void view:show()system(cls);/qipannhightnwidth.state=9;for(int n=0;n=(width)*4+2;n+)std:cout-;for(int h=0;h=hight-1;h+)std:
14、coutstd:endl|;for(int w=0;w=width-1;w+)int i=0;if(qipanhw.state = 0)std:cout ;else if( 0 qipanhw.state & qipanhw.state 9)std:coutqipanhw.state ; /need to advancedelse if(qipanhw.state = 9)std:cout ;else if(qipanhw.state = 10)std:coutp ;else if(qipanhw.state = 11)std:cout ;elseassert(0);std:cout|std:
15、endl;for(int k=0;k=(width)*4+2;k+)std:cout-;couthight:nhight+1 width:nwidth+1endl;view:view()for(int n=0;n=0 & w-1=0)if(qipanh-1w-1.havebomb)bombs+;if(h-1 =0)if(qipanh-1w.havebomb)bombs+;if(h-1 =0 & w+1 =0)if(qipanhw-1.havebomb)bombs+;if(w+1 width )if(qipanhw+1.havebomb)bombs+;if(h+1 =0)if(qipanh+1w
16、-1.havebomb)bombs+;if(h+1 hight)if(qipanh+1w.havebomb)bombs+;if(h+1 hight & w+1 width )if(qipanh+1w+1.havebomb)bombs+;return bombs;void view:falseshow()system(cls);/qipannhightnwidth.state=9;for(int n=0;n=(width)*4+2;n+)std:cout-;for(int h=0;h=hight-1;h+)std:coutstd:endl|;for(int w=0;w=width-1;w+)in
17、t i=0;if(qipanhw.state = 0)if(qipanhw.havebomb)std:cout ;elsestd:cout ;else if( 0 qipanhw.state & qipanhw.state 9)std:coutqipanhw.state ; /need to advancedelse if(qipanhw.state = 9)std:cout ;else if(qipanhw.state = 10)if(qipanhw.havebomb)std:cout ;elsestd:cout ;else if(qipanhw.state = 11)std:cout;el
18、seassert(0);std:cout|std:endl;for(int k=0;k=(width)*4+2;k+)std:cout-;void view:flashshow()system(cls);/qipannhightnwidth.state=9;for(int n=0;n=(width)*4+2;n+)std:cout-;for(int h=0;hhight;h+)std:coutstd:endl|;for(int w=0;wwidth;w+)if( h = nhight & w = nwidth)std:cout ;else if(qipanhw.state = 0)std:co
19、ut ;else if( 0 qipanhw.state & qipanhw.state 9)std:coutqipanhw.state ; /need to advancedelse if(qipanhw.state = 9)std:cout ;else if(qipanhw.state = 10)std:coutp ;else if(qipanhw.state = 11)std:cout ;elseassert(0);std:cout|std:endl;for(int k=0;k=(width)*4+2;k+)std:cout-;couthight:nhight+1 width:nwidt
20、h+1endl;int view:check()if(qipannhightnwidth.state = 0)qipannhightnwidth.state = 10;else if(qipannhightnwidth.state = 10)qipannhightnwidth.state = 0;int finded=0;for(int hs=0;hs = hight;hs+)for(int ws=0;ws = width;ws+)if(qipanhsws.state = 10 /*| qipanhsws.state = 0*/ & qipanhsws.havebomb)finded+;/ check where find all bombif(finded = numbomb) /error/return 2;return 0;公益活动策划方案
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1