aboutsummaryrefslogtreecommitdiff
path: root/cpp04/ex01
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-11-13 14:43:17 +0100
committerCharles Cabergs <me@cacharle.xyz>2020-11-13 14:43:17 +0100
commit4c30e98f4a4018a25d3a9f3ee790d589be803cb0 (patch)
tree655a4bd52a7a6633a32782eab935fb01a47040e9 /cpp04/ex01
parentb8e39b947890e74d82530e25ad9d02668aae1f0c (diff)
downloadpiscine_cpp-4c30e98f4a4018a25d3a9f3ee790d589be803cb0.tar.gz
piscine_cpp-4c30e98f4a4018a25d3a9f3ee790d589be803cb0.tar.bz2
piscine_cpp-4c30e98f4a4018a25d3a9f3ee790d589be803cb0.zip
Added main for cpp04/ex01 and cpp04/ex02, Fixed cpp04/03 MateriaSource
Diffstat (limited to 'cpp04/ex01')
-rw-r--r--cpp04/ex01/Character.cpp6
-rw-r--r--cpp04/ex01/Character.hpp2
-rw-r--r--cpp04/ex01/RadScorpion.hpp4
-rw-r--r--cpp04/ex01/SuperMutant.cpp4
-rw-r--r--cpp04/ex01/SuperMutant.hpp4
-rw-r--r--cpp04/ex01/main.cpp178
6 files changed, 187 insertions, 11 deletions
diff --git a/cpp04/ex01/Character.cpp b/cpp04/ex01/Character.cpp
index 5f865c7..0952738 100644
--- a/cpp04/ex01/Character.cpp
+++ b/cpp04/ex01/Character.cpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 13:44:35 by charles #+# #+# */
-/* Updated: 2020/11/12 14:28:26 by cacharle ### ########.fr */
+/* Updated: 2020/11/13 11:51:31 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -41,7 +41,7 @@ void Character::equip(AWeapon *weapon)
void Character::attack(Enemy *enemy)
{
- if (m_weapon == NULL || m_ap < m_weapon->getAPCost())
+ if (enemy == NULL || m_weapon == NULL || m_ap < m_weapon->getAPCost())
return;
std::cout << m_name << " attacks " << enemy->getType() << " with a " << m_weapon->getName() << std::endl;
m_weapon->attack();
@@ -49,6 +49,8 @@ void Character::attack(Enemy *enemy)
if (enemy->getHP() <= 0)
delete enemy;
m_ap -= m_weapon->getAPCost();
+ if (m_ap < 0)
+ m_ap = 0;
}
std::string const& Character::getName() const { return m_name; }
diff --git a/cpp04/ex01/Character.hpp b/cpp04/ex01/Character.hpp
index 817fd53..08f1b59 100644
--- a/cpp04/ex01/Character.hpp
+++ b/cpp04/ex01/Character.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 13:42:25 by charles #+# #+# */
-/* Updated: 2020/11/12 13:56:59 by cacharle ### ########.fr */
+/* Updated: 2020/11/13 11:14:03 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/cpp04/ex01/RadScorpion.hpp b/cpp04/ex01/RadScorpion.hpp
index f07891e..70bac44 100644
--- a/cpp04/ex01/RadScorpion.hpp
+++ b/cpp04/ex01/RadScorpion.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 13:35:04 by charles #+# #+# */
-/* Updated: 2020/11/12 14:24:38 by cacharle ### ########.fr */
+/* Updated: 2020/11/13 11:41:55 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,7 +22,7 @@ public:
RadScorpion();
RadScorpion(RadScorpion const& other);
RadScorpion& operator=(RadScorpion const& other);
- ~RadScorpion();
+ virtual ~RadScorpion();
private:
};
diff --git a/cpp04/ex01/SuperMutant.cpp b/cpp04/ex01/SuperMutant.cpp
index c9ce83c..8813217 100644
--- a/cpp04/ex01/SuperMutant.cpp
+++ b/cpp04/ex01/SuperMutant.cpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 13:30:47 by charles #+# #+# */
-/* Updated: 2020/11/12 13:54:19 by cacharle ### ########.fr */
+/* Updated: 2020/11/13 10:35:24 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,5 +35,7 @@ SuperMutant::~SuperMutant()
void SuperMutant::takeDamage(int amount)
{
+ if (amount < 0)
+ amount = 3;
Enemy::takeDamage(amount - 3);
}
diff --git a/cpp04/ex01/SuperMutant.hpp b/cpp04/ex01/SuperMutant.hpp
index 64198de..4170a18 100644
--- a/cpp04/ex01/SuperMutant.hpp
+++ b/cpp04/ex01/SuperMutant.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 13:29:53 by charles #+# #+# */
-/* Updated: 2020/11/12 13:50:54 by cacharle ### ########.fr */
+/* Updated: 2020/11/13 11:42:08 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,7 +22,7 @@ public:
SuperMutant();
SuperMutant(SuperMutant const& other);
SuperMutant& operator=(SuperMutant const& other);
- ~SuperMutant();
+ virtual ~SuperMutant();
virtual void takeDamage(int amount);
diff --git a/cpp04/ex01/main.cpp b/cpp04/ex01/main.cpp
index ca5b35e..5b907bd 100644
--- a/cpp04/ex01/main.cpp
+++ b/cpp04/ex01/main.cpp
@@ -6,19 +6,22 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 09:05:58 by charles #+# #+# */
-/* Updated: 2020/11/12 14:36:49 by cacharle ### ########.fr */
+/* Updated: 2020/11/13 12:00:52 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
+#include <climits>
#include "Character.hpp"
+#include "Enemy.hpp"
#include "RadScorpion.hpp"
+#include "SuperMutant.hpp"
#include "PlasmaRifle.hpp"
#include "PowerFist.hpp"
int main()
{
{
- std::cout << "================ SUBJECT MAIN =====================" << std::endl;
+ std::cout << "================ SUBJECT MAIN =====================" << std::endl;
Character* me = new Character("me");
std::cout << *me;
Enemy* b = new RadScorpion();
@@ -36,7 +39,6 @@ int main()
me->attack(b);
std::cout << *me;
delete me;
- ((RadScorpion*)b)->~RadScorpion();
delete pr;
delete pf;
}
@@ -44,7 +46,177 @@ int main()
std::cout << std::endl;
{
+ std::cout << "================ PLASMA RIFLE =====================" << std::endl;
+ PlasmaRifle pr;
+ PlasmaRifle pr2(pr);
+ PlasmaRifle pr3;
+ pr3 = pr2;
+ std::cout << "Name: " << pr.getName() << std::endl;
+ std::cout << "AP Cost: " << pr.getAPCost() << std::endl;
+ std::cout << "Damage: " << pr.getDamage() << std::endl;
+ pr.attack();
+ pr2.attack();
+ pr3.attack();
+ std::cout << "################ VIRTUAL" << std::endl;
+ AWeapon *w = &pr;
+ std::cout << "Name: " << w->getName() << std::endl;
+ std::cout << "AP Cost: " << w->getAPCost() << std::endl;
+ std::cout << "Damage: " << w->getDamage() << std::endl;
+ w->attack();
+ }
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "================ POWER FIST =====================" << std::endl;
+ PowerFist pf;
+ PowerFist pf2(pf);
+ PowerFist pf3;
+ pf3 = pf2;
+ std::cout << "Name: " << pf.getName() << std::endl;
+ std::cout << "AP Cost: " << pf.getAPCost() << std::endl;
+ std::cout << "Damage: " << pf.getDamage() << std::endl;
+ pf.attack();
+ pf2.attack();
+ pf3.attack();
+ std::cout << "################ VIRTUAL" << std::endl;
+ AWeapon *w = &pf;
+ std::cout << "Name: " << w->getName() << std::endl;
+ std::cout << "AP Cost: " << w->getAPCost() << std::endl;
+ std::cout << "Damage: " << w->getDamage() << std::endl;
+ w->attack();
+ }
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "================ ENEMY =====================" << std::endl;
+ Enemy e(2000, "Titan");
+ Enemy e2(e);
+ Enemy e3(0, "should not be printed");
+ e3 = e2;
+ std::cout << "Type: " << e.getType() << ", HP: " << e.getHP() << std::endl;
+ std::cout << "Type: " << e2.getType() << ", HP: " << e2.getHP() << std::endl;
+ std::cout << "Type: " << e3.getType() << ", HP: " << e3.getHP() << std::endl;
+ std::cout << "################ TAKE DAMAGE" << std::endl;
+ std::cout << "HP: " << e.getHP() << std::endl;
+ e.takeDamage(10);
+ std::cout << "HP: " << e.getHP() << std::endl;
+ e.takeDamage(-1);
+ std::cout << "HP: " << e.getHP() << std::endl;
+ e.takeDamage(INT_MIN);
+ std::cout << "HP: " << e.getHP() << std::endl;
+ e.takeDamage(0);
+ std::cout << "HP: " << e.getHP() << std::endl;
+ e.takeDamage(3000);
+ std::cout << "HP: " << e.getHP() << std::endl;
+ }
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "================ SUPER MUTANT ====================" << std::endl;
+ SuperMutant m;
+ SuperMutant m2(m);
+ SuperMutant m3;
+ m3 = m2;
+ std::cout << "Type: " << m.getType() << ", HP: " << m.getHP() << std::endl;
+ std::cout << "Type: " << m2.getType() << ", HP: " << m2.getHP() << std::endl;
+ std::cout << "Type: " << m3.getType() << ", HP: " << m3.getHP() << std::endl;
+ std::cout << "################ TAKE DAMAGE" << std::endl;
+ std::cout << "HP: " << m.getHP() << std::endl;
+ m.takeDamage(10);
+ std::cout << "HP: " << m.getHP() << std::endl;
+ m.takeDamage(-1);
+ std::cout << "HP: " << m.getHP() << std::endl;
+ m.takeDamage(INT_MIN);
+ std::cout << "HP: " << m.getHP() << std::endl;
+ m.takeDamage(0);
+ std::cout << "HP: " << m.getHP() << std::endl;
+ m.takeDamage(180);
+ std::cout << "HP: " << m.getHP() << std::endl;
+ }
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "================ RAD SCORPION ====================" << std::endl;
+ RadScorpion s;
+ RadScorpion s2(s);
+ RadScorpion s3;
+ s3 = s2;
+ std::cout << "Type: " << s.getType() << ", HP: " << s.getHP() << std::endl;
+ std::cout << "Type: " << s2.getType() << ", HP: " << s2.getHP() << std::endl;
+ std::cout << "Type: " << s3.getType() << ", HP: " << s3.getHP() << std::endl;
+ std::cout << "################ TAKE DAMAGE" << std::endl;
+ std::cout << "HP: " << s.getHP() << std::endl;
+ s.takeDamage(10);
+ std::cout << "HP: " << s.getHP() << std::endl;
+ s.takeDamage(-1);
+ std::cout << "HP: " << s.getHP() << std::endl;
+ s.takeDamage(INT_MIN);
+ std::cout << "HP: " << s.getHP() << std::endl;
+ s.takeDamage(0);
+ std::cout << "HP: " << s.getHP() << std::endl;
+ s.takeDamage(90);
+ std::cout << "HP: " << s.getHP() << std::endl;
+ }
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "================ CHARACTER ====================" << std::endl;
+ Character c("Jean-Didier");
+ Character c2(c);
+ Character c3("should not be displayed");
+ c3 = c2;
+ std::cout << c;
+ std::cout << c2;
+ std::cout << c3;
+
+ std::cout << "################ ATTACK NO WEAPON" << std::endl;
+ Enemy* r = new RadScorpion();
+ c.attack(r);
+ Enemy* s = new SuperMutant();
+ c.attack(s);
+
+ std::cout << "################ EQUIP" << std::endl;
+ PowerFist pf;
+ c.equip(&pf);
+ c.attack(r);
+ c.attack(s);
+ std::cout << c;
+ PlasmaRifle pr;
+ c.equip(&pr);
+ c.attack(r);
+ c.attack(s);
+ std::cout << c;
+ c.attack(s);
+ c.attack(s);
+ c.attack(s);
+ c.recoverAP();
+ c.recoverAP();
+ c.attack(s);
+ c.attack(s);
+ c.attack(s);
+ c.attack(s);
+ c.attack(s);
+
+ std::cout << "################ RECOVER AP" << std::endl;
+ std::cout << c;
+ c.recoverAP();
+ std::cout << c;
+ c.recoverAP();
+ std::cout << c;
+ c.recoverAP();
+ std::cout << c;
+ c.recoverAP();
+ std::cout << c;
+ c.recoverAP();
+ std::cout << c;
+ c.attack(r);
+ // std::cout << "################ DESTRUCTORS" << std::endl;
}
return 0;