c++实现拼图游戏Word文档下载推荐.docx
《c++实现拼图游戏Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《c++实现拼图游戏Word文档下载推荐.docx(26页珍藏版)》请在冰豆网上搜索。
(要求写详细过程)
该部分必须包括:
系统结构框图、各模块流程图、关键代码、主要的数据结构、测试与评价等
2.1
2.3头文件(宋体,小三)
#include"
stdh.h"
classBoard{
private:
intm_b[MAXN_BOARD][MAXN_BOARD];
//emptyblockposition:
exey
//oldemptyblockpositionoex,oey
intm_ex,m_ey;
intm_oex,m_oey;
intm_dir;
//reverse-directionofemptyblock
boolm_bMove;
//iftheboardmoved
intm_nx,m_ny;
intm_nBlock;
public:
Board();
intCreate();
boolCheck();
//checkifthegamewin
intGetMat(int,int);
intSetMat(int,int,int);
intSetXY(int,int);
intGetEX();
intGetEY();
intGetOEX();
intGetOEY();
intGetDir();
intMove(int,int);
boolCheckMove();
intResetMove();
intUp();
intDown();
intLeft();
intRight();
intClick(int,int);
//whenUserclickthepicture
intInit();
};
/*
ClassFlipstoredallelementsoftheflipping-block.
Itwillbeusedwhentheclickedblockmovingtothedestination.
*/
classFlip{
intm_cnt;
intm_ux,m_uy;
//uX,uYpixelsaremovedperm_time
intm_time;
//uSeconds
//directionofflip
intm_cx,m_cy;
//currentposofblock
intm_sx,m_sy;
//sourceposofblock
intm_dx,m_dy;
//destinationposofblock
intGetXY(int,int&
int&
);
intGetTime();
intSetXY(int,int,int,int);
intSetTime(int);
intSetCnt(int,int,int);
intSetDir(int);
boolFinish(HWND);
//checkiftheflippingover
Flip.h"
Board.h"
classGame{
Boardm_board;
Flipm_flip;
intm_vFlip;
intm_xFlip,m_yFlip;
HBITMAPm_hBmp;
HBITMAPm_hMemBmp;
BITMAPm_bmp;
HDCm_mdc,m_bufdc;
//doublecache
HWNDm_hWnd;
Game();
intSetLevel(int,int);
intLoadBmp(HINSTANCE,int);
intInit(HWND);
//Initthegame
intCreate(HWND);
intPaint(HWND);
//Userclickedthepicture
intKey(WPARAM);
//Userpushdownthekeyboard
intInitFlip();
//initthem_flipandthenflippingstart.
boolCheckWin();
intRelease(HWND);
//{{NO_DEPENDENCIES}}
//MicrosoftVisualC++generatedincludefile.
//Usedbyres.rc
//
#defineIDB_AC103
#defineIDB_BH104
//Nextdefaultvaluesfornewobjects
//
#ifdefAPSTUDIO_INVOKED
#ifndefAPSTUDIO_READONLY_SYMBOLS
#define_APS_NEXT_RESOURCE_VALUE105
#define_APS_NEXT_COMMAND_VALUE40001
#define_APS_NEXT_CONTROL_VALUE1001
#define_APS_NEXT_SYMED_VALUE101
#endif
#pragmaonce
#include<
windows.h>
ctime>
cmath>
usingnamespacestd;
constintMAXN_BOARD=10;
constintCLIENT_X=1024;
constintCLIENT_Y=768;
constintOFFSET_LINE=1;
constintDIR_UP=0;
constintDIR_DOWN=1;
constintDIR_LEFT=2;
constintDIR_RIGHT=3;
constintFLIP_CNT=8;
constintFLIP_TIME=18;
2.2源文件(宋体,小三)
Board:
:
Board()
{
memset(m_b,-1,sizeof(m_b));
m_nx=m_ny=m_ex=m_ey=0;
m_nBlock=0;
}
intBoard:
Create()
return0;
SetXY(intnx,intny)
m_nx=nx;
m_ny=ny;
m_nBlock=m_nx*m_ny;
m_oex=m_ex=m_nx-1;
m_oey=m_ey=m_ny-1;
SetMat(intx,inty,intval)
m_b[y][x]=val;
returnval;
GetMat(intx,inty)
returnm_b[y][x];
GetEX()
returnm_ex;
GetEY()
returnm_ey;
GetOEX()
returnm_oex;
GetOEY()
returnm_oey;
GetDir()
returnm_dir;
boolBoard:
Check()
intck=0;
boolflag=true;
for(inti=0;
i<
m_ny;
++i)
for(intj=0;
j<
m_nx;
++j)
{
if(ck!
=m_b[i][j])
{
flag=false;
break;
}
++ck;
}
if(flag==false&
&
ck<
m_nBlock-1)
returnfalse;
returntrue;
Init()
//VARIABLE:
nStep,nLoop
//
//nStep:
//Inordertogeneratetherandomblockpositionsand
//tomakesure<
TheProblem>
musthaveasolution,we
//havetosimulateThe-Inverse-Actionoftheuser.
//ThennStepmeanwewillexecutetheInverse-Actiontimes.
//AndeachActionwillberandomgenerated.
//nLoop:
//Duetothereasonthatthemethodthatabove-mentionedhas
//abigDrawback:
ifthenumberofblocksishuge,thenmethodwould
//onlychangeasmallareaofall.AvoidingthisIaddanewvariable
//nloop,whichmeansasameactionsthatexecutedtimes.Itwasproved
//thatafterthisimprovedthenew-methodworkwell.
intnStep=(rand()&
((1<
<
8)-1))+100;
intnLoop;
inttmp;
intid=0;
m_b[i][j]=id;
++id;
m_b[m_ny-1][m_nx-1]=-1;
m_oey=m_ey;
m_oex=m_ex;
nStep;
{
tmp=(rand()&
3);
nLoop=rand()&
7;
//loopofaction
nLoop;
switch(tmp)
case0:
this->
Up();
break;
case1:
Down();
case2:
Left();
case3:
Right();
}
Move(intnx,intny)
m_ex=nx;
m_ey=ny;
CheckMove()
returnm_bMove;
ResetMove()
m_bMove=false;
Up()
Move(m_ex,m_ey);
if(m_ey==0)
return-1;
m_b[m_ey][m_ex]=m_b[m_ey-1][m_ex];
m_b[m_ey-1][m_ex]=-1;
--m_ey;
m_dir=DIR_DOWN;
m_bMove=true;
Down()
if(m_ey==m_ny-1)
m_b[m_ey][m_ex]=m_b[m_ey+1][m_ex];
m_b[m_ey+1][m_ex]=-1;
++m_ey;
m_dir=DIR_UP;
Left()
if(m_ex==0)
m_b[m_ey][m_ex]=m_b[m_ey][m_ex-1];
m_b[m_ey][m_ex-1]=-1;
--m_ex;
m_dir=DIR_RIGHT;
Right()
if(m_ex==m_nx-1)
m_b[m_ey][m_ex]=m_b[m_ey][m_ex+1];
m_b[m_ey][m_ex+1]=-1;
++m_ex;
m_dir=DIR_LEFT;
Click(intx,inty)
if(x<
0||x>
=m_nx||y<
0||y>
=m_ny)
if(abs(x-m_ex)+abs(y-m_ey)>
1)
if(x-m_ex>
0)
this->
return0;
if(m_ex-x>
if(y-m_ey>
if(m_ey-y>
PingTu.h"
Game:
Game()
m_nx=m_ny=m_ux=m_uy=0;
intGame:
Create(HWNDhWnd)
m_mdc=CreateCompatibleDC(GetDC(hWnd));
m_bufdc=CreateCompatibleDC(GetDC(hWnd));
srand(unsigned(time(NULL)));
m_board.Create();
Init(HWNDhWnd)
m_ux=m_bmp.bmWidth/m_nx;
m_uy=m_bmp.bmHeight/m_ny;
m_hWnd=hWnd;
m_board.Init();
MoveWindow(hWnd,0,0,
m_bmp.bmWidth+m_ux+m_nx*OFFSET_LINE+20,
m_bmp.bmHeight+20,
TRUE);
//initializeflip
m_flip.SetCnt(FLIP_CNT,m_ux,m_uy);
m_flip.SetTime(FLIP_TIME);
SetLevel(intx,inty)
m_nx=x;
m_ny=y;
m_board.SetXY(x,y);
LoadBmp(HINSTANCEhInst,intid)
m_hBmp=LoadBitmap(hInst,MAKEINTRESOURCE(id));
GetObject(m_hBmp,sizeof(BITMAP),&
m_bmp);
Paint(HWNDhWnd)
intx,y,k,i,j;
intcur_block;
HDChdc;
hdc=GetDC(hWnd);
DeleteObject(m_hMemBmp);
m_hMemBmp=CreateCompatibleBitmap(GetDC(hWnd),CLIENT_X,CLIENT_Y);
SelectObject(m_mdc,m_hMemBmp);
SelectObject(m_bufdc,m_hBmp);
cur_block=m_board.GetMat(m_board.GetOEX(),m_board.GetOEY());
for(i=0;
for(j=0;
k=m_board.GetMat(j,i);
x=k%m_nx;
y=k/m_nx;
if(-1==k)
//StretchBlt(m_mdc,j*m_ux+OFFSET_LINE,i*m_uy+OFFSET_LINE,
//m_ux-2*OFFSET_LINE,m_uy-2*OFFSET_LINE,
//m_bufdc,0,0,m_bmp.bmWidth,m_bmp.bmHeight,SRCCOPY);
continue;
elseif(k==cur_block&
!
m_flip.Finish(hWnd))
m_flip.GetXY(m_board.GetDir(),x,y);
BitBlt(m_mdc,x,y,m_ux,m_uy,
m_bufdc,(k%m_nx)*m_ux,(k/m_nx)*m_uy,SRCCOPY);
else
BitBlt(m_mdc,j*(m_ux+(OFFSET_LINE<
1))+(OFFSET_LINE),
i*(m_uy+(OFFSET_LINE<
1))+OFFSET_LINE,m_ux,m_uy,
m_bufdc,x*m_ux,y*m_uy,SRCCOPY);
BitBlt(hdc,0,0,m_bmp.bmWidth,m_bmp.bmHeight,m_mdc,0,0,SRCCOPY);
StretchBlt(hdc,m_bmp.bmWidth,0,m_ux,m_uy,
m_bufdc,0,0,m_bmp.bmWidth,m_bmp.bmHeight,SRCCOPY);
Click(intmx,intmy)