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,26 @@
|
||||
#ifndef CLAPTRAP_HPP
|
||||
# define CLAPTRAP_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
class ClapTrap {
|
||||
private:
|
||||
std::string _name;
|
||||
unsigned int _hit_pts;
|
||||
unsigned int _energy_pts;
|
||||
unsigned int _attack_dmg;
|
||||
|
||||
public:
|
||||
ClapTrap();
|
||||
~ClapTrap();
|
||||
ClapTrap(const ClapTrap ©);
|
||||
ClapTrap(std::string name);
|
||||
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,51 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# 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\
|
||||
|
||||
OBJFILES = $(addprefix $(OBJDIR)/, $(INFILES:.cpp=.o))
|
||||
|
||||
NAME = ClapTrap
|
||||
|
||||
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,31 @@
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
int main() {
|
||||
std::cout << "\033[1;36m### TESTING CLAPTRAP ###\033[0m\n" << std::endl;
|
||||
|
||||
{
|
||||
// Construction
|
||||
std::cout << "\033[1;33m--- Construction ---\033[0m" << std::endl;
|
||||
ClapTrap defaultBot;
|
||||
ClapTrap namedBot("R2D2");
|
||||
ClapTrap copiedBot(namedBot);
|
||||
ClapTrap energyTest("EnergyBot");
|
||||
ClapTrap deadBot("DeadBot");
|
||||
|
||||
// Tests basiques
|
||||
std::cout << "\n\033[1;33m--- test ---\033[0m" << std::endl;
|
||||
namedBot.attack("Enemy");
|
||||
namedBot.takeDamage(3);
|
||||
namedBot.beRepaired(2);
|
||||
namedBot.takeDamage(5);
|
||||
std::cout << "\n\033[1;33m--- ep test ---\033[0m" << std::endl;
|
||||
for (int i = 0; i < 11; i++) {
|
||||
energyTest.attack("practice_target");
|
||||
}
|
||||
std::cout << "\n\033[1;33m--- Death Test ---\033[0m" << std::endl;
|
||||
deadBot.takeDamage(15); // >10
|
||||
deadBot.attack("ghost"); // fails
|
||||
std::cout << "\n\033[1;33m--- Destruction ---\033[0m" << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user