first commit
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
ClapTrap::ClapTrap(): _name("default"), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
|
||||
{
|
||||
std::cout << "ClapTrap Default Constructor called" << std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(const ClapTrap ©)
|
||||
{
|
||||
std::cout << "ClapTrap Copy Constructor called" << std::endl;
|
||||
*this = copy;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(std::string name): _name(name), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
|
||||
{
|
||||
std::cout << "ClapTrap Constructor called" <<std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::~ClapTrap()
|
||||
{
|
||||
std::cout << "ClapTrap Deconstructor" << std::endl;
|
||||
}
|
||||
|
||||
ClapTrap &ClapTrap::operator=(const ClapTrap &src)
|
||||
{
|
||||
this->_name = src._name;
|
||||
this->_hit_pts = src._hit_pts;
|
||||
this->_energy_pts = src._energy_pts;
|
||||
this->_attack_dmg = src._attack_dmg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void ClapTrap::takeDamage(unsigned int amount)
|
||||
{
|
||||
if (this->_hit_pts > amount)
|
||||
this->_hit_pts -= amount;
|
||||
else if (this->_hit_pts > 0)
|
||||
this->_hit_pts = 0;
|
||||
else
|
||||
{
|
||||
std::cout << "ClapTrap " << this->_name << " is already dead" << std::endl;
|
||||
return ;
|
||||
}
|
||||
std::cout << "ClapTrap " << this->_name << " was attacked and lost " << amount << " hp, he now has " << this->_hit_pts << " hp." << std::endl;
|
||||
}
|
||||
|
||||
void ClapTrap::beRepaired(unsigned int amount)
|
||||
{
|
||||
if (this->_energy_pts > 0 && this->_hit_pts > 0 && this->_hit_pts + amount <= 10)
|
||||
{
|
||||
this->_hit_pts += amount;
|
||||
std::cout << "ClapTrap " << this->_name << " repaired itself and get " << amount << " hit points, he has " << this->_hit_pts << " hp" << std::endl;
|
||||
this->_energy_pts--;
|
||||
}
|
||||
else if (this->_energy_pts == 0)
|
||||
std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough energy" << std::endl;
|
||||
else if (this->_hit_pts == 0)
|
||||
std::cout << "ClapTrap " << this->_name << " is not able to repair, because he doesn't have enough hit points." << std::endl;
|
||||
else
|
||||
std::cout << "ClapTrap " << this->_name << " can't be repaired to have more than 10 hit points." << std::endl;
|
||||
}
|
||||
|
||||
void ClapTrap::attack(const std::string &target)
|
||||
{
|
||||
if (this->_energy_pts > 0 && this->_hit_pts > 0)
|
||||
{
|
||||
std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl;
|
||||
this->_energy_pts--;
|
||||
}
|
||||
else if (this->_energy_pts == 0)
|
||||
std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl;
|
||||
else
|
||||
std::cout << "ClapTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef CLAPTRAP_HPP
|
||||
# define CLAPTRAP_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
class ClapTrap {
|
||||
|
||||
protected:
|
||||
std::string _name;
|
||||
unsigned int _hit_pts;
|
||||
unsigned int _energy_pts;
|
||||
unsigned int _attack_dmg;
|
||||
|
||||
public:
|
||||
ClapTrap();
|
||||
ClapTrap(const ClapTrap ©);
|
||||
ClapTrap(std::string name);
|
||||
|
||||
virtual ~ClapTrap();
|
||||
|
||||
ClapTrap &operator=(const ClapTrap &src);
|
||||
|
||||
void attack(const std::string &target);
|
||||
void takeDamage(unsigned int amount);
|
||||
void beRepaired(unsigned int amount);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
#include "FragTrap.hpp"
|
||||
|
||||
FragTrap::FragTrap(): ClapTrap()
|
||||
{
|
||||
this->_hit_pts = 100;
|
||||
this->_energy_pts = 100;
|
||||
this->_attack_dmg = 30;
|
||||
std::cout << "FragTrap Default Constructor called" << std::endl;
|
||||
}
|
||||
|
||||
FragTrap::FragTrap(const FragTrap ©): ClapTrap(copy)
|
||||
{
|
||||
std::cout << "FragTrap Copy Constructor called" << std::endl;
|
||||
}
|
||||
|
||||
FragTrap::FragTrap(std::string name): ClapTrap(name)
|
||||
{
|
||||
this->_hit_pts = 100;
|
||||
this->_energy_pts = 100;
|
||||
this->_attack_dmg = 30;
|
||||
std::cout << "FragTrap Constructor " << this->_name << " called" << std::endl;
|
||||
}
|
||||
|
||||
FragTrap::~FragTrap()
|
||||
{
|
||||
std::cout << "FragTrap Deconstructor " << this->_name << " called" << std::endl;
|
||||
}
|
||||
|
||||
FragTrap &FragTrap::operator=(const FragTrap &src)
|
||||
{
|
||||
std::cout << "FragTrap Assignation operator called" << std::endl;
|
||||
this->_name = src._name;
|
||||
this->_hit_pts = src._hit_pts;
|
||||
this->_energy_pts = src._energy_pts;
|
||||
this->_attack_dmg = src._attack_dmg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void FragTrap::attack(const std::string &target)
|
||||
{
|
||||
if (this->_energy_pts > 0 && this->_hit_pts > 0)
|
||||
{
|
||||
std::cout << "FragTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl;
|
||||
this->_energy_pts--;
|
||||
}
|
||||
else if (this->_energy_pts == 0)
|
||||
std::cout << "FragTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl;
|
||||
else
|
||||
std::cout << "FragTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl;
|
||||
}
|
||||
|
||||
void FragTrap::highFivesGuys() {
|
||||
std::cout << "FragTrap " << this->_name << " requests a positive high-five! ✋" << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef FRAGTRAP_HPP
|
||||
# define FRAGTRAP_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
class FragTrap: public ClapTrap {
|
||||
public:
|
||||
FragTrap();
|
||||
FragTrap(const FragTrap ©);
|
||||
FragTrap(std::string name);
|
||||
|
||||
virtual ~FragTrap();
|
||||
|
||||
FragTrap &operator=(const FragTrap &src);
|
||||
|
||||
void attack(const std::string &target);
|
||||
void highFivesGuys(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2025/07/01 13:54:05 by lfirmin #+# #+# #
|
||||
# Updated: 2025/07/01 16:43:28 by lfirmin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
C++ = c++
|
||||
C++_FLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
OBJDIR = obj
|
||||
INFILES = main.cpp\
|
||||
ClapTrap.cpp\
|
||||
ScavTrap.cpp\
|
||||
FragTrap.cpp\
|
||||
|
||||
OBJFILES = $(addprefix $(OBJDIR)/, $(INFILES:.cpp=.o))
|
||||
|
||||
NAME = FragTrap
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(OBJDIR):
|
||||
@echo "📁 Creating obj directory..."
|
||||
@mkdir -p $(OBJDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
|
||||
@echo "🧠 Compiling $< ..."
|
||||
@$(C++) $(C++_FLAGS) -c $< -o $@
|
||||
@echo "✅ $@ ready!"
|
||||
|
||||
$(NAME): $(OBJFILES)
|
||||
@echo "🔗 Linking $(NAME) ..."
|
||||
@$(C++) $(C++_FLAGS) $(OBJFILES) -o $(NAME)
|
||||
@echo "🎉 $(NAME) is ready !"
|
||||
|
||||
clean:
|
||||
@echo "🧹 Cleaning object files..."
|
||||
@rm -rf $(OBJDIR)
|
||||
@echo "✨ Objects cleaned!"
|
||||
|
||||
fclean: clean
|
||||
@echo "🗑️ Removing $(NAME)..."
|
||||
@rm -f $(NAME)
|
||||
@echo "💀 Full clean complete!"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
@@ -0,0 +1,57 @@
|
||||
#include "ScavTrap.hpp"
|
||||
|
||||
ScavTrap::ScavTrap(): ClapTrap()
|
||||
{
|
||||
this->_hit_pts = 100;
|
||||
this->_energy_pts = 50;
|
||||
this->_attack_dmg = 20;
|
||||
this->_guarding_gate = false;
|
||||
std::cout << "ScavTrap Default Constructor called" << std::endl;
|
||||
}
|
||||
|
||||
ScavTrap::ScavTrap(const ScavTrap ©): ClapTrap(copy)
|
||||
{
|
||||
this->_guarding_gate = copy._guarding_gate;
|
||||
std::cout << "ScavTrap Copy Constructor called" << std::endl;
|
||||
}
|
||||
|
||||
ScavTrap::ScavTrap(std::string name): ClapTrap(name)
|
||||
{
|
||||
this->_hit_pts = 100;
|
||||
this->_energy_pts = 50;
|
||||
this->_attack_dmg = 20;
|
||||
this->_guarding_gate = false;
|
||||
std::cout << "ScavTrap Constructor " << this->_name << " called" << std::endl;
|
||||
}
|
||||
|
||||
ScavTrap::~ScavTrap()
|
||||
{
|
||||
std::cout << "ScavTrap Deconstructor " << this->_name << " called" << std::endl;
|
||||
}
|
||||
|
||||
ScavTrap &ScavTrap::operator=(const ScavTrap &src)
|
||||
{
|
||||
std::cout << "ScavTrap Assignation operator called" << std::endl;
|
||||
this->_name = src._name;
|
||||
this->_hit_pts = src._hit_pts;
|
||||
this->_energy_pts = src._energy_pts;
|
||||
this->_attack_dmg = src._attack_dmg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void ScavTrap::attack(const std::string &target)
|
||||
{
|
||||
if (this->_energy_pts > 0 && this->_hit_pts > 0)
|
||||
{
|
||||
std::cout << "ScavTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " damage!" << std::endl;
|
||||
this->_energy_pts--;
|
||||
}
|
||||
else if (this->_energy_pts == 0)
|
||||
std::cout << "ScavTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left." << std::endl;
|
||||
else
|
||||
std::cout << "ScavTrap " << this->_name << " is not able to attack " << target << ", because he is dead" << std::endl;
|
||||
}
|
||||
|
||||
void ScavTrap::guardGate() {
|
||||
std::cout << "ScavTrap " << this->_name << " is now in Gate keeper mode!" << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef SCAVTRAP_HPP
|
||||
# define SCAVTRAP_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
class ScavTrap: public ClapTrap {
|
||||
private:
|
||||
bool _guarding_gate;
|
||||
public:
|
||||
ScavTrap();
|
||||
ScavTrap(const ScavTrap ©);
|
||||
ScavTrap(std::string name);
|
||||
|
||||
virtual ~ScavTrap();
|
||||
|
||||
ScavTrap &operator=(const ScavTrap &src);
|
||||
|
||||
void attack(const std::string &target);
|
||||
void guardGate(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "ScavTrap.hpp"
|
||||
|
||||
int main() {
|
||||
std::cout << "\033[1;36m### TESTING CLAPTRAP ###\033[0m\n" << std::endl;
|
||||
|
||||
{
|
||||
// Construction ClapTrap
|
||||
std::cout << "\033[1;33m--- ClapTrap Construction ---\033[0m" << std::endl;
|
||||
ClapTrap defaultBot;
|
||||
ClapTrap namedBot("R2D2");
|
||||
ClapTrap copiedBot(namedBot);
|
||||
|
||||
// Tests basiques ClapTrap
|
||||
std::cout << "\n\033[1;33m--- ClapTrap Basic Tests ---\033[0m" << std::endl;
|
||||
namedBot.attack("Enemy");
|
||||
namedBot.takeDamage(3);
|
||||
namedBot.beRepaired(2);
|
||||
|
||||
std::cout << "\n\033[1;33m--- ClapTrap Destruction ---\033[0m" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "\n\033[1;35m### TESTING SCAVTRAP ###\033[0m\n" << std::endl;
|
||||
|
||||
{
|
||||
// Construction ScavTrap
|
||||
std::cout << "\033[1;33m--- ScavTrap Construction ---\033[0m" << std::endl;
|
||||
ScavTrap defaultScav;
|
||||
ScavTrap namedScav("R3D3");
|
||||
ScavTrap copiedScav(namedScav);
|
||||
|
||||
// Tests basiques ScavTrap
|
||||
std::cout << "\n\033[1;33m--- ScavTrap Basic Tests ---\033[0m" << std::endl;
|
||||
namedScav.attack("Bandit");
|
||||
namedScav.takeDamage(20);
|
||||
namedScav.beRepaired(15);
|
||||
|
||||
// Test capacité spéciale
|
||||
std::cout << "\n\033[1;33m--- ScavTrap Special Ability ---\033[0m" << std::endl;
|
||||
namedScav.guardGate();
|
||||
defaultScav.guardGate();
|
||||
|
||||
// Test énergie (50 points pour ScavTrap)
|
||||
std::cout << "\n\033[1;33m--- ScavTrap Energy Test ---\033[0m" << std::endl;
|
||||
ScavTrap energyScav("EnergyBot");
|
||||
for (int i = 0; i < 52; i++) {
|
||||
energyScav.attack("practice_target");
|
||||
}
|
||||
|
||||
// Test mort
|
||||
std::cout << "\n\033[1;33m--- ScavTrap Death Test ---\033[0m" << std::endl;
|
||||
ScavTrap deadScav("DeadScav");
|
||||
deadScav.takeDamage(150); // >100 HP
|
||||
deadScav.attack("ghost"); // fails
|
||||
deadScav.guardGate(); // Should still work when dead
|
||||
|
||||
std::cout << "\n\033[1;33m--- ScavTrap Destruction ---\033[0m" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "\n\033[1;32m### ALL TESTS COMPLETED ###\033[0m" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user