diff --git a/.gitignore b/.gitignore index e6e7059..eb87a8b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.o obj/ Polymorphism +AMateria compile_commands.json .cache *.d diff --git a/ex03/AMateria.cpp b/ex03/AMateria.cpp index e2efc87..189124b 100644 --- a/ex03/AMateria.cpp +++ b/ex03/AMateria.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/09 11:24:41 by adjoly #+# #+# */ -/* Updated: 2024/12/11 11:03:42 by adjoly ### ########.fr */ +/* Updated: 2024/12/12 19:22:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ AMateria::AMateria(void) : _type("") { log("➕", "AMateria", "", "default constructor called"); } -AMateria::AMateria(std::string const &type) { +AMateria::AMateria(std::string const &type) : _type(type) { log("➕", "AMateria", "", "copy constructor called"); } @@ -25,12 +25,4 @@ AMateria::~AMateria(void) { log("➖", "AMateria", "", "destructor called"); } -AMateria &AMateria::operator=(const AMateria &cpy) { - log("➕", "AMateria", "", "copy assignement constructor called"); - if (this != &cpy) { - _type = cpy._type; - } - return (*this); -} - std::string const &AMateria::getType(void) const { return (_type); } diff --git a/ex03/AMateria.hpp b/ex03/AMateria.hpp index 5da0e90..b81e7d7 100644 --- a/ex03/AMateria.hpp +++ b/ex03/AMateria.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/06 16:29:29 by adjoly #+# #+# */ -/* Updated: 2024/12/10 12:36:59 by adjoly ### ########.fr */ +/* Updated: 2024/12/12 19:27:30 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,12 +21,12 @@ class AMateria { std::string _type; public: AMateria(void); - ~AMateria(void); + virtual ~AMateria(void); AMateria(const AMateria &); AMateria(std::string const &); AMateria &operator=(const AMateria &); - std::string const &getType() const; - virtual AMateria *clone() const = 0; - virtual void use(ICharacter&); + std::string const &getType(void) const; + virtual AMateria *clone(void) const = 0; + virtual void use(ICharacter &) = 0; }; diff --git a/ex03/Character.cpp b/ex03/Character.cpp index e69de29..8eaccb5 100644 --- a/ex03/Character.cpp +++ b/ex03/Character.cpp @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Character.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/12 16:25:46 by adjoly #+# #+# */ +/* Updated: 2024/12/12 18:55:42 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Character.hpp" +#include "AMateria.hpp" +#include "ICharacter.hpp" +#include "Log.hpp" + +Character::Character(void) : _name("noname") { + log("➕", "Character", "", "default constructor called"); +} + +Character::~Character(void) { + log("➖", "Character", "", "destructor called"); + for (int i = 0; i < ENV_SIZE; i++) { + if (_materias[i].obj) + delete _materias[i].obj; + } +} + +Character::Character(std::string const name) : _name(name) { + log("➕", "Character", name, "name constructor called"); +} + +Character::Character(const Character &cpy) { + for(int i = 0; i < ENV_SIZE; i++) { + _materias[i].equipped = cpy._materias[i].equipped; + _materias[i].obj = cpy._materias[i].obj->clone(); + } + _name = cpy._name; + log("➕", "Character", "", "copy constructor called"); +} + +Character &Character::operator=(const Character &cpy) { + if (this != &cpy) { + _name = cpy._name; + for (int i = 0; i < ENV_SIZE; i++) { + _materias[i].obj = cpy._materias[i].obj->clone(); + _materias[i].equipped = cpy._materias[i].equipped; + } + } + log("➕", "Character", "", "copy assignement constructor called"); + return (*this); +} + +std::string const &Character::getName(void) const { + return(_name); +} + +void Character::equip(AMateria *m) { + for (int i = 0; i < ENV_SIZE; i++) { + if (!_materias[i].equipped) { + _materias[i].obj = m; + _materias[i].equipped = true; + return ; + } + } +} + +void Character::unequip(int idx) { + if (idx >= 0 && idx < ENV_SIZE) { + _materias[idx].equipped = false; + } +} + +void Character::use(int idx, ICharacter &character) { + if (idx >= 0 && idx < ENV_SIZE && _materias[idx].equipped) { + _materias[idx].obj->use(character); + } +} diff --git a/ex03/Character.hpp b/ex03/Character.hpp index d5f345e..8cae086 100644 --- a/ex03/Character.hpp +++ b/ex03/Character.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/10 14:59:12 by adjoly #+# #+# */ -/* Updated: 2024/12/10 15:43:54 by adjoly ### ########.fr */ +/* Updated: 2024/12/12 19:23:43 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,16 +17,22 @@ #define ENV_SIZE 4 -class Character : public ICharacter{ +class Character : public ICharacter { private: - AMateria _materias[ENV_SIZE]; + std::string _name; + struct { + AMateria *obj; + bool equipped; + } _materias[ENV_SIZE]; public: Character(void); ~Character(void); + Character(std::string const); Character(const Character &); Character &operator=(const Character &); - void equip(Character *); - void unequip(int idx); - void use(int idx, Character &); + std::string const &getName(void) const; + void equip(AMateria *); + void unequip(int); + void use(int, ICharacter &); }; diff --git a/ex03/Cure.cpp b/ex03/Cure.cpp index 30a6e6f..80b615b 100644 --- a/ex03/Cure.cpp +++ b/ex03/Cure.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/10 12:34:54 by adjoly #+# #+# */ -/* Updated: 2024/12/11 12:08:03 by adjoly ### ########.fr */ +/* Updated: 2024/12/12 19:28:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,5 +16,5 @@ const std::string Cure::_typeName(void) { return ("cure"); } void Cure::use(ICharacter &character) { - std::cout << "* heals " << character.getName() << "’s wounds *"; + std::cout << "* heals " << character.getName() << "’s wounds *" << std::endl; } diff --git a/ex03/Ice.cpp b/ex03/Ice.cpp index b19601d..d8636e3 100644 --- a/ex03/Ice.cpp +++ b/ex03/Ice.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/07 12:13:03 by adjoly #+# #+# */ -/* Updated: 2024/12/11 12:08:18 by adjoly ### ########.fr */ +/* Updated: 2024/12/12 19:27:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,5 +16,5 @@ const std::string Ice::_typeName(void) { return ("ice"); } void Ice::use(ICharacter &character) { - std::cout << "* shoots an ice bolt at " << character.getName() << " *"; + std::cout << "* shoots an ice bolt at " << character.getName() << " *" << std::endl; } diff --git a/ex03/Makefile b/ex03/Makefile index 09163c8..4720012 100644 --- a/ex03/Makefile +++ b/ex03/Makefile @@ -6,13 +6,13 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2024/12/11 14:08:19 by adjoly ### ########.fr # +# Updated: 2024/12/12 19:28:13 by adjoly ### ########.fr # # # # **************************************************************************** # SHELL = bash -NAME = Amateria +NAME = AMateria CC = c++ @@ -38,12 +38,12 @@ endif all: $(NAME) $(NAME): $(OBJS) - @$(CC) $(FLAGS) $(OBJS) -o $(NAME) + @$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME) @printf "$(YELLOW)「✨」($(NAME)) Program compiled\n" $(OBJSDIR)%.o: %.cpp @mkdir -p $(@D) - @$(CC) $(FLAGS) -c $< -o $@ + @$(CC) $(FLAGS) -I . -c $< -o $@ @printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n" clean: diff --git a/ex03/MateriaSource.cpp b/ex03/MateriaSource.cpp index 554ab60..29dd922 100644 --- a/ex03/MateriaSource.cpp +++ b/ex03/MateriaSource.cpp @@ -6,31 +6,35 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/11 12:17:06 by adjoly #+# #+# */ -/* Updated: 2024/12/11 12:31:04 by adjoly ### ########.fr */ +/* Updated: 2024/12/12 18:55:50 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "MateriaSource.hpp" +#include "AMateria.hpp" #include "Log.hpp" - -MateriaSource::MateriaSource(void) { - log("➕", "MateriaSource", "", "default constructor called"); -} +#include "MateriaTemplate.hpp" +#include "iostream" MateriaSource::~MateriaSource(void) { log("➖", "MateriaSource", "", "destructor called"); - } -MateriaSource::MateriaSource(const MateriaSource &cpy) { - *this = cpy; - log("➕", "MateriaSource", "", "copy constructor called"); -} - -MateriaSource &MateriaSource::operator=(const MateriaSource &cpy) { - log("➕", "MateriaSource", "", "copy assignement constructor called"); - for (int i = 0; i < 4; i++) { - this->_materias[i] = cpy._materias[i]; +void MateriaSource::learnMateria(AMateria *materia) { + for (int i = 0; i < MATERIA_COUNT; i++) { + if (!_materias[i]) { + _materias[i] = materia; + return ; + } } - return (*this); + std::cout << "can't learn any more materia" << std::endl; +} + +AMateria *MateriaSource::createMateria(std::string const &type) { + for (int i = 0; i < MATERIA_COUNT; i++) { + if (_materias[i]->getType() == type) { + return (_materias[i]->clone()); + } + } + return (NULL); } diff --git a/ex03/MateriaSource.hpp b/ex03/MateriaSource.hpp index 0ff8cb2..da94ec0 100644 --- a/ex03/MateriaSource.hpp +++ b/ex03/MateriaSource.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/10 13:51:37 by adjoly #+# #+# */ -/* Updated: 2024/12/11 12:18:02 by adjoly ### ########.fr */ +/* Updated: 2024/12/12 18:53:48 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,14 +15,13 @@ #include "AMateria.hpp" #include "IMateriaSource.hpp" +#define MATERIA_COUNT 4 + class MateriaSource : public IMateriaSource { private: - AMateria *_materias[4]; + AMateria *_materias[MATERIA_COUNT]; public: - MateriaSource(void); ~MateriaSource(void); - MateriaSource(const MateriaSource &); - MateriaSource &operator=(const MateriaSource &); void learnMateria(AMateria *); AMateria *createMateria(std::string const &); diff --git a/ex03/main.cpp b/ex03/main.cpp index 7f79467..747ec7b 100644 --- a/ex03/main.cpp +++ b/ex03/main.cpp @@ -1,6 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/12 18:59:29 by adjoly #+# #+# */ +/* Updated: 2024/12/12 18:59:34 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "AMateria.hpp" +#include "Character.hpp" #include "IMateriaSource.hpp" #include "ICharacter.hpp" +#include "MateriaSource.hpp" #include "Cure.hpp" #include "Ice.hpp"