1
0

」 feat(Ex02): added a very cool feature !

This commit is contained in:
2024-11-29 16:20:12 +01:00
parent 09f2fbf12d
commit a02ca29d87
12 changed files with 465 additions and 5 deletions

View File

@ -6,11 +6,11 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2024/11/27 12:08:05 by adjoly ### ########.fr #
# Updated: 2024/11/29 16:19:08 by adjoly ### ########.fr #
# #
# **************************************************************************** #
NAME = ClapTrap
NAME = ScavTrap
CC = c++

View File

@ -6,12 +6,13 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/27 12:53:46 by adjoly #+# #+# */
/* Updated: 2024/11/29 15:39:05 by adjoly ### ########.fr */
/* Updated: 2024/11/29 16:01:32 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScavTrap.hpp"
#include <iostream>
#include <sched.h>
void logScav(std::string emoji, std::string who, std::string str) {
std::cout << "" << emoji << "」ScavTrap(" << who << "): " << str << std::endl;
@ -64,5 +65,9 @@ void ScavTrap::attack(const std::string& target) {
return ;
}
logScav("💥", _name, "attacks " + target + " causing " + iToS(_attackDamage) + " points of damage!");
setEnergyPoints(getEnergyPoints() - 1);
_energyPoints--;
}
void ScavTrap::guardGate(void) {
logScav("🛡️", _name, "is now in Gate keeper mode");
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 14:25:07 by adjoly #+# #+# */
/* Updated: 2024/11/29 15:21:45 by adjoly ### ########.fr */
/* Updated: 2024/11/29 16:01:21 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,6 +28,7 @@ int main(void) {
logScav("🙀", kanel.getName(), "Oh my god Kanel uses his last teeth to attack " + suki.getName());
kanel.attack(suki.getName());
suki.takeDamage(kanel.getAttackDamage());
kanel.guardGate();
return (0);
}

111
ex02/ClapTrap.cpp Normal file
View File

@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 14:29:16 by adjoly #+# #+# */
/* Updated: 2024/11/29 15:21:39 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
#include <string>
#include <iostream>
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;
}
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;
}

51
ex02/ClapTrap.hpp Normal file
View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 14:08:19 by adjoly #+# #+# */
/* Updated: 2024/11/29 15:24:06 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
# include <string>
typedef unsigned int uint;
class ClapTrap {
protected:
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);
};
std::string iToS(unsigned int i);
void logClap(std::string, std::string);
void logClap(std::string, std::string, std::string);

BIN
ex02/FragTrap Executable file

Binary file not shown.

68
ex02/FragTrap.cpp Normal file
View File

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* FragTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/29 15:40:29 by adjoly #+# #+# */
/* Updated: 2024/11/29 16:15:12 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "FragTrap.hpp"
#include "ClapTrap.hpp"
#include <iostream>
void logFrag(std::string emoji, std::string who, std::string str) {
std::cout << "" << emoji << "」FragTrap(" << who << "): " << str << std::endl;
}
void logFrag(std::string emoji, std::string str) {
std::cout << "" << emoji << "」FragTrap: " << str << std::endl;
}
FragTrap::FragTrap(void) : ClapTrap() {
logFrag("", "default constructor called");
_health = 100;
_energyPoints = 100;
_attackDamage = 30;
}
FragTrap::~FragTrap(void) {
logFrag("", "destructor called");
}
FragTrap::FragTrap(std::string name) : ClapTrap(name) {
logFrag("", name, "constructor called");
_health = 100;
_energyPoints = 100;
_attackDamage = 30;
}
FragTrap &FragTrap::operator=(const FragTrap &cpy) {
logFrag("🟰", _name, "copy assignement constructor called");
if (this != &cpy) {
_name = cpy.getName();
_health = cpy.getHealth();
_energyPoints = cpy.getEnergyPoints();
_attackDamage = cpy.getAttackDamage();
}
return (*this);
}
void FragTrap::attack(const std::string& target) {
if (_health == 0) {
logFrag("💀", _name, "can't attack already dead");
return ;
} else if (_energyPoints == 0) {
logFrag("💤", _name, "can't attack no energy left GO TO SLEEP!");
return ;
}
logFrag("💥", _name, "attacks " + target + " causing " + iToS(_attackDamage) + " points of damage!");
_energyPoints--;
}
void FragTrap::highFivesGuys(void) {
logFrag("", _name, "send a high five request");
}

31
ex02/FragTrap.hpp Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* FragTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/29 15:40:32 by adjoly #+# #+# */
/* Updated: 2024/11/29 16:11:39 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "ClapTrap.hpp"
class FragTrap : public ClapTrap {
public :
FragTrap(void);
~FragTrap(void);
FragTrap(std::string);
FragTrap(const FragTrap &);
FragTrap &operator=(const FragTrap &);
void highFivesGuys(void);
void attack(const std::string &);
};
void logFrag(std::string, std::string);
void logFrag(std::string, std::string, std::string);

56
ex02/Makefile Normal file
View File

@ -0,0 +1,56 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2024/11/29 16:18:59 by adjoly ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
NAME = FragTrap
CC = c++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.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

73
ex02/ScavTrap.cpp Normal file
View File

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/27 12:53:46 by adjoly #+# #+# */
/* Updated: 2024/11/29 16:01:32 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScavTrap.hpp"
#include <iostream>
#include <sched.h>
void logScav(std::string emoji, std::string who, std::string str) {
std::cout << "" << emoji << "」ScavTrap(" << who << "): " << str << std::endl;
}
void logScav(std::string emoji, std::string str) {
std::cout << "" << emoji << "」ScavTrap: " << str << std::endl;
}
ScavTrap::ScavTrap(void) : ClapTrap() {
logScav("", "default constructor called");
_health = 100;
_energyPoints = 50;
_attackDamage = 20;
}
ScavTrap::~ScavTrap(void) {
logScav("", "destructor called");
}
ScavTrap::ScavTrap(std::string name) : ClapTrap(name) {
logScav("", name, "constructor called");
_health = 100;
_energyPoints = 50;
_attackDamage = 20;
}
ScavTrap::ScavTrap(const ScavTrap &other) {
*this = other;
logScav("", _name, "copy constructor called");
}
ScavTrap &ScavTrap::operator=(const ScavTrap &cpy) {
logScav("🟰", _name, "copy assignement constructor called");
if (this != &cpy) {
_name = cpy.getName();
_health = cpy.getHealth();
_energyPoints = cpy.getEnergyPoints();
_attackDamage = cpy.getAttackDamage();
}
return (*this);
}
void ScavTrap::attack(const std::string& target) {
if (_health == 0) {
logScav("💀", _name, "can't attack already dead");
return ;
} else if (_energyPoints == 0) {
logScav("💤", _name, "can't attack no energy left GO TO SLEEP!");
return ;
}
logScav("💥", _name, "attacks " + target + " causing " + iToS(_attackDamage) + " points of damage!");
_energyPoints--;
}
void ScavTrap::guardGate(void) {
logScav("🛡️", _name, "is now in Gate keeper mode");
}

30
ex02/ScavTrap.hpp Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/27 12:29:17 by adjoly #+# #+# */
/* Updated: 2024/11/29 16:14:16 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "ClapTrap.hpp"
class ScavTrap : public ClapTrap {
public:
ScavTrap(void);
~ScavTrap(void);
ScavTrap(std::string);
ScavTrap(const ScavTrap &);
ScavTrap &operator=(const ScavTrap &);
void guardGate(void);
void attack(const std::string &);
};
void logScav(std::string, std::string);
void logScav(std::string, std::string, std::string);

34
ex02/main.cpp Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 14:25:07 by adjoly #+# #+# */
/* Updated: 2024/11/29 16:19:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "FragTrap.hpp"
#include <sstream>
std::string iToS(unsigned int i) {
std::stringstream ss;
ss << i;
return (ss.str());
}
int main(void) {
FragTrap kanel("Kanel");
FragTrap suki("Suki");
kanel.setAttackDamage(51);
logFrag("🙀", kanel.getName(), "Oh my god Kanel uses his last teeth to attack " + suki.getName());
kanel.attack(suki.getName());
suki.takeDamage(kanel.getAttackDamage());
kanel.highFivesGuys();
return (0);
}