Created
March 24, 2018 16:38
-
-
Save KireinaHoro/3cfed3a8d3e77fe7f7b7a1d956e2a267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Weapon { | |
public: | |
virtual bool usable() const = 0; | |
virtual double attack() = 0; | |
}; | |
class Sword : public Weapon { | |
private: | |
constexpr static auto startRatio = 0.2; | |
constexpr static auto wearRatio = 0.8; | |
double attackValue; | |
public: | |
explicit Sword(double ownerAttack) | |
: attackValue(startRatio * ownerAttack) {} | |
bool usable() const override { return attackValue > 0; } | |
double attack() override { | |
auto ret = attackValue; | |
attackValue *= wearRatio; | |
return ret; | |
} | |
}; | |
class Arrow : public Weapon { | |
private: | |
int remaining = 3; | |
public: | |
static double attackValue; | |
Arrow() = default; | |
Arrow(double) {} | |
bool usable() const override { return remaining > 0; } | |
double attack() override { | |
if (!usable()) { return 0; } | |
--remaining; | |
return attackValue; | |
} | |
}; | |
double Arrow::attackValue; | |
class Bomb : public Weapon { | |
public: | |
Bomb() = default; | |
Bomb(double) {} | |
bool usable() const override { return true; } | |
double attack() override { return -1; } | |
}; | |
template<typename T> | |
Weapon *make(double param = 0) { | |
return new T{param}; | |
} | |
using WeaponPtr = Weapon*; | |
using WeaponCreator = WeaponPtr(*)(double); | |
WeaponCreator WeaponList[] = {make<Sword>, make<Arrow>, make<Bomb>}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment