1
0

」 feat(Ex03): AMateria finished

This commit is contained in:
2024-12-12 19:29:36 +01:00
parent 7124f2072f
commit 8dfbf8e78c
11 changed files with 145 additions and 50 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
*.o *.o
obj/ obj/
Polymorphism Polymorphism
AMateria
compile_commands.json compile_commands.json
.cache .cache
*.d *.d

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/09 11:24:41 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"); log("", "AMateria", "", "default constructor called");
} }
AMateria::AMateria(std::string const &type) { AMateria::AMateria(std::string const &type) : _type(type) {
log("", "AMateria", "", "copy constructor called"); log("", "AMateria", "", "copy constructor called");
} }
@ -25,12 +25,4 @@ AMateria::~AMateria(void) {
log("", "AMateria", "", "destructor called"); 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); } std::string const &AMateria::getType(void) const { return (_type); }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/06 16:29:29 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; std::string _type;
public: public:
AMateria(void); AMateria(void);
~AMateria(void); virtual ~AMateria(void);
AMateria(const AMateria &); AMateria(const AMateria &);
AMateria(std::string const &); AMateria(std::string const &);
AMateria &operator=(const AMateria &); AMateria &operator=(const AMateria &);
std::string const &getType() const; std::string const &getType(void) const;
virtual AMateria *clone() const = 0; virtual AMateria *clone(void) const = 0;
virtual void use(ICharacter&); virtual void use(ICharacter &) = 0;
}; };

View File

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Character.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 14:59:12 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,14 +19,20 @@
class Character : public ICharacter { class Character : public ICharacter {
private: private:
AMateria _materias[ENV_SIZE]; std::string _name;
struct {
AMateria *obj;
bool equipped;
} _materias[ENV_SIZE];
public: public:
Character(void); Character(void);
~Character(void); ~Character(void);
Character(std::string const);
Character(const Character &); Character(const Character &);
Character &operator=(const Character &); Character &operator=(const Character &);
void equip(Character *); std::string const &getName(void) const;
void unequip(int idx); void equip(AMateria *);
void use(int idx, Character &); void unequip(int);
void use(int, ICharacter &);
}; };

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 12:34:54 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"); } const std::string Cure::_typeName(void) { return ("cure"); }
void Cure::use(ICharacter &character) { void Cure::use(ICharacter &character) {
std::cout << "* heals " << character.getName() << "s wounds *"; std::cout << "* heals " << character.getName() << "s wounds *" << std::endl;
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/07 12:13:03 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"); } const std::string Ice::_typeName(void) { return ("ice"); }
void Ice::use(ICharacter &character) { 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;
} }

View File

@ -6,13 +6,13 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 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 SHELL = bash
NAME = Amateria NAME = AMateria
CC = c++ CC = c++
@ -38,12 +38,12 @@ endif
all: $(NAME) all: $(NAME)
$(NAME): $(OBJS) $(NAME): $(OBJS)
@$(CC) $(FLAGS) $(OBJS) -o $(NAME) @$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n" @printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp $(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D) @mkdir -p $(@D)
@$(CC) $(FLAGS) -c $< -o $@ @$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n" @printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean: clean:

View File

@ -6,31 +6,35 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/11 12:17:06 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 "MateriaSource.hpp"
#include "AMateria.hpp"
#include "Log.hpp" #include "Log.hpp"
#include "MateriaTemplate.hpp"
MateriaSource::MateriaSource(void) { #include "iostream"
log("", "MateriaSource", "", "default constructor called");
}
MateriaSource::~MateriaSource(void) { MateriaSource::~MateriaSource(void) {
log("", "MateriaSource", "", "destructor called"); log("", "MateriaSource", "", "destructor called");
} }
MateriaSource::MateriaSource(const MateriaSource &cpy) { void MateriaSource::learnMateria(AMateria *materia) {
*this = cpy; for (int i = 0; i < MATERIA_COUNT; i++) {
log("", "MateriaSource", "", "copy constructor called"); if (!_materias[i]) {
_materias[i] = materia;
return ;
}
}
std::cout << "can't learn any more materia" << std::endl;
} }
MateriaSource &MateriaSource::operator=(const MateriaSource &cpy) { AMateria *MateriaSource::createMateria(std::string const &type) {
log("", "MateriaSource", "", "copy assignement constructor called"); for (int i = 0; i < MATERIA_COUNT; i++) {
for (int i = 0; i < 4; i++) { if (_materias[i]->getType() == type) {
this->_materias[i] = cpy._materias[i]; return (_materias[i]->clone());
} }
return (*this); }
return (NULL);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 13:51:37 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 "AMateria.hpp"
#include "IMateriaSource.hpp" #include "IMateriaSource.hpp"
#define MATERIA_COUNT 4
class MateriaSource : public IMateriaSource { class MateriaSource : public IMateriaSource {
private: private:
AMateria *_materias[4]; AMateria *_materias[MATERIA_COUNT];
public: public:
MateriaSource(void);
~MateriaSource(void); ~MateriaSource(void);
MateriaSource(const MateriaSource &);
MateriaSource &operator=(const MateriaSource &);
void learnMateria(AMateria *); void learnMateria(AMateria *);
AMateria *createMateria(std::string const &); AMateria *createMateria(std::string const &);

View File

@ -1,6 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/12 18:59:29 by adjoly #+# #+# */
/* Updated: 2024/12/12 18:59:34 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "AMateria.hpp" #include "AMateria.hpp"
#include "Character.hpp"
#include "IMateriaSource.hpp" #include "IMateriaSource.hpp"
#include "ICharacter.hpp" #include "ICharacter.hpp"
#include "MateriaSource.hpp"
#include "Cure.hpp" #include "Cure.hpp"
#include "Ice.hpp" #include "Ice.hpp"