CPP03 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
ClapTrap.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ClapTrap.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/05/08 12:48:46 by kamitsui #+# #+# */
9/* Updated: 2025/05/09 03:00:37 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
21#include "ClapTrap.hpp"
22#include <iostream>
23
27ClapTrap::ClapTrap() : name("Default"), hitPoints(10), energyPoints(10), attackDamage(0) {
28 std::cout << "ClapTrap Default constructor called" << std::endl;
29}
30
35ClapTrap::ClapTrap(const std::string &name) : name(name), hitPoints(10), energyPoints(10), attackDamage(0) {
36 std::cout << "ClapTrap String constructor called for " << name << std::endl;
37}
38
44 : name(other.name), hitPoints(other.hitPoints), energyPoints(other.energyPoints), attackDamage(other.attackDamage) {
45 std::cout << "ClapTrap Copy constructor called for " << name << std::endl;
46}
47
51ClapTrap::~ClapTrap() { std::cout << "ClapTrap Destructor called for " << name << std::endl; }
52
59 std::cout << "ClapTrap Assignation operator called for " << name << std::endl;
60 if (this != &other) {
61 name = other.name;
62 hitPoints = other.hitPoints;
63 energyPoints = other.energyPoints;
64 attackDamage = other.attackDamage;
65 }
66 return *this;
67}
68
73void ClapTrap::attack(const std::string &target) {
74 if (hitPoints > 0 && energyPoints > 0) {
75 std::cout << "ClapTrap " << name << " attacks " << target << ", causing " << attackDamage
76 << " points of damage!" << std::endl;
77 energyPoints--;
78 } else if (hitPoints == 0) {
79 std::cout << "ClapTrap " << name << " is out of hit points and cannot attack!" << std::endl;
80 } else {
81 std::cout << "ClapTrap " << name << " is out of energy points and cannot attack!" << std::endl;
82 }
83}
84
89void ClapTrap::takeDamage(unsigned int amount) {
90 if (hitPoints == 0) {
91 std::cout << "ClapTrap " << name << " is already out of hit points!" << std::endl;
92 return;
93 }
94 std::cout << "ClapTrap " << name << " takes " << amount << " points of damage!" << std::endl;
95 if (amount >= hitPoints) {
96 hitPoints = 0;
97 std::cout << "ClapTrap " << name << " has died!" << std::endl;
98 } else {
99 hitPoints -= amount;
100 std::cout << "ClapTrap " << name << " has " << hitPoints << " hit points remaining." << std::endl;
101 }
102}
103
108void ClapTrap::beRepaired(unsigned int amount) {
109 if (hitPoints > 0 && energyPoints > 0) {
110 std::cout << "ClapTrap " << name << " repairs itself, regaining " << amount << " hit points!" << std::endl;
111 hitPoints += amount;
112 energyPoints--;
113 std::cout << "ClapTrap " << name << " now has " << hitPoints << " hit points and " << energyPoints
114 << " energy points." << std::endl;
115 } else if (hitPoints == 0) {
116 std::cout << "ClapTrap " << name << " cannot be repaired as it has no hit points!" << std::endl;
117 } else {
118 std::cout << "ClapTrap " << name << " is out of energy points and cannot repair!" << std::endl;
119 }
120}
121
126const std::string &ClapTrap::getName() const { return name; }
127
132unsigned int ClapTrap::getHitPoints() const { return hitPoints; }
133
138unsigned int ClapTrap::getEnergyPoints() const { return energyPoints; }
139
144unsigned int ClapTrap::getAttackDamage() const { return attackDamage; }
Represents a basic robot character.
Definition ClapTrap.hpp:34
void beRepaired(unsigned int amount)
beRepaired function implementation.
Definition ClapTrap.cpp:108
unsigned int getHitPoints() const
Gets the current hit points of the ClapTrap.
Definition ClapTrap.cpp:132
unsigned int getAttackDamage() const
Gets the attack damage of the ClapTrap.
Definition ClapTrap.cpp:144
ClapTrap()
Default constructor implementation.
Definition ClapTrap.cpp:27
void takeDamage(unsigned int amount)
takeDamage function implementation.
Definition ClapTrap.cpp:89
void attack(const std::string &target)
Attack function implementation.
Definition ClapTrap.cpp:73
ClapTrap & operator=(const ClapTrap &other)
Copy assignment operator implementation.
Definition ClapTrap.cpp:58
~ClapTrap()
Destructor implementation.
Definition ClapTrap.cpp:51
const std::string & getName() const
Gets the name of the ClapTrap.
Definition ClapTrap.cpp:126
unsigned int getEnergyPoints() const
Gets the current energy points of the ClapTrap.
Definition ClapTrap.cpp:138
T endl(T... args)
Header file for the ClapTrap class.