From 806c2734cd45f8964e12f59b39cd56380919de88 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 25 Oct 2024 15:51:26 +0200 Subject: [PATCH] Add the ICharacter interface and an implementation Again there is a conversation to be head about the privatization of assignment operator and copy constructor. Assignment is not possible as the name is const. Copy constructor has implementation provided. --- ex03/Character.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++ ex03/Character.h | 31 +++++++++++++++++++++ ex03/ICharacter.h | 15 ++++++++++ 3 files changed, 114 insertions(+) create mode 100644 ex03/Character.cpp create mode 100644 ex03/Character.h create mode 100644 ex03/ICharacter.h diff --git a/ex03/Character.cpp b/ex03/Character.cpp new file mode 100644 index 0000000..f776520 --- /dev/null +++ b/ex03/Character.cpp @@ -0,0 +1,68 @@ +#include "Character.h" +#include "ICharacter.h" +#include "AMateria.h" +#include + +Character::Character() +{ +} + +Character::Character(const std::string &name) + : m_name(name) +{ + for (size_t i(0); i < INVENTORY_SIZE; ++i) + inventory[i] = NULL; +} + +Character::Character(const Character &other) + : m_name(other.m_name) +{ + for (size_t i(0); i < INVENTORY_SIZE; ++i) + { + if (other.inventor[i] == NULL) + inventory[i] = NULL; + else + inventory[i] = other.inventory[i]->clone(); + } +} + +Character::~Character() +{ + for (size_t i(0); i < INVENTORY_SIZE; ++i) + delete inventory[i]; +} + +Character &Character::operator=(const Character &other) +{ +} + +void Character::equip(AMateria *materia) +{ + for (size_t i(0); i < INVENTORY_SIZE; ++i) + if (m_inventory[i] == NULL) + { + m_inventory[i] = materia + return ; + } +} + +void Character::unequip(int idx) +{ + if (idx < 0 || idx >= INVENTORY_SIZE) + return ; + m_inventory[idx] = NULL; +} + +void Character::use(int idx, ICharacter &target) +{ + if (idx < 0 || idx >= INVENTORY_SIZE) + return ; + if (m_inventory[i] == NULL) + return ; + m_inventory[i]->use(target); +} + +const std::string &Character::getName() const +{ + return (m_name); +} diff --git a/ex03/Character.h b/ex03/Character.h new file mode 100644 index 0000000..0428f6d --- /dev/null +++ b/ex03/Character.h @@ -0,0 +1,31 @@ +#ifndef CHARACTER_H +# define CHARACTER_H + +# include "ICharacter.h" +# include "AMateria.h" +# include + +class Character : public ICharacter +{ + private: + Character(); + Character(const Character &other); + + Character &operator=(const Character &other); + + static const size_t INVENTORY_SIZE = 4; + + const std::string m_name; + AMateria *m_inventory[INVENTORY_SIZE]; + + public: + Character(std::string name); + ~Character(); + + void equip(AMateria *materia); + void unequip(int idx); + void use(int idx, ICharacter &target); + +}; + +#endif // CHARACTER_H diff --git a/ex03/ICharacter.h b/ex03/ICharacter.h new file mode 100644 index 0000000..91e015e --- /dev/null +++ b/ex03/ICharacter.h @@ -0,0 +1,15 @@ +#ifndef ICHARACTER_H +# define ICHARACTER_H + +class ICharacter +{ + public: + virtual ~ICharacter() {} + + virtual const std::string &getName() const = 0; + virtual void equip(AMateria *m) = 0; + virtual void unequip(int idx) = 0; + virtual void use(int idx, ICharacter &target) = 0; +}; + +#endif // ICHARACTER_H -- 2.30.2