?? managers.cpp
字號:
// Managers.cpp: implementation of the Managers class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "program.h"
#include "Managers.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:Managers()
* 函數(shù)介紹:初始化程序
* 輸入?yún)?shù):無
* 輸出參數(shù):無
* 返回值 :無
*/
Managers::Managers()
{
int i;
for(i=0;i<4;i++)
{
GameOver[i]=true;
}
for(i=0;i<4;i++)
{
Planes[i].Color=0;
Planes[i].image=IDB_GREENPLANE;
PlReady[i]=false;
}
for(i=4;i<8;i++)
{
Planes[i].Color=1;
Planes[i].image=IDB_REDPLANE;
PlReady[i]=false;
}
for(i=8;i<12;i++)
{
Planes[i].Color=2;
Planes[i].image=IDB_YELLOWPLANE;
PlReady[i]=false;
}
for(i=12;i<16;i++)
{
Planes[i].Color=3;
Planes[i].image=IDB_BLUEPLANE;
PlReady[i]=false;
}
PAUSE=true;
Game_State=-1;
}
Managers::~Managers()
{
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:DoMsg(int steps, 當前點的格子步數(shù)
int state, 當前格子所有的狀態(tài)
int action,動作
int roll) ROLL的點數(shù)
* 函數(shù)介紹:消息響應
* 輸入?yún)?shù):action 0為鼠標經(jīng)過 1為鼠標左鍵,2鼠標右件,3表示ROLL之后的判斷
* 輸出參數(shù):bool
* 返回值 :是否響應成功
*/
bool Managers::DoMsg(int steps, int state,int action,int roll)
{
if(PAUSE)
return false;
Chess *pl;
if(state==4)//已經(jīng)到達目的地,降落了
return false;
if (action==1)
{
if((pl=Search(steps,state))==NULL)//找到目標飛機
return false;
if(roll<1 || roll>6 || state<0 || state>4 || steps<0 || steps> 56) //數(shù)據(jù)非法
return false;
PlLast=*pl;//把當前信息拷貝到PlLast中去
PlThis=pl;//把當前改變的飛機的地址傳到PlThis中
if(state==0)//還停在飛機場,需要Roll到6才能準備起飛
{
if(roll!=6)
return false;
pl->state=1;
}
return true;
}
else if(action==3)//自動走
{
if(PlAuto!=NULL)
{
PlThis=PlAuto;
PlLast=*PlAuto;
return true;
}
else
{
int k;
for(k=Game_State*4;k<Game_State*4+4;k++)
if(Planes[k].state!=0 && Planes[k].state!=4)
break;
if(k==Game_State*4+4)
{
if(roll==6)
return false;
do
{
Game_State=(Game_State+1)%4;
}
while(GameOver[Game_State]);
return true;
}
}
}
return false;
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:UpData(int roll)
* 函數(shù)介紹:游戲更新
* 輸入?yún)?shù):roll的值,因為ROLL到6可以連續(xù)走棋
* 輸出參數(shù):bool
* 返回值 :true成功
*/
bool Managers::UpData(int roll)
{
Chess &p=(*PlThis);//讓P代替PlThis存的值,提高效率,節(jié)約代碼
if(p.step==OverStep(p.Color)+6 && p.state==3)
{
p.state=4;
}
int k;
if(Game_State>=0 && Game_State<4 && p.state==2)//判斷飛行中的碰撞以及跳棋問題
{
PlBack=GoBack(PlThis,roll);//看看是不是有人要回家了
if(PlBack==PlThis)
return true;//更新結(jié)束,他絕對沒發(fā)讓游戲結(jié)束
if(p.step==(DefStep(p.Color)+18)%52)
{
p.step=(p.step+12)%52;
PlBack=GoBack(PlThis,roll);
if(PlBack==PlThis)
return true;//更新結(jié)束,他絕對沒發(fā)讓游戲結(jié)束
}
else if((p.step+3)%4==p.Color && p.step!=OverStep(p.Color))
{
p.step=(p.step+4)%52;
PlBack=GoBack(PlThis,roll);
if(PlBack==PlThis)
return true;//更新結(jié)束,他絕對沒發(fā)讓游戲結(jié)束
}
if(p.step==(DefStep(p.Color)+18)%52)
{
p.step=(p.step+12)%52;
PlBack=GoBack(PlThis,roll);
if(PlBack==PlThis)
return true;//更新結(jié)束,他絕對沒發(fā)讓游戲結(jié)束
}
}
//判斷是否有玩家勝利,是否游戲結(jié)束
for(k=Game_State*4;k<Game_State*4+4;k++)
{
if(Planes[k].state!=4)
break;
}
if(k==Game_State*4+4)
{
GameOver[Game_State]=NumOfWiner;
NumOfWiner++;
}
else if(roll!=6)
{
do
{
Game_State=(Game_State+1)%4;
}
while(GameOver[Game_State]);
}
if(NumOfWiner==1)//有3個玩家下玩就全部結(jié)束
{
Game_State=8;
PAUSE=true;
MessageBox(GetActiveWindow(),"勝利!恭喜你順利完成任務","游戲結(jié)束",0);
}
return true;
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:GameStart()
* 函數(shù)介紹:游戲開始初始化
* 輸入?yún)?shù):無
* 輸出參數(shù):無
* 返回值 :無
*/
void Managers::GameStart()
{
PlAuto=NULL;
NumOfWiner=0;
int i;
Game_State=0;
for(i=0;i<4;i++)
GameOver[i]=false;
for(i=0;i<4;i++)
{
Planes[i].state=0;
Planes[i].step=51;
PlReady[i]=true;
Planes[i].dou=0;
}
for(i=4;i<8;i++)
{
Planes[i].state=0;
Planes[i].step=12;
PlReady[i]=true;
Planes[i].dou=0;
}
for(i=8;i<12;i++)
{
Planes[i].state=0;
Planes[i].step=25;
PlReady[i]=true;
Planes[i].dou=0;
}
for(i=12;i<16;i++)
{
Planes[i].state=0;
Planes[i].step=38;
PlReady[i]=true;
Planes[i].dou=0;
}
PAUSE=false;
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:Search(int step, int state)
* 函數(shù)介紹:查找在第step步,在state狀態(tài)是否存在一個飛機
* 輸入?yún)?shù):step 步數(shù) state 狀態(tài)
* 輸出參數(shù):Chess *
* 返回值 :找到的話則是那個飛機地址,沒找到是NULL
*/
Chess * Managers::Search(int step, int state)
{
Chess *p=Planes;
int k;
for(k=0;k<16;k++)
{
if(p->state==state && p->step==step && p->Color==Game_State)
return p;
p++;
}
return NULL;
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:GoBack(Chess *p)
* 函數(shù)介紹:判斷飛機P的移動是否造成有飛機被打回家
* 輸入?yún)?shù):Chess *p 飛機P的地址
* 輸出參數(shù):Chess *
* 返回值 :被打回去的飛機地址,沒飛機被打回去返回NULL
*/
Chess * Managers::GoBack(Chess *p,int roll)
{
int i;
int dou=0;//記錄碰裝時,被碰的有幾個重疊
int Che=0;//記錄碰撞時,被碰的是幾號
if(p->state!=2)
return NULL;
for(i=0;i<16;i++)//判斷飛機是否相撞
if(Planes[i].step==p->step && Planes[i].state==2 && Planes+i != p)
//最后一句表示不是同一架飛機
{
if(Planes[i].Color==p->Color)//顏色相同,重疊之
{
p->dou=Planes[i].dou++;
PlAuto=NULL;//沒有給任何飛機自動走的機會
return NULL;
}
else //顏色不同,打回去;(重疊了多個不同顏色的就該自己回去了)
{
dou++;
Che=Che*100+i+1;
}
}
p->dou=0;//沒有自己的飛機重疊,改回0
if(dou==1)
{
Che--;
Planes[Che].state=0;
Planes[Che].step=DefStep(Planes[Che].Color);//回到出發(fā)點
return Planes+Che;
}
if(dou>=2)
{
if(roll!=6)//全部飛機都得回家
{
while(Che!=0)
{
i=Che%100-1;
Che/=100;
Planes[i].state=0;
Planes[i].step=DefStep(Planes[i].Color);
}
p->state=0;
p->step=DefStep(p->Color);
PlAuto=NULL;//沒有給任何飛機自動走的機會
return p;
}
else
{
PlAuto=p;//P可以停在多架飛機上,沒人回家,而且下次P必須自動走一次
return NULL;
}
}
PlAuto=NULL;//沒有給任何飛機自動走的機會
return NULL;
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:DefStep(int color)
* 函數(shù)介紹:得到不同顏色飛機的起飛步數(shù)
* 輸入?yún)?shù):color 飛機顏色(不同的陣營)
* 輸出參數(shù):int
* 返回值 :出生所在步數(shù)
*/
int Managers::DefStep(int color)
{
if(color==0)
return 51;
if(color==1)
return 12;
if(color==2)
return 25;
if(color==3)
return 38;
return 0;
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:OverStep(int color)
* 函數(shù)介紹:得到不同顏色飛機的降落步數(shù)
* 輸入?yún)?shù):color 飛機顏色(不同的陣營)
* 輸出參數(shù):int
* 返回值 :降落所在步數(shù)
*/
int Managers::OverStep(int color)
{
if(color==0)
return 49;
if(color==1)
return 10;
if(color==2)
return 23;
if(color==3)
return 36;
return 0;
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:StepUp(Chess *pl, int roll)
* 函數(shù)介紹:飛機向前進
* 輸入?yún)?shù):pl 飛機 roll點數(shù)
* 輸出參數(shù):bool
* 返回值 :是否成功移動
*/
bool Managers::StepUp(Chess *pl, int roll)
{
int ro=roll;
if(pl->state==1 && roll!=0)
pl->state=2;
while(ro!=0 && pl->state==2)
{
pl->step++;
if(ro>0)
ro--;
else
ro++;
if(pl->step==OverStep(pl->Color)+1)//判斷飛機是否該降落
{
pl->state=3;
}
else if(SearchTwice(pl->step,pl->Color))
{
if(roll==6)
ro=0;
else
ro*=-1;
}
else if(pl->step>51)
pl->step-=52;
}
if(pl->state==3 && ro!=0)
{
int k;
k=OverStep(pl->Color)+6;//飛機降落點
if(pl->step+ro==k)
{
pl->step=k;
pl->state=4;
}
else if(pl->step+ro<k)
pl->step+=ro;
else
{
pl->step=k-ro+(k-pl->step);
}
}
return true;
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:SearchTwice(int step,int notcolor)
* 函數(shù)介紹:搜索在step的地方是否存在2個或以上不是notcolor的飛機重疊(只有state==2才算)
* 輸入?yún)?shù):step 步數(shù),notcolor 重疊飛機不能是的顏色
* 輸出參數(shù):bool
* 返回值 :是否存在
*/
bool Managers::SearchTwice(int step,int butcolor)
{
int k,i;
i=0;
for(k=0;k<16;k++)
if(Planes[k].state==2 && Planes[k].step==step && Planes[k].Color!=butcolor)
i++;
if(i>1)
return true;
return false;
}
////////////////////////////////////////////////////////////////////////
/*
* 函數(shù)名稱:OneStep(int gh, int roll)
* 函數(shù)介紹:飛機一次只非一步
* 輸入?yún)?shù):gh 向前飛還是向后飛,roll 丟的色子數(shù)
* 輸出參數(shù):int
* 返回值 :下一步是向前飛還是向后飛
*/
int Managers::OneStep(int gh, int roll)
{
Chess *pl=PlThis;
if(pl->state==1)
pl->state=2;
if(pl->state==2)
{
if(SearchTwice(pl->step,pl->Color) && pl!=PlAuto)
{
if(roll!=6)
{
pl->step-=gh;
gh*=-1;
}
else
{
return 1;
}
}
else
{
pl->step+=gh;
}
if(pl->step==OverStep(pl->Color)+1)//判斷飛機是否該降落
{
pl->state=3;
}
else if(pl->step>51)
pl->step-=52;
else if(pl->step<0)
pl->step=51;
}
else if(pl->state==3)
{
int k;
k=OverStep(pl->Color)+6;//飛機降落點
if(pl->step==k)
{
pl->step--;
gh=-1;
}
else
{
pl->step+=gh;
}
}
return gh;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -