Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
LithiumOx committed Jul 4, 2024
1 parent bd840d4 commit 7908139
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 02/ex02/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void test_min_max(std::string name, Fixed& x, Fixed& test) {
std::cout << "Max of " << x << " and " << test << " is " << Fixed::max(x, test) << std::endl;
}

void test_pre_post(std::string name, Fixed &x) {
void test_pre_post(std::string name, Fixed& x) {
std::cout << "Initial value of " << name << " is " << x << std::endl;
std::cout << "Pre-increment of " << name << " is " << ++x << std::endl;
std::cout << "Post-increment of " << name << " is " << x++ << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion 03/ex01/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NAME = claptrap
SRC = main.cpp ClapTrap.cpp ScavTrap.cpp
HEADERS = ClapTrap.hpp
HEADERS = ClapTrap.hpp ScavTrap.hpp
BUILD_DIR = build
BUILD = $(addprefix $(BUILD_DIR)/, $(SRC:.cpp=.o))

Expand Down
71 changes: 71 additions & 0 deletions 03/ex02/ClapTrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "ClapTrap.hpp"

#include <iostream>

bool ClapTrap::checkAction() {
if (this->_hitPoints <= 0) {
std::cout << this->_name << " is dead" << std::endl;
return false;
}
if (this->_energyPoints <= 0) {
std::cout << this->_name << " has no energy" << std::endl;
return false;
}
return true;
}

ClapTrap::ClapTrap() : _hitPoints(10), _energyPoints(10), _attackDamage(0) {
_name = "ClapTrap";
std::cout << "Default ClapTrap constructor called" << std::endl;
}

ClapTrap::ClapTrap(std::string name) : _hitPoints(10), _energyPoints(10), _attackDamage(0) {
_name = name;
std::cout << "ClapTrap constructor called" << std::endl;
}

ClapTrap::ClapTrap(const ClapTrap& clapTrap) {
_name = clapTrap._name;
_hitPoints = clapTrap._hitPoints;
_energyPoints = clapTrap._energyPoints;
_attackDamage = clapTrap._attackDamage;
std::cout << "ClapTrap copy constructor called" << std::endl;
}

ClapTrap& ClapTrap::operator=(const ClapTrap& clapTrap) {
_name = clapTrap._name;
_hitPoints = clapTrap._hitPoints;
_energyPoints = clapTrap._energyPoints;
_attackDamage = clapTrap._attackDamage;
std::cout << "ClapTrap assignation operator called" << std::endl;
return *this;
}

ClapTrap::~ClapTrap() { std::cout << "ClapTrap destructor called" << std::endl; }

void ClapTrap::takeDamage(unsigned int amount) {
if (amount > _hitPoints) {
_hitPoints = 0;
std::cout << _name << " died" << std::endl;
return;
}
_hitPoints -= amount;
std::cout << _name << " took " << amount << " damage" << std::endl;
if (_hitPoints == 0) {
std::cout << _name << " died" << std::endl;
}
}

void ClapTrap::attack(const std::string& target) {
if (!this->checkAction()) return;
_energyPoints--;
std::cout << "ClapTrap" << _name << " attacks " << target << " causing " << _attackDamage << " points of damage!"
<< std::endl;
}

void ClapTrap::beRepaired(unsigned int amount) {
if (!this->checkAction()) return;
_hitPoints += amount;
_energyPoints--;
std::cout << _name << " was repaired for " << amount << " hit points" << std::endl;
}
22 changes: 22 additions & 0 deletions 03/ex02/ClapTrap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <string>

class ClapTrap {
protected:
std::string _name;
unsigned int _hitPoints;
unsigned int _energyPoints;
unsigned int _attackDamage;

public:
ClapTrap();
ClapTrap(std::string name);
ClapTrap(const ClapTrap &claptrap);
ClapTrap &operator=(const ClapTrap &claptrap);
virtual ~ClapTrap();
virtual void attack(const std::string &target);
void takeDamage(unsigned int amount);
void beRepaired(unsigned int amount);
bool checkAction();
};
16 changes: 16 additions & 0 deletions 03/ex02/FragTrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "./FragTrap.hpp"

#include <iostream>

FragTrap::FragTrap(std::string name) : ClapTrap(name) {
std::cout << "FragTrap constructor called" << std::endl;
this->_hitPoints = 100;
this->_energyPoints = 100;
this->_attackDamage = 30;
}

FragTrap::~FragTrap() { std::cout << "FragTrap destructor called" << std::endl; }

void FragTrap::highFivesGuys(void) {
std::cout << "FragTrap " << this->_name << " is requesting a high five" << std::endl;
}
16 changes: 16 additions & 0 deletions 03/ex02/FragTrap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <string>
#include "./ClapTrap.hpp"

class FragTrap : public ClapTrap
{
public:
FragTrap();
FragTrap(std::string name);
FragTrap(const FragTrap &fragTrap);
virtual ~FragTrap();
FragTrap &operator=(const FragTrap &fragTrap);

void highFivesGuys(void);
};
33 changes: 33 additions & 0 deletions 03/ex02/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
NAME = claptrap
SRC = main.cpp ClapTrap.cpp ScavTrap.cpp FragTrap.cpp
HEADERS = ClapTrap.hpp FragTrap.cpp ScavTrap.hpp
BUILD_DIR = build
BUILD = $(addprefix $(BUILD_DIR)/, $(SRC:.cpp=.o))

all: $(NAME)

$(NAME): $(BUILD_DIR) $(BUILD)
@c++ $(BUILD) -o $(NAME)

$(BUILD_DIR)/%.o: %.cpp | $(BUILD_DIR)
@c++ -Werror -Wall -Wextra -Wno-implicit-fallthrough -c $< -o $@

$(BUILD_DIR):
@mkdir -p $(BUILD_DIR)

clean:
@rm -rf $(NAME)

fclean: clean
@rm -rf $(BUILD_DIR)

re: fclean all

format:
@clang-format -i $(SRC) $(HEADERS)

run: all
@printf "\n🤖 03/ex01 $(NAME) output:\n\n"
@./$(NAME)

.PHONY: all clean fclean re
43 changes: 43 additions & 0 deletions 03/ex02/ScavTrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "ScavTrap.hpp"

#include <iostream>

ScavTrap::ScavTrap() : ClapTrap() {
std::cout << "ScavTrap constructor called" << std::endl;
this->_hitPoints = 100;
this->_energyPoints = 50;
this->_attackDamage = 20;
}

ScavTrap::ScavTrap(std::string name) : ClapTrap(name) {
std::cout << "ScavTrap constructor called" << std::endl;
this->_hitPoints = 100;
this->_energyPoints = 50;
this->_attackDamage = 20;
}

ScavTrap::ScavTrap(ScavTrap const &other) : ClapTrap(other) {
std::cout << "ScavTrap copy constructor called" << std::endl;
}

ScavTrap &ScavTrap::operator=(ScavTrap const &other) {
std::cout << "ScavTrap assignation operator called" << std::endl;
if (this != &other) {
this->_name = other._name;
this->_hitPoints = other._hitPoints;
this->_energyPoints = other._energyPoints;
this->_attackDamage = other._attackDamage;
}
return *this;
}

ScavTrap::~ScavTrap() { std::cout << "ScavTrap destructor called" << std::endl; }

void ScavTrap::guardGate() {
std::cout << "ScavTrap " << this->_name << " have enterred in Gate keeper mode" << std::endl;
}

void ScavTrap::attack(std::string const &target) {
std::cout << "ScavTrap " << this->_name << " attack " << target << ", causing " << this->_attackDamage
<< " points of damage!" << std::endl;
}
15 changes: 15 additions & 0 deletions 03/ex02/ScavTrap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "ClapTrap.hpp"

class ScavTrap : public ClapTrap {
public:
ScavTrap();
ScavTrap(std::string name);
ScavTrap(const ScavTrap &other);
ScavTrap &operator=(const ScavTrap &other);
~ScavTrap();

void guardGate();
void attack(std::string const &target);
};
Binary file added 03/ex02/claptrap
Binary file not shown.
26 changes: 26 additions & 0 deletions 03/ex02/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "ClapTrap.hpp"
#include "FragTrap.hpp"
#include "ScavTrap.hpp"

int main(void) {
ClapTrap x("Geert");
ScavTrap y("Mees");
FragTrap z("Jelle");

for (int i = 0; i < 50; i++) {
x.attack("a tree");
x.takeDamage(10);
x.beRepaired(1);
}
for (int i = 0; i < 50; i++) {
y.attack("a tree");
y.takeDamage(10);
y.beRepaired(1);
}
for (int i = 0; i < 50; i++) {
z.attack("a tree");
z.takeDamage(10);
z.beRepaired(1);
}
return 0;
}

0 comments on commit 7908139

Please sign in to comment.