Add the ICharacter interface and an implementation
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 13:51:26 +0000 (15:51 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 13:56:08 +0000 (15:56 +0200)
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 [new file with mode: 0644]
ex03/Character.h [new file with mode: 0644]
ex03/ICharacter.h [new file with mode: 0644]

diff --git a/ex03/Character.cpp b/ex03/Character.cpp
new file mode 100644 (file)
index 0000000..f776520
--- /dev/null
@@ -0,0 +1,68 @@
+#include "Character.h"
+#include "ICharacter.h"
+#include "AMateria.h"
+#include <string>
+
+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 (file)
index 0000000..0428f6d
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef CHARACTER_H
+# define CHARACTER_H
+
+# include "ICharacter.h"
+# include "AMateria.h"
+# include <string>
+
+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 (file)
index 0000000..91e015e
--- /dev/null
@@ -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