Bläddra i källkod

added ex0 module03

bastien 1 månad sedan
förälder
incheckning
60397c1347

+ 50 - 0
Module_03/ex00/Makefile

@@ -0,0 +1,50 @@
+# **************************************************************************** #
+#                                                                              #
+#                                                         :::      ::::::::    #
+#    Makefile                                           :+:      :+:    :+:    #
+#                                                     +:+ +:+         +:+      #
+#    By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+         #
+#                                                 +#+#+#+#+#+   +#+            #
+#    Created: 2016/07/24 00:00:08 by bchanot           #+#    #+#              #
+#    Updated: 2025/12/18 13:36:33 by bchanot          ###   ########.fr        #
+#                                                                              #
+# **************************************************************************** #
+
+NAME = a.out
+MAKE_LIBS = make --no-print-directory
+SRCS_DIR = srcs/
+OBJS_DIR = .objects/
+CC = g++ -Wall -Wextra -Werror -g
+INC = -I./includes
+FILES = main ClapTrap
+SRCS = $(FILES)
+OBJS = $(addprefix $(OBJS_DIR), $(addsuffix .o, $(SRCS)))
+RED = \033[1;31m
+BLUE = \033[1;34m
+CYAN = \033[0;36m
+GREEN = \033[1;32m
+YELLOW = \033[1;33m
+EOC = \033[0m
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+	@echo -e "$(GREEN)Objects created.$(EOC)"
+	@$(CC) $(OBJS) $(INC) -o $(NAME)
+	@echo -e "$(GREEN)Compilation complete.$(EOC)"
+
+$(OBJS_DIR)%.o: $(SRCS_DIR)%.cpp
+	@mkdir -p $(dir $@)
+	$(CC) -c $< $(INC) -o $@
+
+clean:
+	@echo -e "$(RED)Deleting objects.$(EOC)"
+	@/bin/rm -rf $(OBJS_DIR)
+
+fclean: clean
+	@echo -e "$(RED)Deleting binary.$(EOC)"
+	@/bin/rm -rf $(NAME)
+
+re: fclean all
+
+.PHONY: all clean fclean re

+ 44 - 0
Module_03/ex00/includes/ClapTrap.hpp

@@ -0,0 +1,44 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ClapTrap.hpp                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/18 13:30:30 by bchanot           #+#    #+#             */
+/*   Updated: 2025/12/18 13:52:24 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef CLAPTRAP_HPP
+# define CLAPTRAP_HPP
+
+# include <iostream>
+# include <string>
+
+class ClapTrap
+{
+
+	public:
+
+		ClapTrap();
+		ClapTrap( std::string const & name );
+		ClapTrap( ClapTrap const & src );
+		~ClapTrap();
+
+		ClapTrap &		operator=( ClapTrap const & rhs );
+
+		void			attack(const std::string & target);
+		void			takeDamage(unsigned int amount);
+		void			beRepaired(unsigned int amount);
+
+	private:
+		std::string		const	_name;
+		u_int16_t				_hitPoints;
+		u_int16_t				_energyPoints;
+		u_int16_t				_attackDamage;
+};
+
+std::ostream &			operator<<( std::ostream & o, ClapTrap const & i );
+
+#endif /* ******************************************************** CLAPTRAP_H */

+ 116 - 0
Module_03/ex00/srcs/ClapTrap.cpp

@@ -0,0 +1,116 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ClapTrap.cpp                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/18 13:30:29 by bchanot           #+#    #+#             */
+/*   Updated: 2025/12/18 14:26:20 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ClapTrap.hpp"
+#include <limits>
+
+/*
+** ------------------------------- CONSTRUCTOR --------------------------------
+*/
+
+ClapTrap::ClapTrap() : _name("Default"), _hitPoints(10), _energyPoints(10), _attackDamage(0)
+{
+	std::cout << "ClapTrap " << this->_name << " has been created" << std::endl;
+}
+
+ClapTrap::ClapTrap(std::string const & name) : _name(name), _hitPoints(10), _energyPoints(10), _attackDamage(0)
+{
+	std::cout << "ClapTrap " << this->_name << " has been created" << std::endl;
+}
+
+ClapTrap::ClapTrap( const ClapTrap & src ) : _name(src._name), _hitPoints(src._hitPoints), _energyPoints(src._energyPoints), _attackDamage(src._attackDamage)
+{
+	std::cout << "Copy of ClapTrap " << this->_name << " has been created" << std::endl;
+}
+
+
+/*
+** -------------------------------- DESTRUCTOR --------------------------------
+*/
+
+ClapTrap::~ClapTrap()
+{
+	std::cout << "ClapTrap " << this->_name << " has been destroyed !" << std::endl;
+}
+
+
+/*
+** --------------------------------- OVERLOAD ---------------------------------
+*/
+
+ClapTrap &				ClapTrap::operator=( ClapTrap const & rhs )
+{
+	(void)rhs;
+	//if ( this != &rhs )
+	//{
+		//this->_value = rhs.getValue();
+	//}
+	return *this;
+}
+
+std::ostream &			operator<<( std::ostream & o, ClapTrap const & i )
+{
+	(void)i;
+	//o << "Value = " << i.getValue();
+	return o;
+}
+
+
+/*
+** --------------------------------- METHODS ----------------------------------
+*/
+
+void					ClapTrap::attack(const std::string& target) {
+	if (this->_hitPoints > 0) {
+		if (this->_energyPoints > 0) {
+			this->_energyPoints--;
+			std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage !" << std::endl;
+		} else
+			std::cout << "ClapTrap " << this->_name << " Doesn't have enough energy to attack !" << std::endl;
+	} else 
+		std::cout << "ClapTrap " << this->_name << " is dead.. How could he attack ?" << std::endl;
+}
+
+void					ClapTrap::takeDamage(unsigned int amount) {
+	if (this->_hitPoints > 0) {
+		if (this->_hitPoints < amount) {
+			this->_hitPoints = 0;
+			std::cout << this->_name << " is taking " << amount << " damage.. Causing his death ! he is now 0 HP..." << std::endl;
+		} else {
+			this->_hitPoints -= amount;
+			std::cout << this->_name << " is taking " << amount << " damage. He got now " << this->_hitPoints << " HP" << std::endl;
+		}
+	} else
+		std::cout << this->_name << " is 0 HP, already dead. Cannot attack a dead ClapTrap. Do we .. ?" << std::endl;
+}
+
+void					ClapTrap::beRepaired(unsigned int amount) {
+	if (this->_hitPoints > 0) {
+		if (this->_energyPoints > 0) {
+			if (this->_hitPoints + amount > std::numeric_limits<short unsigned int>::max()) 
+				this->_hitPoints = std::numeric_limits<short unsigned int>::max();
+			else
+				this->_hitPoints += amount;
+			this->_energyPoints--;
+			std::cout << this->_name << " is getting repaired about " << amount << " HP. Getting now " << this->_hitPoints << " HP." << std::endl;
+		} else
+			std::cout << "ClapTrap " << this->_name << " Doesn't have enought energy..." << std::endl;
+	} else
+		std::cout << this->_name << " is 0 HP, it mean DEAD. so, he cannot do anything more..." << std::endl;
+}
+
+/*
+** --------------------------------- ACCESSOR ---------------------------------
+*/
+
+
+/* ************************************************************************** */

+ 39 - 0
Module_03/ex00/srcs/main.cpp

@@ -0,0 +1,39 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.cpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/07/17 15:10:40 by bchanot           #+#    #+#             */
+/*   Updated: 2025/12/18 14:23:31 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ClapTrap.hpp"
+
+#include <iostream>
+#include <limits>
+
+int main( void ) {
+		ClapTrap	clap = ClapTrap("Clap");
+	ClapTrap	trap = ClapTrap("Trap");
+
+	clap.attack("Trap");
+	trap.beRepaired(2);
+	trap.takeDamage(12);
+
+	// Can't attack because he is dead
+	trap.attack("Clap");
+	clap.beRepaired(std::numeric_limits<short unsigned int>::max());
+	clap.takeDamage(100);
+	clap.takeDamage(std::numeric_limits<short unsigned int>::max());
+	clap = ClapTrap("Clap");
+	for (int i = 0; i < 100; i++)
+		clap.attack("Bidule");
+	trap = ClapTrap(clap);
+	trap.takeDamage(100);
+	clap = trap;
+	trap.takeDamage(100);
+	return (0);
+}