




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
-
.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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 肝臟疾病病理班
- 窒息患兒的急救流程與管理
- T/ZHCA 603-2021化妝品生產(chǎn)企業(yè)消毒技術規(guī)范
- 游戲機課程設計
- 院外急救基礎與操作規(guī)范
- 2025西昌學院輔導員考試試題及答案
- 2025西安電力高等??茖W校輔導員考試試題及答案
- 認真寫好楷書課件
- 貴安新區(qū)科創(chuàng)產(chǎn)業(yè)發(fā)展有限公司招聘筆試題庫2025
- 貴州金沙能源投資集團有限公司招聘筆試題庫2025
- 貴州游船傾覆防災減災安全教育時事熱點
- 公務員法律考試題及答案
- 黑龍江省大慶市石油高級中學2024-2025學年高二上學期期末語文試題 含解析
- 呼吸性酸中毒試題及答案
- 基于深度學習的手術機器人在后交叉韌帶斷裂中的導航優(yōu)化-洞察闡釋
- 檢察院相關試題及答案
- 安全生產(chǎn)管理機制
- 遴選公務員筆試真題及答案
- 高瞻課程師幼互動
- 鍋爐檢修作業(yè)安全保障方案
- 2025-2030中國三醋酸纖維素膜行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
評論
0/150
提交評論