Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- // Abstract class ASpell
- class ASpell {
- protected:
- std::string name;
- std::string effects;
- public:
- ASpell(const std::string& name, const std::string& effects)
- : name(name), effects(effects) {}
- virtual ~ASpell() {}
- const std::string& getName() const {
- return name;
- }
- const std::string& getEffects() const {
- return effects;
- }
- virtual ASpell* clone() const = 0;
- void launch(const ATarget& target) const {
- target.getHitBySpell(*this);
- }
- };
- // Concrete class Fwoosh derived from ASpell
- class Fwoosh : public ASpell {
- public:
- Fwoosh() : ASpell("Fwoosh", "fwooshed") {}
- virtual Fwoosh* clone() const {
- return new Fwoosh(*this);
- }
- };
- // Abstract class ATarget
- class ATarget {
- protected:
- std::string type;
- public:
- ATarget(const std::string& type) : type(type) {}
- virtual ~ATarget() {}
- const std::string& getType() const {
- return type;
- }
- virtual ATarget* clone() const = 0;
- void getHitBySpell(const ASpell& spell) const {
- std::cout << type << " has been " << spell.getEffects() << "!" << std::endl;
- }
- };
- // Concrete class Dummy derived from ATarget
- class Dummy : public ATarget {
- public:
- Dummy() : ATarget("Target Practice Dummy") {}
- virtual Dummy* clone() const {
- return new Dummy(*this);
- }
- };
- // Warlock class
- class Warlock {
- private:
- std::string name;
- std::string title;
- std::vector<ASpell*> spells;
- public:
- Warlock(const std::string& name, const std::string& title)
- : name(name), title(title) {}
- virtual ~Warlock() {
- for (auto spell : spells) {
- delete spell;
- }
- }
- const std::string& getName() const {
- return name;
- }
- const std::string& getTitle() const {
- return title;
- }
- void setTitle(const std::string& title) {
- this->title = title;
- }
- void introduce() const {
- std::cout << name << ": I am " << name << ", " << title << "!" << std::endl;
- }
- void learnSpell(ASpell* spell) {
- // Check if the Warlock already knows this spell
- if (std::find(spells.begin(), spells.end(), spell) == spells.end()) {
- spells.push_back(spell);
- }
- }
- void forgetSpell(const std::string& spellName) {
- for (auto it = spells.begin(); it != spells.end(); ++it) {
- if ((*it)->getName() == spellName) {
- delete *it;
- spells.erase(it);
- break;
- }
- }
- }
- void launchSpell(const std::string& spellName, ATarget& target) const {
- for (auto spell : spells) {
- if (spell->getName() == spellName) {
- spell->launch(target);
- break;
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement