d03 ex01 refondus avec meilleurs gestion de la classe de base
This commit is contained in:
@@ -1,88 +1,58 @@
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
/*
|
||||
* statics
|
||||
*/
|
||||
/*********************************************
|
||||
* CONSTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
int ClapTrap::_totalNumber = 0;
|
||||
|
||||
/*
|
||||
* assign values for default or secondary constructors
|
||||
*/
|
||||
|
||||
void ClapTrap::assignValues( ClapTrap & src ) {
|
||||
src._class = "ClapTrap";
|
||||
src._hit = 10;
|
||||
src._energy = 10;
|
||||
src._attack = 1;
|
||||
src._number = getNumber();
|
||||
}
|
||||
|
||||
/*
|
||||
* default/parametric constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( void ) {
|
||||
assignValues(*this);
|
||||
std::cout << "claptrap default creation without name\n";
|
||||
ClapTrap::ClapTrap( std::string name ) {
|
||||
ClapTrap::_increaseNumber();
|
||||
_name = name;
|
||||
_class = _dClass;
|
||||
_hit = _dHit;
|
||||
_energy = _dEnergy;
|
||||
_attack = _dAttack;
|
||||
_number = getNumber();
|
||||
std::cout << _class << " " << _name << " nb:" << _number << " created\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* destructor
|
||||
*/
|
||||
|
||||
ClapTrap::~ClapTrap( void ) {
|
||||
std::cout << _class << " " << _name << " destructed\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* copy constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( ClapTrap const & src ) {
|
||||
*this = src;
|
||||
std::cout << _class << " " << _name << " copied\n";
|
||||
std::cout << _class << " " << _name << " nb:" << _number << " copied from " << src._class << "-" << src._name << "-" << src._number << "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* assignement operator
|
||||
*/
|
||||
/*********************************************
|
||||
* DESTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
ClapTrap::~ClapTrap( void ) {
|
||||
std::cout << _class << " " << _name << " nb:" << _number << " destructed\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* OPERATORS
|
||||
*********************************************/
|
||||
|
||||
ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
|
||||
|
||||
if ( this != &rhs )
|
||||
{
|
||||
ClapTrap::_increaseNumber();
|
||||
_number = getNumber();
|
||||
_class = "ClapTrap";
|
||||
_hit = rhs.getHit();
|
||||
_energy = rhs.getEnergy();
|
||||
_attack = rhs.getAttack();
|
||||
_name = rhs.getName();
|
||||
}
|
||||
|
||||
std::cout << _class << " " << _name << " assigned\n";
|
||||
std::cout << _class << " " << _name << " nb:" << _number << " assigned\n";
|
||||
return *this;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* parameters constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( std::string name ) : _name(name) {
|
||||
ClapTrap::_increaseNumber();
|
||||
assignValues(*this);
|
||||
std::cout << _class << " " << _name << " named creation with number " << _number << "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* getters
|
||||
*/
|
||||
/*********************************************
|
||||
* ACCESSORS
|
||||
*********************************************/
|
||||
|
||||
std::string ClapTrap::getName() const {return _name;}
|
||||
std::string ClapTrap::getClass() const {return _class;}
|
||||
@@ -92,9 +62,9 @@ int ClapTrap::getAttack() const {return _attack;}
|
||||
int ClapTrap::getNumber() const {return ClapTrap::_totalNumber;}
|
||||
void ClapTrap::_increaseNumber() {ClapTrap::_totalNumber++;}
|
||||
|
||||
/*
|
||||
* robots
|
||||
*/
|
||||
/*********************************************
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
|
||||
void ClapTrap::attack(const std::string & target) {
|
||||
|
||||
@@ -132,20 +102,15 @@ void ClapTrap::takeDamage(unsigned int amount) {
|
||||
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;
|
||||
|
||||
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();
|
||||
@@ -178,3 +143,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;
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class ClapTrap {
|
||||
|
||||
public:
|
||||
|
||||
ClapTrap( std::string name );
|
||||
ClapTrap( std::string name = ClapTrap::_dName );
|
||||
ClapTrap( ClapTrap const & src ); // copy constructor
|
||||
~ClapTrap( void ); // destructor
|
||||
|
||||
@@ -38,13 +38,18 @@ protected:
|
||||
|
||||
void _increaseNumber();
|
||||
|
||||
ClapTrap( void ); // default/parametric constructor
|
||||
|
||||
private:
|
||||
|
||||
void assignValues(ClapTrap & src);
|
||||
static int _totalNumber;
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
BIN
d03/ex01/robots
BIN
d03/ex01/robots
Binary file not shown.
78
d03/jodufour/Makefile
Normal file
78
d03/jodufour/Makefile
Normal file
@@ -0,0 +1,78 @@
|
||||
######################################
|
||||
# COMMANDS #
|
||||
######################################
|
||||
CXX = c++ -c
|
||||
LINK = c++
|
||||
MKDIR = mkdir -p
|
||||
RM = rm -rf
|
||||
|
||||
######################################
|
||||
# EXECUTABLE #
|
||||
######################################
|
||||
NAME = ex01.out
|
||||
|
||||
#######################################
|
||||
# DIRECTORIES #
|
||||
#######################################
|
||||
SRC_DIR = srcs/
|
||||
OBJ_DIR = objs/
|
||||
PRV_DIR = private/
|
||||
|
||||
######################################
|
||||
# SOURCE FILES #
|
||||
######################################
|
||||
SRC = \
|
||||
${addprefix class/, \
|
||||
ClapTrap.cpp \
|
||||
ScavTrap.cpp \
|
||||
} \
|
||||
main.cpp
|
||||
|
||||
######################################
|
||||
# OBJECT FILES #
|
||||
######################################
|
||||
OBJ = ${SRC:.cpp=.o}
|
||||
OBJ := ${addprefix ${OBJ_DIR}, ${OBJ}}
|
||||
|
||||
DEP = ${OBJ:.o=.d}
|
||||
|
||||
#######################################
|
||||
# FLAGS #
|
||||
#######################################
|
||||
CPPFLAGS = -Wall -Wextra -Werror
|
||||
CPPFLAGS += -std=c++98
|
||||
CPPFLAGS += -MMD -MP
|
||||
CPPFLAGS += -I${PRV_DIR}
|
||||
|
||||
LDFLAGS =
|
||||
|
||||
ifeq (${DEBUG}, 1)
|
||||
CPPFLAGS += -g
|
||||
CPPFLAGS += -DDEBUG=1
|
||||
endif
|
||||
|
||||
#######################################
|
||||
# RULES #
|
||||
#######################################
|
||||
${NAME}: ${OBJ}
|
||||
${LINK} ${OBJ} ${LDFLAGS} ${OUTPUT_OPTION}
|
||||
|
||||
all: ${NAME}
|
||||
|
||||
-include ${DEP}
|
||||
|
||||
${OBJ_DIR}%.o: ${SRC_DIR}%.cpp
|
||||
@${MKDIR} ${@D}
|
||||
${CXX} ${CPPFLAGS} $< ${OUTPUT_OPTION}
|
||||
|
||||
clean:
|
||||
${RM} ${OBJ_DIR} ${NAME} vgcore.*
|
||||
|
||||
fclean:
|
||||
${RM} ${OBJ_DIR} ${NAME} vgcore.*
|
||||
|
||||
re: clean all
|
||||
|
||||
fre: fclean all
|
||||
|
||||
.PHONY: all clean fclean re fre
|
||||
BIN
d03/jodufour/ex01.out
Executable file
BIN
d03/jodufour/ex01.out
Executable file
Binary file not shown.
3
d03/jodufour/objs/class/ClapTrap.d
Normal file
3
d03/jodufour/objs/class/ClapTrap.d
Normal file
@@ -0,0 +1,3 @@
|
||||
objs/class/ClapTrap.o: srcs/class/ClapTrap.cpp private/class/ClapTrap.hpp
|
||||
|
||||
private/class/ClapTrap.hpp:
|
||||
BIN
d03/jodufour/objs/class/ClapTrap.o
Normal file
BIN
d03/jodufour/objs/class/ClapTrap.o
Normal file
Binary file not shown.
6
d03/jodufour/objs/class/ScavTrap.d
Normal file
6
d03/jodufour/objs/class/ScavTrap.d
Normal file
@@ -0,0 +1,6 @@
|
||||
objs/class/ScavTrap.o: srcs/class/ScavTrap.cpp private/class/ScavTrap.hpp \
|
||||
private/class/ClapTrap.hpp
|
||||
|
||||
private/class/ScavTrap.hpp:
|
||||
|
||||
private/class/ClapTrap.hpp:
|
||||
BIN
d03/jodufour/objs/class/ScavTrap.o
Normal file
BIN
d03/jodufour/objs/class/ScavTrap.o
Normal file
Binary file not shown.
6
d03/jodufour/objs/main.d
Normal file
6
d03/jodufour/objs/main.d
Normal file
@@ -0,0 +1,6 @@
|
||||
objs/main.o: srcs/main.cpp private/class/ScavTrap.hpp \
|
||||
private/class/ClapTrap.hpp
|
||||
|
||||
private/class/ScavTrap.hpp:
|
||||
|
||||
private/class/ClapTrap.hpp:
|
||||
BIN
d03/jodufour/objs/main.o
Normal file
BIN
d03/jodufour/objs/main.o
Normal file
Binary file not shown.
59
d03/jodufour/private/class/ClapTrap.hpp
Normal file
59
d03/jodufour/private/class/ClapTrap.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef CLAPTRAP_HPP
|
||||
# define CLAPTRAP_HPP
|
||||
|
||||
# include <iostream>
|
||||
|
||||
# ifndef DEBUG
|
||||
# define DEBUG 0
|
||||
# endif
|
||||
|
||||
class ClapTrap
|
||||
{
|
||||
private:
|
||||
// Attributes
|
||||
static std::string const _defaultName;
|
||||
static unsigned int const _defaultHitPoints;
|
||||
static unsigned int const _defaultEnergyPoints;
|
||||
static unsigned int const _defaultAttackDamages;
|
||||
|
||||
protected:
|
||||
// Attributes
|
||||
std::string _name;
|
||||
unsigned int _hitPoints;
|
||||
unsigned int _energyPoints;
|
||||
unsigned int _attackDamages;
|
||||
|
||||
// Constructors
|
||||
ClapTrap(
|
||||
std::string const &name,
|
||||
unsigned int const hitPoints,
|
||||
unsigned int const energyPoints,
|
||||
unsigned int const attackDamages);
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
ClapTrap(std::string const &name = ClapTrap::_defaultName);
|
||||
ClapTrap(ClapTrap const &src);
|
||||
|
||||
// Destructors
|
||||
virtual ~ClapTrap(void);
|
||||
|
||||
// Accessors
|
||||
std::string const &getName(void) const;
|
||||
|
||||
unsigned int getHitPoints(void) const;
|
||||
unsigned int getEnergyPoints(void) const;
|
||||
unsigned int getAttackDamages(void) const;
|
||||
|
||||
// Member functions
|
||||
virtual void attack(std::string const &target);
|
||||
void beRepaired(unsigned int const amount);
|
||||
void takeDamage(unsigned int const amount);
|
||||
|
||||
// Operators
|
||||
ClapTrap &operator=(ClapTrap const &rhs);
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &o, ClapTrap const &rhs);
|
||||
|
||||
#endif
|
||||
39
d03/jodufour/private/class/ScavTrap.hpp
Normal file
39
d03/jodufour/private/class/ScavTrap.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef SCAVTRAP_HPP
|
||||
# define SCAVTRAP_HPP
|
||||
|
||||
# include "ClapTrap.hpp"
|
||||
|
||||
# ifndef DEBUG
|
||||
# define DEBUG 0
|
||||
# endif
|
||||
|
||||
class ScavTrap : public ClapTrap
|
||||
{
|
||||
private:
|
||||
// Attributes
|
||||
bool _activeMode;
|
||||
|
||||
static std::string const _defaultName;
|
||||
static unsigned int const _defaultHitPoints;
|
||||
static unsigned int const _defaultEnergyPoints;
|
||||
static unsigned int const _defaultAttackDamages;
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
ScavTrap(std::string const &name = ScavTrap::_defaultName);
|
||||
ScavTrap(ScavTrap const &src);
|
||||
|
||||
// Destructors
|
||||
virtual ~ScavTrap(void);
|
||||
|
||||
// Member functions
|
||||
virtual void attack(std::string const &target);
|
||||
void guardGate(void);
|
||||
|
||||
// Operators
|
||||
ScavTrap &operator=(ScavTrap const &rhs);
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &o, ScavTrap const &rhs);
|
||||
|
||||
#endif
|
||||
235
d03/jodufour/srcs/class/ClapTrap.cpp
Normal file
235
d03/jodufour/srcs/class/ClapTrap.cpp
Normal file
@@ -0,0 +1,235 @@
|
||||
#include "class/ClapTrap.hpp"
|
||||
|
||||
// ************************************************************************** //
|
||||
// Constructors //
|
||||
// ************************************************************************** //
|
||||
|
||||
ClapTrap::ClapTrap(std::string const &name) :
|
||||
_name(name),
|
||||
_hitPoints(ClapTrap::_defaultHitPoints),
|
||||
_energyPoints(ClapTrap::_defaultEnergyPoints),
|
||||
_attackDamages(ClapTrap::_defaultAttackDamages)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Creating ClapTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(ClapTrap const &src) :
|
||||
_name(src._name),
|
||||
_hitPoints(src._hitPoints),
|
||||
_energyPoints(src._energyPoints),
|
||||
_attackDamages(src._attackDamages)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Creating ClapTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(
|
||||
std::string const &name,
|
||||
unsigned int const hitPoints,
|
||||
unsigned int const energyPoints,
|
||||
unsigned int const attackDamages) :
|
||||
_name(name),
|
||||
_hitPoints(hitPoints),
|
||||
_energyPoints(energyPoints),
|
||||
_attackDamages(attackDamages)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Creating ClapTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Destructors //
|
||||
// ************************************************************************* //
|
||||
|
||||
ClapTrap::~ClapTrap(void)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Destroying ClapTrap "
|
||||
<< this->_name
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Accessors //
|
||||
// ************************************************************************* //
|
||||
|
||||
std::string const &ClapTrap::getName(void) const
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::getName()"
|
||||
<< std::endl;
|
||||
return this->_name;
|
||||
}
|
||||
|
||||
unsigned int ClapTrap::getHitPoints(void) const
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::getHitPoints()"
|
||||
<< std::endl;
|
||||
return this->_hitPoints;
|
||||
}
|
||||
|
||||
unsigned int ClapTrap::getEnergyPoints(void) const
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::getEnergyPoints()"
|
||||
<< std::endl;
|
||||
return this->_energyPoints;
|
||||
}
|
||||
|
||||
unsigned int ClapTrap::getAttackDamages(void) const
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::getAttackDamages()"
|
||||
<< std::endl;
|
||||
return this->_attackDamages;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Public Member Functions //
|
||||
// ************************************************************************* //
|
||||
|
||||
void ClapTrap::attack(std::string const &target)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::attack()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ClapTrap "
|
||||
<< this->_name;
|
||||
if (this->_hitPoints && this->_energyPoints)
|
||||
std::cout
|
||||
<< " deals "
|
||||
<< this->_attackDamages
|
||||
<< " damages to ";
|
||||
else if (!this->_hitPoints)
|
||||
std::cout
|
||||
<< " hasn't enough hit points to attack ";
|
||||
else
|
||||
std::cout
|
||||
<< " hasn't enough energy points to attack ";
|
||||
std::cout
|
||||
<< target
|
||||
<< std::endl;
|
||||
this->_energyPoints -= !!this->_energyPoints;
|
||||
}
|
||||
|
||||
void ClapTrap::beRepaired(unsigned int const amount)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::beRepaired()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ClapTrap "
|
||||
<< this->_name;
|
||||
if (this->_hitPoints && this->_energyPoints)
|
||||
{
|
||||
std::cout
|
||||
<< " repairs itself for an amount of "
|
||||
<< amount;
|
||||
this->_hitPoints += amount;
|
||||
}
|
||||
else if (!this->_hitPoints)
|
||||
std::cout
|
||||
<< " hasn't enough hit points to repair itself";
|
||||
else
|
||||
std::cout
|
||||
<< " hasn't enough energy points to repair itself";
|
||||
std::cout << std::endl;
|
||||
this->_energyPoints -= !!this->_energyPoints;
|
||||
}
|
||||
|
||||
void ClapTrap::takeDamage(unsigned int const amount)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::takeDamage()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ClapTrap "
|
||||
<< this->_name;
|
||||
if (this->_hitPoints && this->_energyPoints)
|
||||
{
|
||||
std::cout
|
||||
<< " takes "
|
||||
<< amount
|
||||
<< " damages";
|
||||
if (this->_hitPoints < amount)
|
||||
this->_hitPoints = 0;
|
||||
else
|
||||
this->_hitPoints -= amount;
|
||||
}
|
||||
else if (!this->_hitPoints)
|
||||
std::cout
|
||||
<< " hasn't enough hit points to take damages";
|
||||
else
|
||||
std::cout
|
||||
<< " hasn't enough energy points to take damages";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Operators //
|
||||
// ************************************************************************* //
|
||||
|
||||
ClapTrap &ClapTrap::operator=(ClapTrap const &rhs)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ClapTrap::operator=()"
|
||||
<< std::endl;
|
||||
if (this != &rhs)
|
||||
{
|
||||
this->_name = rhs._name;
|
||||
this->_hitPoints = rhs._hitPoints;
|
||||
this->_energyPoints = rhs._energyPoints;
|
||||
this->_attackDamages = rhs._attackDamages;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &o, ClapTrap const &rhs)
|
||||
{
|
||||
o
|
||||
<< "ClapTrap:" << std::endl
|
||||
<< "\t" "name: " << rhs.getName() << std::endl
|
||||
<< "\t" "hitPoints: " << rhs.getHitPoints() << std::endl
|
||||
<< "\t" "energyPoints: " << rhs.getEnergyPoints() << std::endl
|
||||
<< "\t" "attackDamages: " << rhs.getAttackDamages() << std::endl;
|
||||
return o;
|
||||
}
|
||||
|
||||
// ************************************************************************** //
|
||||
// Private Attributes //
|
||||
// ************************************************************************** //
|
||||
|
||||
std::string const ClapTrap::_defaultName = std::string("defaultName");
|
||||
unsigned int const ClapTrap::_defaultHitPoints = 10;
|
||||
unsigned int const ClapTrap::_defaultEnergyPoints = 10;
|
||||
unsigned int const ClapTrap::_defaultAttackDamages = 0;
|
||||
148
d03/jodufour/srcs/class/ScavTrap.cpp
Normal file
148
d03/jodufour/srcs/class/ScavTrap.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
#include "class/ScavTrap.hpp"
|
||||
|
||||
// ************************************************************************** //
|
||||
// Constructors //
|
||||
// ************************************************************************** //
|
||||
|
||||
ScavTrap::ScavTrap(std::string const &name) :
|
||||
ClapTrap(
|
||||
name,
|
||||
ScavTrap::_defaultHitPoints,
|
||||
ScavTrap::_defaultEnergyPoints,
|
||||
ScavTrap::_defaultAttackDamages),
|
||||
_activeMode(false)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< std::boolalpha
|
||||
<< "Creating ScavTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< " (" << this->_activeMode << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
ScavTrap::ScavTrap(ScavTrap const &src) :
|
||||
ClapTrap(
|
||||
src._name,
|
||||
src._hitPoints,
|
||||
src._energyPoints,
|
||||
src._attackDamages)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< std::boolalpha
|
||||
<< "Creating ScavTrap "
|
||||
<< this->_name
|
||||
<< " (" << this->_hitPoints << ")"
|
||||
<< " (" << this->_energyPoints << ")"
|
||||
<< " (" << this->_attackDamages << ")"
|
||||
<< " (" << this->_activeMode << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Destructors //
|
||||
// ************************************************************************* //
|
||||
|
||||
ScavTrap::~ScavTrap(void)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Destroying ScavTrap "
|
||||
<< this->_name
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Public Member Functions //
|
||||
// ************************************************************************* //
|
||||
|
||||
void ScavTrap::attack(std::string const &target)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ScavTrap::attack()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ScavTrap "
|
||||
<< this->_name;
|
||||
if (this->_hitPoints && this->_energyPoints)
|
||||
std::cout
|
||||
<< " deals "
|
||||
<< this->_attackDamages
|
||||
<< " damages to ";
|
||||
else if (!this->_hitPoints)
|
||||
std::cout
|
||||
<< " hasn't enough hit points to attack ";
|
||||
else
|
||||
std::cout
|
||||
<< " hasn't enough energy points to attack ";
|
||||
std::cout
|
||||
<< target
|
||||
<< std::endl;
|
||||
this->_energyPoints -= !!this->_energyPoints;
|
||||
}
|
||||
|
||||
void ScavTrap::guardGate(void)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ScavTrap::guardGate()"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "ScavTrap "
|
||||
<< this->_name;
|
||||
if (!this->_activeMode)
|
||||
std::cout
|
||||
<< " enters ";
|
||||
else
|
||||
std::cout
|
||||
<< " leaves ";
|
||||
std::cout
|
||||
<< "Gate keeper mode"
|
||||
<< std::endl;
|
||||
this->_activeMode ^= true;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
// Operators //
|
||||
// ************************************************************************* //
|
||||
|
||||
ScavTrap &ScavTrap::operator=(ScavTrap const &rhs)
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout
|
||||
<< "Calling ScavTrap::operator=()"
|
||||
<< std::endl;
|
||||
if (this != &rhs)
|
||||
{
|
||||
this->_name = rhs._name;
|
||||
this->_hitPoints = rhs._hitPoints;
|
||||
this->_energyPoints = rhs._energyPoints;
|
||||
this->_attackDamages = rhs._attackDamages;
|
||||
this->_activeMode = rhs._activeMode;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &o, ScavTrap const &rhs)
|
||||
{
|
||||
o << "ScavTrap:" << std::endl
|
||||
<< "\t" "name: " << rhs.getName() << std::endl
|
||||
<< "\t" "hitPoints: " << rhs.getHitPoints() << std::endl
|
||||
<< "\t" "energyPoints: " << rhs.getEnergyPoints() << std::endl
|
||||
<< "\t" "attackDamages: " << rhs.getAttackDamages() << std::endl;
|
||||
return o;
|
||||
}
|
||||
|
||||
// ************************************************************************** //
|
||||
// Private Attributes //
|
||||
// ************************************************************************** //
|
||||
|
||||
std::string const ScavTrap::_defaultName = std::string("defaultName");
|
||||
unsigned int const ScavTrap::_defaultHitPoints = 100;
|
||||
unsigned int const ScavTrap::_defaultEnergyPoints = 50;
|
||||
unsigned int const ScavTrap::_defaultAttackDamages = 20;
|
||||
53
d03/jodufour/srcs/main.cpp
Normal file
53
d03/jodufour/srcs/main.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <cstdlib>
|
||||
#include "class/ScavTrap.hpp"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
ClapTrap robot1("robot1");
|
||||
ScavTrap robot2("robot2");
|
||||
ScavTrap robot3("robot3");
|
||||
ClapTrap robot4("robot4");
|
||||
|
||||
std::cout << "\nassignement 1:\n";
|
||||
ScavTrap robotmp1("robot5");
|
||||
robotmp1.guardGate();
|
||||
std::cout << "copy:\n";
|
||||
ClapTrap robot6(robotmp1); // PBM : it says it's a ScavTrap but it has no guardGate()...
|
||||
// robot6.guardGate();
|
||||
std::cout << "END assignement 1:\n\n";
|
||||
|
||||
//std::cout << "assignement 2:\n";
|
||||
// ScavTrap robotmp2("robot7");
|
||||
// ScavTrap robot8(robotmp2);
|
||||
// robot8.guardGate();
|
||||
//
|
||||
//std::cout << "assignement 3:\n";
|
||||
// ClapTrap robotmp3("robot9"); // PBM : assignation doesn't work...
|
||||
//// ScavTrap robot10(robotmp3);
|
||||
//// robot10.guardGate();
|
||||
//
|
||||
//std::cout << "assignement 4:\n";
|
||||
// ClapTrap robotmp4("robot11");
|
||||
// ClapTrap robot12(robotmp4);
|
||||
//// robot12.guardGate();
|
||||
|
||||
|
||||
|
||||
ClapTrap ct(std::string("T800"));
|
||||
ScavTrap st(std::string("T1000"));
|
||||
|
||||
std::cout << ct << std::endl;
|
||||
std::cout << st << std::endl;
|
||||
|
||||
st = ScavTrap(std::string("TX"));
|
||||
|
||||
std::cout << st << std::endl;
|
||||
|
||||
st.attack(std::string("Sarah CONNOR"));
|
||||
|
||||
st.guardGate();
|
||||
st.guardGate();
|
||||
st.guardGate();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user