diff --git a/ex01/ClapTrap.cpp b/ex01/ClapTrap.cpp new file mode 100644 index 0000000..1814543 --- /dev/null +++ b/ex01/ClapTrap.cpp @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/20 14:29:16 by adjoly #+# #+# */ +/* Updated: 2024/11/27 13:02:14 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" +#include + +ClapTrap::ClapTrap(void) : + _name("Kanel"), _health(10), _energyPoints(10), _attackDamage(0) { + logClap("βž•", "default constructor called"); +} + +ClapTrap::~ClapTrap(void) { + logClap("βž–", "destructor called"); + +} + +ClapTrap::ClapTrap(std::string name) : + _name(name), _health(10), _energyPoints(10), _attackDamage(0) { + logClap("βž•", _name, "constructor called"); +} + +ClapTrap::ClapTrap(const ClapTrap &other) { + *this = other; + logClap("βž•", _name, "copy constructor called"); +} + +ClapTrap &ClapTrap::operator=(const ClapTrap &cpy) { + logClap("🟰", _name, "copy assignement constructor called"); + if (this != &cpy) { + _name = cpy.getName(); + _health = cpy.getHealth(); + _energyPoints = cpy.getEnergyPoints(); + _attackDamage= cpy.getAttackDamage(); + } + return (*this); +} + +void ClapTrap::attack(const std::string& target) { + if (_health == 0) { + logClap("πŸ’€", _name, "can't attack already dead"); + return ; + } else if (_energyPoints == 0) { + logClap("πŸ’€", _name, "can't attack no energy left GO TO SLEEP!"); + return ; + } + logClap("πŸ’₯", _name, "attacks " + target + " causing " + iToS(getAttackDamage()) + " points of damage!"); + _energyPoints--; +} + +void ClapTrap::takeDamage(unsigned int amount) { + logClap("πŸ₯Š", _name, "take " + iToS(amount) + " damage"); + if (_health >= amount) { + logClap("πŸ’€", _name, "is dead HAHA"); + _health = 0; + } else { + _health -= amount; + } +} + +void ClapTrap::beRepaired(uint amount) { + if (_health == 0) { + logClap("πŸ’€", _name, "can't repair already dead"); + return ; + } else if (_energyPoints == 0) { + logClap("πŸ’€", _name, "can't repair no energy left GO TO SLEEP!"); + return ; + } + logClap("🩹", _name, iToS(amount) + ""); + _health += amount; + _energyPoints--; +} + +uint ClapTrap::getAttackDamage(void) const { return (_attackDamage); } +uint ClapTrap::getEnergyPoints(void) const { return (_energyPoints); } +uint ClapTrap::getHealth(void) const { return (_health); } +std::string ClapTrap::getName(void) const { return (_name); } + +void ClapTrap::setAttackDamage(uint in) { + logClap("🟰", _name, "set attack damage"); + _attackDamage = in; +} +void ClapTrap::setEnergyPoints(uint in) { + logClap("🟰", _name, "set energy points"); + _energyPoints = in; +} +void ClapTrap::setHealth(uint in) { + logClap("🟰", _name, "set health"); + _health = in; +} +void ClapTrap::setName(std::string in) { + logClap("🟰", _name, "set a new name"); + _name = in; +} diff --git a/ex01/ClapTrap.hpp b/ex01/ClapTrap.hpp new file mode 100644 index 0000000..767207c --- /dev/null +++ b/ex01/ClapTrap.hpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/20 14:08:19 by adjoly #+# #+# */ +/* Updated: 2024/11/27 13:02:23 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +# include + +typedef unsigned int uint; + +class ClapTrap { + private: + std::string _name; + uint _health; + uint _energyPoints; + uint _attackDamage; + + public: + ClapTrap(void); + ClapTrap(std::string); + ClapTrap(const ClapTrap &); + ~ClapTrap(void); + + ClapTrap &operator=(const ClapTrap &); + + void attack(const std::string& target); + void takeDamage(uint amount); + void beRepaired(uint amount); + + uint getAttackDamage(void) const; + uint getEnergyPoints(void) const; + uint getHealth(void) const; + std::string getName(void) const; + + void setAttackDamage(uint); + void setEnergyPoints(uint); + void setHealth(uint); + void setName(std::string); +}; + +void logClap(std::string, std::string, std::string); +void logClap(std::string, std::string); +std::string iToS(unsigned int i); diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..98bd7ba --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,56 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # +# Updated: 2024/11/27 12:08:05 by adjoly ### ########.fr # +# # +# **************************************************************************** # + +NAME = ClapTrap + +CC = c++ + +OBJSDIR = obj/ + +SRCS = main.cpp \ + ClapTrap.cpp \ + ScavTrap.cpp + +OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) + +FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP + +RED = \033[0;31m +GREEN = \033[0;32m +YELLOW = \033[1;33m +PURPLE = \e[0;35m +NC = \033[0m +DELETE = \x1B[2K\r + +all: $(NAME) + +$(NAME): $(OBJS) + @$(CC) $(FLAGS) $(OBJS) -o $(NAME) + @printf "$(YELLOW)γ€Œβœ¨γ€($(NAME)) Program compiled\n" + +$(OBJSDIR)%.o: %.cpp + @mkdir -p $(@D) + @$(CC) $(FLAGS) -c $< -o $@ + @printf "$(DELETE)$(GREEN)γ€ŒπŸ”¨γ€($<) Object compiled\n" + +clean: + @rm -f $(OBJS) + @printf "$(DELETE)$(RED)γ€ŒπŸ—‘οΈγ€($(OBJS)) Object deleted\n" + +fclean: clean + @rm -f $(NAME) + @rm -Rf $(OBJSDIR) + @printf "$(RED)γ€ŒπŸ—‘οΈγ€($(NAME)) Program deleted\n" + +re: fclean all + +.PHONY: clean fclean all re diff --git a/ex01/ScavTrap.cpp b/ex01/ScavTrap.cpp new file mode 100644 index 0000000..e021d33 --- /dev/null +++ b/ex01/ScavTrap.cpp @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/27 12:53:46 by adjoly #+# #+# */ +/* Updated: 2024/11/27 12:59:02 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScavTrap.hpp" +#include "ClapTrap.hpp" + +ScavTrap::ScavTrap(void) : ClapTrap() { + log("βž•", "default constructor called"); +} + +ScavTrap::~ScavTrap(void) { + log("βž–", "destructor called"); +} + +ScavTrap::ScavTrap(std::string name) : ClapTrap(name) { + log("βž•", getName(), "constructor called"); +} + +ScavTrap::ScavTrap(const ScavTrap &other) { + *this = other; + log("βž•", getName(), "copy constructor called"); +} + +ScavTrap &ScavTrap::operator=(const ScavTrap &cpy) { + log("🟰", getName(), "copy assignement constructor called"); + if (this != &cpy) { + setName(cpy.getName()); + setHealth(cpy.getHealth()); + setEnergyPoints(cpy.getEnergyPoints()); + setAttackDamage(cpy.getAttackDamage()); + } + return (*this); +} + +void ScavTrap::attack(const std::string& target) { + if (getHealth() == 0) { + log("πŸ’€", getName(), "can't attack already dead"); + return ; + } else if (getEnergyPoints() == 0) { + log("πŸ’€", getName(), "can't attack no energy left GO TO SLEEP!"); + return ; + } + log("πŸ’₯", getName(), "attacks " + target + " causing " + iToS(getAttackDamage()) + " points of damage!"); + setEnergyPoints(getEnergyPoints() - 1); +} diff --git a/ex01/ScavTrap.hpp b/ex01/ScavTrap.hpp new file mode 100644 index 0000000..cd1b5de --- /dev/null +++ b/ex01/ScavTrap.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/27 12:29:17 by adjoly #+# #+# */ +/* Updated: 2024/11/27 13:00:47 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" + +class ScavTrap : public ClapTrap { + public: + ScavTrap(void); + ScavTrap(std::string); + ScavTrap(const ScavTrap &); + ~ScavTrap(void); + ScavTrap &operator=(const ScavTrap &); + + void guardGate(void); + void attack(const std::string &); +}; + +void log(std::string, std::string, std::string); +void log(std::string, std::string); +std::string iToS(unsigned int i); diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..7e5a854 --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/20 14:25:07 by adjoly #+# #+# */ +/* Updated: 2024/11/27 13:01:55 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScavTrap.hpp" +#include +#include + +void log(std::string emoji, std::string who, std::string str) { + std::cout << "γ€Œ" << emoji << "」ScavTrap(" << who << "): " << str << std::endl; +} + +void log(std::string emoji, std::string str) { + std::cout << "γ€Œ" << emoji << "」ScavTrap: " << str << std::endl; +} + +void logClap(std::string emoji, std::string who, std::string str) { + std::cout << "γ€Œ" << emoji << "」ClapTrap(" << who << "): " << str << std::endl; +} + +void logClap(std::string emoji, std::string str) { + std::cout << "γ€Œ" << emoji << "」ClapTrap: " << str << std::endl; +} + +std::string iToS(unsigned int i) { + std::stringstream ss; + + ss << i; + return (ss.str()); +} + +int main(void) { + ScavTrap kanel("Kanel"); + ScavTrap suki("Suki"); + + kanel.setAttackDamage(5); + log("πŸ™€", kanel.getName(), "Oh my god Kanel uses his last teeth to attack " + suki.getName()); + kanel.attack(suki.getName()); + suki.takeDamage(kanel.getAttackDamage()); + + return (0); +}