commit e66bfd90a0cf2f3136c9640e3d7100ecd3d3c425 Author: lfirmin Date: Sun Sep 6 18:54:24 2026 +0200 first commit diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..226ca24 --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,580 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..5d61f73 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + { + "associatedIndex": 5 +} + + + + { + "keyToString": { + "RunOnceActivity.RadMigrateCodeStyle": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "RunOnceActivity.cidr.known.project.marker": "true", + "RunOnceActivity.readMode.enableVisualFormatting": "true", + "cf.first.check.clang-format": "false", + "cidr.known.project.marker": "true", + "git-widget-placeholder": "master", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "vue.rearranger.settings.migration": "true" + } +} + + + + + 1754352062008 + + + + + + \ No newline at end of file diff --git a/ex00/ClapTrap.cpp b/ex00/ClapTrap.cpp new file mode 100644 index 0000000..f0bbcf3 --- /dev/null +++ b/ex00/ClapTrap.cpp @@ -0,0 +1,74 @@ +#include "ClapTrap.hpp" + +ClapTrap::ClapTrap(): _name("default"), _hit_pts(10), _energy_pts(10), _attack_dmg(0) +{ + std::cout << "ClapTrap Default Constructor called" << std::endl; +} + +ClapTrap::ClapTrap(const ClapTrap ©) +{ + std::cout << "ClapTrap Copy Constructor called" << std::endl; + *this = copy; +} + +ClapTrap::ClapTrap(std::string name): _name(name), _hit_pts(10), _energy_pts(10), _attack_dmg(0) +{ + std::cout << "ClapTrap Constructor called" <_name = src._name; + this->_hit_pts = src._hit_pts; + this->_energy_pts = src._energy_pts; + this->_attack_dmg = src._attack_dmg; + return *this; +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + if (this->_hit_pts > amount) + this->_hit_pts -= amount; + else if (this->_hit_pts > 0) + this->_hit_pts = 0; + else + { + std::cout << "ClapTrap " << this->_name << " is already dead" << std::endl; + return ; + } + std::cout << "ClapTrap " << this->_name << " was attacked and lost " << amount << " hp, he now has " << this->_hit_pts << " hp." << std::endl; +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (this->_energy_pts > 0 && this->_hit_pts > 0 && this->_hit_pts + amount <= 10) + { + this->_hit_pts += amount; + std::cout << "ClapTrap " << this->_name << " repaired itself and get " << amount << " hit points, he has " << this->_hit_pts << " hp" << std::endl; + this->_energy_pts--; + } + else if (this->_energy_pts == 0) + std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough energy" << std::endl; + else if (this->_hit_pts == 0) + std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough hit points." << std::endl; + else + std::cout << "ClapTrap " << this->_name << " can't be repaired to have more than 10 hit points." << std::endl; +} + +void ClapTrap::attack(const std::string &target) +{ + if (this->_energy_pts > 0 && this->_hit_pts > 0) + { + std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl; + this->_energy_pts--; + } + else if (this->_energy_pts == 0) + std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl; + else + std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl; +} \ No newline at end of file diff --git a/ex00/ClapTrap.hpp b/ex00/ClapTrap.hpp new file mode 100644 index 0000000..00a497b --- /dev/null +++ b/ex00/ClapTrap.hpp @@ -0,0 +1,26 @@ +#ifndef CLAPTRAP_HPP +# define CLAPTRAP_HPP + +#include +#include + +class ClapTrap { + private: + std::string _name; + unsigned int _hit_pts; + unsigned int _energy_pts; + unsigned int _attack_dmg; + + public: + ClapTrap(); + ~ClapTrap(); + ClapTrap(const ClapTrap ©); + ClapTrap(std::string name); + ClapTrap &operator=(const ClapTrap &src); + + void attack(const std::string& target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); +}; + +#endif \ No newline at end of file diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..81a8235 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,51 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: lfirmin +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2025/07/01 13:54:05 by lfirmin #+# #+# # +# Updated: 2025/07/01 16:43:28 by lfirmin ### ########.fr # +# # +# **************************************************************************** # + +C++ = c++ +C++_FLAGS = -Wall -Wextra -Werror -std=c++98 +OBJDIR = obj +INFILES = main.cpp\ + ClapTrap.cpp\ + +OBJFILES = $(addprefix $(OBJDIR)/, $(INFILES:.cpp=.o)) + +NAME = ClapTrap + +all: $(NAME) + +$(OBJDIR): + @echo "📁 Creating obj directory..." + @mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: %.cpp | $(OBJDIR) + @echo "🧠 Compiling $< ..." + @$(C++) $(C++_FLAGS) -c $< -o $@ + @echo "✅ $@ ready!" + +$(NAME): $(OBJFILES) + @echo "🔗 Linking $(NAME) ..." + @$(C++) $(C++_FLAGS) $(OBJFILES) -o $(NAME) + @echo "🎉 $(NAME) is ready !" + +clean: + @echo "🧹 Cleaning object files..." + @rm -rf $(OBJDIR) + @echo "✨ Objects cleaned!" + +fclean: clean + @echo "🗑️ Removing $(NAME)..." + @rm -f $(NAME) + @echo "💀 Full clean complete!" + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..4c5681c --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,31 @@ +#include "ClapTrap.hpp" + +int main() { + std::cout << "\033[1;36m### TESTING CLAPTRAP ###\033[0m\n" << std::endl; + + { + // Construction + std::cout << "\033[1;33m--- Construction ---\033[0m" << std::endl; + ClapTrap defaultBot; + ClapTrap namedBot("R2D2"); + ClapTrap copiedBot(namedBot); + ClapTrap energyTest("EnergyBot"); + ClapTrap deadBot("DeadBot"); + + // Tests basiques + std::cout << "\n\033[1;33m--- test ---\033[0m" << std::endl; + namedBot.attack("Enemy"); + namedBot.takeDamage(3); + namedBot.beRepaired(2); + namedBot.takeDamage(5); + std::cout << "\n\033[1;33m--- ep test ---\033[0m" << std::endl; + for (int i = 0; i < 11; i++) { + energyTest.attack("practice_target"); + } + std::cout << "\n\033[1;33m--- Death Test ---\033[0m" << std::endl; + deadBot.takeDamage(15); // >10 + deadBot.attack("ghost"); // fails + std::cout << "\n\033[1;33m--- Destruction ---\033[0m" << std::endl; + } + return 0; +} \ No newline at end of file diff --git a/ex01/ClapTrap.cpp b/ex01/ClapTrap.cpp new file mode 100644 index 0000000..f0bbcf3 --- /dev/null +++ b/ex01/ClapTrap.cpp @@ -0,0 +1,74 @@ +#include "ClapTrap.hpp" + +ClapTrap::ClapTrap(): _name("default"), _hit_pts(10), _energy_pts(10), _attack_dmg(0) +{ + std::cout << "ClapTrap Default Constructor called" << std::endl; +} + +ClapTrap::ClapTrap(const ClapTrap ©) +{ + std::cout << "ClapTrap Copy Constructor called" << std::endl; + *this = copy; +} + +ClapTrap::ClapTrap(std::string name): _name(name), _hit_pts(10), _energy_pts(10), _attack_dmg(0) +{ + std::cout << "ClapTrap Constructor called" <_name = src._name; + this->_hit_pts = src._hit_pts; + this->_energy_pts = src._energy_pts; + this->_attack_dmg = src._attack_dmg; + return *this; +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + if (this->_hit_pts > amount) + this->_hit_pts -= amount; + else if (this->_hit_pts > 0) + this->_hit_pts = 0; + else + { + std::cout << "ClapTrap " << this->_name << " is already dead" << std::endl; + return ; + } + std::cout << "ClapTrap " << this->_name << " was attacked and lost " << amount << " hp, he now has " << this->_hit_pts << " hp." << std::endl; +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (this->_energy_pts > 0 && this->_hit_pts > 0 && this->_hit_pts + amount <= 10) + { + this->_hit_pts += amount; + std::cout << "ClapTrap " << this->_name << " repaired itself and get " << amount << " hit points, he has " << this->_hit_pts << " hp" << std::endl; + this->_energy_pts--; + } + else if (this->_energy_pts == 0) + std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough energy" << std::endl; + else if (this->_hit_pts == 0) + std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough hit points." << std::endl; + else + std::cout << "ClapTrap " << this->_name << " can't be repaired to have more than 10 hit points." << std::endl; +} + +void ClapTrap::attack(const std::string &target) +{ + if (this->_energy_pts > 0 && this->_hit_pts > 0) + { + std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl; + this->_energy_pts--; + } + else if (this->_energy_pts == 0) + std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl; + else + std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl; +} \ No newline at end of file diff --git a/ex01/ClapTrap.hpp b/ex01/ClapTrap.hpp new file mode 100644 index 0000000..d557dfd --- /dev/null +++ b/ex01/ClapTrap.hpp @@ -0,0 +1,29 @@ +#ifndef CLAPTRAP_HPP +# define CLAPTRAP_HPP + +#include +#include + +class ClapTrap { + + protected: + std::string _name; + unsigned int _hit_pts; + unsigned int _energy_pts; + unsigned int _attack_dmg; + + public: + ClapTrap(); + ClapTrap(const ClapTrap ©); + ClapTrap(std::string name); + + virtual ~ClapTrap(); + + ClapTrap &operator=(const ClapTrap &src); + + void attack(const std::string &target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); +}; + +#endif \ No newline at end of file diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..40eae06 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,52 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: lfirmin +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2025/07/01 13:54:05 by lfirmin #+# #+# # +# Updated: 2025/07/01 16:43:28 by lfirmin ### ########.fr # +# # +# **************************************************************************** # + +C++ = c++ +C++_FLAGS = -Wall -Wextra -Werror -std=c++98 +OBJDIR = obj +INFILES = main.cpp\ + ClapTrap.cpp\ + ScavTrap.cpp\ + +OBJFILES = $(addprefix $(OBJDIR)/, $(INFILES:.cpp=.o)) + +NAME = ScavTrap + +all: $(NAME) + +$(OBJDIR): + @echo "📁 Creating obj directory..." + @mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: %.cpp | $(OBJDIR) + @echo "🧠 Compiling $< ..." + @$(C++) $(C++_FLAGS) -c $< -o $@ + @echo "✅ $@ ready!" + +$(NAME): $(OBJFILES) + @echo "🔗 Linking $(NAME) ..." + @$(C++) $(C++_FLAGS) $(OBJFILES) -o $(NAME) + @echo "🎉 $(NAME) is ready !" + +clean: + @echo "🧹 Cleaning object files..." + @rm -rf $(OBJDIR) + @echo "✨ Objects cleaned!" + +fclean: clean + @echo "🗑️ Removing $(NAME)..." + @rm -f $(NAME) + @echo "💀 Full clean complete!" + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/ex01/ScavTrap.cpp b/ex01/ScavTrap.cpp new file mode 100644 index 0000000..fe89611 --- /dev/null +++ b/ex01/ScavTrap.cpp @@ -0,0 +1,57 @@ +#include "ScavTrap.hpp" + +ScavTrap::ScavTrap(): ClapTrap() +{ + this->_hit_pts = 100; + this->_energy_pts = 50; + this->_attack_dmg = 20; + this->_guarding_gate = false; + std::cout << "FragTrap Default Constructor called" << std::endl; +} + +ScavTrap::ScavTrap(const ScavTrap ©): ClapTrap(copy) +{ + this->_guarding_gate = copy._guarding_gate; + std::cout << "FragTrap Copy Constructor called" << std::endl; +} + +ScavTrap::ScavTrap(std::string name): ClapTrap(name) +{ + this->_hit_pts = 100; + this->_energy_pts = 50; + this->_attack_dmg = 20; + this->_guarding_gate = false; + std::cout << "ScavTrap Constructor " << this->_name << " called" << std::endl; +} + +ScavTrap::~ScavTrap() +{ + std::cout << "ScavTrap Deconstructor " << this->_name << " called" << std::endl; +} + +ScavTrap &ScavTrap::operator=(const ScavTrap &src) +{ + std::cout << "ScavTrap Assignation operator called" << std::endl; + this->_name = src._name; + this->_hit_pts = src._hit_pts; + this->_energy_pts = src._energy_pts; + this->_attack_dmg = src._attack_dmg; + return *this; +} + +void ScavTrap::attack(const std::string &target) +{ + if (this->_energy_pts > 0 && this->_hit_pts > 0) + { + std::cout << "ScavTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl; + this->_energy_pts--; + } + else if (this->_energy_pts == 0) + std::cout << "ScavTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl; + else + std::cout << "ScavTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl; +} + +void ScavTrap::guardGate() { + std::cout << "ScavTrap " << this->_name << " is now in Gate keeper mode!" << std::endl; +} \ No newline at end of file diff --git a/ex01/ScavTrap.hpp b/ex01/ScavTrap.hpp new file mode 100644 index 0000000..5935438 --- /dev/null +++ b/ex01/ScavTrap.hpp @@ -0,0 +1,25 @@ +#ifndef SCAVTRAP_HPP +# define SCAVTRAP_HPP + +#include +#include + +#include "ClapTrap.hpp" + +class ScavTrap: public ClapTrap { + private: + bool _guarding_gate; + public: + ScavTrap(); + ScavTrap(const ScavTrap ©); + ScavTrap(std::string name); + + virtual ~ScavTrap(); + + ScavTrap &operator=(const ScavTrap &src); + + void attack(const std::string &target); + void guardGate(void); +}; + +#endif \ No newline at end of file diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..ded63f4 --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,61 @@ +#include "ScavTrap.hpp" + +int main() { + std::cout << "\033[1;36m### TESTING CLAPTRAP ###\033[0m\n" << std::endl; + + { + // Construction ClapTrap + std::cout << "\033[1;33m--- ClapTrap Construction ---\033[0m" << std::endl; + ClapTrap defaultBot; + ClapTrap namedBot("R2D2"); + ClapTrap copiedBot(namedBot); + + // Tests basiques ClapTrap + std::cout << "\n\033[1;33m--- ClapTrap Basic Tests ---\033[0m" << std::endl; + namedBot.attack("Enemy"); + namedBot.takeDamage(3); + namedBot.beRepaired(2); + + std::cout << "\n\033[1;33m--- ClapTrap Destruction ---\033[0m" << std::endl; + } + + std::cout << "\n\033[1;35m### TESTING SCAVTRAP ###\033[0m\n" << std::endl; + + { + // Construction ScavTrap + std::cout << "\033[1;33m--- ScavTrap Construction ---\033[0m" << std::endl; + ScavTrap defaultScav; + ScavTrap namedScav("R3D3"); + ScavTrap copiedScav(namedScav); + + // Tests basiques ScavTrap + std::cout << "\n\033[1;33m--- ScavTrap Basic Tests ---\033[0m" << std::endl; + namedScav.attack("Bandit"); + namedScav.takeDamage(20); + namedScav.beRepaired(15); + + // Test capacité spéciale + std::cout << "\n\033[1;33m--- ScavTrap Special Ability ---\033[0m" << std::endl; + namedScav.guardGate(); + defaultScav.guardGate(); + + // Test énergie (50 points pour ScavTrap) + std::cout << "\n\033[1;33m--- ScavTrap Energy Test ---\033[0m" << std::endl; + ScavTrap energyScav("EnergyBot"); + for (int i = 0; i < 52; i++) { + energyScav.attack("practice_target"); + } + + // Test mort + std::cout << "\n\033[1;33m--- ScavTrap Death Test ---\033[0m" << std::endl; + ScavTrap deadScav("DeadScav"); + deadScav.takeDamage(150); // >100 HP + deadScav.attack("ghost"); // fails + deadScav.guardGate(); // Should still work when dead + + std::cout << "\n\033[1;33m--- ScavTrap Destruction ---\033[0m" << std::endl; + } + + std::cout << "\n\033[1;32m### ALL TESTS COMPLETED ###\033[0m" << std::endl; + return 0; +} \ No newline at end of file diff --git a/ex02/ClapTrap.cpp b/ex02/ClapTrap.cpp new file mode 100644 index 0000000..f0bbcf3 --- /dev/null +++ b/ex02/ClapTrap.cpp @@ -0,0 +1,74 @@ +#include "ClapTrap.hpp" + +ClapTrap::ClapTrap(): _name("default"), _hit_pts(10), _energy_pts(10), _attack_dmg(0) +{ + std::cout << "ClapTrap Default Constructor called" << std::endl; +} + +ClapTrap::ClapTrap(const ClapTrap ©) +{ + std::cout << "ClapTrap Copy Constructor called" << std::endl; + *this = copy; +} + +ClapTrap::ClapTrap(std::string name): _name(name), _hit_pts(10), _energy_pts(10), _attack_dmg(0) +{ + std::cout << "ClapTrap Constructor called" <_name = src._name; + this->_hit_pts = src._hit_pts; + this->_energy_pts = src._energy_pts; + this->_attack_dmg = src._attack_dmg; + return *this; +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + if (this->_hit_pts > amount) + this->_hit_pts -= amount; + else if (this->_hit_pts > 0) + this->_hit_pts = 0; + else + { + std::cout << "ClapTrap " << this->_name << " is already dead" << std::endl; + return ; + } + std::cout << "ClapTrap " << this->_name << " was attacked and lost " << amount << " hp, he now has " << this->_hit_pts << " hp." << std::endl; +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (this->_energy_pts > 0 && this->_hit_pts > 0 && this->_hit_pts + amount <= 10) + { + this->_hit_pts += amount; + std::cout << "ClapTrap " << this->_name << " repaired itself and get " << amount << " hit points, he has " << this->_hit_pts << " hp" << std::endl; + this->_energy_pts--; + } + else if (this->_energy_pts == 0) + std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough energy" << std::endl; + else if (this->_hit_pts == 0) + std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough hit points." << std::endl; + else + std::cout << "ClapTrap " << this->_name << " can't be repaired to have more than 10 hit points." << std::endl; +} + +void ClapTrap::attack(const std::string &target) +{ + if (this->_energy_pts > 0 && this->_hit_pts > 0) + { + std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl; + this->_energy_pts--; + } + else if (this->_energy_pts == 0) + std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl; + else + std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl; +} \ No newline at end of file diff --git a/ex02/ClapTrap.hpp b/ex02/ClapTrap.hpp new file mode 100644 index 0000000..d557dfd --- /dev/null +++ b/ex02/ClapTrap.hpp @@ -0,0 +1,29 @@ +#ifndef CLAPTRAP_HPP +# define CLAPTRAP_HPP + +#include +#include + +class ClapTrap { + + protected: + std::string _name; + unsigned int _hit_pts; + unsigned int _energy_pts; + unsigned int _attack_dmg; + + public: + ClapTrap(); + ClapTrap(const ClapTrap ©); + ClapTrap(std::string name); + + virtual ~ClapTrap(); + + ClapTrap &operator=(const ClapTrap &src); + + void attack(const std::string &target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); +}; + +#endif \ No newline at end of file diff --git a/ex02/FragTrap.cpp b/ex02/FragTrap.cpp new file mode 100644 index 0000000..6105c90 --- /dev/null +++ b/ex02/FragTrap.cpp @@ -0,0 +1,54 @@ +#include "FragTrap.hpp" + +FragTrap::FragTrap(): ClapTrap() +{ + this->_hit_pts = 100; + this->_energy_pts = 100; + this->_attack_dmg = 30; + std::cout << "FragTrap Default Constructor called" << std::endl; +} + +FragTrap::FragTrap(const FragTrap ©): ClapTrap(copy) +{ + std::cout << "FragTrap Copy Constructor called" << std::endl; +} + +FragTrap::FragTrap(std::string name): ClapTrap(name) +{ + this->_hit_pts = 100; + this->_energy_pts = 100; + this->_attack_dmg = 30; + std::cout << "FragTrap Constructor " << this->_name << " called" << std::endl; +} + +FragTrap::~FragTrap() +{ + std::cout << "FragTrap Deconstructor " << this->_name << " called" << std::endl; +} + +FragTrap &FragTrap::operator=(const FragTrap &src) +{ + std::cout << "FragTrap Assignation operator called" << std::endl; + this->_name = src._name; + this->_hit_pts = src._hit_pts; + this->_energy_pts = src._energy_pts; + this->_attack_dmg = src._attack_dmg; + return *this; +} + +void FragTrap::attack(const std::string &target) +{ + if (this->_energy_pts > 0 && this->_hit_pts > 0) + { + std::cout << "FragTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl; + this->_energy_pts--; + } + else if (this->_energy_pts == 0) + std::cout << "FragTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl; + else + std::cout << "FragTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl; +} + +void FragTrap::highFivesGuys() { + std::cout << "FragTrap " << this->_name << " requests a positive high-five! ✋" << std::endl; +} \ No newline at end of file diff --git a/ex02/FragTrap.hpp b/ex02/FragTrap.hpp new file mode 100644 index 0000000..7d148a1 --- /dev/null +++ b/ex02/FragTrap.hpp @@ -0,0 +1,23 @@ +#ifndef FRAGTRAP_HPP +# define FRAGTRAP_HPP + +#include +#include + +#include "ClapTrap.hpp" + +class FragTrap: public ClapTrap { + public: + FragTrap(); + FragTrap(const FragTrap ©); + FragTrap(std::string name); + + virtual ~FragTrap(); + + FragTrap &operator=(const FragTrap &src); + + void attack(const std::string &target); + void highFivesGuys(void); +}; + +#endif \ No newline at end of file diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..2f6beba --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,53 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: lfirmin +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2025/07/01 13:54:05 by lfirmin #+# #+# # +# Updated: 2025/07/01 16:43:28 by lfirmin ### ########.fr # +# # +# **************************************************************************** # + +C++ = c++ +C++_FLAGS = -Wall -Wextra -Werror -std=c++98 +OBJDIR = obj +INFILES = main.cpp\ + ClapTrap.cpp\ + ScavTrap.cpp\ + FragTrap.cpp\ + +OBJFILES = $(addprefix $(OBJDIR)/, $(INFILES:.cpp=.o)) + +NAME = FragTrap + +all: $(NAME) + +$(OBJDIR): + @echo "📁 Creating obj directory..." + @mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: %.cpp | $(OBJDIR) + @echo "🧠 Compiling $< ..." + @$(C++) $(C++_FLAGS) -c $< -o $@ + @echo "✅ $@ ready!" + +$(NAME): $(OBJFILES) + @echo "🔗 Linking $(NAME) ..." + @$(C++) $(C++_FLAGS) $(OBJFILES) -o $(NAME) + @echo "🎉 $(NAME) is ready !" + +clean: + @echo "🧹 Cleaning object files..." + @rm -rf $(OBJDIR) + @echo "✨ Objects cleaned!" + +fclean: clean + @echo "🗑️ Removing $(NAME)..." + @rm -f $(NAME) + @echo "💀 Full clean complete!" + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/ex02/ScavTrap.cpp b/ex02/ScavTrap.cpp new file mode 100644 index 0000000..0a53a75 --- /dev/null +++ b/ex02/ScavTrap.cpp @@ -0,0 +1,57 @@ +#include "ScavTrap.hpp" + +ScavTrap::ScavTrap(): ClapTrap() +{ + this->_hit_pts = 100; + this->_energy_pts = 50; + this->_attack_dmg = 20; + this->_guarding_gate = false; + std::cout << "ScavTrap Default Constructor called" << std::endl; +} + +ScavTrap::ScavTrap(const ScavTrap ©): ClapTrap(copy) +{ + this->_guarding_gate = copy._guarding_gate; + std::cout << "ScavTrap Copy Constructor called" << std::endl; +} + +ScavTrap::ScavTrap(std::string name): ClapTrap(name) +{ + this->_hit_pts = 100; + this->_energy_pts = 50; + this->_attack_dmg = 20; + this->_guarding_gate = false; + std::cout << "ScavTrap Constructor " << this->_name << " called" << std::endl; +} + +ScavTrap::~ScavTrap() +{ + std::cout << "ScavTrap Deconstructor " << this->_name << " called" << std::endl; +} + +ScavTrap &ScavTrap::operator=(const ScavTrap &src) +{ + std::cout << "ScavTrap Assignation operator called" << std::endl; + this->_name = src._name; + this->_hit_pts = src._hit_pts; + this->_energy_pts = src._energy_pts; + this->_attack_dmg = src._attack_dmg; + return *this; +} + +void ScavTrap::attack(const std::string &target) +{ + if (this->_energy_pts > 0 && this->_hit_pts > 0) + { + std::cout << "ScavTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl; + this->_energy_pts--; + } + else if (this->_energy_pts == 0) + std::cout << "ScavTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl; + else + std::cout << "ScavTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl; +} + +void ScavTrap::guardGate() { + std::cout << "ScavTrap " << this->_name << " is now in Gate keeper mode!" << std::endl; +} \ No newline at end of file diff --git a/ex02/ScavTrap.hpp b/ex02/ScavTrap.hpp new file mode 100644 index 0000000..5935438 --- /dev/null +++ b/ex02/ScavTrap.hpp @@ -0,0 +1,25 @@ +#ifndef SCAVTRAP_HPP +# define SCAVTRAP_HPP + +#include +#include + +#include "ClapTrap.hpp" + +class ScavTrap: public ClapTrap { + private: + bool _guarding_gate; + public: + ScavTrap(); + ScavTrap(const ScavTrap ©); + ScavTrap(std::string name); + + virtual ~ScavTrap(); + + ScavTrap &operator=(const ScavTrap &src); + + void attack(const std::string &target); + void guardGate(void); +}; + +#endif \ No newline at end of file diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..ded63f4 --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,61 @@ +#include "ScavTrap.hpp" + +int main() { + std::cout << "\033[1;36m### TESTING CLAPTRAP ###\033[0m\n" << std::endl; + + { + // Construction ClapTrap + std::cout << "\033[1;33m--- ClapTrap Construction ---\033[0m" << std::endl; + ClapTrap defaultBot; + ClapTrap namedBot("R2D2"); + ClapTrap copiedBot(namedBot); + + // Tests basiques ClapTrap + std::cout << "\n\033[1;33m--- ClapTrap Basic Tests ---\033[0m" << std::endl; + namedBot.attack("Enemy"); + namedBot.takeDamage(3); + namedBot.beRepaired(2); + + std::cout << "\n\033[1;33m--- ClapTrap Destruction ---\033[0m" << std::endl; + } + + std::cout << "\n\033[1;35m### TESTING SCAVTRAP ###\033[0m\n" << std::endl; + + { + // Construction ScavTrap + std::cout << "\033[1;33m--- ScavTrap Construction ---\033[0m" << std::endl; + ScavTrap defaultScav; + ScavTrap namedScav("R3D3"); + ScavTrap copiedScav(namedScav); + + // Tests basiques ScavTrap + std::cout << "\n\033[1;33m--- ScavTrap Basic Tests ---\033[0m" << std::endl; + namedScav.attack("Bandit"); + namedScav.takeDamage(20); + namedScav.beRepaired(15); + + // Test capacité spéciale + std::cout << "\n\033[1;33m--- ScavTrap Special Ability ---\033[0m" << std::endl; + namedScav.guardGate(); + defaultScav.guardGate(); + + // Test énergie (50 points pour ScavTrap) + std::cout << "\n\033[1;33m--- ScavTrap Energy Test ---\033[0m" << std::endl; + ScavTrap energyScav("EnergyBot"); + for (int i = 0; i < 52; i++) { + energyScav.attack("practice_target"); + } + + // Test mort + std::cout << "\n\033[1;33m--- ScavTrap Death Test ---\033[0m" << std::endl; + ScavTrap deadScav("DeadScav"); + deadScav.takeDamage(150); // >100 HP + deadScav.attack("ghost"); // fails + deadScav.guardGate(); // Should still work when dead + + std::cout << "\n\033[1;33m--- ScavTrap Destruction ---\033[0m" << std::endl; + } + + std::cout << "\n\033[1;32m### ALL TESTS COMPLETED ###\033[0m" << std::endl; + return 0; +} \ No newline at end of file