「✨」 feat(Ex00): Finished ClapTrap
This commit is contained in:
101
ex00/ClapTrap.cpp
Normal file
101
ex00/ClapTrap.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ClapTrap.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/20 14:29:16 by adjoly #+# #+# */
|
||||
/* Updated: 2024/11/25 16:22:54 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ClapTrap.hpp"
|
||||
#include <iostream>
|
||||
|
||||
ClapTrap::ClapTrap(void) :
|
||||
_name("Kanel"), _health(10), _energyPoints(10), _attackDamage(0) {
|
||||
std::cout << "「➕」ClapTrap: default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::~ClapTrap(void) {
|
||||
std::cout << "「➖」ClapTrap(" << _name << "): destructor called" << std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(std::string name) :
|
||||
_name(name), _health(10), _energyPoints(10), _attackDamage(0) {
|
||||
std::cout << "「➕」ClapTrap(" << _name << "): constructor called" << std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(const ClapTrap &other) {
|
||||
*this = other;
|
||||
std::cout << "「➕」ClapTrap(" << _name << "): copy constructor called" << std::endl;
|
||||
}
|
||||
|
||||
ClapTrap &ClapTrap::operator=(const ClapTrap &cpy) {
|
||||
std::cout << "「🟰」ClapTrap(" << _name << "): copy assignement constructor called" << std::endl;
|
||||
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) {
|
||||
std::cout << "「💀」ClapTrap(" << _name << "): can't attack already dead" << std::endl;
|
||||
return ;
|
||||
} else if (_energyPoints == 0) {
|
||||
std::cout << "「💤」ClapTrap(" << _name << "): can't attack no energy left GO TO SLEEP!" << std::endl;
|
||||
return ;
|
||||
}
|
||||
std::cout << "「💥」ClapTrap(" << _name << "): attacks " << target << " causing " << getAttackDamage() << " points of damage!" << std::endl;
|
||||
_energyPoints--;
|
||||
}
|
||||
|
||||
void ClapTrap::takeDamage(unsigned int amount) {
|
||||
std::cout << "「🥊」ClapTrap(" << _name << "): take "<< amount << " damage" << std::endl;
|
||||
if (_health >= amount) {
|
||||
std::cout << "「💀」ClapTrap(" << _name << "): is dead HAHA" << std::endl;
|
||||
_health = 0;
|
||||
} else {
|
||||
_health -= amount;
|
||||
}
|
||||
}
|
||||
|
||||
void ClapTrap::beRepaired(uint amount) {
|
||||
if (_health == 0) {
|
||||
std::cout << "「💀」ClapTrap(" << _name << "): can't repair already dead" << std::endl;
|
||||
return ;
|
||||
} else if (_energyPoints == 0) {
|
||||
std::cout << "「💤」ClapTrap(" << _name << "): can't repair no energy left GO TO SLEEP!" << std::endl;
|
||||
return ;
|
||||
}
|
||||
std::cout << "「🩹」ClapTrap(" << _name << "): " << amount << "" << std::endl;
|
||||
_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) {
|
||||
std::cout << "「🟰」ClapTrap(" << _name << "): set attack damage" << std::endl;
|
||||
_attackDamage = in;
|
||||
}
|
||||
void ClapTrap::setEnergyPoints(uint in) {
|
||||
std::cout << "「🟰」ClapTrap(" << _name << "): set energy points" << std::endl;
|
||||
_energyPoints = in;
|
||||
}
|
||||
void ClapTrap::setHealth(uint in) {
|
||||
std::cout << "「🟰」ClapTrap(" << _name << "): set health" << std::endl;
|
||||
_health = in;
|
||||
}
|
||||
void ClapTrap::setName(std::string in) {
|
||||
std::cout << "「🟰」ClapTrap(" << _name << "): set a new name" << std::endl;
|
||||
_name = in;
|
||||
}
|
47
ex00/ClapTrap.hpp
Normal file
47
ex00/ClapTrap.hpp
Normal file
@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ClapTrap.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/20 14:08:19 by adjoly #+# #+# */
|
||||
/* Updated: 2024/11/25 15:51:49 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);
|
||||
};
|
55
ex00/Makefile
Normal file
55
ex00/Makefile
Normal file
@ -0,0 +1,55 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
|
||||
# Updated: 2024/11/20 14:07:58 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = ClapTrap
|
||||
|
||||
CC = c++
|
||||
|
||||
OBJSDIR = obj/
|
||||
|
||||
SRCS = main.cpp \
|
||||
ClapTrap.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
|
24
ex00/main.cpp
Normal file
24
ex00/main.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/20 14:25:07 by adjoly #+# #+# */
|
||||
/* Updated: 2024/11/25 16:21:51 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
int main(void) {
|
||||
ClapTrap kanel("Kanel");
|
||||
ClapTrap suki("Suki");
|
||||
|
||||
kanel.setAttackDamage(10);
|
||||
kanel.attack(suki.getName());
|
||||
suki.takeDamage(kanel.getAttackDamage());
|
||||
|
||||
return (0);
|
||||
}
|
Reference in New Issue
Block a user