From 11672811b23996b6bc42114e844b098094094e33 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 18 Oct 2024 12:28:09 +0200 Subject: [PATCH] Add guardGate, fix typo in var --- ex01/ScavTrap.cpp | 17 +++++++++++++++-- ex01/ScavTrap.h | 2 +- ex01/main.cpp | 5 ++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ex01/ScavTrap.cpp b/ex01/ScavTrap.cpp index 63fcf58..f867bbe 100644 --- a/ex01/ScavTrap.cpp +++ b/ex01/ScavTrap.cpp @@ -9,7 +9,7 @@ ScavTrap::ScavTrap(std::string name): ClapTrap(name) m_hp = 100; m_energy = 50; m_attack = 20; - m_is_guading = 0; + m_is_guarding = 0; } ScavTrap::ScavTrap(const ScavTrap &other) @@ -27,7 +27,7 @@ ScavTrap &ScavTrap::operator=(const ScavTrap &other) if (this == &other) return (*this); ClapTrap::operator=(other); - m_is_guading = other.m_is_guading; + m_is_guarding = other.m_is_guarding; return (*this); } @@ -46,3 +46,16 @@ void ScavTrap::attack(const std::string &target) --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/ex01/ScavTrap.h b/ex01/ScavTrap.h index 5bdd16c..4a538e5 100644 --- a/ex01/ScavTrap.h +++ b/ex01/ScavTrap.h @@ -7,7 +7,7 @@ class ScavTrap : public ClapTrap { private: - bool m_is_guading; + bool m_is_guarding; public: ScavTrap(std::string name = "DEFAULT"); diff --git a/ex01/main.cpp b/ex01/main.cpp index b4026d4..2f16fe4 100644 --- a/ex01/main.cpp +++ b/ex01/main.cpp @@ -16,13 +16,16 @@ int main() def.takeDamage(1); def.attack("foo"); def.beRepaired(10); - for (int i(0); i < 10; ++i) + for (int i(0); i < 50; ++i) a.attack("bar"); 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); return (0); -- 2.30.2