d03 ex00 messages et actions plus au point

This commit is contained in:
Hugo LAMY
2022-02-17 15:31:05 +01:00
parent 5b836dd5d4
commit 1640b9a15d
4 changed files with 108 additions and 20 deletions

View File

@@ -5,6 +5,7 @@
*/
ClapTrap::ClapTrap( void ) {
std::cout << "claptrap created without name\n";
return;
}
@@ -13,6 +14,7 @@ ClapTrap::ClapTrap( void ) {
*/
ClapTrap::~ClapTrap( void ) {
std::cout << "claptrap " << _name << " destructed\n";
return;
}
@@ -21,6 +23,7 @@ ClapTrap::~ClapTrap( void ) {
*/
ClapTrap::ClapTrap( ClapTrap const & src ) {
std::cout << "claptrap " << _name << " copied\n";
*this = src;
return;
}
@@ -30,6 +33,7 @@ ClapTrap::ClapTrap( ClapTrap const & src ) {
*/
ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
std::cout << "claptrap " << _name << " assigned\n";
if ( this != &rhs )
{
@@ -47,6 +51,7 @@ ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
*/
ClapTrap::ClapTrap( std::string name ) : _name(name) {
std::cout << "claptrap " << _name << " created\n";
_hit = 10;
_energy = 10;
_attack = 1;
@@ -67,36 +72,78 @@ int ClapTrap::getAttack() const {return _attack;}
*/
void ClapTrap::attack(const std::string & target) {
std::cout << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
_energy--;
std::ostringstream action;
std::ostringstream state;
std::cout << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET
<< " ClapTrap " << _name
<< " attacked " << target
<< ", causing " B_YELLOW << _attack << RESET
<< " points of damage!" << '\n';
state << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
action << " ClapTrap " << _name;
if (_energy && _hit)
{
_energy--;
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;
}
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
std::cout << state.str() << action.str();
}
void ClapTrap::takeDamage(unsigned int amount) {
std::cout << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
_hit -= amount;
std::ostringstream action;
std::ostringstream state;
std::cout << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET
<< " ClapTrap " << _name
<< " looses " B_YELLOW << amount << RESET
<< " points of damage :/" << '\n';
state << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
action << " ClapTrap " << _name;
if (_energy && _hit)
{
_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;
}
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
std::cout << state.str() << action.str();
}
void ClapTrap::beRepaired(unsigned int amount) {
std::cout << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
_energy--;
_hit += amount;
std::ostringstream action;
std::ostringstream state;
std::cout << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET
<< " ClapTrap " << _name
<< " repaired itself and gained " B_YELLOW << amount << RESET
<< " points of life :)" << '\n';
state << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
action << " ClapTrap " << _name;
if (_energy && _hit)
{
_energy--;
_hit += amount;
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;
}
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
std::cout << state.str() << action.str();
}

View File

@@ -2,6 +2,7 @@
# define CLAPTRAP_HPP
#include <iostream>
#include <sstream>
#include <string>
#include <color.h>

Binary file not shown.

View File

@@ -9,5 +9,45 @@ int main() {
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());
return 0;
}