1
0

」 feat(Ex00): Added log func to claptrap

This commit is contained in:
2024-11-27 12:06:09 +01:00
parent 4cd1845e9a
commit 41ca750253
3 changed files with 46 additions and 21 deletions

View File

@ -6,34 +6,37 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 14:29:16 by adjoly #+# #+# */ /* Created: 2024/11/20 14:29:16 by adjoly #+# #+# */
/* Updated: 2024/11/25 16:22:54 by adjoly ### ########.fr */ /* Updated: 2024/11/27 11:55:35 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ClapTrap.hpp" #include "ClapTrap.hpp"
#include <iostream> #include <iostream>
#include <pthread.h>
#include <string>
ClapTrap::ClapTrap(void) : ClapTrap::ClapTrap(void) :
_name("Kanel"), _health(10), _energyPoints(10), _attackDamage(0) { _name("Kanel"), _health(10), _energyPoints(10), _attackDamage(0) {
std::cout << "」ClapTrap: default constructor called" << std::endl; log("", "default constructor called");
} }
ClapTrap::~ClapTrap(void) { ClapTrap::~ClapTrap(void) {
std::cout << "」ClapTrap(" << _name << "): destructor called" << std::endl; log("", "destructor called");
} }
ClapTrap::ClapTrap(std::string name) : ClapTrap::ClapTrap(std::string name) :
_name(name), _health(10), _energyPoints(10), _attackDamage(0) { _name(name), _health(10), _energyPoints(10), _attackDamage(0) {
std::cout << "」ClapTrap(" << _name << "): constructor called" << std::endl; log("", _name, "constructor called");
} }
ClapTrap::ClapTrap(const ClapTrap &other) { ClapTrap::ClapTrap(const ClapTrap &other) {
*this = other; *this = other;
std::cout << "」ClapTrap(" << _name << "): copy constructor called" << std::endl; log("", _name, "copy constructor called");
} }
ClapTrap &ClapTrap::operator=(const ClapTrap &cpy) { ClapTrap &ClapTrap::operator=(const ClapTrap &cpy) {
std::cout << "「🟰」ClapTrap(" << _name << "): copy assignement constructor called" << std::endl; log("🟰", _name, "copy assignement constructor called");
if (this != &cpy) { if (this != &cpy) {
_name = cpy.getName(); _name = cpy.getName();
_health = cpy.getHealth(); _health = cpy.getHealth();
@ -45,20 +48,20 @@ ClapTrap &ClapTrap::operator=(const ClapTrap &cpy) {
void ClapTrap::attack(const std::string& target) { void ClapTrap::attack(const std::string& target) {
if (_health == 0) { if (_health == 0) {
std::cout << "「💀」ClapTrap(" << _name << "): can't attack already dead" << std::endl; log("💀", _name, "can't attack already dead");
return ; return ;
} else if (_energyPoints == 0) { } else if (_energyPoints == 0) {
std::cout << "「💤」ClapTrap(" << _name << "): can't attack no energy left GO TO SLEEP!" << std::endl; log("💤", _name, "can't attack no energy left GO TO SLEEP!");
return ; return ;
} }
std::cout << "「💥」ClapTrap(" << _name << "): attacks " << target << " causing " << getAttackDamage() << " points of damage!" << std::endl; log("💥", _name, "attacks " + target + " causing " + iToS(getAttackDamage()) + " points of damage!");
_energyPoints--; _energyPoints--;
} }
void ClapTrap::takeDamage(unsigned int amount) { void ClapTrap::takeDamage(unsigned int amount) {
std::cout << "「🥊」ClapTrap(" << _name << "): take "<< amount << " damage" << std::endl; log("🥊", _name, "take " + iToS(amount) + " damage");
if (_health >= amount) { if (_health >= amount) {
std::cout << "「💀」ClapTrap(" << _name << "): is dead HAHA" << std::endl; log("💀", _name, "is dead HAHA");
_health = 0; _health = 0;
} else { } else {
_health -= amount; _health -= amount;
@ -67,13 +70,13 @@ void ClapTrap::takeDamage(unsigned int amount) {
void ClapTrap::beRepaired(uint amount) { void ClapTrap::beRepaired(uint amount) {
if (_health == 0) { if (_health == 0) {
std::cout << "「💀」ClapTrap(" << _name << "): can't repair already dead" << std::endl; log("💀", _name, "can't repair already dead");
return ; return ;
} else if (_energyPoints == 0) { } else if (_energyPoints == 0) {
std::cout << "「💤」ClapTrap(" << _name << "): can't repair no energy left GO TO SLEEP!" << std::endl; log("💤", _name, "can't repair no energy left GO TO SLEEP!");
return ; return ;
} }
std::cout << "「🩹」ClapTrap(" << _name << "): " << amount << "" << std::endl; log("🩹", _name, iToS(amount) + "");
_health += amount; _health += amount;
_energyPoints--; _energyPoints--;
} }
@ -84,18 +87,18 @@ uint ClapTrap::getHealth(void) const { return (_health); }
std::string ClapTrap::getName(void) const { return (_name); } std::string ClapTrap::getName(void) const { return (_name); }
void ClapTrap::setAttackDamage(uint in) { void ClapTrap::setAttackDamage(uint in) {
std::cout << "「🟰」ClapTrap(" << _name << "): set attack damage" << std::endl; log("🟰", _name, "set attack damage");
_attackDamage = in; _attackDamage = in;
} }
void ClapTrap::setEnergyPoints(uint in) { void ClapTrap::setEnergyPoints(uint in) {
std::cout << "「🟰」ClapTrap(" << _name << "): set energy points" << std::endl; log("🟰", _name, "set energy points");
_energyPoints = in; _energyPoints = in;
} }
void ClapTrap::setHealth(uint in) { void ClapTrap::setHealth(uint in) {
std::cout << "「🟰」ClapTrap(" << _name << "): set health" << std::endl; log("🟰", _name, "set health");
_health = in; _health = in;
} }
void ClapTrap::setName(std::string in) { void ClapTrap::setName(std::string in) {
std::cout << "「🟰」ClapTrap(" << _name << "): set a new name" << std::endl; log("🟰", _name, "set a new name");
_name = in; _name = in;
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 14:08:19 by adjoly #+# #+# */ /* Created: 2024/11/20 14:08:19 by adjoly #+# #+# */
/* Updated: 2024/11/25 15:51:49 by adjoly ### ########.fr */ /* Updated: 2024/11/27 11:51:42 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,3 +45,7 @@ class ClapTrap {
void setHealth(uint); void setHealth(uint);
void setName(std::string); void setName(std::string);
}; };
void log(std::string, std::string, std::string);
void log(std::string, std::string);
std::string iToS(unsigned int i);

View File

@ -6,17 +6,35 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 14:25:07 by adjoly #+# #+# */ /* Created: 2024/11/20 14:25:07 by adjoly #+# #+# */
/* Updated: 2024/11/25 16:21:51 by adjoly ### ########.fr */ /* Updated: 2024/11/27 11:52:11 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ClapTrap.hpp" #include "ClapTrap.hpp"
#include <iostream>
#include <sstream>
void log(std::string emoji, std::string who, std::string str) {
std::cout << "" << emoji << "」ClapTrap(" << who << "): " << str << std::endl;
}
void log(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) { int main(void) {
ClapTrap kanel("Kanel"); ClapTrap kanel("Kanel");
ClapTrap suki("Suki"); ClapTrap suki("Suki");
kanel.setAttackDamage(10); kanel.setAttackDamage(5);
log("🙀", kanel.getName(), "Oh my god Kanel uses his last teeth to attack " + suki.getName());
kanel.attack(suki.getName()); kanel.attack(suki.getName());
suki.takeDamage(kanel.getAttackDamage()); suki.takeDamage(kanel.getAttackDamage());