cpp
复制代码
#include <iostream>
using namespace std;
class NPC
{
protected:
int atk;
int def;
int spd;
int hp;
string name;
public:
NPC(int atk,int def,int spd,int hp,const string& name)
:atk(atk), def(def), spd(spd), hp(hp), name{name}
{}
virtual void show()=0;
virtual int getAtk(){return atk;};
virtual int getDef(){return def;};
virtual int getSpd(){return spd;};
virtual int getHp(){return hp;};
virtual void setAtk(int atk){this->atk=atk;};
virtual void setDef(int def){this->def=def;};
virtual void setSpd(int spd){this->spd=spd;};
virtual void setHp(int hp){this->hp=hp;};
virtual ~NPC(){};
};
class weapon
{
private:
/* data */
public:
virtual ~weapon(){};
virtual bool onEquip(NPC&)=0;
virtual bool onUnequip(NPC&)=0;
};
class Blade:public weapon
{
public:
bool onEquip(NPC& npc){
npc.setAtk(npc.getAtk()+1);
npc.setSpd(npc.getSpd()+1);
return true;
}
bool onUnequip(NPC& npc){
npc.setAtk(npc.getAtk()-1);
npc.setSpd(npc.getSpd()-1);
return true;
}
};
class Sword:public weapon
{
public:
bool onEquip(NPC& npc){
npc.setAtk(npc.getAtk()+1);
npc.setHp(npc.getHp()+1);
return true;
}
bool onUnequip(NPC& npc){
npc.setAtk(npc.getAtk()-1);
npc.setHp(npc.getHp()-1);
return true;
}
};
class Axe:public weapon
{
public:
bool onEquip(NPC& npc){
npc.setAtk(npc.getAtk()+1);
npc.setDef(npc.getDef()+1);
return true;
}
bool onUnequip(NPC& npc){
npc.setAtk(npc.getAtk()-1);
npc.setDef(npc.getDef()-1);
return true;
}
};
class Hero:public NPC
{
private:
/* data */
public:
Hero(int atk,int def,int spd,int hp,const string& name)
:NPC(atk, def, spd, hp, name)
{}
void show() override {
cout << "Hero: " << name << ", Atk: " << atk << ", Def: " << def
<< ", Spd: " << spd << ", Hp: " << hp << endl;
}
bool equipWeapon(weapon& w) {
return w.onEquip(*this);
}
bool unequipWeapon(weapon& w) {
return w.onUnequip(*this);
}
~Hero(){};
};
int main(int argc, char const *argv[])
{
auto hero = Hero(10, 5, 3, 100, "一点都不想去大坝的深蓝");
hero.show();
Blade blade;
Sword sword;
Axe axe;
hero.equipWeapon(blade);
hero.show();
hero.unequipWeapon(blade);
hero.show();
hero.equipWeapon(sword);
hero.show();
hero.unequipWeapon(sword);
hero.show();
hero.equipWeapon(axe);
hero.show();
hero.unequipWeapon(axe);
hero.show();
return 0;
}