-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
244 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |