妙空

空妙
随笔 - 0, 文章 - 11, 评论 - 0, 引用 - 0
数据加载中……

STATE


class Army;
class State
{
public:
 
virtual void Attack() = 0;
 
virtual void Rest() = 0;
 
virtual void Advance() = 0;
private:
};

class AdvanceState : public State
{
public:
 AdnvaceState(Army 
*pArmy) : m_pArmy(pArmy){};
 
virtual void Advance();
 
virtual void Attack();
 
virtual void Rest();
private:
 Army 
*m_pArmy;
};

class RestState : public State
{
public:
 RestState(Army 
*pArmy) : m_pArmy(pArmy){}
 
virtual void Advance();
 
virtual void Attack();
 
virtual void Rest();
private:
 Army 
*m_pArmy;
};

class Army
{
public:
 Army()
 {
  m_pAdvanceState 
= new AdvanceState(this);
  m_pAttackState 
= new AttackState(this);
  m_pRestState 
= new RestState(this);
  m_pState 
= m_pRestState;
  m_iEnemyCount 
= 0;
 }
 
void SetState(State *pState) {m_pState = pState;}
 State
* GetAdvanceState() {return m_pAdvanceState;}
 State
* GetAttackState() {return m_pAttackState;}
 State
* GetRestState() {return m_pRestState;}
 
void Advance() {m_pState->Advance();}
 
void Attack() {m_pState->Attack();}
 
void Rest() {m_pState->Rest();}
 
void SetEmptyCount(int iEnemyCount) {m_iEnemyCount = iEnemyCount;}
 
int GetEmptyCount() {return m_iEnemyCount;}
private:
 State
* m_pAdvanceState;
 State
* m_pAttackState;
 State
* m_pRestState;
 
int m_iEnemyCount;
};


void AdvanceState::Advance()
{
 cout
<<"Advancing"<<endl;
}

void AdvanceState::Attack()
{
 m_pArmy
->SetEmptyCount(200);
 cout
<<"OK,Attack"<<endl;
 m_pArmy
->SetState(m_pArmy->GetAttackState());
}

void AdvanceState::Rest()
{
 m_pArmy
->SetEmptyCount(0);
 cout
<<"OK,Rest"<<endl;
 m_pArmy
->SetState(m_pArmy->GetRestState());
}

void RestState::Advance()
{
 cout
<<"OK,Advance"<<endl;
 m_pArmy
->SetState()
}
void RestState::Attack()
{
 m_pArmy
->SetEmptyCount(200);
 cout
<<"OK,Attack"<<endl;
 m_pArmy
->SetState(m_pArmy->GetAttackState());
}

void RestState::Rest()
{
 m_pArmy
->SetEmptyCount(0);
 cout
<<"OK,Rest"<<endl;
 m_pArmy
->SetState(m_pArmy->GetRestState());
}

void AttackState::Advance()
{
 cout
<<"We are Attacking, can not go Advance"<<endl;
}
void AttackState::Attack()
{
 m_pArmy
->SetEmptyCount(0);
 cout
<<"Attacking"<<endl;
}
void AttackState::Rest()
{
 
if(m_pArmy->GetEmptyCount()>0)
  cout
<<"There still enemies! cannot rest"<<endl;
 
else
  {
   cout
<<"OK!Rest"<<endl;
   m_pArmy
->SetState(m_pArmy->GetRestState());
  }
}
class Army;
class State
{
public:
 
virtual void Attack() = 0;
 
virtual void Rest() = 0;
 
virtual void Advance() = 0;
private:
};

class AdvanceState : public State
{
public:
 AdnvaceState(Army 
*pArmy) : m_pArmy(pArmy){};
 
virtual void Advance();
 
virtual void Attack();
 
virtual void Rest();
private:
 Army 
*m_pArmy;
};

class RestState : public State
{
public:
 RestState(Army 
*pArmy) : m_pArmy(pArmy){}
 
virtual void Advance();
 
virtual void Attack();
 
virtual void Rest();
private:
 Army 
*m_pArmy;
};

class Army
{
public:
 Army()
 {
  m_pAdvanceState 
= new AdvanceState(this);
  m_pAttackState 
= new AttackState(this);
  m_pRestState 
= new RestState(this);
  m_pState 
= m_pRestState;
  m_iEnemyCount 
= 0;
 }
 
void SetState(State *pState) {m_pState = pState;}
 State
* GetAdvanceState() {return m_pAdvanceState;}
 State
* GetAttackState() {return m_pAttackState;}
 State
* GetRestState() {return m_pRestState;}
 
void Advance() {m_pState->Advance();}
 
void Attack() {m_pState->Attack();}
 
void Rest() {m_pState->Rest();}
 
void SetEmptyCount(int iEnemyCount) {m_iEnemyCount = iEnemyCount;}
 
int GetEmptyCount() {return m_iEnemyCount;}
private:
 State
* m_pAdvanceState;
 State
* m_pAttackState;
 State
* m_pRestState;
 
int m_iEnemyCount;
};


void AdvanceState::Advance()
{
 cout
<<"Advancing"<<endl;
}

void AdvanceState::Attack()
{
 m_pArmy
->SetEmptyCount(200);
 cout
<<"OK,Attack"<<endl;
 m_pArmy
->SetState(m_pArmy->GetAttackState());
}

void AdvanceState::Rest()
{
 m_pArmy
->SetEmptyCount(0);
 cout
<<"OK,Rest"<<endl;
 m_pArmy
->SetState(m_pArmy->GetRestState());
}

void RestState::Advance()
{
 cout
<<"OK,Advance"<<endl;
 m_pArmy
->SetState()
}
void RestState::Attack()
{
 m_pArmy
->SetEmptyCount(200);
 cout
<<"OK,Attack"<<endl;
 m_pArmy
->SetState(m_pArmy->GetAttackState());
}

void RestState::Rest()
{
 m_pArmy
->SetEmptyCount(0);
 cout
<<"OK,Rest"<<endl;
 m_pArmy
->SetState(m_pArmy->GetRestState());
}

void AttackState::Advance()
{
 cout
<<"We are Attacking, can not go Advance"<<endl;
}
void AttackState::Attack()
{
 m_pArmy
->SetEmptyCount(0);
 cout
<<"Attacking"<<endl;
}
void AttackState::Rest()
{
 
if(m_pArmy->GetEmptyCount()>0)
  cout
<<"There still enemies! cannot rest"<<endl;
 
else
  {
   cout
<<"OK!Rest"<<endl;
   m_pArmy
->SetState(m_pArmy->GetRestState());
  }
}

class Army;
class State
{
public:
 
virtual void Attack() = 0;
 
virtual void Rest() = 0;
 
virtual void Advance() = 0;
private:
};

class AdvanceState : public State
{
public:
 AdnvaceState(Army 
*pArmy) : m_pArmy(pArmy){};
 
virtual void Advance();
 
virtual void Attack();
 
virtual void Rest();
private:
 Army 
*m_pArmy;
};

class RestState : public State
{
public:
 RestState(Army 
*pArmy) : m_pArmy(pArmy){}
 
virtual void Advance();
 
virtual void Attack();
 
virtual void Rest();
private:
 Army 
*m_pArmy;
};

class Army
{
public:
 Army()
 {
  m_pAdvanceState 
= new AdvanceState(this);
  m_pAttackState 
= new AttackState(this);
  m_pRestState 
= new RestState(this);
  m_pState 
= m_pRestState;
  m_iEnemyCount 
= 0;
 }
 
void SetState(State *pState) {m_pState = pState;}
 State
* GetAdvanceState() {return m_pAdvanceState;}
 State
* GetAttackState() {return m_pAttackState;}
 State
* GetRestState() {return m_pRestState;}
 
void Advance() {m_pState->Advance();}
 
void Attack() {m_pState->Attack();}
 
void Rest() {m_pState->Rest();}
 
void SetEmptyCount(int iEnemyCount) {m_iEnemyCount = iEnemyCount;}
 
int GetEmptyCount() {return m_iEnemyCount;}
private:
 State
* m_pAdvanceState;
 State
* m_pAttackState;
 State
* m_pRestState;
 
int m_iEnemyCount;
};


void AdvanceState::Advance()
{
 cout
<<"Advancing"<<endl;
}

void AdvanceState::Attack()
{
 m_pArmy
->SetEmptyCount(200);
 cout
<<"OK,Attack"<<endl;
 m_pArmy
->SetState(m_pArmy->GetAttackState());
}

void AdvanceState::Rest()
{
 m_pArmy
->SetEmptyCount(0);
 cout
<<"OK,Rest"<<endl;
 m_pArmy
->SetState(m_pArmy->GetRestState());
}

void RestState::Advance()
{
 cout
<<"OK,Advance"<<endl;
 m_pArmy
->SetState()
}
void RestState::Attack()
{
 m_pArmy
->SetEmptyCount(200);
 cout
<<"OK,Attack"<<endl;
 m_pArmy
->SetState(m_pArmy->GetAttackState());
}

void RestState::Rest()
{
 m_pArmy
->SetEmptyCount(0);
 cout
<<"OK,Rest"<<endl;
 m_pArmy
->SetState(m_pArmy->GetRestState());
}

void AttackState::Advance()
{
 cout
<<"We are Attacking, can not go Advance"<<endl;
}
void AttackState::Attack()
{
 m_pArmy
->SetEmptyCount(0);
 cout
<<"Attacking"<<endl;
}
void AttackState::Rest()
{
 
if(m_pArmy->GetEmptyCount()>0)
  cout
<<"There still enemies! cannot rest"<<endl;
 
else
  {
   cout
<<"OK!Rest"<<endl;
   m_pArmy
->SetState(m_pArmy->GetRestState());
  }
}


posted on 2008-01-21 17:01 龙人 阅读(68) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理