#include "ASpell.hpp" ASpell::ASpell() { } ASpell::ASpell(std::string const &name, std::string const &effects) : _name(name), _effects(effects) { } ASpell::ASpell(ASpell const &other) { *this = other; } ASpell &ASpell::operator=(ASpell const &other) { this->_name = other._name; this->_effects = other._effects; return *this; } ASpell::~ASpell() { } std::string const &ASpell::getName() const { return this->_name; } std::string const &ASpell::getEffects() const { return this->_effects; } void ASpell::launch(ATarget const &atargetref) const { atargetref.getHitBySpell(*this); } #pragma once #include class ATarget; class ASpell { private: std::string _name; std::string _effects; public: ASpell(); ASpell(std::string const &name, std::string const &effects); ASpell(ASpell const &other); ASpell &operator=(ASpell const &other); virtual ~ASpell(); std::string const &getName() const; std::string const &getEffects() const; virtual ASpell *clone() const = 0; void launch(ATarget const &atargetref) const; }; #include "ATarget.hpp"#include "ATarget.hpp" ATarget::ATarget() { } ATarget::ATarget(std::string const &type) : _type(type){ } ATarget::ATarget(ATarget const &other) { *this = other; } ATarget &ATarget::operator=(ATarget const &other) { this->_type = other._type; return *this; } ATarget::~ATarget() { } std::string const &ATarget::getType() const { return this->_type; } void ATarget::getHitBySpell(ASpell const &aspellref) const { std::cout << _type << " has been " << aspellref.getEffects() << "!\n"; } #pragma once #include class ASpell; class ATarget { private: std::string _type; public: ATarget(); ATarget(std::string const &type); ATarget(ATarget const &other); ATarget &operator=(ATarget const &other); virtual ~ATarget(); std::string const &getType() const; virtual ATarget *clone() const = 0; void getHitBySpell(ASpell const &aspellref) const; }; #include "ASpell.hpp"#include "BrickWall.hpp" BrickWall::BrickWall() : ATarget("Inconspicuous Red-brick Wall") { } BrickWall::~BrickWall() { } ATarget *BrickWall::clone() const { return (new BrickWall()); } #pragma once #include "ATarget.hpp" class BrickWall : public ATarget { public: BrickWall(); ~BrickWall(); ATarget *clone() const; }; #include "Dummy.hpp" Dummy::Dummy() : ATarget("Target Practice Dummy") { } Dummy::~Dummy() { } ATarget *Dummy::clone() const { return (new Dummy()); } #pragma once #include "ATarget.hpp" class Dummy : public ATarget { public: Dummy(); ~Dummy(); ATarget *clone() const; }; #include "Fireball.hpp" Fireball::Fireball() : ASpell("Fireball", "burnt to a crisp") { } Fireball::~Fireball() { } ASpell *Fireball::clone() const { return (new Fireball()); } #pragma once #include "ASpell.hpp" class Fireball : public ASpell { public: Fireball(); ~Fireball(); ASpell *clone() const; }; #include "Fwoosh.hpp" Fwoosh::Fwoosh() : ASpell("Fwoosh", "fwooshed") { } Fwoosh::~Fwoosh() { } ASpell *Fwoosh::clone() const { return (new Fwoosh()); } #pragma once #include "ASpell.hpp" class Fwoosh : public ASpell { public: Fwoosh(); ~Fwoosh(); ASpell *clone() const; }; #include "Polymorph.hpp" Polymorph::Polymorph() : ASpell("Polymorph", "turned into a critter") { } Polymorph::~Polymorph() { } ASpell *Polymorph::clone() const { return (new Polymorph()); } #pragma once #include "ASpell.hpp" class Polymorph : public ASpell { public: Polymorph(); ~Polymorph(); ASpell *clone() const; }; #include "SpellBook.hpp" SpellBook::SpellBook() { } SpellBook::~SpellBook() { std::map::iterator itb = _spellbook.begin(); std::map::iterator ite = _spellbook.end(); while (itb != ite) { delete itb->second; ++itb; } _spellbook.clear(); } void SpellBook::learnSpell(ASpell *aspellptr) { if (aspellptr) _spellbook.insert(std::pair(aspellptr->getName(), aspellptr->clone())); } void SpellBook::forgetSpell(std::string const &spellname) { std::map::iterator itf = _spellbook.find(spellname); if (itf != _spellbook.end()) delete itf->second; _spellbook.erase(spellname); } ASpell* SpellBook::createSpell(std::string const &spellname) { ASpell *spell = _spellbook[spellname]; if (spell) return _spellbook[spellname]; return NULL; } #pragma once #include #include #include "ASpell.hpp" class SpellBook { private: SpellBook(SpellBook const &other); SpellBook &operator=(SpellBook const &other); std::map _spellbook; public: SpellBook(); ~SpellBook(); void learnSpell(ASpell *aspellptr); void forgetSpell(std::string const &spellname); ASpell* createSpell(std::string const &spellname); }; #include "TargetGenerator.hpp" TargetGenerator::TargetGenerator() { } TargetGenerator::~TargetGenerator() { std::map::iterator itb = _targetbook.begin(); std::map::iterator ite = _targetbook.end(); while (itb != ite) { delete itb->second; ++itb; } _targetbook.clear(); } void TargetGenerator::learnTargetType(ATarget *targetptr) { if (targetptr) _targetbook.insert(std::pair(targetptr->getType(), targetptr->clone())); } void TargetGenerator::forgetTargetType(std::string const &targetname) { std::map::iterator itf = _targetbook.find(targetname); if (itf != _targetbook.end()) delete itf->second; _targetbook.erase(targetname); } ATarget* TargetGenerator::createTarget(std::string const &targetname) { ATarget *spell = _targetbook[targetname]; if (spell) return _targetbook[targetname]; return NULL; } #pragma once #include #include #include "ATarget.hpp" class TargetGenerator { private: TargetGenerator(TargetGenerator const &other); TargetGenerator &operator=(TargetGenerator const &other); std::map _targetbook; public: TargetGenerator(); ~TargetGenerator(); void learnTargetType(ATarget *targetptr); void forgetTargetType(std::string const &targetname); ATarget* createTarget(std::string const &targetname); };#include "Warlock.hpp" Warlock::Warlock(std::string const &name, std::string const &title) : _name(name), _title(title) { std::cout << _name << ": This looks like another boring day.\n"; } Warlock::~Warlock() { std::cout << _name << ": My job here is done!\n"; } std::string const &Warlock::getName() const { return this->_name; } std::string const &Warlock::getTitle() const { return this->_title; } void Warlock::setTitle(std::string const &title) { this->_title = title; } void Warlock::introduce() const { std::cout << _name << ": I am " << _name << ", " << _title << "!\n"; } void Warlock::learnSpell(ASpell *aspellptr) { _spellbook.learnSpell(aspellptr); } void Warlock::forgetSpell(std::string spellname) { _spellbook.forgetSpell(spellname); } void Warlock::launchSpell(std::string spellname, ATarget const &atargetref) { ATarget const *test = 0; if (test == &atargetref) return; ASpell *tmp = _spellbook.createSpell(spellname); if (tmp) tmp->launch(atargetref); } #pragma once #include #include "ASpell.hpp" #include "ATarget.hpp" #include "SpellBook.hpp" #include class Warlock { private: std::string _name; std::string _title; Warlock(); Warlock(Warlock const &other); Warlock &operator=(Warlock const &other); SpellBook _spellbook; public: Warlock(std::string const &name, std::string const &title); ~Warlock(); std::string const &getName() const; std::string const &getTitle() const; void setTitle(std::string const &title); void introduce() const; void learnSpell(ASpell *aspellptr); void forgetSpell(std::string spellname); void launchSpell(std::string spellname, ATarget const &atargetref); };