Skip to content

Commit 5b13594

Browse files
committed
CPP_03/ex00
1 parent 3d9a144 commit 5b13594

File tree

6 files changed

+284
-27
lines changed

6 files changed

+284
-27
lines changed

CPP_03/ex00/ClapTrap.cpp

-12
This file was deleted.

CPP_03/ex00/ClapTrap.hpp

-12
This file was deleted.

CPP_03/ex00/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ CYAN = \033[0;36m
1818
RESET = \033[0m
1919

2020
# PROGRAM
21-
NAME = zombie
21+
NAME = ex00
2222

2323
# FOLDERS & FILES
24-
SRCS_DIR = ./
24+
SRCS_DIR = ./srcs/
2525
SRCS_FILES = main.cpp
26-
SRCS_FILES +=
26+
SRCS_FILES += ClapTrap.cpp
2727
SRCS := ${patsubst %, ${SRCS_DIR}%, ${SRCS_FILES}}
2828

2929
O_DIR = ./objs/

CPP_03/ex00/inc/ClapTrap.hpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ClapTrap.hpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: jmartin <jmartin@student.42lausanne.ch> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/11/08 15:21:27 by jmartin #+# #+# */
9+
/* Updated: 2022/11/08 15:21:27 by jmartin ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef CLAPTRAP_HPP
14+
# define CLAPTRAP_HPP
15+
16+
// Colors
17+
# define GREEN "\033[0;32m"
18+
# define RED "\033[0;31m"
19+
# define ORANGE "\033[0;33m"
20+
# define CYAN "\033[0;36m"
21+
# define NC "\033[0m"
22+
23+
// Libs
24+
# include <iostream>
25+
# include <string>
26+
# include <unistd.h>
27+
28+
class ClapTrap
29+
{
30+
public:
31+
ClapTrap( void );
32+
ClapTrap( std::string name );
33+
ClapTrap( ClapTrap const &instance );
34+
~ClapTrap( void );
35+
36+
ClapTrap &operator=( ClapTrap const &rhs );
37+
38+
void attack( std::string const &target );
39+
void takeDamage( unsigned int amount );
40+
void beRepaired( unsigned int amount );
41+
42+
void setName( std::string name );
43+
void setEnergyPoints( unsigned int energyPoints );
44+
void setAttackDamage( unsigned int attackDamage );
45+
void setHitPoints( unsigned int hitPoints );
46+
47+
std::string getName( void ) const;
48+
int getHitPoints( void ) const;
49+
int getEnergyPoints( void ) const;
50+
int getAttackDamage( void ) const;
51+
52+
int energyCost( void );
53+
54+
private:
55+
std::string _name;
56+
int _hitPoints;
57+
int _energyPoints;
58+
int _attackDamage;
59+
};
60+
61+
#endif

CPP_03/ex00/srcs/ClapTrap.cpp

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ClapTrap.cpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: jmartin <jmartin@student.42lausanne.ch> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/11/08 15:21:31 by jmartin #+# #+# */
9+
/* Updated: 2022/11/08 15:21:32 by jmartin ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "../inc/ClapTrap.hpp"
14+
15+
/******************************************************************************
16+
* CONSTRUCTORS / DESTRUCTORS
17+
*/
18+
19+
ClapTrap::ClapTrap(void): _name(), _hitPoints(10), _energyPoints(10), _attackDamage(0)
20+
{
21+
std::cout << "ClapTrap default constructor called" << std::endl;
22+
}
23+
24+
ClapTrap::ClapTrap(std::string name): _name(name), _hitPoints(10), _energyPoints(10), _attackDamage(0)
25+
{
26+
std::cout << "ClapTrap "
27+
<< name
28+
<< " is created."
29+
<< std::endl;
30+
}
31+
32+
ClapTrap::~ClapTrap(void) {
33+
std::cout << "ClapTrap "
34+
<< this->getName()
35+
<< " is destroyed."
36+
<< std::endl;
37+
}
38+
39+
ClapTrap::ClapTrap(ClapTrap const &instance): _name(instance.getName()), _hitPoints(instance.getHitPoints()), _energyPoints(instance.getEnergyPoints()), _attackDamage(instance.getAttackDamage())
40+
{
41+
*this = instance; // *this = ClapTrap instance
42+
std::cout << "ClapTrap copy constructor called" << std::endl;
43+
std::cout << "ClapTrap"
44+
<< " is a copy of "
45+
<< instance.getName()
46+
<< std::endl;
47+
return ;
48+
}
49+
50+
ClapTrap &ClapTrap::operator=(ClapTrap const &rhs)
51+
{
52+
std::cout << "ClapTrap assignation operator called" << std::endl;
53+
if (&rhs != this) // self-assignment check expected
54+
{
55+
this->_name = rhs.getName();
56+
this->_hitPoints = rhs.getHitPoints();
57+
this->_energyPoints = rhs.getEnergyPoints();
58+
this->_attackDamage = rhs.getAttackDamage();
59+
}
60+
return (*this);
61+
}
62+
63+
/******************************************************************************
64+
* FUNCTION MEMBER
65+
*/
66+
67+
void ClapTrap::attack(std::string const &target) {
68+
if (this->getEnergyPoints() < 1 || this->getHitPoints() < 1)
69+
{
70+
std::cout << "ClapTrap "
71+
<< this->getName()
72+
<< " has no energy left."
73+
<< std::endl;
74+
return ;
75+
}
76+
energyCost();
77+
std::cout << "ClapTrap "
78+
<< this->getName()
79+
<< " attacks "
80+
<< target
81+
<< std::endl;
82+
}
83+
84+
void ClapTrap::takeDamage(unsigned int amount) {
85+
if (this->getHitPoints() < 1)
86+
{
87+
std::cout << "ClapTrap "
88+
<< this->getName()
89+
<< " has no hit points left."
90+
<< std::endl;
91+
return ;
92+
}
93+
this->setHitPoints(this->getHitPoints() - amount);
94+
if (this->getHitPoints() < 0)
95+
this->setHitPoints(0);
96+
std::cout << "ClapTrap "
97+
<< this->getName()
98+
<< " takes "
99+
<< amount
100+
<< " points of damage!"
101+
<< "\nCurrent hit points: "
102+
<< this->getHitPoints()
103+
<< std::endl;
104+
}
105+
106+
void ClapTrap::beRepaired(unsigned int amount) {
107+
if (energyCost() == 0) {
108+
std::cout << "ClapTrap "
109+
<< this->getName()
110+
<< " has no energy left."
111+
<< std::endl;
112+
return ;
113+
}
114+
if (this->getHitPoints() < 1)
115+
{
116+
std::cout << "ClapTrap "
117+
<< this->getName()
118+
<< " has no hit points left."
119+
<< std::endl;
120+
return ;
121+
}
122+
if (amount < 1) {
123+
std::cout << "Wrong amount of hit points to repair."
124+
<< std::endl;
125+
return ;
126+
}
127+
this->setHitPoints(this->getHitPoints() + amount);
128+
std::cout << "ClapTrap "
129+
<< this->getName()
130+
<< " is repaired by "
131+
<< amount
132+
<< " points!"
133+
<< "\nCurrent hit points: "
134+
<< this->getHitPoints()
135+
<< std::endl;
136+
}
137+
138+
int ClapTrap::energyCost(void) {
139+
if (this->getEnergyPoints() < 1)
140+
this->setEnergyPoints(0);
141+
else
142+
this->setEnergyPoints(this->getEnergyPoints() - 1);
143+
return (this->getEnergyPoints());
144+
}
145+
146+
/******************************************************************************
147+
* GETTERS / SETTERS
148+
*/
149+
150+
std::string ClapTrap::getName(void) const {
151+
return (this->_name);
152+
}
153+
154+
int ClapTrap::getHitPoints(void) const {
155+
return (this->_hitPoints);
156+
}
157+
158+
int ClapTrap::getEnergyPoints(void) const {
159+
return (this->_energyPoints);
160+
}
161+
162+
int ClapTrap::getAttackDamage(void) const {
163+
return (this->_attackDamage);
164+
}
165+
166+
void ClapTrap::setName(std::string name) {
167+
this->_name = name;
168+
}
169+
170+
void ClapTrap::setHitPoints(unsigned int hitPoints) {
171+
this->_hitPoints = hitPoints;
172+
}
173+
174+
void ClapTrap::setEnergyPoints(unsigned int energyPoints) {
175+
this->_energyPoints = energyPoints;
176+
}
177+
178+
void ClapTrap::setAttackDamage(unsigned int attackDamage) {
179+
this->_attackDamage = attackDamage;
180+
}
181+

CPP_03/ex00/main.cpp renamed to CPP_03/ex00/srcs/main.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,42 @@
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "../inc/ClapTrap.hpp"
14+
15+
static void tic_tac(int usleep_time, int duration)
16+
{
17+
for (int i = 0; i < duration; i++)
18+
{
19+
std::cout << "." << std::endl;
20+
usleep(usleep_time);
21+
}
22+
}
23+
24+
int main(void)
25+
{
26+
ClapTrap nameless;
27+
tic_tac(500000, 1);
28+
29+
ClapTrap one("Jotaro");
30+
tic_tac(500000, 1);
31+
32+
ClapTrap two("polnareff");
33+
tic_tac(500000, 1);
34+
35+
ClapTrap duplicated(one);
36+
tic_tac(500000, 1);
37+
38+
while (one.getEnergyPoints() > 0)
39+
one.attack("Dio");
40+
tic_tac(500000, 2);
41+
one.beRepaired(2);
42+
tic_tac(500000, 2);
43+
one.takeDamage(17);
44+
tic_tac(500000, 2);
45+
46+
two.attack("Dio");
47+
tic_tac(500000, 2);
48+
49+
two.beRepaired(2);
50+
tic_tac(500000, 2);
51+
}

0 commit comments

Comments
 (0)