C编写的小游戏

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C编写的小游戏集团档案编码:[YTTR-YTPT28-YTNTL98-UYTYNN08]

#i n c l u d e

usingnamespacestd;

constcharMOUSE='*';

constcharWAY='';

constcharWALL='@';

constcharPASS='.';

constcharIMPASS='X';

classGame{

public:

Game(size_tuWidth,size_tuHeight):m_uWidth(uWidth),m_uHeight(uHe ight),m_pcMaze(newchar[uWidth*uHeight]),m_mouse(0,1){

srand(time(NULL));

char(*pcMaze)[m_uWidth]=reinterpret_cast(m_p cMaze);

for(size_ti=0;i

for(size_tj=0;j

if((i==1&&j<4)||(i==m_uHeight-2&&j>m_uWidth-5))

pcMaze[i][j]=WAY;

elseif(i==0||i==m_uHeight-

1||j==0||j==m_uWidth-1)

pcMaze[i][j]=WALL;

else

pcMaze[i][j]=rand()%4?WAY:WALL;

}

~Game(void){

if(m_pcMaze){

delete[]m_pcMaze;

m_pcMaze=NULL;

}

}

voidRun(void){

for(Show();!Quit()&&Step(););

}

private:

classMouse{

public:

Mouse(size_tx,size_ty):m_x(x),m_y(y){}

size_tGetX(void){returnm_x;}

size_tGetY(void){returnm_y;}

voidStepRight(void){

m_x++;

m_brain.Remember(EDIR_RIGHT);

}

voidStepDown(void){

m_y++;

m_brain.Remember(EDIR_DOWN);

}

voidStepLeft(void){

m_x--;

m_brain.Remember(EDIR_LEFT);

}

voidStepUp(void){

m_y--;

m_brain.Remember(EDIR_UP);

}

voidStepBack(void){

switch(m_brain.Recollect()){

caseEDIR_RIGHT:

m_x--;

break;

caseEDIR_DOWN:

m_y--;

break;

caseEDIR_LEFT:

m_x++;

break;

caseEDIR_UP:

m_y++;

break;

}

}

private:

typedefenumtag_Direction{

EDIR_RIGHT,

EDIR_DOWN,

EDIR_LEFT,

EDIR_UP,

EDIR_STILL

}EDIR;

classBrain{

public:

Brain(void):m_pMemory(NULL){}

~Brain(void){

for(Step*pStep=m_pMemory,*pLast;pStep;pStep=pLast){

pLast=pStep->m_pLast;

deletepStep;

}

}

voidRemember(EDIReDir){

m_pMemory=newStep(eDir,m_pMemory);

}

EDIRRecollect(void){

EDIReDir=EDIR_STILL;

if(m_pMemory){

Step*pStep=m_pMemory;

m_pMemory=m_pMemory->m_pLast;

eDir=pStep->m_eDir;

deletepStep;

}

returneDir;

}

private:

classStep{

public:

Step(EDIReDir,Step*pLast):m_eDir(eDir),m_pLast(pLast){}

EDIRm_eDir;

Step*m_pLast;

};

Step*m_pMemory;

};

size_tm_x;

size_tm_y;

Brainm_brain;

};

voidShow(void){

char(*pcMaze)[m_uWidth]=reinterpret_cast(m_p cMaze);

for(size_ti=0;i

for(size_tj=0;j

cout<

cout<

}

}

boolQuit(void){