状态模式2

/****************************
* 文件名:  状态模式2
* 创建人:  陈泽丹
* 创建时间:20100906
* 版本:    2.0
****************************/

#pragma once
#include "State.h"


class GameModel
{
public:
 GameModel():m_bLive(true),m_iSuccees(0){}
 virtual ~GameModel(){}
 void BeKill() { m_bLive = false;}
 void AddSuccees() { m_iSuccees++; }
 bool getLive() { return m_bLive; }
 int getSuccees() { return m_iSuccees; }
 
private:
 bool m_bLive;
 int m_iSuccees;
};

class StateControl: public State
{
public:
 StateControl(GameModel* i_pGameData);
 virtual ~StateControl(void);

 bool setState(State* i_pState);
 bool handle();
 bool toStarting();
 bool toRuning();
 bool toFighting();
 bool toStoping();

 State* m_pStarting;
 State* m_pRuning;
 State* m_pFighting;
 State* m_pStoping;
 GameModel* m_pGameModel;
private:
 State* m_pState;
};

 

//////////////////////////////

 

#pragma once
#include <iostream>

 


class State
{
protected:
 State(void){}
 
public:
 virtual ~State(void){}
 virtual bool handle() { return false;}
 virtual bool toStarting() { return false;}
 virtual bool toRuning() { return false;}
 virtual bool toFighting() { return false;}
 virtual bool toStoping() { return false;}
};

//////////////////////////////

 

#pragma once
#include "State.h"
#include "StateControl.h"


class Starting: public State
{
public:
 Starting(StateControl* i_pState);
 virtual ~Starting(void);

 bool handle();
 bool toRuning();
 bool toStoping();

private:
 StateControl* m_pState;
};

//////////////////////////////

 

#pragma once
#include "State.h"
#include "StateControl.h"


class Runing: public State
{
public:
 Runing(StateControl* i_pState);
 virtual ~Runing(void);

 bool handle();
 bool toFighting();
 bool toStoping();

private:
 StateControl* m_pState;
};

///////////////////////////

 

#pragma once
#include "State.h"
#include "StateControl.h"


class Fighting: public State
{
public:
 Fighting(StateControl* i_pState);
 virtual ~Fighting(void);
 bool handle();
 bool toRuning();
 bool toStoping();


private:
 StateControl* m_pState;
};

//////////////////////////////

 

#pragma once
#include "State.h"
#include "StateControl.h"


class Stoping: public State
{
public:
 Stoping(StateControl* i_pState);
 virtual ~Stoping(void);
 bool handle();

private:
 StateControl* m_pState;
};

//////////////////////////

 

#include "StateControl.h"
#include "Starting.h"
#include "Runing.h"
#include "Fighting.h"
#include "Stoping.h"


StateControl::StateControl(GameModel* i_pGameData):
m_pGameModel(i_pGameData),
m_pState(NULL),
m_pStarting(NULL)
{
 m_pStarting = new Starting(this);
 m_pRuning = new Runing(this);
 m_pFighting = new Fighting(this);
 m_pStoping = new Stoping(this);
}

StateControl::~StateControl(void)
{
 if( NULL != m_pStarting)
  delete m_pStarting;
 m_pStarting = NULL;

 if( NULL != m_pRuning)
  delete m_pRuning;
 m_pRuning = NULL;

 if( NULL != m_pFighting)
  delete m_pFighting;
 m_pFighting = NULL;

 if( NULL != m_pStoping)
  delete m_pStoping;
 m_pStoping = NULL;

 m_pGameModel = NULL;
}

bool StateControl::setState(State* i_pState)
{
 m_pState = i_pState;
 return handle();
}

bool StateControl::handle()
{
 if( NULL == m_pState)
  return false;
 return m_pState->handle();
}

bool StateControl::toStarting()
{
 if( NULL == m_pState)
  return false;
 return m_pState->toStarting();
}

bool StateControl::toRuning()
{
 if( NULL == m_pState)
  return false;
 return m_pState->toRuning();
}

bool StateControl::toFighting()
{
 if( NULL == m_pState)
  return false;
 return m_pState->toFighting();
}

bool StateControl::toStoping()
{
 if( NULL == m_pState)
  return false;
 return m_pState->toStoping();
}

 

//////////////////////////

 

#include "Starting.h"
#include <iostream>

using namespace std;

class ViewStart
{
public:
 void show()
 {
  cout<<"<  Starting!   >"<<endl;
 }
};

Starting::Starting(StateControl* i_pState)
{
 m_pState = i_pState;
}

Starting::~Starting(void)
{
 m_pState = NULL;
}

bool Starting::handle()
{
 ViewStart vsStart;
 vsStart.show();
 return true;
}

bool Starting::toRuning()
{
 return m_pState->setState(m_pState->m_pRuning);
}

bool Starting::toStoping()
{
 return m_pState->setState(m_pState->m_pStoping);
}

 

///////////////////////////////////

 

#include "Runing.h"
#include <iostream>


using namespace std;


class ViewRun
{
public:
 void show()
 {
  cout<<"<  Runing!     >"<<endl;
 }
};


Runing::Runing(StateControl* i_pState)
{
 m_pState = i_pState;
}

Runing::~Runing(void)
{
 m_pState = NULL;
}

bool Runing::handle()
{
 ViewRun vrRun;
 vrRun.show();
 int iRand = rand()%4;
 if( iRand < 3)
  toFighting();
 else
  toStoping();
 return false;
}

bool Runing::toFighting()
{
 return m_pState->setState(m_pState->m_pFighting);
}

bool Runing::toStoping()
{
 return m_pState->setState(m_pState->m_pStoping);
}

 

///////////////////////////////////

 

#include "Fighting.h"
#include <iostream>

using namespace std;

class ViewFight
{
public:
 void show()
 {
  cout<<"<  Fighting!   >"<<endl;
 }
 void showSucceed()
 {
  cout<<"<  Kill enemy! >"<<endl;
 }
 void showFailded()
 {
  cout<<"<  Be killed!  >"<<endl;
 }
};


Fighting::Fighting(StateControl* i_pState)
{
 m_pState = i_pState;
}

Fighting::~Fighting(void)
{
 m_pState = NULL;
}

bool Fighting::handle()
{
 ViewFight vfFight;
 vfFight.show();
 int iRand = rand()%4;
 if( iRand < 3)
 {
  vfFight.showSucceed();
  m_pState->m_pGameModel->AddSuccees();
  toRuning();
 }
 else
 {
  vfFight.showFailded();
  m_pState->m_pGameModel->BeKill();
  toStoping();
 }
 return true;
}

bool Fighting::toRuning()
{
 return m_pState->setState(m_pState->m_pRuning);
}

bool Fighting::toStoping()
{
 return m_pState->setState(m_pState->m_pStoping);
}

 

////////////////////////////

 

#include "Stoping.h"
#include <iostream>

using namespace std;


Stoping::Stoping(StateControl* i_pState)
{
 m_pState = i_pState;
}

Stoping::~Stoping(void)
{
 m_pState = NULL;
}

bool Stoping::handle()
{
 
 if( m_pState->m_pGameModel->getLive())
 {
  cout<<"/nGameOver, Succeed! "<<endl;
  cout<<"Kill enemys: "<<m_pState->m_pGameModel->getSuccees()<<endl;
 }
 else
 {
  cout<<"/nGameOver, Filed! "<<endl;
  cout<<"Kill enemys: "<<m_pState->m_pGameModel->getSuccees()<<endl;
 }
 
 return false;
}

 

//////////////////////////////////////////

 

#include <time.h>
#include <iostream>
#include "StateControl.h"


using namespace std;

void main()
{
 srand(time(0));
 for(int i=0; i<5; i++)
 {
  cout<<"***************************************"<<endl;
  GameModel game;
  StateControl csState(&game);
  csState.setState(csState.m_pStarting);
  csState.toRuning();
  cout<<"***************************************/n/n/n"<<endl;
 }


 system("pause");
}

你可能感兴趣的:(kill,null,delete,System,Class,2010)