resolved one error and one segfault in xpp02

This commit is contained in:
lenovo
2022-12-12 22:34:11 +01:00
parent fecde1a8b1
commit 5b19461090
33 changed files with 567 additions and 326 deletions

View File

@@ -1,29 +1,2 @@
# README # # README #
This README would normally document whatever steps are necessary to get your application up and running.
### What is this repository for? ###
* Quick summary
* Version
* [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo)
### How do I get set up? ###
* Summary of set up
* Configuration
* Dependencies
* Database configuration
* How to run tests
* Deployment instructions
### Contribution guidelines ###
* Writing tests
* Code review
* Other guidelines
### Who do I talk to? ###
* Repo owner or admin
* Other community or team contact

View File

@@ -4,7 +4,8 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
class Warlock { class Warlock
{
private: private:
Warlock(); Warlock();
Warlock(Warlock const & other); Warlock(Warlock const & other);
@@ -14,34 +15,36 @@ class Warlock {
std::string title; std::string title;
public: public:
Warlock(std::string const & name, std::string const & title) Warlock(std::string const name, std::string const title)
{ {
this->name = name; this->name = name;
this->title = title; this->title = title;
std::cout << this->name << ": This looks like another boring day.\n"; std::cout << this->name << ": This looks like another boring day.\n";
}; }
~Warlock() ~Warlock()
{ {
std::cout << this->name << ": My job here is done!\n"; std::cout << this->name << ": My job here is done!\n";
}; }
std::string const & getName() const std::string const & getName() const
{ {
return (this->name); return this->name;
}; }
std::string const & getTitle() const std::string const & getTitle() const
{ {
return (this->title); return this->title;
}; }
void setTitle(std::string const & title) void setTitle(std::string const & title)
{ {
this->title = title; this->title = title;
}; }
void introduce() const void introduce() const
{ {
std::cout << this->name << ": I am " << this->name << ", " << this->title << "!\n"; std::cout << this->name << ": I am " << this->name << ", " << this->title << "!\n";
}; }
}; };
#endif #endif

Binary file not shown.

View File

@@ -3,4 +3,5 @@
void ASpell::launch(ATarget const & atarget) const void ASpell::launch(ATarget const & atarget) const
{ {
atarget.getHitBySpell(*this); atarget.getHitBySpell(*this);
}; }

View File

@@ -6,50 +6,49 @@
class ATarget; class ATarget;
class ASpell { class ASpell
{
private: protected:
std::string name; std::string name;
std::string effects; std::string effects;
public: public:
ASpell() ASpell()
{}; {}
ASpell(ASpell const & other) ASpell(ASpell const & other)
{ {
*this = other; *this = other;
}; }
ASpell & operator=(ASpell const & other) ASpell & operator=(ASpell const & other)
{ {
this->name = other.name; this->name = other.name;
this->effects = other.effects; this->effects = other.effects;
return (*this); return *this;
}; }
ASpell(std::string const & name, std::string const & effects) ASpell(std::string const & name, std::string const & effects)
{ {
this->name = name; this->name = name;
this->effects = effects; this->effects = effects;
}; }
virtual ~ASpell() virtual ~ASpell()
{}; {}
std::string const & getName() const std::string const & getName() const
{ {
return (this->name); return this->name;
}; }
std::string const & getEffects() const std::string const & getEffects() const
{ {
return (this->effects); return this->effects;
}; }
void launch(ATarget const & atarget) const; void launch(ATarget const &) const;
virtual ASpell * clone() const = 0; virtual ASpell * clone() const = 0;
}; };
#include "ATarget.hpp" #include "ATarget.hpp"
#endif #endif

View File

@@ -3,4 +3,5 @@
void ATarget::getHitBySpell(ASpell const & aspell) const void ATarget::getHitBySpell(ASpell const & aspell) const
{ {
std::cout << this->type << " has been " << aspell.getEffects() << "!\n"; std::cout << this->type << " has been " << aspell.getEffects() << "!\n";
}; }

View File

@@ -6,40 +6,42 @@
class ASpell; class ASpell;
class ATarget { class ATarget
{
private: protected:
std::string type; std::string type;
public: public:
ATarget() ATarget()
{}; {}
ATarget(ATarget const & other) ATarget(ATarget const & other)
{ {
* this = other; * this = other;
}; }
ATarget & operator=(ATarget const & other) ATarget & operator=(ATarget const & other)
{ {
this->type = other.type; this->type = other.type;
return (*this); return *this;
}; }
ATarget(std::string const & type) ATarget(std::string const & type)
{ {
this->type = type; this->type = type;
}; }
~ATarget() virtual ~ATarget()
{}; {}
std::string const & getType() const std::string const & getType() const
{ {
return (this->type); return this->type;
}; }
void getHitBySpell(ASpell const & aspell) const; void getHitBySpell(ASpell const &) const;
virtual ATarget * clone() const = 0; virtual ATarget * clone() const = 0;
}; };
#include "ASpell.hpp" #include "ASpell.hpp"
#endif #endif

View File

@@ -1,2 +1 @@
#include "Dummy.hpp" #include "Dummy.hpp"

View File

@@ -3,18 +3,19 @@
#include "ATarget.hpp" #include "ATarget.hpp"
class Dummy: public ATarget { class Dummy: public ATarget
{
public: public:
Dummy(): ATarget("Target Practice Dummy") Dummy(): ATarget("Target Practice Dummy")
{}; {}
~Dummy() ~Dummy()
{}; {}
virtual ATarget * clone() const virtual ATarget * clone() const
{ {
return (new Dummy()); return (new Dummy());
}; }
}; };
#endif #endif

View File

@@ -3,18 +3,19 @@
#include "ASpell.hpp" #include "ASpell.hpp"
class Fwoosh: public ASpell { class Fwoosh: public ASpell
{
public: public:
Fwoosh(): ASpell("Fwoosh", "fwooshed") Fwoosh(): ASpell("Fwoosh", "fwooshed")
{}; {}
~Fwoosh() ~Fwoosh()
{}; {}
virtual ASpell * clone() const virtual ASpell * clone() const
{ {
return (new Fwoosh()); return (new Fwoosh());
}; }
}; };
#endif #endif

View File

@@ -8,8 +8,8 @@
#include "ASpell.hpp" #include "ASpell.hpp"
#include "ATarget.hpp" #include "ATarget.hpp"
class Warlock { class Warlock
{
private: private:
Warlock(); Warlock();
Warlock(Warlock const & other); Warlock(Warlock const & other);
@@ -25,37 +25,37 @@ class Warlock {
this->name = name; this->name = name;
this->title = title; this->title = title;
std::cout << this->name << ": This looks like another boring day.\n"; std::cout << this->name << ": This looks like another boring day.\n";
}; }
~Warlock() ~Warlock()
{ {
std::cout << this->name << ": My job here is done!\n"; std::cout << this->name << ": My job here is done!\n";
std::map<std::string, ASpell *>::iterator it_begin = this->arr.begin();
std::map<std::string, ASpell *>::iterator it_end = this->arr.end(); std::map<std::string, ASpell*>::iterator it;
while (it_begin != it_end) {
delete it_begin->second; for (it = arr.begin(); it != arr.end(); ++it)
++it_begin; delete it->second;
}
this->arr.clear(); this->arr.clear();
}; }
std::string const & getName() const std::string const & getName() const
{ {
return (this->name); return this->name;
}; }
std::string const & getTitle() const std::string const & getTitle() const
{ {
return (this->title); return this->title;
}; }
void setTitle(std::string const & title) void setTitle(std::string const & title)
{ {
this->title = title; this->title = title;
}; }
void introduce() const void introduce() const
{ {
std::cout << this->name << ": I am " << this->name << ", " << this->title << "!\n"; std::cout << this->name << ": I am " << this->name << ", " << this->title << "!\n";
}; }
void learnSpell(ASpell * aspell) void learnSpell(ASpell * aspell)
{ {
@@ -67,21 +67,23 @@ class Warlock {
aspell->clone() aspell->clone()
)); ));
} }
}; }
void forgetSpell(std::string name) void forgetSpell(std::string spell_name)
{ {
std::map<std::string, ASpell *>::iterator it = arr.find(name); ASpell * spell = arr[spell_name];
if (it == arr.end())
return;
delete it->second;
arr.erase(name);
};
void launchSpell(std::string name, ATarget const & target)
{
ASpell * spell = arr[name];
if (spell) if (spell)
spell->launch(target); {
}; delete spell;
arr.erase(spell_name);
}
}
void launchSpell(std::string spell_name, ATarget const & atarget)
{
ASpell * spell = arr[spell_name];
if (spell)
spell->launch(atarget);
}
}; };
#endif #endif

Binary file not shown.

BIN
cpp_module_01/test Executable file

Binary file not shown.

View File

@@ -1,5 +1,7 @@
#include "ASpell.hpp" #include "ASpell.hpp"
void ASpell::launch(ATarget const & atarget) const { void ASpell::launch(ATarget const & atarget) const
{
atarget.getHitBySpell(*this); atarget.getHitBySpell(*this);
}; }

View File

@@ -6,40 +6,49 @@
class ATarget; class ATarget;
class ASpell { class ASpell
private: {
protected:
std::string name; std::string name;
std::string effects; std::string effects;
public: public:
ASpell() { ASpell()
}; {}
ASpell(ASpell const & other) { ASpell(ASpell const & other)
{
*this = other; *this = other;
}; }
ASpell & operator=(ASpell const & other) { ASpell & operator=(ASpell const & other)
{
this->name = other.name; this->name = other.name;
this->effects = other.effects; this->effects = other.effects;
return (*this); return *this;
}; }
ASpell(std::string const & name, std::string const & effects) { ASpell(std::string const & name, std::string const & effects)
{
this->name = name; this->name = name;
this->effects = effects; this->effects = effects;
}; }
virtual ~ASpell() { virtual ~ASpell()
}; {}
std::string const & getName() const { std::string const & getName() const
return (this->name); {
}; return this->name;
std::string const & getEffects() const { }
return (this->effects); std::string const & getEffects() const
}; {
void launch(ATarget const & atarget) const; return this->effects;
}
void launch(ATarget const &) const;
virtual ASpell * clone() const = 0; virtual ASpell * clone() const = 0;
}; };
#include "ATarget.hpp" #include "ATarget.hpp"
#endif #endif

View File

@@ -1,5 +1,7 @@
#include "ATarget.hpp" #include "ATarget.hpp"
void ATarget::getHitBySpell(ASpell const & aspell) const { void ATarget::getHitBySpell(ASpell const & aspell) const
{
std::cout << this->type << " has been " << aspell.getEffects() << "!\n"; std::cout << this->type << " has been " << aspell.getEffects() << "!\n";
}; }

View File

@@ -6,35 +6,42 @@
class ASpell; class ASpell;
class ATarget { class ATarget
private: {
protected:
std::string type; std::string type;
public: public:
ATarget() { ATarget()
}; {}
ATarget(std::string const & type) { ATarget(ATarget const & other)
this->type = type; {
};
ATarget(ATarget const & other) {
* this = other; * this = other;
}; }
ATarget & operator=(ATarget const & other) { ATarget & operator=(ATarget const & other)
{
this->type = other.type; this->type = other.type;
return (*this); return *this;
}; }
virtual ~ATarget() {}; ATarget(std::string const & type)
{
this->type = type;
}
virtual ~ATarget()
{}
std::string const & getType() const { std::string const & getType() const
return (this->type); {
}; return this->type;
}
void getHitBySpell(ASpell const & aspell) const; void getHitBySpell(ASpell const &) const;
virtual ATarget * clone() const = 0; virtual ATarget * clone() const = 0;
}; };
#include "ASpell.hpp" #include "ASpell.hpp"
#endif #endif

View File

@@ -3,14 +3,19 @@
#include "ATarget.hpp" #include "ATarget.hpp"
class BrickWall: public ATarget { class BrickWall: public ATarget
{
public: public:
BrickWall(): ATarget("Inconspicuous Red-brick Wall") {}; BrickWall(): ATarget("Inconspicuous Red-brick Wall")
~BrickWall() {}; {}
~BrickWall()
{}
virtual ATarget * clone() const { virtual ATarget * clone() const
{
return (new BrickWall()); return (new BrickWall());
}; }
}; };
#endif #endif

View File

@@ -1,2 +1 @@
#include "Dummy.hpp" #include "Dummy.hpp"

View File

@@ -3,14 +3,19 @@
#include "ATarget.hpp" #include "ATarget.hpp"
class Dummy: public ATarget { class Dummy: public ATarget
{
public: public:
Dummy(): ATarget("Target Practice Dummy") {}; Dummy(): ATarget("Target Practice Dummy")
~Dummy() {}; {}
~Dummy()
{}
virtual ATarget * clone() const { virtual ATarget * clone() const
{
return (new Dummy()); return (new Dummy());
}; }
}; };
#endif #endif

View File

@@ -3,14 +3,19 @@
#include "ASpell.hpp" #include "ASpell.hpp"
class Fireball: public ASpell { class Fireball: public ASpell
{
public: public:
Fireball(): ASpell("Fireball", "burnt to a crisp") {}; Fireball(): ASpell("Fireball", "burnt to a crisp")
~Fireball() {}; {}
~Fireball()
{}
virtual ASpell * clone() const { virtual ASpell * clone() const
{
return (new Fireball()); return (new Fireball());
}; }
}; };
#endif #endif

View File

@@ -3,14 +3,19 @@
#include "ASpell.hpp" #include "ASpell.hpp"
class Fwoosh: public ASpell { class Fwoosh: public ASpell
{
public: public:
Fwoosh(): ASpell("Fwoosh", "fwooshed") {}; Fwoosh(): ASpell("Fwoosh", "fwooshed")
~Fwoosh() {}; {}
~Fwoosh()
{}
virtual ASpell * clone() const { virtual ASpell * clone() const
{
return (new Fwoosh()); return (new Fwoosh());
}; }
}; };
#endif #endif

View File

@@ -1,2 +1 @@
#include "Polymorph.hpp" #include "Polymorph.hpp"

View File

@@ -3,14 +3,19 @@
#include "ASpell.hpp" #include "ASpell.hpp"
class Polymorph: public ASpell { class Polymorph: public ASpell
{
public: public:
Polymorph(): ASpell("Polymorph", "turned into a critter") {}; Polymorph(): ASpell("Polymorph", "turned into a critter")
~Polymorph() {}; {}
~Polymorph()
{}
virtual ASpell * clone() const { virtual ASpell * clone() const
{
return (new Polymorph()); return (new Polymorph());
}; }
}; };
#endif #endif

View File

@@ -1,2 +1,2 @@
#include "Warlock.hpp" #include "SpellBook.hpp"

View File

@@ -3,10 +3,12 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
# include "ASpell.hpp"
#include <map> #include <map>
class SpellBook { #include "ASpell.hpp"
class SpellBook
{
private: private:
SpellBook(SpellBook const & other); SpellBook(SpellBook const & other);
SpellBook & operator=(SpellBook const & other); SpellBook & operator=(SpellBook const & other);
@@ -14,37 +16,46 @@ class SpellBook {
std::map<std::string, ASpell*> arr; std::map<std::string, ASpell*> arr;
public: public:
SpellBook() {}; SpellBook()
~SpellBook() { {}
std::map<std::string, ASpell *>::iterator it_begin = this->arr.begin(); ~SpellBook()
std::map<std::string, ASpell *>::iterator it_end = this->arr.end(); {
while (it_begin != it_end) { std::map<std::string, ASpell*>::iterator it;
delete it_begin->second; for (it = arr.begin(); it != arr.end(); ++it)
++it_begin; delete it->second;
}
this->arr.clear(); this->arr.clear();
}; }
void learnSpell(ASpell *aspell) { void learnSpell(ASpell * aspell)
{
if (aspell) if (aspell)
arr.insert(std::pair<std::string, ASpell *>( {
arr.insert(std::pair<std::string, ASpell*>
(
aspell->getName(), aspell->getName(),
aspell->clone() aspell->clone()
)); ));
}; }
void forgetSpell(std::string & name) { }
std::map<std::string, ASpell *>::iterator it = arr.find(name); void forgetSpell(std::string const & spell_name)
if (it == arr.end()) {
return; std::map<std::string, ASpell*>::iterator it;
it = arr.find(spell_name);
if (it != arr.end())
{
delete it->second; delete it->second;
arr.erase(name); arr.erase(spell_name);
}; }
ASpell * createSpell(std::string & name) { }
std::map<std::string, ASpell *>::iterator it = arr.find(name); ASpell * createSpell(std::string const & spell_name)
if (it == arr.end()) {
std::map<std::string, ASpell*>::iterator it;
it = arr.find(spell_name);
if (it != arr.end())
return arr[spell_name];
return NULL; return NULL;
return arr[name]; }
};
}; };
#endif #endif

View File

@@ -3,10 +3,12 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
# include "ATarget.hpp"
#include <map> #include <map>
class TargetGenerator { #include "ATarget.hpp"
class TargetGenerator
{
private: private:
TargetGenerator(TargetGenerator const & other); TargetGenerator(TargetGenerator const & other);
TargetGenerator & operator=(TargetGenerator const & other); TargetGenerator & operator=(TargetGenerator const & other);
@@ -14,37 +16,50 @@ class TargetGenerator {
std::map<std::string, ATarget*> arr; std::map<std::string, ATarget*> arr;
public: public:
TargetGenerator() { TargetGenerator()
}; {}
~TargetGenerator() { ~TargetGenerator()
std::map<std::string, ATarget *>::iterator it_begin = this->arr.begin(); {
std::map<std::string, ATarget *>::iterator it_end = this->arr.end(); std::map<std::string, ATarget*>::iterator it;
while (it_begin != it_end) { for (it = arr.begin(); it != arr.end(); ++it)
delete it_begin->second; delete it->second;
++it_begin;
}
this->arr.clear(); this->arr.clear();
}; }
void learnTargetType(ATarget * atarget) { void learnTargetType(ATarget * atarget)
{
if (atarget) if (atarget)
arr.insert(std::pair<std::string, ATarget *>( {
arr.insert(std::pair<std::string, ATarget*>
(
atarget->getType(), atarget->getType(),
atarget->clone() atarget->clone()
)); ));
}; }
void forgetTargetType(std::string const & name) { }
std::map<std::string, ATarget *>::iterator it = arr.find(name); void forgetTargetType(std::string const & type)
{
std::map<std::string, ATarget*>::iterator it;
it = arr.find(type);
if (it != arr.end()) if (it != arr.end())
{
delete it->second; delete it->second;
arr.erase(name); arr.erase(type);
}; }
ATarget * createTarget(std::string const & name) { }
std::map<std::string, ATarget *>::iterator it = arr.find(name);
ATarget * createTarget(std::string const & type)
{
std::map<std::string, ATarget*>::iterator it;
it = arr.find(type);
if (it != arr.end()) if (it != arr.end())
return arr[name]; return arr[type]->clone();
return NULL; return NULL;
}; }
}; };
#endif #endif

View File

@@ -1,2 +1,2 @@
#include "SpellBook.hpp" #include "Warlock.hpp"

View File

@@ -3,11 +3,14 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <map>
#include "SpellBook.hpp"
#include "ASpell.hpp" #include "ASpell.hpp"
#include "ATarget.hpp" #include "ATarget.hpp"
# include "SpellBook.hpp"
class Warlock { class Warlock
{
private: private:
Warlock(); Warlock();
Warlock(Warlock const & other); Warlock(Warlock const & other);
@@ -15,57 +18,61 @@ class Warlock {
std::string name; std::string name;
std::string title; std::string title;
SpellBook book;
SpellBook * book;
public: public:
Warlock(std::string const & name, std::string const & title) Warlock(std::string const & name, std::string const & title)
{ {
this->name = name; this->name = name;
this->title = title; this->title = title;
book = new SpellBook();
std::cout << this->name << ": This looks like another boring day.\n"; std::cout << this->name << ": This looks like another boring day.\n";
}; }
~Warlock() ~Warlock()
{ {
std::cout << this->name << ": My job here is done!\n"; std::cout << this->name << ": My job here is done!\n";
};
delete book;
}
std::string const & getName() const std::string const & getName() const
{ {
return (this->name); return this->name;
}; }
std::string const & getTitle() const std::string const & getTitle() const
{ {
return (this->title); return this->title;
}; }
void setTitle(std::string const & title) void setTitle(std::string const & title)
{ {
this->title = title; this->title = title;
}; }
void introduce() const void introduce() const
{ {
std::cout << this->name << ": I am " << this->name << ", " << this->title << "!\n"; std::cout << this->name << ": I am " << this->name << ", " << this->title << "!\n";
}; }
void learnSpell(ASpell * aspell) void learnSpell(ASpell * aspell)
{ {
book.learnSpell(aspell); book->learnSpell(aspell);
}; }
void forgetSpell(std::string name) void forgetSpell(std::string spell_name)
{ {
book.forgetSpell(name); book->forgetSpell(spell_name);
}; }
void launchSpell(std::string name, ATarget const & atarget) void launchSpell(std::string spell_name, ATarget const & atarget)
{ {
//ATarget const * test = 0; ATarget * test = NULL;
//if (test == & atarget) if (&atarget == test)
// return; return;
ASpell * spell = book->createSpell(spell_name);
ASpell * spell = book.createSpell(name);
if (spell) if (spell)
spell->launch(atarget); spell->launch(atarget);
}; }
}; };
#endif #endif

Binary file not shown.

View File

@@ -7,6 +7,8 @@
#include "Fireball.hpp" #include "Fireball.hpp"
int main() int main()
{
// test subect
{ {
Warlock richard("Richard", "foo"); Warlock richard("Richard", "foo");
richard.setTitle("Hello, I'm Richard the Warlock!"); richard.setTitle("Hello, I'm Richard the Warlock!");
@@ -28,3 +30,183 @@ int main()
richard.launchSpell("Polymorph", *wall); richard.launchSpell("Polymorph", *wall);
richard.launchSpell("Fireball", *wall); richard.launchSpell("Fireball", *wall);
} }
// my test
{
Warlock richard("Richard", "foo");
BrickWall * wall = new BrickWall();
Fireball * fireball = new Fireball();
richard.launchSpell("Fireball", *wall);
std::cout << "-\n";
richard.learnSpell(fireball);
richard.launchSpell("Fireball", *wall);
richard.learnSpell(fireball);
richard.launchSpell("Fireball", *wall);
std::cout << "-\n";
richard.forgetSpell(fireball->getName());
richard.launchSpell("Fireball", *wall);
richard.forgetSpell(fireball->getName());
richard.launchSpell("Fireball", *wall);
richard.forgetSpell(fireball->getName());
richard.launchSpell("Fireball", *wall);
std::cout << "--\n";
richard.learnSpell(fireball);
TargetGenerator tarGen;
Dummy dummy;
ATarget * dum = tarGen.createTarget(dummy.getType());
richard.launchSpell("Fireball", *dum);
std::cout << "-\n";
tarGen.learnTargetType(&dummy);
richard.launchSpell("Fireball", *dum);
dum = tarGen.createTarget(dummy.getType());
richard.launchSpell("Fireball", *dum);
tarGen.learnTargetType(&dummy);
richard.launchSpell("Fireball", *dum);
dum = tarGen.createTarget(dummy.getType());
richard.launchSpell("Fireball", *dum);
std::cout << "-\n";
tarGen.forgetTargetType(dummy.getType());
richard.launchSpell("Fireball", *dum);
dum = tarGen.createTarget(dummy.getType());
richard.launchSpell("Fireball", *dum);
tarGen.forgetTargetType(dummy.getType());
richard.launchSpell("Fireball", *dum);
dum = tarGen.createTarget(dummy.getType());
richard.launchSpell("Fireball", *dum);
tarGen.forgetTargetType(dummy.getType());
richard.launchSpell("Fireball", *dum);
dum = tarGen.createTarget(dummy.getType());
richard.launchSpell("Fireball", *dum);
}
std::cout << "\n-----------------\n";
// https://github.com/adbenoit-9/42_exams/tree/main/Exam_rank_05/cpp_module_02
{
Warlock richard("Richard", "foo");
// test copy
Fireball * fire = new Fireball();
BrickWall * wall = new BrickWall();
TargetGenerator tarGen;
tarGen.learnTargetType(wall);
richard.learnSpell(fire);
const std::string name(fire->getName());
const std::string effects(fire->getEffects());
const std::string type(wall->getType());
delete fire;
delete wall;
ATarget* target = tarGen.createTarget(type);
richard.launchSpell(name, *target);
// test double
fire = new Fireball();
richard.learnSpell(fire);
tarGen.learnTargetType(target);
richard.forgetSpell(name);
tarGen.forgetTargetType(type);
std::cout << "have to be empy : ";
richard.launchSpell(name, *target);
if (tarGen.createTarget(type))
std::cout << "is not...";
std::cout << std::endl;
}
std::cout << "\n-----------------\n";
//https://github.com/Glagan/42-exam-rank-05/blob/master/cpp_module_02/main.cc
{
std::cout << "--- Constructors:\n";
Warlock richard("Aang", "The Avatar");
std::cout << "--- Spells:\n";
Polymorph *water = new Polymorph();
Fireball *fire = new Fireball();
Fwoosh *air = new Fwoosh();
richard.learnSpell(water);
richard.learnSpell(fire);
richard.forgetSpell("Fwoosh");
richard.learnSpell(air);
richard.forgetSpell("Fwoosh");
richard.forgetSpell("Fwoosh");
richard.learnSpell(air);
std::cout << "--- Targets:\n";
Dummy *hay = new Dummy();
BrickWall *earth = new BrickWall();
TargetGenerator tarGen;
tarGen.learnTargetType(hay);
tarGen.learnTargetType(earth);
std::cout << "--- Spells (all):\n";
richard.launchSpell("Fwoosh", *tarGen.createTarget("Dummy Practice"));
richard.launchSpell("Fireball", *tarGen.createTarget("BrickWall Practice"));
richard.launchSpell("Polymorph", *tarGen.createTarget("Dummy Practice"));
std::cout << "--- Forgotten \"Fwoosh\":\n";
richard.forgetSpell("Fwoosh");
richard.launchSpell("Fwoosh", *tarGen.createTarget("Dummy Practice"));
richard.launchSpell("Fireball", *tarGen.createTarget("BrickWall Practice"));
richard.launchSpell("Polymorph", *tarGen.createTarget("Dummy Practice"));
std::cout << "--- Spells (all):\n";
richard.learnSpell(air);
richard.launchSpell("Fwoosh", *tarGen.createTarget("Dummy Practice"));
richard.launchSpell("Fireball", *tarGen.createTarget("BrickWall Practice"));
richard.launchSpell("Polymorph", *tarGen.createTarget("Dummy Practice"));
std::cout << "--- Non-existant spell:\n";
richard.launchSpell("ACID", *tarGen.createTarget("BrickWall Practice"));
richard.forgetSpell("ACID");
richard.launchSpell("ACID", *tarGen.createTarget("Dummy Practice"));
std::cout << "--- Destructors:\n";
return (0);
}
std::cout << "\n-----------------\n";
// https://github.com/Kromolux/42_exam_rank_05/blob/main/cpp_module_02/main.cpp
{
Warlock richard("Richard", "foo");
richard.setTitle("Hello, I'm Richard the Warlock!");
BrickWall model1;
BrickWall test1(model1);
Polymorph* polymorph = new Polymorph();
TargetGenerator tarGen;
tarGen.learnTargetType(&test1);
richard.learnSpell(polymorph);
Fireball* fireball = new Fireball();
richard.learnSpell(fireball);
ATarget* wall = tarGen.createTarget("Inconspicuous Red-brick Wall");
richard.introduce();
richard.launchSpell("Polymorph", *wall);
richard.launchSpell("Fireball", *wall);
delete wall;
delete fireball;
delete polymorph;
}
}

View File

@@ -51,6 +51,7 @@ It will have the following functions:
* ATarget* createTarget(string const &), that creates a target of the * ATarget* createTarget(string const &), that creates a target of the
specified type specified type
--------------------------------------------------------------------------------
Phew, that's done. Now here's a test main. It's not very thorough, so make sure Phew, that's done. Now here's a test main. It's not very thorough, so make sure
to use your own aswell. to use your own aswell.