#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#define Max 5
#define NULL 0
#define bool int
#define false 0
#define true 1
using namespace std;
class ArmyGroup;
class Soldier //Soldier 类定义
{
public:
Soldier();
~Soldier();
virtual bool MakeDecision(ArmyGroup *parmy);//
virtual bool Attack();//
virtual void Rest();
virtual bool TakingDrug();
virtual void GetDrug(){DrugNum+=pTarget->GetDrugNum();} //获得敌人药瓶函数;
virtual void Hurted(int i){HP-=i;}
virtual void NPHurted(){HP-=0.5*MaxHP;}
virtual bool Die(){if(HP<=0)return 1;else return NULL;}
virtual void Update();
public:
int GetLevel(){return Level;}
int GetMaxHP(){return MaxHP;}
int GetHP(){return HP;}
int GetSP(){return SP;}
int GetMaxSP(){return MaxSP;}
int GetExp(){return Exp;}
int GetDrugNum(){return DrugNum;}
public:
virtual void GetTarget(ArmyGroup *parmy,int Target);
virtual void Print();
Soldier* pTarget;////////////////////////////////////////////////important!
public:
virtual bool WizardAttack(){return 0;}//wizard攻击行为函数
virtual bool MasterAttack(){return 0;}
virtual int GetMaxMP(){return 0;}
virtual int GetMP(){return 0;}
virtual int GetMaxNP(){return 0;}
virtual int GetNP(){return 0;}
protected:
char name;
int Level; //等级
int MaxHP; //最大生命
int HP; //当前生命
int MaxSP; //最大体力
int SP; //当前体力
int Damage; //攻击力
int Exp; //经验
int DrugNum; //药瓶数量
int Counter; //升级计数器
};
class Wizard:public Soldier //Wizard类定义
{
public:
Wizard();
virtual bool MakeDecision(ArmyGroup *parmy); //Wizard决策函数
virtual bool WizardAttack(); //攻击行为函数
virtual void Rest();
virtual void Print();
virtual void Update();
public:
virtual int GetMaxMP(){return MaxMP;}
virtual int GetMP(){return MP;}
protected:
int MaxMP; //最大魔法值
int MP; //当前魔法值
};
class Master:public Wizard //Master类定义
{
public:
Master();
bool MakeDecision(ArmyGroup *parmy); //Master决策函数
bool MasterAttack(); //攻击行为函数
void Rest();
virtual void Print();
virtual void Update();
public:
int GetMaxNP(){return MaxNP;}
int GetNP(){return NP;}
protected:
int MaxNP; //最大内力
int NP; //当前内力
};
//军团定义
class ArmyGroup
{
public:
ArmyGroup();
~ArmyGroup();
public:
Soldier* pSOLDIER;
Wizard * pWIZARD;
Master * pMASTER;
Soldier * pRole;
void GetRole(int Role);
};
class WarSystem//战斗系统类定义
{
public:
WarSystem(); //WarSystem构造函数
bool StartWar(); //开局
void RunGame(); //游戏进程
bool StartRound(bool r); //回合展开
bool Result(); //结果
~WarSystem();
private:
ArmyGroup* parmyA;
ArmyGroup* parmyB;
};
Soldier::Soldier() //Soldier构造函数
{
Level =1;
MaxHP =100;
HP =100;
MaxSP =100;
SP =100;
Damage =20;
Exp =0;
DrugNum=5;
Counter=0;
pTarget = new Soldier;
}
Soldier::~Soldier()
{
delete pTarget;
}
void Soldier::Print() //生理状况输出
{
cout<<"Level: "<<GetLevel()<<" Exp:"<<GetExp()<<" DrugNum: "<<GetDrugNum()<<endl;
cout<<" MaxHP: "<<GetMaxHP()<<" HP: "<<GetHP()<<" MaxSP: "<<GetMaxSP()<<" SP: "<<GetSP()<<endl;
}
bool Soldier::MakeDecision(ArmyGroup *parmy) //决策行为函数
{
int Decision;
cout<<"please input decision!"<<endl;
cin>>Decision;
switch (Decision)
{
case 0: if(SP>=10)
{
int Target;
cout<<"please input target for the Attack!"<<endl;
cin>>Target;
GetTarget(parmy,Target);
Attack();
}
break;
case 1: if(SP<0.3*MaxSP)Rest();
break;
case 2: if(HP<0.5*MaxHP)TakingDrug();
break;
default:
break;
}
if((Decision>=0)&&(Decision<=2))
return 1;
else
return 0;
}
void Soldier::GetTarget(ArmyGroup *parmy,int Target)
{
if((Target<=3)&&(Target>=1))
pTarget = ((parmy->pSOLDIER)+Target-1);
else if(Target<=5)
pTarget = ((parmy->pWIZARD)+Target-4);
else if(Target==6)
pTarget = parmy->pMASTER;
else
pTarget = 0;
}
bool Soldier::Attack() //攻击行为函数
{
SP-=10; Exp+=5;
Counter++;
if(Counter==4){Counter=0;Update();} //升级
int i=Damage;
pTarget->Hurted(i);
if(pTarget->Die())GetDrug();//敌人已死,获得药瓶
return 1;
}
//休息行为函数:
void Soldier::Rest()
{
(HP+0.5*MaxHP)>MaxHP?HP=MaxHP:HP+=0.5*MaxHP;
(SP+0.5*MaxSP)>MaxSP?SP=MaxSP:SP+=0.5*MaxSP;
}
//升级函数:
void Soldier::Update()
{
Level+=1;MaxHP+=20;MaxSP+=20;Damage+=20;
HP=int(HP*(MaxHP/(MaxHP-20)));
SP=int(SP*(MaxSP/(MaxSP-20)));
}
//吃药行为函数:
bool Soldier::TakingDrug()
{
if(DrugNum==0)return NULL;
else {DrugNum--;(HP+0.8*MaxHP)>MaxHP?HP=MaxHP:HP+=0.8*MaxHP;return 1;}
}
//Wizard构造函数:
Wizard::Wizard():Soldier()
{
MaxMP = 100;
MP = 100;
}
//Wizard显示函数:
void Wizard::Print()
{
cout<<"Level: "<<GetLevel()<<" Exp:"<<GetExp()<<" DrugNum: "<<GetDrugNum()<<endl;
cout<<" MaxHP: "<<GetMaxHP()<<" HP: "<<GetHP()<<" MaxSP: "<<GetMaxSP()<<" SP: "<<GetSP()<<" MaxMP: "<<GetMaxMP()<<" MP: "<<GetMP()<<endl;
}
//Wizard决策行为函数:
bool Wizard::MakeDecision(ArmyGroup *parmy)
{
int Decision;
cout<<"please input decision!"<<endl;
cin>>Decision;
switch (Decision)
{
case 0: if(SP>=10)
{
int Target;
cout<<"please input target for the Attack!"<<endl;
cin>>Target;
GetTarget(parmy,Target);
Attack();
}
break;
case 1: if(SP<0.3*MaxSP)Rest();
break;
case 2: if(HP<0.5*MaxHP)TakingDrug();
break;
case 3: for(int a=2;a>0;a--)
{
if((MP>=25)&&(SP>=10))
{
int TargetW;
cout<<"please input target for the WizardAttack!"<<endl;
cin>>TargetW;
GetTarget(parmy,TargetW);
WizardAttack();
}
}
break;
default:
break;
}
if((Decision>=0)&&(Decision<=3))
return 1;
else
return NULL;
}
//Wizard攻击行为函数:
bool Wizard::WizardAttack()
{
SP-=10; MP-=25; Exp+=5;
Counter++;
if(Counter==4){Counter = 0;Update();}//调用升级函数
int i=Damage;
pTarget->Hurted(i);
if(pTarget->Die())GetDrug();//敌人已死,获得药瓶
return 1;
}
//Wizard休息行为函数:
void Wizard::Rest()
{
(HP+0.5*MaxHP)>MaxHP?HP=MaxHP:HP+=0.5*MaxHP;
(SP+0.5*MaxSP)>MaxSP?SP=MaxSP:SP+=0.5*MaxSP;
(MP+0.3*MaxMP)>MaxMP?MP=MaxMP:MP+=0.3*MaxMP;
}
//Wizard升级函数
void Wizard::Update()
{
Level+=1;
MaxHP+=20;MaxSP+=20;Damage+=20;MaxMP+=20;
HP=int(HP*(MaxHP/(MaxHP-20)));
SP=int(SP*(MaxSP/(MaxSP-20)));
MP=int(MP*(MaxMP/(MaxMP-20)));
}
//Master构造函数
Master::Master():Wizard()
{
MaxNP = 100;
NP = 100;
}
void Master::Print()
{
cout<<"Level: "<<GetLevel()<<" Exp:"<<GetExp()<<" DrugNum: "<<GetDrugNum()<<endl;
cout<<" MaxHP: "<<GetMaxHP()<<" HP: "<<GetHP()<<" MaxSP: "<<GetMaxSP()
<<" SP: "<<GetSP()<<" MaxMP: "<<GetMaxMP()<<" MP: "<<GetMP()<<" MaxNP: "<<GetMaxNP()<<" NP: "<<GetNP()<<endl;
}
//Master决策行为函数
bool Master::MakeDecision(ArmyGroup *parmy)
{
int Decision;
cout<<"please input decision!"<<endl;
cin>>Decision;
switch (Decision)
{
case 0: if(SP>=10)
{
int Target;
cout<<"please input target for the Attack!"<<endl;
cin>>Target;
GetTarget(parmy,Target);
Attack();
}
break;
case 1: if(SP<0.3*MaxSP)Rest();
break;
case 2: if(HP<0.5*MaxHP)TakingDrug();
break;
case 3: for(int a=2;a>0;a--)
{
if((MP>=25)&&(SP>=10))
{
int TargetW;
cout<<"please input target for the WizardAttack!"<<endl;
cin>>TargetW;
GetTarget(parmy,TargetW);
WizardAttack();
}
}
break;
case 4: if((NP>=50)&&(SP>=30))
{
int TargetM;
cout<<"please input target for the MasterAttack!"<<endl;
cin>>TargetM;
GetTarget(parmy,TargetM);
MasterAttack();
}
break;
default:
break;
}
if((Decision>=0)&&(Decision<=4))
return 1;
else
return NULL;
}
bool Master::MasterAttack()
{
NP-=50;SP-=30;Exp+=5;
Counter++;
if(Counter==4){Counter=0;Update();}//调用升级函数
(pTarget)->NPHurted();
if((pTarget)->Die())GetDrug();//敌人已死,获得药瓶
return 1;
}
void Master::Rest() //Master休息函数
{
(HP+0.5*MaxHP)>MaxHP?HP=MaxHP:HP+=0.5*MaxHP;
(SP+0.5*MaxSP)>MaxSP?SP=MaxSP:SP+=0.5*MaxSP;
(MP+0.3*MaxMP)>MaxMP?MP=MaxMP:MP+=0.3*MaxMP;
(NP+0.3*MaxNP)>MaxNP?NP=MaxNP:NP+=0.3*MaxNP;
}
void Master::Update()
{
Level+=1;MaxHP+=20;MaxSP+=20;Damage+=20;
HP=int(HP*(MaxHP/(MaxHP-20)));
SP=int(SP*(MaxSP/(MaxSP-20)));
MaxMP+=20;
MP=int(MP*(MaxMP/(MaxMP-20)));
MaxNP+=20;
NP=int(NP*(MaxNP/(MaxNP-20)));
}
////////////////////////////////////////////////////////////////////////////////////////////以下为军团类各个成员函数:
//ArmyGroup构造函数
ArmyGroup::ArmyGroup()
{
pSOLDIER = new Soldier[3];
pWIZARD = new Wizard[2];
pMASTER = new Master;
}
//ArmyGroup析构函数
ArmyGroup::~ArmyGroup()
{
delete [] pSOLDIER;
delete [] pWIZARD;
delete pMASTER;
}
void ArmyGroup::GetRole(int Role)//抽取玩家的军团中的一个角色当做一次回合中的主角色
{
if((Role<=3)&&(Role>=1))
pRole = pSOLDIER+Role-1;
else if(Role<=5)
pRole = pWIZARD+Role-4;
else if(Role==6)
pRole = pMASTER;
else
pRole = 0;
}
//战斗系统类构造函数:
WarSystem::WarSystem()
{
parmyA = new ArmyGroup;
parmyB = new ArmyGroup;
}
//战斗系统类析构函数:
WarSystem::~WarSystem()
{
delete[] parmyA;
delete[] parmyB;
}
//开始游戏,随机决定哪一方率先发起攻击:
bool WarSystem::StartWar()
{
return rand()%2;
}
bool WarSystem::StartRound(bool r)//每一回合的展开,按照输入的数据决定玩家A或B先进行决策
{
int RoleA,RoleB;
if(r==1)
{
cout<<"It's A's term"<<endl<<"Player A,please select your soldier: "<<endl;
cin>>RoleA;
parmyA->GetRole(RoleA);
parmyA->pRole->MakeDecision(parmyB);
cout<<"It's B's term"<<endl<<"Player B,please select your soldier: "<<endl;
cin>>RoleB;
parmyB->GetRole(RoleB);
parmyB->pRole->MakeDecision(parmyA);
}
else if(r==0)
{
cout<<"It's B's term"<<endl<<"Player B,please select your soldier: "<<endl;
cin>>RoleB;
parmyB->GetRole(RoleB);
parmyB->pRole->MakeDecision(parmyA);
cout<<"It's A's term"<<endl<<"Player A,please select your soldier: "<<endl;
cin>>RoleA;
parmyA->GetRole(RoleA);
parmyA->pRole->MakeDecision(parmyB);
}
return 1;
}
bool WarSystem::Result()
{ int i;
int DieNumA=0;
for(i=0;i<=2;i++)
if((parmyA->pSOLDIER+i)->Die()==1)
DieNumA++;
for(i=0;i<=1;i++)
if((parmyA->pWIZARD+i)->Die()==1)
DieNumA++;
if((parmyA->pMASTER)->Die()==1)DieNumA++;
int DieNumB=0;
for(i=0;i<=2;i++)
if((parmyB->pSOLDIER+i)->Die()==1)
DieNumB++;
for(i=0;i<=1;i++)
if((parmyB->pWIZARD+i)->Die()==1)
DieNumB++;
if((parmyB->pMASTER)->Die()==1)DieNumB++;
if((DieNumA==3)||(DieNumB==3))
{
if(DieNumA==3)cout<<"Player B win !"<<endl;
if(DieNumB==3)cout<<"Player A win !"<<endl;
return 1;
}
else return 0;
}
void WarSystem::RunGame()
{
char Begin;
int StartTurn=StartWar();
cout<<"Ready for the game?"<<endl<<"Y(Yes)/N(No)"<<endl;
cin>>Begin;
if(Begin=='Y')
while(!Result())
{ int i;
cout<<"Team of A:"<<endl;
for(i=1;i<=6;i++)
{
if(i<=3)
{cout<<"A"<<i<<endl;(parmyA->pSOLDIER+i-1)->Print();}
else if(i<=5)
{cout<<"A"<<i<<endl;(parmyA->pWIZARD+i-4)->Print();}
else
{cout<<"A"<<i<<endl;(parmyA->pMASTER)->Print();}
}
cout<<"Team of B:"<<endl;
for(i=1;i<=6;i++)
{
if(i<=3)
{cout<<"A"<<i<<endl;(parmyA->pSOLDIER+i-1)->Print();}
else if(i<=5)
{cout<<"A"<<i<<endl;(parmyA->pWIZARD+i-4)->Print();}
else
{cout<<"A"<<i<<endl;(parmyA->pMASTER)->Print();}
}
StartRound(StartWar());
}
else
cout<<"QUIT !"<<endl;
}
void main()
{
WarSystem warsystem;
warsystem.RunGame();
}