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,96 +1,109 @@
#include "ClapTrap.hpp"
/*
* default/parametric constructor
*/
# 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"
ClapTrap::ClapTrap( void ) {
std::cout << "claptrap created without name\n";
# define RESET "\e[0m"
/*********************************************
* CONSTRUCTORS
*********************************************/
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 << "claptrap " << _name << " destructed\n";
return;
}
/*
* copy constructor
*/
ClapTrap::ClapTrap( ClapTrap const & src ) {
std::cout << "claptrap " << _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 ) {
std::cout << "claptrap " << _name << " assigned\n";
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 << "-" << _number << " assigned\n";
return *this;
}
/*
* constructor
*/
ClapTrap::ClapTrap( std::string name ) : _name(name) {
std::cout << "claptrap " << _name << " created\n";
_hit = 10;
_energy = 10;
_attack = 1;
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() 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) {
std::ostringstream action;
std::ostringstream state;
state << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
action << " ClapTrap " << _name;
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 << "-" << _number;
if (_energy && _hit)
if (_energy > 0 && _hit > 0)
{
_energy--;
action << " attacked " << target << ", causing " B_YELLOW << _attack << RESET << " points of damage!" << '\n';
if (_energy < 0)
_energy = 0;
action << " attacked " << target << ", causing " B_YELLOW << _attack << RESET << " points of damage" << '\n';
}
else
{
_attack = 0;
if (!_energy)
action << "cannot attack because " B_RED " is out of energy\n" RESET;
else if (!_hit)
action << "cannot attack because " B_RED " is out of hit\n" RESET;
if (_energy <= 0)
action << " cannot attack because " B_RED " is out of energy" RESET"\n";
else if (_hit <= 0)
action << " cannot attack because " B_RED " is out of hit" RESET "\n";
}
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
@@ -102,21 +115,18 @@ void ClapTrap::takeDamage(unsigned int amount) {
std::ostringstream action;
std::ostringstream state;
state << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
action << " ClapTrap " << _name;
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 << "-" << _number;
if (_energy && _hit)
if (_hit > 0)
{
_hit -= amount;
action << " looses " B_YELLOW << amount << RESET << " points of damage :/" << '\n';
}
else
{
if (!_energy)
action << "cannot take damage because " B_RED " is out of energy\n" RESET;
else if (!_hit)
action << "cannot take damage because " B_RED " is out of hit\n" RESET;
if (_hit < 0)
_hit = 0;
action << " looses " B_YELLOW << amount << RESET << " hit points" << '\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();
@@ -127,23 +137,38 @@ void ClapTrap::beRepaired(unsigned int amount) {
std::ostringstream action;
std::ostringstream state;
state << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
action << " ClapTrap " << _name;
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 << "-" << _number;
if (_energy && _hit)
if (_energy > 0 && _hit > 0)
{
_energy--;
if (_energy < 0)
_energy = 0;
_hit += amount;
action << " repaired itself and gained " B_YELLOW << amount << RESET << " points of life :)" << '\n';
action << " repaired itself and gained " B_YELLOW << amount << RESET << " points of life" << '\n';
}
else
{
if (!_energy)
action << "cannot repair itself because " B_RED " is out of energy\n" RESET;
else if (!_hit)
action << "cannot repair itself because " B_RED " is out of hit\n" RESET;
if (_energy <= 0)
action << " cannot repair itself because " B_RED " is out of energy" RESET "\n";
else if (_hit <= 0)
action << " cannot repair itself because " B_RED " is out of hit" RESET "\n";
}
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;

View File

@@ -4,14 +4,13 @@
#include <iostream>
#include <sstream>
#include <string>
#include <color.h>
class ClapTrap {
public:
ClapTrap( std::string name ); // default/parametric constructor
ClapTrap( ClapTrap const & src ); // copy constructor
ClapTrap( std::string name = ClapTrap::_dName );
ClapTrap( ClapTrap const & src ); // copy constructor
~ClapTrap( void ); // destructor
ClapTrap & operator=( ClapTrap const & rhs ); // assignement operator
@@ -21,18 +20,36 @@ public:
void beRepaired(unsigned int amount);
std::string getName() const;
std::string getClass() const;
int getHit() const;
int getEnergy() const;
int getAttack() const;
int getNumber() const;
protected:
int getTotalNumber() const;
std::string _name;
std::string _class;
int _hit;
int _energy;
int _attack;
int _number;
void _increaseNumber();
private:
ClapTrap( void ); // default/parametric constructor
void assignValues(ClapTrap & src);
static int _totalNumber;
std::string const _name;
int _hit;
int _energy;
int _attack;
static const std::string _dName;
static const std::string _dClass;
static const int _dHit;
static const int _dEnergy;
static const int _dAttack;
static const int _dNumber;
};

View File

@@ -1,24 +0,0 @@
#ifndef COLOR_H
# define COLOR_H
# define GRAY "\e[0;30m"
# define RED "\e[0;31m"
# define GREEN "\e[0;32m"
# define YELLOW "\e[0;33m"
# define BLUE "\e[0;34m"
# define PURPLE "\e[0;35m"
# define CYAN "\e[0;36m"
# define WHITE "\e[0;37m"
# 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"
# define RESET "\e[0m"
#endif