diff --git a/cpp03/ex01/ClapTrap.cpp b/cpp03/ex01/ClapTrap.cpp new file mode 100644 index 0000000..dea7cfe --- /dev/null +++ b/cpp03/ex01/ClapTrap.cpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/10 13:59:49 by rparodi #+# #+# */ +/* Updated: 2025/01/14 21:59:42 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" +#include +#include + +ClapTrap::ClapTrap(std::string name) { + _name = name; + _hit_point = 10; + _energy_point = 10; + _attack_damage = 0; + std::cout << "\n[Init] ClapTrap:\n\t" << "Name: " << _name << std::endl; +} + +ClapTrap::~ClapTrap() { + std::cout << "\n[Delete] ClapTrap:\n\t" << "Name: " << _name << std::endl; +} + +void ClapTrap::attack(const std::string& target) { + if (_energy_point <= 0 || _hit_point <= 0) { + std::cerr << "\n[Attack] ClapTrap:\n\t" << "Not enough energy to attack" << std::endl; + return ; + } + _energy_point--; + std::cout << "\n[Attack] ClapTrap:\n\t" << _name << " attacks " << target << ", causing " << _attack_damage << " points of damage!" << std::endl; +} + +void ClapTrap::takeDamage(unsigned int amount) { + _hit_point -= amount; + std::cout << "\n[Hurt] ClapTrap:\n\t" << _name << " take " << amount << " now hit_point are " << _hit_point << std::endl; +} + +void ClapTrap::beRepaired(unsigned int amount) { + if (_energy_point <= 0 || _hit_point <= 0) { + std::cerr << "\n[Repair] ClapTrap:\n\t" << "Not enough energy to repair" << std::endl; + return ; + } + _energy_point--; + _hit_point += amount; + std::cout << "\n[Repair] ClapTrap:\n\t" << _name << " repair, for an amount " << amount << " now hit_point are " << _hit_point << std::endl; +} diff --git a/cpp03/ex01/ClapTrap.hpp b/cpp03/ex01/ClapTrap.hpp new file mode 100644 index 0000000..5cfa268 --- /dev/null +++ b/cpp03/ex01/ClapTrap.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/10 14:54:21 by rparodi #+# #+# */ +/* Updated: 2025/01/10 15:52:10 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLAPTRAP_HPP +#define CLAPTRAP_HPP + +#include + +class ClapTrap { + public: + ClapTrap(std::string name); + ~ClapTrap(); + void attack(const std::string& target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); + private: + ClapTrap(); + std::string _name; + int _hit_point; + int _energy_point; + int _attack_damage; + +}; + +#endif diff --git a/cpp03/ex01/Makefile b/cpp03/ex01/Makefile new file mode 100644 index 0000000..d7f5be4 --- /dev/null +++ b/cpp03/ex01/Makefile @@ -0,0 +1,123 @@ +# **************************************************************************** # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2024/12/21 19:50:58 by rparodi ### ########.fr # +# # +# **************************************************************************** # + +# Variables + +# Name +NAME = open + +# Commands +CXX = c++ +RM = rm -rf + +# Flags +# Mandatory flags for 42 CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/ +# Flags to debug and have the dependences (can be removed for correction) +CXXFLAGS += -MMD -g3 +# Flag to debug (TO REMOVE) +# CXXFLAGS += -D DEBUG=1 +# Sources +SRC = ClapTrap.cpp \ + ScavTrap.cpp \ + main.cpp + +# Objects +OBJDIRNAME = ./build +OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o)) + +# Colors +GREEN = \033[32m +GREY = \033[0;90m +RED = \033[0;31m +GOLD = \033[38;5;220m +END = \033[0m + +# Rules + +# All (make all) +all: header $(NAME) footer + +# Bonus (make bonus) +bonus: header $(OBJ) footer + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Clean (make clean) +clean: + @printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n' + @printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n' + @$(RM) $(OBJDIRNAME) + +# Clean (make fclean) +fclean: clean + @printf '$(GREY) Removing $(END)$(RED)Program$(END)\n' + @$(RM) $(NAME) + @echo "" + +# Restart (make re) +re: header fclean all + +# Dependences for all +$(NAME): $(OBJ) + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Creating the objects +$(OBJDIRNAME)/%.o: %.cpp + @mkdir -p $(dir $@) + @printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n' + @$(CXX) $(CXXFLAGS) -o $@ -c $< + +# Header +header: + @clear + @printf '\n\n' + @printf '$(GOLD) ******* ****** ******* $(END)\n' + @printf '$(GOLD) ****** *** ******* $(END)\n' + @printf '$(GOLD) ******* * ******* $(END)\n' + @printf '$(GOLD) ****** ******* $(END)\n' + @printf '$(GOLD) ******* ******* $(END)\n' + @printf '$(GOLD) ******************* ******* * $(END)\n' + @printf '$(GOLD) ******************* ******* *** $(END)\n' + @printf '$(GOLD) ****** ******* ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GREY) Made by rparodi$(END)\n\n' + +# Footer +footer: + @printf "\n" + @printf "$(GOLD) ,_ _,$(END)\n" + @printf "$(GOLD) | \\___//|$(END)\n" + @printf "$(GOLD) |=6 6=|$(END)\n" + @printf "$(GOLD) \\=._Y_.=/$(END)\n" + @printf "$(GOLD) ) \` ( ,$(END)\n" + @printf "$(GOLD) / \\ (('$(END)\n" + @printf "$(GOLD) | | ))$(END)\n" + @printf "$(GOLD) /| | | |\\_//$(END)\n" + @printf "$(GOLD) \\| |._.| |/-\`$(END)\n" + @printf "$(GOLD) '\"' '\"'$(END)\n" + @printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n' + +clangd: + @echo \ + "CompileFlags:\n" \ + " Add:\n" \ + " - \"-std=c++98 -Wall -Wextra -Werror\"\n" \ + " - \"-I"$(shell pwd)"/includes\"\n" \ + " - \"-xc++\"\n" \ + > .clangd + +# Phony +.PHONY: all bonus clean fclean re clangd +-include ${OBJ:.o=.d} diff --git a/cpp03/ex01/ScavTrap.cpp b/cpp03/ex01/ScavTrap.cpp new file mode 100644 index 0000000..e69de29 diff --git a/cpp03/ex01/ScavTrap.hpp b/cpp03/ex01/ScavTrap.hpp new file mode 100644 index 0000000..e69de29 diff --git a/cpp03/ex01/main.cpp b/cpp03/ex01/main.cpp new file mode 100644 index 0000000..c61ddf4 --- /dev/null +++ b/cpp03/ex01/main.cpp @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/10 15:50:07 by rparodi #+# #+# */ +/* Updated: 2025/01/14 22:04:48 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" +#include + +int main(void) { + std::cout << "[Broadcast]\n\tIn the Red corner the first cat of the 42 school !" << std::endl; + ClapTrap test("Norminet"); + test.attack("Moulinette"); + test.takeDamage(42); + test.beRepaired(42); + std::cout << "\n\n[Broadcast]\n\tIn the Blue corner the actual cat of the 42 school !" << std::endl; + ClapTrap test2("Moulinette"); + test2.beRepaired(32); + return (0); +}