From 4c30e98f4a4018a25d3a9f3ee790d589be803cb0 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 13 Nov 2020 14:43:17 +0100 Subject: Added main for cpp04/ex01 and cpp04/ex02, Fixed cpp04/03 MateriaSource --- cpp04/ex03/AMateria.cpp | 20 ++++++++++---------- cpp04/ex03/AMateria.hpp | 11 ++++++----- cpp04/ex03/Character.cpp | 24 +++++++++++++----------- cpp04/ex03/Character.hpp | 8 ++++++-- cpp04/ex03/Cure.cpp | 3 ++- cpp04/ex03/Cure.hpp | 4 ++-- cpp04/ex03/ICharacter.hpp | 9 ++------- cpp04/ex03/IMateriaSource.hpp | 2 +- cpp04/ex03/Ice.cpp | 4 ++-- cpp04/ex03/Ice.hpp | 4 ++-- cpp04/ex03/MateriaSource.cpp | 28 +++++++++++++++++++++++----- cpp04/ex03/MateriaSource.hpp | 8 +++++--- cpp04/ex03/main.cpp | 2 +- 13 files changed, 75 insertions(+), 52 deletions(-) (limited to 'cpp04/ex03') diff --git a/cpp04/ex03/AMateria.cpp b/cpp04/ex03/AMateria.cpp index 1dc7ca9..b08c4f2 100644 --- a/cpp04/ex03/AMateria.cpp +++ b/cpp04/ex03/AMateria.cpp @@ -6,21 +6,21 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 16:26:57 by charles #+# #+# */ -/* Updated: 2020/11/12 15:40:23 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:26:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "AMateria.hpp" -AMateria::AMateria(AMateria const& other) +AMateria::AMateria(AMateria const& other) : m_type(other.m_type) { *this = other; - m_type = ""; } AMateria& AMateria::operator=(AMateria const& other) { - _xp = other._xp; + _xp = other._xp; + // subject doesn't want type to be copied return *this; } @@ -28,13 +28,13 @@ AMateria::~AMateria() {} AMateria::AMateria(std::string const& type) : m_type(type), _xp(0) {} -std::string const& AMateria::getType() const +std::string const& AMateria::getType() const { return m_type; } +unsigned int AMateria::getXP() const { return _xp; } + +void AMateria::use(ICharacter& target) { - return m_type; + (void)target; + _xp += 10; } -unsigned int AMateria::getXP() const { return _xp; } - -void AMateria::use(ICharacter& target) { _xp += 10; } - AMateria::AMateria() {} diff --git a/cpp04/ex03/AMateria.hpp b/cpp04/ex03/AMateria.hpp index 7fef1c8..41ef132 100644 --- a/cpp04/ex03/AMateria.hpp +++ b/cpp04/ex03/AMateria.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 16:24:00 by charles #+# #+# */ -/* Updated: 2020/11/12 15:41:54 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:42:03 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,17 +26,18 @@ public: virtual ~AMateria(); AMateria(std::string const& type); + std::string const& getType() const; - unsigned int getXP() const; + unsigned int getXP() const; virtual AMateria* clone() const = 0; - virtual void use(ICharacter& target); + virtual void use(ICharacter& target); protected: AMateria(); - std::string m_type; - unsigned int _xp; // subject force _xp instead of m_xp + std::string m_type; + unsigned int _xp; // subject force _xp }; #endif diff --git a/cpp04/ex03/Character.cpp b/cpp04/ex03/Character.cpp index ee03e63..d159aaf 100644 --- a/cpp04/ex03/Character.cpp +++ b/cpp04/ex03/Character.cpp @@ -6,20 +6,18 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 16:45:54 by charles #+# #+# */ -/* Updated: 2020/11/12 15:43:14 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:20:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "Character.hpp" -Character::Character() : m_name(""), m_inventory_size(0) {} - -Character::Character(Character const& other) { *this = other; } +Character::Character(Character const& other) + : m_inventory_size(0) { *this = other; } Character& Character::operator=(Character const& other) { - for (int i = 0; i < m_inventory_size; i++) - delete m_inventory[i]; + destroyInventory(); m_inventory_size = other.m_inventory_size; for (int i = 0; i < m_inventory_size; i++) m_inventory[i] = other.m_inventory[i]->clone(); @@ -27,11 +25,7 @@ Character& Character::operator=(Character const& other) return *this; } -Character::~Character() -{ - for (int i = 0; i < m_inventory_size; i++) - delete m_inventory[i]; -} +Character::~Character() { destroyInventory(); } Character::Character(std::string const& name) : m_name(name), m_inventory_size(0) {} @@ -60,3 +54,11 @@ void Character::use(int idx, ICharacter& target) return; m_inventory[idx]->use(target); } + +void Character::destroyInventory() +{ + for (int i = 0; i < m_inventory_size; i++) + delete m_inventory[i]; +} + +Character::Character() {} diff --git a/cpp04/ex03/Character.hpp b/cpp04/ex03/Character.hpp index 6526ba2..5a27d2c 100644 --- a/cpp04/ex03/Character.hpp +++ b/cpp04/ex03/Character.hpp @@ -6,13 +6,14 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 16:44:43 by charles #+# #+# */ -/* Updated: 2020/11/12 15:42:43 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:17:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CHARACTER_HPP # define CHARACTER_HPP +# include # include "ICharacter.hpp" # define INVENTORY_MAX_SIZE 4 @@ -20,7 +21,6 @@ class Character : public ICharacter { public: - Character(); Character(Character const& other); Character& operator=(Character const& other); virtual ~Character(); @@ -35,6 +35,10 @@ private: AMateria* m_inventory[INVENTORY_MAX_SIZE]; int m_inventory_size; std::string m_name; + + void destroyInventory(); + + Character(); }; #endif diff --git a/cpp04/ex03/Cure.cpp b/cpp04/ex03/Cure.cpp index e27ae26..dc464ff 100644 --- a/cpp04/ex03/Cure.cpp +++ b/cpp04/ex03/Cure.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 17:38:01 by charles #+# #+# */ -/* Updated: 2020/11/12 15:43:44 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:09:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,4 +29,5 @@ AMateria* Cure::clone() const { return new Cure(*this); } void Cure::use(ICharacter& target) { std::cout << "* heals " << target.getName() << "'s wounds *" << std::endl; + AMateria::use(target); } diff --git a/cpp04/ex03/Cure.hpp b/cpp04/ex03/Cure.hpp index ccc86d5..71647ff 100644 --- a/cpp04/ex03/Cure.hpp +++ b/cpp04/ex03/Cure.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 17:37:11 by charles #+# #+# */ -/* Updated: 2020/11/12 15:43:24 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:09:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ public: ~Cure(); virtual AMateria* clone() const; - virtual void use(ICharacter& target); + virtual void use(ICharacter& target); private: }; diff --git a/cpp04/ex03/ICharacter.hpp b/cpp04/ex03/ICharacter.hpp index bb84d7e..786be6a 100644 --- a/cpp04/ex03/ICharacter.hpp +++ b/cpp04/ex03/ICharacter.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 16:40:25 by charles #+# #+# */ -/* Updated: 2020/11/12 15:42:12 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:27:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ # include # include "AMateria.hpp" -class IMateria; +class AMateria; class ICharacter { @@ -26,11 +26,6 @@ public: virtual void equip(AMateria* m) = 0; virtual void unequip(int idx) = 0; virtual void use(int idx, ICharacter& target) = 0; - -protected: - ICharacter(); - ICharacter(ICharacter const& other); - ICharacter& operator=(ICharacter const& other); }; #endif diff --git a/cpp04/ex03/IMateriaSource.hpp b/cpp04/ex03/IMateriaSource.hpp index 5161604..63b5f01 100644 --- a/cpp04/ex03/IMateriaSource.hpp +++ b/cpp04/ex03/IMateriaSource.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 17:15:52 by charles #+# #+# */ -/* Updated: 2020/04/14 17:21:54 by charles ### ########.fr */ +/* Updated: 2020/11/13 14:23:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp04/ex03/Ice.cpp b/cpp04/ex03/Ice.cpp index c663104..9df83b2 100644 --- a/cpp04/ex03/Ice.cpp +++ b/cpp04/ex03/Ice.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 16:32:27 by charles #+# #+# */ -/* Updated: 2020/11/12 15:44:48 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:06:33 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ Ice::Ice() : AMateria("ice") {} -Ice::Ice(Ice const& other) { *this = other; } +Ice::Ice(Ice const& other) : AMateria(other) {} Ice& Ice::operator=(Ice const& other) { diff --git a/cpp04/ex03/Ice.hpp b/cpp04/ex03/Ice.hpp index 8b670f6..41f07de 100644 --- a/cpp04/ex03/Ice.hpp +++ b/cpp04/ex03/Ice.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 16:31:34 by charles #+# #+# */ -/* Updated: 2020/11/12 15:44:31 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:09:24 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ public: ~Ice(); virtual AMateria* clone() const; - virtual void use(ICharacter& target); + virtual void use(ICharacter& target); private: }; diff --git a/cpp04/ex03/MateriaSource.cpp b/cpp04/ex03/MateriaSource.cpp index 087af3e..774506b 100644 --- a/cpp04/ex03/MateriaSource.cpp +++ b/cpp04/ex03/MateriaSource.cpp @@ -6,28 +6,46 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 17:19:10 by charles #+# #+# */ -/* Updated: 2020/11/12 15:45:23 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:40:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "MateriaSource.hpp" -MateriaSource::MateriaSource() {} +MateriaSource::MateriaSource() : m_learned_size(0) {} -MateriaSource::MateriaSource(MateriaSource const& other) { *this = other; } +MateriaSource::MateriaSource(MateriaSource const& other) + : m_learned_size(0) { *this = other; } -void MateriaSource::operator=(MateriaSource const& other) +MateriaSource& MateriaSource::operator=(MateriaSource const& other) { + destroyLearned(); + m_learned_size = other.m_learned_size; + for (int i = 0; i < m_learned_size; i++) + m_learned[i] = other.m_learned[i]->clone(); return *this; } -MateriaSource::~MateriaSource() {} +MateriaSource::~MateriaSource() { destroyLearned(); } void MateriaSource::learnMateria(AMateria* materia) { + if (m_learned_size >= 4) + return; + m_learned[m_learned_size] = materia->clone(); + m_learned_size++; } AMateria* MateriaSource::createMateria(std::string const& type) { + for (int i = 0; i < m_learned_size; i++) + if (m_learned[i]->getType() == type) + return m_learned[i]->clone(); return NULL; } + +void MateriaSource::destroyLearned() +{ + for (int i = 0; i < m_learned_size; i++) + delete m_learned[i]; +} diff --git a/cpp04/ex03/MateriaSource.hpp b/cpp04/ex03/MateriaSource.hpp index eb233eb..51f0a7e 100644 --- a/cpp04/ex03/MateriaSource.hpp +++ b/cpp04/ex03/MateriaSource.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 17:17:40 by charles #+# #+# */ -/* Updated: 2020/11/12 15:45:28 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:41:04 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,12 +25,14 @@ public: MateriaSource& operator=(MateriaSource const& other); virtual ~MateriaSource(); - virtual void learnMateria(AMateria* materia); + virtual void learnMateria(AMateria* materia); virtual AMateria* createMateria(std::string const& type); private: AMateria* m_learned[LEARNED_MAX_SIZE]; - int m_learned_size; + int m_learned_size; + + void destroyLearned(); }; #endif diff --git a/cpp04/ex03/main.cpp b/cpp04/ex03/main.cpp index 11f471f..871007d 100644 --- a/cpp04/ex03/main.cpp +++ b/cpp04/ex03/main.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 17:58:01 by charles #+# #+# */ -/* Updated: 2020/04/14 17:59:02 by charles ### ########.fr */ +/* Updated: 2020/11/13 14:41:49 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -- cgit