d03 ex01 inheritence ok, avec ref a parent constructor
This commit is contained in:
@@ -4,13 +4,14 @@
|
||||
* statics
|
||||
*/
|
||||
|
||||
int ClapTrap::_number = 0;
|
||||
int ClapTrap::_totalNumber = 0;
|
||||
|
||||
/*
|
||||
* default/parametric constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( void ) {
|
||||
|
||||
std::cout << "claptrap created without name\n";
|
||||
return;
|
||||
}
|
||||
@@ -20,7 +21,7 @@ ClapTrap::ClapTrap( void ) {
|
||||
*/
|
||||
|
||||
ClapTrap::~ClapTrap( void ) {
|
||||
std::cout << "claptrap " << _name << " destructed\n";
|
||||
std::cout << _class << " " << _name << " destructed\n";
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -29,7 +30,7 @@ ClapTrap::~ClapTrap( void ) {
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( ClapTrap const & src ) {
|
||||
std::cout << "claptrap " << _name << " copied\n";
|
||||
std::cout << _class << " " << _name << " copied\n";
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
@@ -39,7 +40,6 @@ ClapTrap::ClapTrap( ClapTrap const & src ) {
|
||||
*/
|
||||
|
||||
ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
|
||||
std::cout << "claptrap " << _name << " assigned\n";
|
||||
|
||||
if ( this != &rhs )
|
||||
{
|
||||
@@ -48,6 +48,7 @@ ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
|
||||
this->_attack = rhs.getAttack();
|
||||
}
|
||||
|
||||
std::cout << _class << " " << _name << " assigned\n";
|
||||
return *this;
|
||||
|
||||
}
|
||||
@@ -57,10 +58,16 @@ ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( std::string name ) : _name(name) {
|
||||
std::cout << "claptrap " << _name << " created\n";
|
||||
|
||||
ClapTrap::_increaseNumber();
|
||||
|
||||
_class = "ClapTrap";
|
||||
_hit = 10;
|
||||
_energy = 10;
|
||||
_attack = 1;
|
||||
_number = _getNumber();
|
||||
|
||||
std::cout << _class << " " << _name << " created with number " << _number << "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -72,6 +79,8 @@ std::string ClapTrap::getName() const {return _name;}
|
||||
int ClapTrap::getHit() const {return _hit;}
|
||||
int ClapTrap::getEnergy() const {return _energy;}
|
||||
int ClapTrap::getAttack() const {return _attack;}
|
||||
int ClapTrap::_getNumber() {return ClapTrap::_totalNumber;}
|
||||
void ClapTrap::_increaseNumber() {ClapTrap::_totalNumber++;}
|
||||
|
||||
/*
|
||||
* robots
|
||||
@@ -82,21 +91,23 @@ 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;
|
||||
|
||||
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;
|
||||
@@ -108,20 +119,22 @@ 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;
|
||||
|
||||
if (_energy && _hit)
|
||||
if (_energy > 0 && _hit > 0)
|
||||
{
|
||||
_hit -= amount;
|
||||
action << " looses " B_YELLOW << amount << RESET << " points of damage :/" << '\n';
|
||||
if (_hit < 0)
|
||||
_hit = 0;
|
||||
action << " looses " B_YELLOW << amount << RESET << " hit points" << '\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 (_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";
|
||||
}
|
||||
|
||||
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
|
||||
@@ -133,21 +146,23 @@ 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;
|
||||
|
||||
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;
|
||||
|
||||
@@ -29,16 +29,18 @@ public:
|
||||
protected:
|
||||
|
||||
std::string _name;
|
||||
std::string _class;
|
||||
int _hit;
|
||||
int _energy;
|
||||
int _attack;
|
||||
int _number;
|
||||
|
||||
void _getNumber();
|
||||
int _getNumber();
|
||||
void _increaseNumber();
|
||||
|
||||
private:
|
||||
|
||||
static int _number;
|
||||
static int _totalNumber;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
#include "ScavTrap.hpp"
|
||||
|
||||
ScavTrap::ScavTrap( std::string name ) {
|
||||
ScavTrap::ScavTrap( std::string name ) : ClapTrap(name) {
|
||||
|
||||
_name = name;
|
||||
_class = "ScavTrap";
|
||||
_hit = 100;
|
||||
_energy = 50;
|
||||
_attack = 20;
|
||||
|
||||
std::cout << "scavtrap " << _name << " created\n";
|
||||
std::cout << _class << " " << _name << " created with number " << _number << "\n";
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -17,6 +18,6 @@ ScavTrap::ScavTrap( std::string name ) {
|
||||
*/
|
||||
|
||||
ScavTrap::~ScavTrap( void ) {
|
||||
std::cout << "scavtrap " << _name << " destructed\n";
|
||||
std::cout << _class << " " << _name << " destructed\n";
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,53 +1,56 @@
|
||||
#include "ScavTrap.hpp"
|
||||
|
||||
void goAttack(ClapTrap & robot1, ClapTrap & robot2) {
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
|
||||
}
|
||||
|
||||
void goAttack(ScavTrap & robot1, ScavTrap & robot2) {
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
|
||||
}
|
||||
|
||||
void goAttack(ClapTrap & robot1, ScavTrap & robot2) {
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
|
||||
}
|
||||
|
||||
void goAttack(ScavTrap & robot1, ClapTrap & robot2) {
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
ClapTrap robot1("robot1");
|
||||
ScavTrap robot2("robot2");
|
||||
ScavTrap robot3("robot3");
|
||||
ClapTrap robot4("robot4");
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
|
||||
robot2.attack(robot1.getName());
|
||||
robot1.takeDamage(robot2.getAttack());
|
||||
robot1.beRepaired(robot2.getAttack());
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
|
||||
robot2.attack(robot1.getName());
|
||||
robot1.takeDamage(robot2.getAttack());
|
||||
robot1.beRepaired(robot2.getAttack());
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
|
||||
robot2.attack(robot1.getName());
|
||||
robot1.takeDamage(robot2.getAttack());
|
||||
robot1.beRepaired(robot2.getAttack());
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
|
||||
robot2.attack(robot1.getName());
|
||||
robot1.takeDamage(robot2.getAttack());
|
||||
robot1.beRepaired(robot2.getAttack());
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
|
||||
robot2.attack(robot1.getName());
|
||||
robot1.takeDamage(robot2.getAttack());
|
||||
robot1.beRepaired(robot2.getAttack());
|
||||
|
||||
robot1.attack(robot2.getName());
|
||||
robot2.takeDamage(robot1.getAttack());
|
||||
robot2.beRepaired(robot1.getAttack());
|
||||
goAttack(robot1, robot2);
|
||||
goAttack(robot2, robot1);
|
||||
goAttack(robot1, robot3);
|
||||
goAttack(robot1, robot4);
|
||||
goAttack(robot4, robot2);
|
||||
goAttack(robot2, robot3);
|
||||
goAttack(robot2, robot4);
|
||||
goAttack(robot3, robot1);
|
||||
goAttack(robot3, robot4);
|
||||
goAttack(robot2, robot1);
|
||||
goAttack(robot2, robot4);
|
||||
goAttack(robot1, robot3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
d03/ex01/robots
Executable file
BIN
d03/ex01/robots
Executable file
Binary file not shown.
Reference in New Issue
Block a user