d03 ex03 introduction template pour main

This commit is contained in:
Hugo LAMY
2022-02-19 10:03:39 +01:00
parent a2bd7db6a9
commit 817fac957b
10 changed files with 476 additions and 0 deletions

170
d03/ex03/ClapTrap.cpp Normal file
View File

@@ -0,0 +1,170 @@
#include "ClapTrap.hpp"
/*
* statics
*/
int ClapTrap::_totalNumber = 0;
/*
* default/parametric constructor
*/
ClapTrap::ClapTrap( void ) {
std::cout << "claptrap created without name\n";
return;
}
/*
* destructor
*/
ClapTrap::~ClapTrap( void ) {
std::cout << _class << " " << _name << " destructed\n";
return;
}
/*
* copy constructor
*/
ClapTrap::ClapTrap( ClapTrap const & src ) {
std::cout << _class << " " << _name << " copied\n";
*this = src;
return;
}
/*
* assignement operator
*/
ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
if ( this != &rhs )
{
this->_hit = rhs.getHit();
this->_energy = rhs.getEnergy();
this->_attack = rhs.getAttack();
}
std::cout << _class << " " << _name << " assigned\n";
return *this;
}
/*
* constructor
*/
ClapTrap::ClapTrap( std::string name ) : _name(name) {
ClapTrap::_increaseNumber();
_class = "ClapTrap";
_hit = 10;
_energy = 10;
_attack = 1;
_number = _getNumber();
std::cout << _class << " " << _name << " created with number " << _number << "\n";
return;
}
/*
* getters
*/
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
*/
void ClapTrap::attack(const std::string & target) {
std::ostringstream action;
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 "->";
action << _class << " " << _name;
if (_energy > 0 && _hit > 0)
{
_energy--;
if (_energy < 0)
_energy = 0;
action << " attacked " << target << ", causing " B_YELLOW << _attack << RESET << " points of damage" << '\n';
}
else
{
_attack = 0;
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;
std::cout << state.str() << action.str();
}
void ClapTrap::takeDamage(unsigned int amount) {
std::ostringstream action;
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 "->";
action << _class << " " << _name;
if (_energy > 0 && _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";
}
state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
std::cout << state.str() << action.str();
}
void ClapTrap::beRepaired(unsigned int amount) {
std::ostringstream action;
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 "->";
action << _class << " " << _name;
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';
}
else
{
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();
}

49
d03/ex03/ClapTrap.hpp Normal file
View File

@@ -0,0 +1,49 @@
#ifndef CLAPTRAP_HPP
# define CLAPTRAP_HPP
#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( void ); // destructor
ClapTrap & operator=( ClapTrap const & rhs ); // assignement operator
void attack(const std::string & target);
void takeDamage(unsigned int amount);
void beRepaired(unsigned int amount);
std::string getName() const;
int getHit() const;
int getEnergy() const;
int getAttack() const;
protected:
std::string const _name;
std::string _class;
int _hit;
int _energy;
int _attack;
int _number;
int _getNumber();
void _increaseNumber();
private:
ClapTrap( void ); // default/parametric constructor
static int _totalNumber;
};
#endif

30
d03/ex03/FragTrap.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "FragTrap.hpp"
FragTrap::FragTrap( std::string name ) : ClapTrap(name) {
_class = "FragTrap";
_hit = 100;
_energy = 100;
_attack = 30;
std::cout << _class << " " << FragTrap::_name << " created with number " << _number << "\n";
return;
}
/*
* destructor
*/
FragTrap::~FragTrap( void ) {
std::cout << _class << " " << FragTrap::_name << " destructed\n";
return;
}
/*
* special capacity
*/
void FragTrap::highFivesGuys() {
std::cout << _class << " " << FragTrap::_name << " wait for a high fives, common guys\n";
}

19
d03/ex03/FragTrap.hpp Normal file
View File

@@ -0,0 +1,19 @@
#ifndef FRAGTRAP_HPP
# define FRAGTRAP_HPP
#include <iostream>
#include <string>
#include "ClapTrap.hpp"
class FragTrap : public ClapTrap {
public:
FragTrap(std::string name);
~FragTrap( void );
void highFivesGuys();
};
#endif

66
d03/ex03/Makefile Normal file
View File

@@ -0,0 +1,66 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . name = value . name is case sensitive #
# VARIABLES . or name = value \ . use VPATH only for .c #
# . value . or .cpp #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = robots
CC = c++
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98
VPATH = $(D_SRCS)
LIBS =
INCLUDES = -I$(D_HEADERS)
D_SRCS = .
SRCS = main.cpp \
ClapTrap.cpp \
ScavTrap.cpp \
FragTrap.cpp
D_HEADERS = .
HEADERS = ClapTrap.hpp \
ScavTrap.hpp \
FragTrap.hpp
D_OBJS = builds
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)
RM_D_OBJS = rm -rf $(D_OBJS)
ifeq "$(D_OBJS)" "."
RM_D_OBJS =
endif
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . target: prerequisites . $@ : target #
# RULES . recipe . $< : 1st prerequisite #
# . recipe . $^ : all prerequisites #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
all: $(NAME)
$(D_OBJS)/%.o: %.cpp | $(D_OBJS)
$(CC) $(CFLAGS) -c $< -o $@
$(D_OBJS):
mkdir $@
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
$(RM_D_OBJS)
re: fclean all
.PHONY : all clean fclean re bonus run valgrind

30
d03/ex03/ScavTrap.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "ScavTrap.hpp"
ScavTrap::ScavTrap( std::string name ) : ClapTrap(name) {
_class = "ScavTrap";
_hit = 100;
_energy = 50;
_attack = 20;
std::cout << _class << " " << ScavTrap::_name << " created with number " << _number << "\n";
return;
}
/*
* destructor
*/
ScavTrap::~ScavTrap( void ) {
std::cout << _class << " " << ScavTrap::_name << " destructed\n";
return;
}
/*
* special capacity
*/
void ScavTrap::guardGate() {
std::cout << _class << " " << ScavTrap::_name << " entered special mode Gate Keeper\n";
}

20
d03/ex03/ScavTrap.hpp Normal file
View File

@@ -0,0 +1,20 @@
#ifndef SCAVTRAP_HPP
# define SCAVTRAP_HPP
#include <iostream>
#include <string>
#include "ClapTrap.hpp"
class ScavTrap : public ClapTrap {
public:
ScavTrap(std::string name);
~ScavTrap();
void guardGate();
};
#endif

24
d03/ex03/color.h Normal file
View File

@@ -0,0 +1,24 @@
#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

68
d03/ex03/main.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include "ScavTrap.hpp"
#include "FragTrap.hpp"
#include <iostream>
template < typename U, typename V >
void goAttack(U & robot1, V & robot2) {
std::cout << ".";
robot1.attack(robot2.getName());
std::cout << " ";
robot2.takeDamage(robot1.getAttack());
std::cout << " ";
robot2.beRepaired(robot1.getAttack());
}
int main() {
FragTrap robot1("robot1");
ClapTrap robot2("robot2");
ScavTrap robot3("robot3");
ScavTrap robot4("robot4");
ClapTrap robot5("robot5");
ScavTrap robot6("robot6");
FragTrap robot7("robot7");
FragTrap robot8("robot8");
ClapTrap robot9("robot9");
goAttack(robot1, robot2);
goAttack(robot6, robot3);
goAttack(robot6, robot7);
goAttack(robot8, robot1);
goAttack(robot1, robot3);
robot4.guardGate();
goAttack(robot1, robot4);
goAttack(robot9, robot2);
goAttack(robot1, robot5);
goAttack(robot3, robot2);
goAttack(robot4, robot8);
robot8.highFivesGuys();
goAttack(robot2, robot7);
goAttack(robot3, robot2);
goAttack(robot1, robot2);
goAttack(robot8, robot4);
goAttack(robot2, robot4);
robot3.guardGate();
goAttack(robot3, robot1);
goAttack(robot5, robot7);
goAttack(robot1, robot9);
goAttack(robot9, robot6);
goAttack(robot9, robot2);
robot4.guardGate();
robot1.highFivesGuys();
robot7.highFivesGuys();
goAttack(robot3, robot4);
goAttack(robot5, robot2);
goAttack(robot7, robot6);
goAttack(robot2, robot1);
robot7.highFivesGuys();
goAttack(robot2, robot4);
goAttack(robot9, robot6);
goAttack(robot3, robot2);
goAttack(robot6, robot5);
goAttack(robot4, robot9);
goAttack(robot7, robot6);
return 0;
}

BIN
d03/ex03/robots Executable file

Binary file not shown.