d03 ex01 debut
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
# . value . or .cpp #
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
|
||||
NAME = action
|
||||
NAME = robots
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98
|
||||
|
||||
155
d03/ex01/ClapTrap.cpp
Normal file
155
d03/ex01/ClapTrap.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
/*
|
||||
* statics
|
||||
*/
|
||||
|
||||
int ClapTrap::_number = 0;
|
||||
|
||||
/*
|
||||
* default/parametric constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( void ) {
|
||||
std::cout << "claptrap created without name\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* destructor
|
||||
*/
|
||||
|
||||
ClapTrap::~ClapTrap( void ) {
|
||||
std::cout << "claptrap " << _name << " destructed\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* copy constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( ClapTrap const & src ) {
|
||||
std::cout << "claptrap " << _name << " copied\n";
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* assignement operator
|
||||
*/
|
||||
|
||||
ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
|
||||
std::cout << "claptrap " << _name << " assigned\n";
|
||||
|
||||
if ( this != &rhs )
|
||||
{
|
||||
this->_hit = rhs.getHit();
|
||||
this->_energy = rhs.getEnergy();
|
||||
this->_attack = rhs.getAttack();
|
||||
}
|
||||
|
||||
return *this;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* constructor
|
||||
*/
|
||||
|
||||
ClapTrap::ClapTrap( std::string name ) : _name(name) {
|
||||
std::cout << "claptrap " << _name << " created\n";
|
||||
_hit = 10;
|
||||
_energy = 10;
|
||||
_attack = 1;
|
||||
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;}
|
||||
|
||||
/*
|
||||
* robots
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
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::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;
|
||||
|
||||
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::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;
|
||||
|
||||
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();
|
||||
}
|
||||
46
d03/ex01/ClapTrap.hpp
Normal file
46
d03/ex01/ClapTrap.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef CLAPTRAP_HPP
|
||||
# define CLAPTRAP_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <color.h>
|
||||
|
||||
class ClapTrap {
|
||||
|
||||
public:
|
||||
|
||||
ClapTrap( void ); // default/parametric constructor
|
||||
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 _name;
|
||||
int _hit;
|
||||
int _energy;
|
||||
int _attack;
|
||||
|
||||
void _getNumber();
|
||||
void _increaseNumber();
|
||||
|
||||
private:
|
||||
|
||||
static int _number;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
64
d03/ex01/Makefile
Normal file
64
d03/ex01/Makefile
Normal file
@@ -0,0 +1,64 @@
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
# . 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
|
||||
|
||||
D_HEADERS = .
|
||||
HEADERS = ClapTrap.hpp \
|
||||
ScavTrap.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
|
||||
22
d03/ex01/ScavTrap.cpp
Normal file
22
d03/ex01/ScavTrap.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "ScavTrap.hpp"
|
||||
|
||||
ScavTrap::ScavTrap( std::string name ) {
|
||||
|
||||
_name = name;
|
||||
_hit = 100;
|
||||
_energy = 50;
|
||||
_attack = 20;
|
||||
|
||||
std::cout << "scavtrap " << _name << " created\n";
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* destructor
|
||||
*/
|
||||
|
||||
ScavTrap::~ScavTrap( void ) {
|
||||
std::cout << "scavtrap " << _name << " destructed\n";
|
||||
return;
|
||||
}
|
||||
18
d03/ex01/ScavTrap.hpp
Normal file
18
d03/ex01/ScavTrap.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef SCAVTRAP_HPP
|
||||
# define SCAVTRAP_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
class ScavTrap : public ClapTrap {
|
||||
|
||||
public:
|
||||
|
||||
ScavTrap(std::string name);
|
||||
~ScavTrap();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
24
d03/ex01/color.h
Normal file
24
d03/ex01/color.h
Normal 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
|
||||
53
d03/ex01/main.cpp
Normal file
53
d03/ex01/main.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "ScavTrap.hpp"
|
||||
|
||||
int main() {
|
||||
|
||||
ClapTrap robot1("robot1");
|
||||
ScavTrap robot2("robot2");
|
||||
|
||||
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());
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user