From 7cce8266c4bed11ad401f3d5b5ddb32656d33ede Mon Sep 17 00:00:00 2001 From: aortigos Date: Fri, 19 Sep 2025 12:37:04 +0200 Subject: [PATCH] ex01 done --- ex01/ClapTrap/ClapTrap.cpp | 97 ++++++++++++++++++++++++++++++++++++++ ex01/ClapTrap/ClapTrap.hpp | 40 ++++++++++++++++ ex01/Makefile | 25 ++++++++++ ex01/ScavTrap/ScavTrap.cpp | 76 +++++++++++++++++++++++++++++ ex01/ScavTrap/ScavTrap.hpp | 34 +++++++++++++ ex01/main.cpp | 39 +++++++++++++++ 6 files changed, 311 insertions(+) create mode 100644 ex01/ClapTrap/ClapTrap.cpp create mode 100644 ex01/ClapTrap/ClapTrap.hpp create mode 100644 ex01/Makefile create mode 100644 ex01/ScavTrap/ScavTrap.cpp create mode 100644 ex01/ScavTrap/ScavTrap.hpp create mode 100644 ex01/main.cpp diff --git a/ex01/ClapTrap/ClapTrap.cpp b/ex01/ClapTrap/ClapTrap.cpp new file mode 100644 index 0000000..4fba4a6 --- /dev/null +++ b/ex01/ClapTrap/ClapTrap.cpp @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos name = target.name; + this->health = target.health; + this->energy = target.energy; + this->attackDamage = target.attackDamage; +} + +ClapTrap& ClapTrap::operator=(const ClapTrap &target) +{ + if (this != &target) + { + this->name = target.name; + this->health = target.health; + this->energy = target.energy; + this->attackDamage = target.attackDamage; + } + return (*this); +} + +ClapTrap::~ClapTrap() +{ + std::cout << "Destructor called for " + << this->name << std::endl; +} + +void ClapTrap::attack(const std::string &target) +{ + if (this->health > 0 && this->energy > 0) + { + std::cout << "ClapTrap " << this->name + << " attacks " << target + << ", causing " << this->attackDamage + << " points of damage!" << std::endl; + + this->energy -= 1; + } else { + std::cout << "ClapTrap " << this->name + << " cant attack anymore..." << std::endl; + } +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + std::cout << "ClapTrap " << this->name + << " recieves " << amount + << " points of damage!" << std::endl; + if (amount >= this->health) + this->health = 0; + else + this->health -= amount; +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (this->energy > 0) + { + std::cout << "ClapTrap " << this->name + << " repairs itself, recovering " << amount + << " hit points!" << std::endl; + + this->health += amount; + this->energy -= 1; + } else { + std::cout << "ClapTrap " << this->name + << " cant be repaired..." << std::endl; + } +} diff --git a/ex01/ClapTrap/ClapTrap.hpp b/ex01/ClapTrap/ClapTrap.hpp new file mode 100644 index 0000000..b06be08 --- /dev/null +++ b/ex01/ClapTrap/ClapTrap.hpp @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos + +class ClapTrap +{ + protected: + std::string name; + unsigned int health; + unsigned int energy; + unsigned int attackDamage; + + public: + ClapTrap(); + ClapTrap(std::string name); + ClapTrap(const ClapTrap &target); + ClapTrap& operator=(const ClapTrap &target); + ~ClapTrap(); + + void attack(const std::string &target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); + +}; + +#endif diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..6575a07 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,25 @@ +NAME = scavtrap + +SRCS = main.cpp ClapTrap/ClapTrap.cpp ScavTrap/ScavTrap.cpp +OBJS=$(SRCS:.cpp=.o) + +CC=c++ +CFLAGS=-Wall -Wextra -Werror -std=c++98 + +all: $(NAME) + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) $(OBJS) -o $(NAME) + +%.o: %.cpp + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ex01/ScavTrap/ScavTrap.cpp b/ex01/ScavTrap/ScavTrap.cpp new file mode 100644 index 0000000..f934d1e --- /dev/null +++ b/ex01/ScavTrap/ScavTrap.cpp @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos health = 100; + this->energy = 50; + this->attackDamage = 20; + std::cout << "ScavTrap default constructor called " << this->name + << std::endl; +} + +ScavTrap::ScavTrap(std::string name) : ClapTrap(name) +{ + this->health = 100; + this->energy = 50; + this->attackDamage = 20; + std::cout << "ScavTrap constructor for " << this->name + << std::endl; +} + +ScavTrap::ScavTrap(const ScavTrap &other) : ClapTrap(other) +{ + *this = other; +} + +ScavTrap& ScavTrap::operator=(const ScavTrap &other) +{ + if (this != &other) + { + this->name = other.name; + this->health = other.health; + this->energy = other.energy; + this->attackDamage = other.attackDamage; + } + return (*this); +} + +ScavTrap::~ScavTrap() +{ + std::cout << "ScavTrap destructor called for " + << this->name << std::endl; +} + +void ScavTrap::attack(const std::string &target) +{ + if (this->health > 0 && this->energy > 0) + { + std::cout << "ScavTrap " << this->name + << " attacks " << target + << ", causing " << this->attackDamage + << " points of damage!" << std::endl; + + this->energy -= 1; + } else { + std::cout << "ScavTrap " << this->name + << " cant attack anymore..." << std::endl; + } +} + +void ScavTrap::guardGate() +{ + std::cout << "ScavTrap " << this->name << " is in Gate keeper mode" + << std::endl; +} diff --git a/ex01/ScavTrap/ScavTrap.hpp b/ex01/ScavTrap/ScavTrap.hpp new file mode 100644 index 0000000..6096965 --- /dev/null +++ b/ex01/ScavTrap/ScavTrap.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# include "../ClapTrap/ClapTrap.hpp" + +class ScavTrap : public ClapTrap +{ + public: + ScavTrap(); + ScavTrap(std::string name); + ScavTrap(const ScavTrap &other); + ScavTrap& operator=(const ScavTrap &other); + ~ScavTrap(); + + void attack(const std::string &target); + + void guardGate(); +}; + +#endif diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..87f74e4 --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos