d03 ex01 refonte complete ok
This commit is contained in:
@@ -1,5 +1,16 @@
|
|||||||
#include "ClapTrap.hpp"
|
#include "ClapTrap.hpp"
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
|
||||||
/*********************************************
|
/*********************************************
|
||||||
* CONSTRUCTORS
|
* CONSTRUCTORS
|
||||||
*********************************************/
|
*********************************************/
|
||||||
@@ -12,7 +23,7 @@ ClapTrap::ClapTrap( std::string name ) {
|
|||||||
_energy = _dEnergy;
|
_energy = _dEnergy;
|
||||||
_attack = _dAttack;
|
_attack = _dAttack;
|
||||||
_number = getTotalNumber();
|
_number = getTotalNumber();
|
||||||
std::cout << _class << " " << _name << " nb:" << _number << " created\n";
|
std::cout << _class << " " << _name << "-" << _number << " created\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,7 +32,7 @@ ClapTrap::ClapTrap( ClapTrap const & src ) {
|
|||||||
_class = _dClass;
|
_class = _dClass;
|
||||||
*this = src;
|
*this = src;
|
||||||
_number = getTotalNumber();
|
_number = getTotalNumber();
|
||||||
std::cout << _class << " " << _name << "-" << _number << " copied from " << src._class << "-" << src._name << "-" << src._number << "\n";
|
std::cout << _class << " " << _name << "-" << _number << " copied from " << src._class << " " << src._name << "-" << src._number << "\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +88,7 @@ void ClapTrap::attack(const std::string & target) {
|
|||||||
std::ostringstream state;
|
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 "->";
|
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 (_energy > 0 && _hit > 0)
|
||||||
{
|
{
|
||||||
@@ -105,7 +116,7 @@ void ClapTrap::takeDamage(unsigned int amount) {
|
|||||||
std::ostringstream state;
|
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 "->";
|
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 (_hit > 0)
|
if (_hit > 0)
|
||||||
{
|
{
|
||||||
@@ -127,7 +138,7 @@ void ClapTrap::beRepaired(unsigned int amount) {
|
|||||||
std::ostringstream state;
|
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 "->";
|
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 (_energy > 0 && _hit > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <color.h>
|
|
||||||
|
|
||||||
class ClapTrap {
|
class ClapTrap {
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,7 @@
|
|||||||
NAME = robots
|
NAME = robots
|
||||||
|
|
||||||
CC = c++
|
CC = c++
|
||||||
#CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98
|
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98
|
||||||
CFLAGS = $(INCLUDES) -std=c++98
|
|
||||||
|
|
||||||
VPATH = $(D_SRCS)
|
VPATH = $(D_SRCS)
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ ScavTrap::ScavTrap( std::string name ) : ClapTrap(name) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScavTrap::ScavTrap( ScavTrap const & src ) : ClapTrap() {
|
ScavTrap::ScavTrap( ScavTrap const & src ) {
|
||||||
_class = _dClass;
|
_class = _dClass;
|
||||||
*this = src;
|
*this = src;
|
||||||
_number = getTotalNumber();
|
_number = getTotalNumber();
|
||||||
@@ -35,20 +35,8 @@ ScavTrap::~ScavTrap( void ) {
|
|||||||
*********************************************/
|
*********************************************/
|
||||||
|
|
||||||
ScavTrap & ScavTrap::operator=( ScavTrap const & rhs ) {
|
ScavTrap & ScavTrap::operator=( ScavTrap const & rhs ) {
|
||||||
|
ClapTrap::operator=(rhs);
|
||||||
if ( this != &rhs )
|
|
||||||
{
|
|
||||||
ClapTrap::operator=(rhs);
|
|
||||||
// _name = rhs.getName();
|
|
||||||
// _hit = rhs.getHit();
|
|
||||||
// _energy = rhs.getEnergy();
|
|
||||||
// _attack = rhs.getAttack();
|
|
||||||
// _name = rhs.getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << _class << "T " << _name << "-" << _number << " assigned\n";
|
|
||||||
return *this;
|
return *this;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*********************************************
|
/*********************************************
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -1,47 +1,34 @@
|
|||||||
|
#include "ClapTrap.hpp"
|
||||||
#include "ScavTrap.hpp"
|
#include "ScavTrap.hpp"
|
||||||
|
|
||||||
void goAttack(ClapTrap & robot1, ClapTrap & robot2) {
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
|
||||||
|
# define COLOR1 B_YELLOW
|
||||||
|
|
||||||
|
template < typename U, typename V >
|
||||||
|
void goAttack(U & robot1, V & robot2) {
|
||||||
|
std::stringstream ss;
|
||||||
|
std::string num;
|
||||||
|
ss << robot2.getNumber();
|
||||||
|
ss >> num;
|
||||||
std::cout << ".";
|
std::cout << ".";
|
||||||
robot1.attack(robot2.getName());
|
robot1.attack(robot2.getName() + "-" + num);
|
||||||
std::cout << " ";
|
std::cout << " ";
|
||||||
robot2.takeDamage(robot1.getAttack());
|
robot2.takeDamage(robot1.getAttack());
|
||||||
std::cout << " ";
|
std::cout << " ";
|
||||||
robot2.beRepaired(robot1.getAttack());
|
robot2.beRepaired(robot1.getAttack());
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void goAttack(ScavTrap & robot1, ScavTrap & robot2) {
|
|
||||||
|
|
||||||
std::cout << ".";
|
|
||||||
robot1.attack(robot2.getName());
|
|
||||||
std::cout << " ";
|
|
||||||
robot2.takeDamage(robot1.getAttack());
|
|
||||||
std::cout << " ";
|
|
||||||
robot2.beRepaired(robot1.getAttack());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void goAttack(ClapTrap & robot1, ScavTrap & robot2) {
|
|
||||||
|
|
||||||
std::cout << ".";
|
|
||||||
robot1.attack(robot2.getName());
|
|
||||||
std::cout << " ";
|
|
||||||
robot2.takeDamage(robot1.getAttack());
|
|
||||||
std::cout << " ";
|
|
||||||
robot2.beRepaired(robot1.getAttack());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void goAttack(ScavTrap & robot1, ClapTrap & robot2) {
|
|
||||||
|
|
||||||
std::cout << ".";
|
|
||||||
robot1.attack(robot2.getName());
|
|
||||||
std::cout << " ";
|
|
||||||
robot2.takeDamage(robot1.getAttack());
|
|
||||||
std::cout << " ";
|
|
||||||
robot2.beRepaired(robot1.getAttack());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@@ -52,42 +39,44 @@ int main() {
|
|||||||
ScavTrap robot3("robot3");
|
ScavTrap robot3("robot3");
|
||||||
ClapTrap robot4("robot4");
|
ClapTrap robot4("robot4");
|
||||||
|
|
||||||
std::cout << "\nassignement 1 (ClapTrap(ScavTrap)):\n";
|
std::cout << COLOR1 "\nassignement 1 (ClapTrap(ScavTrap)):\n" RESET;
|
||||||
ClapTrap robot5(robot2);
|
ClapTrap robot5(robot2);
|
||||||
// robot5.guardGate();
|
// robot5.guardGate();
|
||||||
goAttack(robot5, robot2);
|
goAttack(robot5, robot2);
|
||||||
|
|
||||||
std::cout << "\nassignement 2 (ScavTrap(ScavTrap)):\n";
|
std::cout << COLOR1 "\nassignement 2 (ScavTrap(ScavTrap)):\n" RESET;
|
||||||
ScavTrap robot6(robot2);
|
ScavTrap robot6(robot2);
|
||||||
robot6.guardGate();
|
robot6.guardGate();
|
||||||
goAttack(robot6, robot2);
|
goAttack(robot6, robot2);
|
||||||
robot2.takeDamage(robot6.getAttack());
|
robot2.takeDamage(robot6.getAttack());
|
||||||
robot6 = robot6;
|
robot6 = robot2;
|
||||||
goAttack(robot6, robot2);
|
goAttack(robot6, robot2);
|
||||||
|
|
||||||
std::cout << "\nassignement 3 (ScavTrap(ClapTrap)):\n";
|
std::cout << COLOR1 "\nassignement 3 (ScavTrap(ClapTrap)):\n" RESET;
|
||||||
// ScavTrap robot7(robot1);
|
// ScavTrap robot7(robot1);
|
||||||
|
|
||||||
std::cout << "\nassignement 4 (ClapTrap(ClapTrap)):\n";
|
std::cout << COLOR1 "\nassignement 4 (ClapTrap(ClapTrap)):\n" RESET;
|
||||||
ClapTrap robot7(robot1);
|
ClapTrap robot7(robot1);
|
||||||
goAttack(robot7, robot1);
|
goAttack(robot7, robot1);
|
||||||
|
|
||||||
std::cout << "\nwar:\n";
|
std::cout << COLOR1 "\nwar:\n" RESET;
|
||||||
// goAttack(robot1, robot2);
|
goAttack(robot1, robot2);
|
||||||
// goAttack(robot2, robot1);
|
goAttack(robot2, robot1);
|
||||||
// goAttack(robot1, robot3);
|
goAttack(robot1, robot3);
|
||||||
// robot2.guardGate();
|
robot2.guardGate();
|
||||||
// goAttack(robot1, robot4);
|
goAttack(robot1, robot4);
|
||||||
// goAttack(robot4, robot2);
|
goAttack(robot4, robot2);
|
||||||
// goAttack(robot2, robot3);
|
goAttack(robot2, robot3);
|
||||||
// goAttack(robot2, robot4);
|
goAttack(robot2, robot4);
|
||||||
// robot3.guardGate();
|
robot3.guardGate();
|
||||||
// goAttack(robot3, robot1);
|
goAttack(robot3, robot1);
|
||||||
// robot2.guardGate();
|
robot2.guardGate();
|
||||||
// goAttack(robot3, robot4);
|
goAttack(robot3, robot4);
|
||||||
// goAttack(robot2, robot1);
|
goAttack(robot2, robot1);
|
||||||
// goAttack(robot2, robot4);
|
goAttack(robot2, robot4);
|
||||||
// goAttack(robot1, robot3);
|
goAttack(robot1, robot3);
|
||||||
|
|
||||||
|
std::cout << COLOR1 "\ndestruction:\n" RESET;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
d03/ex01/robots
BIN
d03/ex01/robots
Binary file not shown.
Binary file not shown.
@@ -2,38 +2,38 @@
|
|||||||
|
|
||||||
class Base {
|
class Base {
|
||||||
public:
|
public:
|
||||||
Base() {
|
Base () : _n(1) {}
|
||||||
std::cout << "base default constructor\n";}
|
|
||||||
Base(int i) {
|
|
||||||
std::cout << "base parameters constructor\n";}
|
|
||||||
Base(Base const & src) {
|
|
||||||
std::cout << "base copy constructor\n"; *this = src;}
|
|
||||||
Base & operator=(Base const & rhs) {
|
Base & operator=(Base const & rhs) {
|
||||||
std::cout << "base assignation operator\n"; return *this;}
|
_n = rhs.getN();
|
||||||
~Base() {
|
std::cout << "base assignation operator\n";
|
||||||
std::cout << "base default destructor\n";}
|
return *this;
|
||||||
|
}
|
||||||
|
int getN() const {
|
||||||
|
return _n;
|
||||||
|
}
|
||||||
|
void putN(int i) {
|
||||||
|
_n = i;
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
int _n;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Derived : public Base {
|
class Derived : public Base {
|
||||||
public:
|
public:
|
||||||
Derived() {
|
|
||||||
std::cout << "derived default constructor\n";}
|
|
||||||
Derived(int i) {
|
|
||||||
std::cout << "derived parameters constructor\n";}
|
|
||||||
Derived(Derived const & src) {
|
|
||||||
std::cout << "derived copy constructor\n"; *this = src;}
|
|
||||||
Derived & operator=(Derived const & rhs) {
|
Derived & operator=(Derived const & rhs) {
|
||||||
std::cout << "derived assignation operator\n"; return *this;}
|
Base::operator=(rhs);
|
||||||
~Derived() {
|
std::cout << "derived assignation operator\n";
|
||||||
std::cout << "derived default destructor\n";}
|
return *this;}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
|
|
||||||
Base base1(1); std::cout << "\n";
|
Derived foo1;
|
||||||
Derived derived1(1); std::cout << "\n";
|
Derived foo2;
|
||||||
Base base2(derived1); std::cout << "\n";
|
foo2.putN(2);
|
||||||
Derived derived2(base1);
|
std::cout << foo1.getN() << " " << foo2.getN() << "\n";
|
||||||
|
foo2 = foo1;
|
||||||
|
std::cout << foo1.getN() << " " << foo2.getN() << "\n";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user