Add solution to ex02
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 18 Oct 2024 10:44:59 +0000 (12:44 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 18 Oct 2024 10:45:37 +0000 (12:45 +0200)
ex02/ClapTrap.cpp [new file with mode: 0644]
ex02/ClapTrap.h [new file with mode: 0644]
ex02/FragTrap.cpp [new file with mode: 0644]
ex02/FragTrap.h [new file with mode: 0644]
ex02/Makefile [new file with mode: 0644]
ex02/ScavTrap.cpp [new file with mode: 0644]
ex02/ScavTrap.h [new file with mode: 0644]
ex02/main.cpp [new file with mode: 0644]

diff --git a/ex02/ClapTrap.cpp b/ex02/ClapTrap.cpp
new file mode 100644 (file)
index 0000000..390ca63
--- /dev/null
@@ -0,0 +1,82 @@
+#include "ClapTrap.h"
+#include <string>
+#include <iostream>
+
+ClapTrap::ClapTrap(std::string name):
+       m_name(name), m_hp(10), m_energy(10), m_attack(0)
+{
+       std::cout << "ClapTrap " << m_name << " has spawned.\n";
+}
+
+ClapTrap::ClapTrap(const ClapTrap &other)
+{
+       std::cout << "ClapTrap " << m_name << " has spawned.\n";
+       *this = other;
+}
+
+ClapTrap::~ClapTrap()
+{
+       std::cout << "ClapTrap " << m_name << " has despawned.\n";
+}
+
+ClapTrap       &ClapTrap::operator=(const ClapTrap &other)
+{
+       if (this == &other)
+               return (*this);
+       m_name = other.m_name;
+       m_hp = other.m_hp;
+       m_energy = other.m_energy;
+       m_attack = other.m_attack;
+       return (*this);
+}
+
+void   ClapTrap::attack(const std::string &target)
+{
+       if (m_hp == 0)
+               std::cout << "ClapTrap " << m_name
+                       << " is dead, hence cannot attack.\n";
+       else if (m_energy == 0)
+               std::cout << "ClapTrap " << m_name
+                       << " does not have enough energy to attack.\n";
+       else
+       {
+               std::cout << "ClapTrap " << m_name
+                       << " attacks " << target << " and deals " << m_attack << " damage.\n";
+               --m_energy;
+       }
+}
+
+void   ClapTrap::takeDamage(unsigned int amount)
+{
+       if (m_hp == 0)
+               std::cout << m_name <<
+                       " is already dead and cannot be damaged further.\n";
+       else if (amount >= m_hp)
+       {
+               std::cout << m_name
+                       << " takes " << amount << " damage and dies\n";
+               m_hp = 0;
+       }
+       else
+       {
+               std::cout << m_name
+                       << " takes " << amount << " damage.\n";
+               m_hp -= amount;
+       }
+}
+
+void   ClapTrap::beRepaired(unsigned int amount)
+{
+       if (m_hp == 0)
+               std::cout << m_name
+                       << " is dead, hence cannot repair itself.\n";
+       else if (m_energy == 0)
+               std::cout << m_name
+                       << " does not have enough energy to repair itself.\n";
+       else
+       {
+               std::cout << m_name
+                       << " repairs iself for " << amount << " hit points.\n";
+               m_hp += amount;
+       }
+}
diff --git a/ex02/ClapTrap.h b/ex02/ClapTrap.h
new file mode 100644 (file)
index 0000000..59e7d33
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef CLAPTRAP_H
+# define CLAPTRAP_H
+
+# include <string>
+
+class ClapTrap
+{
+       protected:
+               std::string             m_name;
+               unsigned int    m_hp;
+               unsigned int    m_energy;
+               unsigned int    m_attack;
+
+       public:
+               ClapTrap(std::string name = "DEFAULT");
+               ClapTrap(const ClapTrap &other);
+               ~ClapTrap();
+
+               ClapTrap        &operator=(const ClapTrap &other);
+
+               void    attack(const std::string &target);
+               void    takeDamage(unsigned int amount);
+               void    beRepaired(unsigned int amount);
+};
+
+#endif // CLAPTRAP_H
diff --git a/ex02/FragTrap.cpp b/ex02/FragTrap.cpp
new file mode 100644 (file)
index 0000000..882213e
--- /dev/null
@@ -0,0 +1,56 @@
+#include "FragTrap.h"
+#include "ClapTrap.h"
+#include <string>
+#include <iostream>
+
+FragTrap::FragTrap(std::string name): ClapTrap(name)
+{
+       std::cout << "FragTrap " << m_name << " has spawned.\n";
+       m_hp = 100;
+       m_energy = 50;
+       m_attack = 20;
+       m_is_guarding = 0;
+}
+
+FragTrap::FragTrap(const FragTrap &other)
+{
+       *this = other;
+}
+
+FragTrap::~FragTrap()
+{
+       std::cout << "FragTrap " << m_name << " has despawned.\n";
+}
+
+FragTrap       &FragTrap::operator=(const FragTrap &other)
+{
+       if (this == &other)
+               return (*this);
+       ClapTrap::operator=(other);
+       m_is_guarding = other.m_is_guarding;
+       return (*this);
+}
+
+void   FragTrap::attack(const std::string &target)
+{
+       if (m_hp == 0)
+               std::cout << "FragTrap " << m_name
+                       << " is dead, hence cannot attack.\n";
+       else if (m_energy == 0)
+               std::cout << "FragTrap " << m_name
+                       << " does not have enough energy to attack.\n";
+       else
+       {
+               std::cout << "FragTrap " << m_name
+                       << " attacks " << target << " and deals " << m_attack << " damage.\n";
+               --m_energy;
+       }
+}
+
+void   FragTrap::highFivesGuys()
+{
+       if (m_hp == 0)
+               std::cout << m_name << " is dead and cannot request high fives.\n";
+       else
+               std::cout << m_name << " raises hand and shouts, \"Up top!\"\n";
+}
diff --git a/ex02/FragTrap.h b/ex02/FragTrap.h
new file mode 100644 (file)
index 0000000..64984d3
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef FRAGTRAP_H
+# define FRAGTRAP_H
+
+# include "ClapTrap.h"
+# include <string>
+
+class FragTrap : public ClapTrap
+{
+       private:
+               bool    m_is_guarding;
+
+       public:
+               FragTrap(std::string name = "DEFAULT");
+               FragTrap(const FragTrap &other);
+               ~FragTrap();
+
+               FragTrap        &operator=(const FragTrap &other);
+
+               void    attack(const std::string &target);
+               void    highFivesGuys();
+};
+
+#endif // FRAGTRAP_H
diff --git a/ex02/Makefile b/ex02/Makefile
new file mode 100644 (file)
index 0000000..3cc7ef3
--- /dev/null
@@ -0,0 +1,55 @@
+CC := c++
+CFLAGS = -std=c++98 -Wall -Wextra -Werror -Wpedantic
+
+ifneq ("$(wildcard .debug)","")
+       CFLAGS += -g
+endif
+
+RM := rm -f
+
+INCDIR := inc
+INCDIR += $(addsuffix /inc, $(SUBPROJECTS));
+ifneq ($(INCLUDE),)
+       INCLUDE := $(addprefix -I, $(INCDIR))
+endif
+
+SRCDIR := .
+
+SOURCES :=     main.cpp                \
+                       ClapTrap.cpp    \
+                       ScavTrap.cpp    \
+                       FragTrap.cpp    \
+
+SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
+
+OBJECTS := $(SOURCES:.cpp=.o)
+
+NAME := fragtrap
+
+all : $(NAME)
+
+debug : .debug
+       $(MAKE) all
+
+nodebug :
+       $(RM) .debug
+       $(MAKE) re
+
+.% :
+       $(MAKE) shallow_fclean
+       touch $@
+
+$(NAME) : $(OBJECTS)
+       $(CC) $(CFLAGS) -o $@ $^ $(LINKS)
+
+%.o : %.cpp
+       $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE)
+
+clean :
+       $(RM) $(OBJECTS)
+
+fclean : clean
+       $(RM) $(NAME)
+
+re : fclean
+       $(MAKE) all
diff --git a/ex02/ScavTrap.cpp b/ex02/ScavTrap.cpp
new file mode 100644 (file)
index 0000000..f867bbe
--- /dev/null
@@ -0,0 +1,61 @@
+#include "ScavTrap.h"
+#include "ClapTrap.h"
+#include <string>
+#include <iostream>
+
+ScavTrap::ScavTrap(std::string name): ClapTrap(name)
+{
+       std::cout << "ScavTrap " << m_name << " has spawned.\n";
+       m_hp = 100;
+       m_energy = 50;
+       m_attack = 20;
+       m_is_guarding = 0;
+}
+
+ScavTrap::ScavTrap(const ScavTrap &other)
+{
+       *this = other;
+}
+
+ScavTrap::~ScavTrap()
+{
+       std::cout << "ScavTrap " << m_name << " has despawned.\n";
+}
+
+ScavTrap       &ScavTrap::operator=(const ScavTrap &other)
+{
+       if (this == &other)
+               return (*this);
+       ClapTrap::operator=(other);
+       m_is_guarding = other.m_is_guarding;
+       return (*this);
+}
+
+void   ScavTrap::attack(const std::string &target)
+{
+       if (m_hp == 0)
+               std::cout << "ScavTrap " << m_name
+                       << " is dead, hence cannot attack.\n";
+       else if (m_energy == 0)
+               std::cout << "ScavTrap " << m_name
+                       << " does not have enough energy to attack.\n";
+       else
+       {
+               std::cout << "ScavTrap " << m_name
+                       << " attacks " << target << " and deals " << m_attack << " damage.\n";
+               --m_energy;
+       }
+}
+
+void   ScavTrap::guardGate()
+{
+       if (m_hp == 0)
+               std::cout << m_name << " is dead and cannot guard the gate.\n";
+       else if (m_is_guarding == 0)
+       {
+               std::cout << m_name << " is now guarding the gate.\n";
+               m_is_guarding = 1;
+       }
+       else
+               std::cout << m_name << " is already guarding the gate.\n";
+}
diff --git a/ex02/ScavTrap.h b/ex02/ScavTrap.h
new file mode 100644 (file)
index 0000000..4a538e5
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef SCAVTRAP_H
+# define SCAVTRAP_H
+
+# include "ClapTrap.h"
+# include <string>
+
+class ScavTrap : public ClapTrap
+{
+       private:
+               bool    m_is_guarding;
+
+       public:
+               ScavTrap(std::string name = "DEFAULT");
+               ScavTrap(const ScavTrap &other);
+               ~ScavTrap();
+
+               ScavTrap        &operator=(const ScavTrap &other);
+
+               void    attack(const std::string &target);
+               void    guardGate();
+};
+
+#endif // SCAVTRAP_H
diff --git a/ex02/main.cpp b/ex02/main.cpp
new file mode 100644 (file)
index 0000000..03093ee
--- /dev/null
@@ -0,0 +1,33 @@
+#include "ClapTrap.h"
+#include "ScavTrap.h"
+#include "FragTrap.h"
+
+int    main()
+{
+       ScavTrap        a("Alen");
+       ClapTrap        def;
+       FragTrap        b("Glenn");
+
+       def.attack("foo");
+       def.takeDamage(9);
+       def.takeDamage(0);
+       def.beRepaired(6);
+       def.takeDamage(5);
+       def.takeDamage(1);
+       def.takeDamage(1);
+       def.takeDamage(1);
+       def.attack("foo");
+       def.beRepaired(10);
+       a.takeDamage(9);
+       a.guardGate();
+       a.guardGate();
+       a.attack("bar");
+       a.beRepaired(1);
+       a.takeDamage(100);
+       a.takeDamage(1);
+       a.guardGate();
+       a.attack("bar");
+       a.beRepaired(1);
+       b.highFivesGuys();
+       return (0);
+}