diff --git a/d03/ex00/Makefile b/d03/ex00/Makefile index 930ba65..04faa47 100644 --- a/d03/ex00/Makefile +++ b/d03/ex00/Makefile @@ -4,7 +4,7 @@ # . value . or .cpp # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # -NAME = action +NAME = robots CC = c++ CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 diff --git a/d03/ex01/ClapTrap.cpp b/d03/ex01/ClapTrap.cpp new file mode 100644 index 0000000..8ba15d2 --- /dev/null +++ b/d03/ex01/ClapTrap.cpp @@ -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(); +} diff --git a/d03/ex01/ClapTrap.hpp b/d03/ex01/ClapTrap.hpp new file mode 100644 index 0000000..d836cd6 --- /dev/null +++ b/d03/ex01/ClapTrap.hpp @@ -0,0 +1,46 @@ +#ifndef CLAPTRAP_HPP +# define CLAPTRAP_HPP + +#include +#include +#include +#include + +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 + diff --git a/d03/ex01/Makefile b/d03/ex01/Makefile new file mode 100644 index 0000000..6c6c6d0 --- /dev/null +++ b/d03/ex01/Makefile @@ -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 diff --git a/d03/ex01/ScavTrap.cpp b/d03/ex01/ScavTrap.cpp new file mode 100644 index 0000000..63689e3 --- /dev/null +++ b/d03/ex01/ScavTrap.cpp @@ -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; +} diff --git a/d03/ex01/ScavTrap.hpp b/d03/ex01/ScavTrap.hpp new file mode 100644 index 0000000..e6fb39e --- /dev/null +++ b/d03/ex01/ScavTrap.hpp @@ -0,0 +1,18 @@ +#ifndef SCAVTRAP_HPP +# define SCAVTRAP_HPP + +#include +#include +#include "ClapTrap.hpp" + +class ScavTrap : public ClapTrap { + +public: + + ScavTrap(std::string name); + ~ScavTrap(); + +}; + +#endif + diff --git a/d03/ex01/color.h b/d03/ex01/color.h new file mode 100644 index 0000000..e313f5f --- /dev/null +++ b/d03/ex01/color.h @@ -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 diff --git a/d03/ex01/main.cpp b/d03/ex01/main.cpp new file mode 100644 index 0000000..e66d143 --- /dev/null +++ b/d03/ex01/main.cpp @@ -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; +}