From 4971decf2dfbc9df1f20f48053dfbcfcf54321ff Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 10 Jan 2025 16:02:45 +0100 Subject: [PATCH] feat: finishing the exercice 01 of the module 03 --- .gitignore | 1 + cpp03/ex00/ClapTrap.cpp | 51 +++++++++++++++++ cpp03/ex00/ClapTrap.hpp | 34 +++++++++++ cpp03/ex00/Makefile | 124 ++++++++++++++++++++++++++++++++++++++++ cpp03/ex00/main.cpp | 21 +++++++ 5 files changed, 231 insertions(+) create mode 100644 cpp03/ex00/ClapTrap.cpp create mode 100644 cpp03/ex00/ClapTrap.hpp create mode 100644 cpp03/ex00/Makefile create mode 100644 cpp03/ex00/main.cpp diff --git a/.gitignore b/.gitignore index 6ae1c98..7b26563 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ sed_is_for_loser harl_2.0 harlFilter Fixed +open *.replace diff --git a/cpp03/ex00/ClapTrap.cpp b/cpp03/ex00/ClapTrap.cpp new file mode 100644 index 0000000..0a2cc56 --- /dev/null +++ b/cpp03/ex00/ClapTrap.cpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/10 13:59:49 by rparodi #+# #+# */ +/* Updated: 2025/01/10 16:00:29 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 << "Not enough energy to attack" << std::endl; + return ; + } + _energy_point--; + std::cout << "[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 << "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/ex00/ClapTrap.hpp b/cpp03/ex00/ClapTrap.hpp new file mode 100644 index 0000000..5cfa268 --- /dev/null +++ b/cpp03/ex00/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/ex00/Makefile b/cpp03/ex00/Makefile new file mode 100644 index 0000000..2f0c1ec --- /dev/null +++ b/cpp03/ex00/Makefile @@ -0,0 +1,124 @@ +# **************************************************************************** # + +# ::: :::::::: # +# 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 cpp +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 \ + 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/ex00/main.cpp b/cpp03/ex00/main.cpp new file mode 100644 index 0000000..07536c4 --- /dev/null +++ b/cpp03/ex00/main.cpp @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/10 15:50:07 by rparodi #+# #+# */ +/* Updated: 2025/01/10 15:57:10 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" + +int main(void) { + ClapTrap test("Norminet"); + test.attack("Moulinette"); + test.takeDamage(42); + test.beRepaired(42); + return (0); +}