d03 code refonte ex00 01 et 02

This commit is contained in:
Hugo LAMY
2022-02-22 19:05:24 +01:00
parent 088a54d036
commit 8c9d50dd68
12 changed files with 349 additions and 330 deletions

View File

@@ -1,90 +1,86 @@
#include "ClapTrap.hpp"
/*
* statics
*/
# define B_GRAY "\e[1;30m"
# define B_RED "\e[1;31m"
# define B_GREEN "\e[1;32m"
# define B_YELLOW "\e[1;33m"
# define B_BLUE "\e[1;34m"
# define B_PURPLE "\e[1;35m"
# define B_CYAN "\e[1;36m"
# define B_WHITE "\e[1;37m"
int ClapTrap::_totalNumber = 0;
# define RESET "\e[0m"
/*
* default/parametric constructor
*/
/*********************************************
* CONSTRUCTORS
*********************************************/
ClapTrap::ClapTrap( void ) {
std::cout << "claptrap created without name\n";
ClapTrap::ClapTrap( std::string name ) {
ClapTrap::_increaseNumber();
_class = _dClass;
_name = name;
_hit = _dHit;
_energy = _dEnergy;
_attack = _dAttack;
_number = getTotalNumber();
std::cout << _class << " " << _name << "-" << _number << " created\n";
return;
}
/*
* destructor
*/
ClapTrap::~ClapTrap( void ) {
std::cout << _class << " " << _name << " destructed\n";
return;
}
/*
* copy constructor
*/
ClapTrap::ClapTrap( ClapTrap const & src ) {
std::cout << _class << " " << _name << " copied\n";
ClapTrap::_increaseNumber();
_class = _dClass;
*this = src;
_number = getTotalNumber();
std::cout << _class << " " << _name << "-" << _number << " copied from " << src._class << " " << src._name << "-" << src._number << "\n";
return;
}
/*
* assignement operator
*/
/*********************************************
* DESTRUCTORS
*********************************************/
ClapTrap::~ClapTrap( void ) {
std::cout << _class << " " << _name << "-" << _number << " destructed\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
if ( this != &rhs )
{
this->_hit = rhs.getHit();
this->_energy = rhs.getEnergy();
this->_attack = rhs.getAttack();
_name = rhs.getName();
_hit = rhs.getHit();
_energy = rhs.getEnergy();
_attack = rhs.getAttack();
_number = rhs.getNumber();
}
std::cout << _class << " " << _name << " assigned\n";
std::cout << _class << " " << _name << "-" << _number << " assigned\n";
return *this;
}
/*
* constructor
*/
ClapTrap::ClapTrap( std::string name ) : _name(name) {
ClapTrap::_increaseNumber();
_class = "ClapTrap";
_hit = 10;
_energy = 10;
_attack = 1;
_number = _getNumber();
std::cout << _class << " " << _name << " created with number " << _number << "\n";
return;
}
/*
* getters
*/
/*********************************************
* ACCESSORS
*********************************************/
std::string ClapTrap::getName() const {return _name;}
std::string ClapTrap::getClass() const {return _class;}
int ClapTrap::getHit() const {return _hit;}
int ClapTrap::getEnergy() const {return _energy;}
int ClapTrap::getAttack() const {return _attack;}
int ClapTrap::_getNumber() {return ClapTrap::_totalNumber;}
int ClapTrap::getNumber() const {return _number;}
int ClapTrap::getTotalNumber() const {return _totalNumber;}
void ClapTrap::_increaseNumber() {ClapTrap::_totalNumber++;}
/*
* robots
*/
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void ClapTrap::attack(const std::string & target) {
@@ -92,7 +88,7 @@ void ClapTrap::attack(const std::string & target) {
std::ostringstream state;
state << B_CYAN "[" B_GREEN << _class[0] << _number << B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
action << _class << " " << _name;
action << _class << " " << _name << "-" << _number;
if (_energy > 0 && _hit > 0)
{
@@ -120,22 +116,17 @@ void ClapTrap::takeDamage(unsigned int amount) {
std::ostringstream state;
state << B_CYAN "[" B_GREEN << _class[0] << _number << B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
action << _class << " " << _name;
action << _class << " " << _name << "-" << _number;
if (_energy > 0 && _hit > 0)
if (_hit > 0)
{
_hit -= amount;
if (_hit < 0)
_hit = 0;
action << " looses " B_YELLOW << amount << RESET << " hit points" << '\n';
}
else
{
if (_energy <= 0)
action << " cannot take damage because " B_RED " is out of energy" RESET "\n";
else if (_hit <= 0)
action << " cannot take damage because " B_RED " is out of hit" RESET "\n";
}
else if (_hit <= 0)
action << " cannot take damage because " B_RED " is out of hit" RESET "\n";
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
std::cout << state.str() << action.str();
@@ -147,7 +138,7 @@ void ClapTrap::beRepaired(unsigned int amount) {
std::ostringstream state;
state << B_CYAN "[" B_GREEN << _class[0] << _number << B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
action << _class << " " << _name;
action << _class << " " << _name << "-" << _number;
if (_energy > 0 && _hit > 0)
{
@@ -168,3 +159,16 @@ void ClapTrap::beRepaired(unsigned int amount) {
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
std::cout << state.str() << action.str();
}
/*********************************************
* STATICS
*********************************************/
int ClapTrap::_totalNumber = 0;
std::string const ClapTrap::_dName = "robot";
std::string const ClapTrap::_dClass = "ClapTrap";
int const ClapTrap::_dHit = 10;
int const ClapTrap::_dEnergy = 10;
int const ClapTrap::_dAttack = 0;