C++编写的扫雷源程序_第1页
C++编写的扫雷源程序_第2页
C++编写的扫雷源程序_第3页
C++编写的扫雷源程序_第4页
C++编写的扫雷源程序_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

-

.z.

//headerfile

*include<iostream>

*include<cstdlib>

*include<ctime>

*include<windows.h>

*include<conio.h>

//defines

*defineKEY_UP0*E048

*defineKEY_DOWN0*E050

*defineKEY_LEFT0*E04B

*defineKEY_RIGHT0*E04D

*defineKEY_ESC0*001B

*defineKEY_1'1'

*defineKEY_2'2'

*defineKEY_3'3'

*defineGAME_MA*_WIDTH100

*defineGAME_MA*_HEIGHT100

//StringsResource

*defineSTR_GAMETITLE"ArrowKey:MoveCursorKey1:Open\

Key2:MarkKey3:OpenNeighbors"

*defineSTR_GAMEWIN"Congratulations!YouWin!Thankyouforplaying!\n"

*defineSTR_GAMEOVER"GameOver,thankyouforplaying!\n"

*defineSTR_GAMEEND"Presentedbyyzfy.PressESCtoe*it\n"

//-------------------------------------------------------------

//Baseclass

classCConsoleWnd

{

public:

staticintTe*tOut(constchar*);

staticintGoto*Y(int,int);

staticintCharOut(int,int,constint);

staticintTe*tOut(int,int,constchar*);

staticintGetKey();

public:

};

//{{//classCConsoleWnd

//

//intCConsoleWnd::GetKey()

//WaitforstandardinputandreturntheKeyCode

//

intCConsoleWnd::GetKey()

{

intnkey=getch(),nk=0;

if(nkey>=128||nkey==0)nk=getch();

returnnk>0"nkey*256+nk:nkey;

}

//

//intCConsoleWnd::Goto*Y(int*,inty)

//Movecursorto(*,y)

//OnlyConsoleApplication

//

intCConsoleWnd::Goto*Y(int*,inty)

{

COORDcd;

cd.*=*;cd.Y=y;

returnSetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cd);

}

//

//intCConsoleWnd::Te*tOut(constchar*pstr)

//Outputastringatcurrentposition

//

intCConsoleWnd::Te*tOut(constchar*pstr)

{

for(;*pstr;++pstr)putchar(*pstr);

return0;

}

//

//intCConsoleWnd::CharOut(int*,inty,constintpstr)

//Outputacharat(*,y)

//

intCConsoleWnd::CharOut(int*,inty,constintpstr)

{

Goto*Y(*,y);

returnputchar(pstr);

}

//

//intCConsoleWnd::Te*tOut(constchar*pstr)

//Outputastringat(*,y)

//

intCConsoleWnd::Te*tOut(int*,inty,constchar*pstr)

{

Goto*Y(*,y);

returnTe*tOut(pstr);

}

//}}

//-------------------------------------------------------------

//Applicationclass

classCSLGame:publicCConsoleWnd

{

private:

private:

intcur*,curY;

intpoolWidth,poolHeight;

intbm_gamepool[GAME_MA*_HEIGHT+2][GAME_MA*_WIDTH+2];

public:

CSLGame():cur*(0),curY(0){poolWidth=poolHeight=0;}

intInitPool(int,int,int);

intMoveCursor(){returnCConsoleWnd::Goto*Y(cur*,curY);}

intDrawPool(int);

intWaitMessage();

intGetShowNum(int,int);

intTryOpen(int,int);

private:

intDFSShowNum(int,int);

private:

conststaticintGMARK_BOOM;

conststaticintGMARK_EMPTY;

conststaticintGMARK_MARK;

};

constintCSLGame::GMARK_BOOM=0*10;

constintCSLGame::GMARK_EMPTY=0*100;

constintCSLGame::GMARK_MARK=0*200;

//{{//classCSLGame:publicCConsoleWnd

//

//intCSLGame::InitPool(intWidth,intHeight,intnBoom)

//Initializethegamepool.

//IfWidth*Height<=nBoom,ornBoom<=0,

//orWidthandHeighte*ceedlimit,thenreturn1

//otherwisereturn0

//

intCSLGame::InitPool(intWidth,intHeight,intnBoom)

{

poolWidth=Width;poolHeight=Height;

if(nBoom<=0||nBoom>=Width*Height

||Width<=0||Width>GAME_MA*_WIDTH

||Height<=0||Height>GAME_MA*_HEIGHT

){

return1;

}

//zeromemory

for(inty=0;y<=Height+1;++y)

{

for(int*=0;*<=Width+1;++*)

{

bm_gamepool[y][*]=0;

}

}

//initseed

srand(time(NULL));

//initBooms

while(nBoom)

{

int*=rand()%Width+1,y=rand()%Height+1;

if(bm_gamepool[y][*]==0)

{

bm_gamepool[y][*]=GMARK_BOOM;

--nBoom;

}

}

//initcursorposition

cur*=curY=1;

MoveCursor();

return0;

}

//

//intCSLGame::DrawPool(intbDrawBoom=0)

//DrawgamepooltoConsolewindow

//

intCSLGame::DrawPool(intbDrawBoom=0)

{

for(inty=1;y<=poolHeight;++y)

{

CConsoleWnd::Goto*Y(1,y);

for(int*=1;*<=poolWidth;++*)

{

if(bm_gamepool[y][*]==0)

{

putchar('.');

}

elseif(bm_gamepool[y][*]==GMARK_EMPTY)

{

putchar('');

}

elseif(bm_gamepool[y][*]>0&&bm_gamepool[y][*]<=8)

{

putchar('0'+bm_gamepool[y][*]);

}

elseif(bDrawBoom==0&&(bm_gamepool[y][*]&GMARK_MARK))

{

putchar('*');

}

elseif(bm_gamepool[y][*]&GMARK_BOOM)

{

if(bDrawBoom)

putchar('*');

else

putchar('.');

}

}

}

return0;

}

//

//intCSLGame::GetShowNum(int*,inty)

//returnShowNumat(*,y)

//

intCSLGame::GetShowNum(int*,inty)

{

intnCount=0;

for(intY=-1;Y<=1;++Y)

for(int*=-1;*<=1;++*)

{

if(bm_gamepool[y+Y][*+*]&GMARK_BOOM)++nCount;

}

returnnCount;

}

//

//intCSLGame::TryOpen(int*,inty)

//Tryopen(*,y)andshowthenumber

//Ifthereisaboom,thenreturnEOF

//

intCSLGame::TryOpen(int*,inty)

{

intnRT=0;

if(bm_gamepool[y][*]&GMARK_BOOM)

{

nRT=EOF;

}

else

{

intnCount=GetShowNum(*,y);

if(nCount==0)

{

DFSShowNum(*,y);

}

elsebm_gamepool[y][*]=nCount;

}

returnnRT;

}

//

//intCSLGame::DFSShowNum(int*,inty)

//Privatefunction,noment

//

intCSLGame::DFSShowNum(int*,inty)

{

if((0<*&&*<=poolWidth)&&

(0<y&&y<=poolHeight)&&

(bm_gamepool[y][*]==0))

{

intnCount=GetShowNum(*,y);

if(nCount==0)

{

bm_gamepool[y][*]=GMARK_EMPTY;

for(intY=-1;Y<=1;++Y)

for(int*=-1;*<=1;++*)

{

DFSShowNum(*+*,y+Y);

}

}

elsebm_gamepool[y][*]=nCount;

}

return0;

}

//

//intCSLGame::WaitMessage()

//Gameloop,waitandprocessaninputmessage

//return:0:notend;1:Win;otherwise:Lose

//

intCSLGame::WaitMessage()

{

intnKey=CConsoleWnd::GetKey();

intnRT=0,nArrow=0;

switch(nKey)

{

caseKEY_UP:

{

if(curY>1)--curY;

nArrow=1;

}break;

caseKEY_DOWN:

{

if(curY<poolHeight)++curY;

nArrow=1;

}break;

caseKEY_LEFT:

{

if(cur*>1)--cur*;

nArrow=1;

}break;

caseKEY_RIGHT:

{

if(cur*<poolWidth)++cur*;

nArrow=1;

}break;

caseKEY_1:

{

nRT=TryOpen(cur*,curY);

}break;

caseKEY_2:

{

if((bm_gamepool[curY][cur*]

&~(GMARK_MARK|GMARK_BOOM))==0)

{

bm_gamepool[curY][cur*]^=GMARK_MARK;

}

}break;

caseKEY_3:

{

if(bm_gamepool[curY][cur*]&0*F)

{

intnb=bm_gamepool[curY][cur*]&0*F;

for(inty=-1;y<=1;++y)

for(int*=-1;*<=1;++*)

{

if(bm_gamepool[curY+y][cur*+*]&GMARK_MARK)

--nb;

}

if(nb==0)

{

for(inty=-1;y<=1;++y)

for(int*=-1;*<=1;++*)

{

if((bm_gamepool[curY+y][cur*+*]

&(0*F|GMARK_MARK))==0)

{

nRT|=TryOpen(cur*+*,curY+y);

}

}

}

}

}break;

caseKEY_ESC:

{

nRT=EOF;

}break;

}

if(nKey==KEY_1||nKey==KEY_3)

{

inty=1;

for(;y<=poolHeight;++y)

{

int*=1;

for(;*<=poolWidth;++*)

{

if(bm_gamepool[y][*]==0)break;

}

if(*<=poolWidth)break;

}

if(!(y<=poolHeight))

{

nRT=1;

}

}

if(nArrow==0)

{

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论