「✨」 feat(Ex01): Finished
This commit is contained in:
102
ex01/ClapTrap.cpp
Normal file
102
ex01/ClapTrap.cpp
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ClapTrap.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/20 14:29:16 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/27 13:02:14 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ClapTrap.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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
ex01/ClapTrap.hpp
Normal file
51
ex01/ClapTrap.hpp
Normal 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/27 13:02:23 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
# include <string>
|
||||||
|
|
||||||
|
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);
|
56
ex01/Makefile
Normal file
56
ex01/Makefile
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# 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 #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
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
|
54
ex01/ScavTrap.cpp
Normal file
54
ex01/ScavTrap.cpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ScavTrap.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
29
ex01/ScavTrap.hpp
Normal file
29
ex01/ScavTrap.hpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ScavTrap.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
50
ex01/main.cpp
Normal file
50
ex01/main.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/20 14:25:07 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/11/27 13:01:55 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ScavTrap.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
Reference in New Issue
Block a user