├── .gitignore
├── LICENSE
├── README.md
├── ex00
├── Makefile
├── include
│ └── ClapTrap.hpp
└── src
│ ├── ClapTrap.cpp
│ └── main.cpp
├── ex01
├── Makefile
├── include
│ ├── ClapTrap.hpp
│ └── ScavTrap.hpp
└── src
│ ├── ClapTrap.cpp
│ ├── ScavTrap.cpp
│ └── main.cpp
├── ex02
├── Makefile
├── include
│ ├── ClapTrap.hpp
│ ├── FragTrap.hpp
│ └── ScavTrap.hpp
└── src
│ ├── ClapTrap.cpp
│ ├── FragTrap.cpp
│ ├── ScavTrap.cpp
│ └── main.cpp
├── ex03
├── Makefile
├── include
│ ├── ClapTrap.hpp
│ ├── DiamondTrap.hpp
│ ├── FragTrap.hpp
│ └── ScavTrap.hpp
└── src
│ ├── ClapTrap.cpp
│ ├── DiamondTrap.cpp
│ ├── FragTrap.cpp
│ ├── ScavTrap.cpp
│ └── main.cpp
└── readme_additions
└── result.png
/.gitignore:
--------------------------------------------------------------------------------
1 | # Ignore all
2 | *
3 |
4 | # Unignore all with extensions
5 | !*.*
6 |
7 | # Unignore all dirs
8 | !*/
9 |
10 | # Unignore Makefile
11 | !Makefile
12 |
13 | # Prerequisites
14 | *.d
15 |
16 | # Object files
17 | *.o
18 | *.ko
19 | *.obj
20 | *.elf
21 |
22 | # Linker output
23 | *.ilk
24 | *.map
25 | *.exp
26 |
27 | # Precompiled Headers
28 | *.gch
29 | *.pch
30 |
31 | # Libraries
32 | *.lib
33 | *.a
34 | *.la
35 | *.lo
36 |
37 | # Shared objects (inc. Windows DLLs)
38 | *.dll
39 | *.so
40 | *.so.*
41 | *.dylib
42 |
43 | # Executables
44 | *.exe
45 | *.out
46 | *.app
47 | *.i*86
48 | *.x86_64
49 | *.hex
50 |
51 | # Debug files
52 | *.dSYM/
53 | *.su
54 | *.idb
55 | *.pdb
56 |
57 | # Kernel Module Compile Results
58 | *.mod*
59 | *.cmd
60 | .tmp_versions/
61 | modules.order
62 | Module.symvers
63 | Mkfile.old
64 | dkms.conf
65 |
66 | # Visual Studio Code Debug Files
67 | .vscode
68 |
69 | # ignore .log
70 | *.log
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Tim Blaase
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CPP-Module-03
2 |
3 | My solution for CPP Module 03 of the Common core of 42 school.
4 |
5 | 
6 |
7 | ## List of all my cpp-modules
8 |
9 | - [CPP-Module-00](https://github.com/tblaase/CPP-Module-00)
10 | - [CPP-Module-01](https://github.com/tblaase/CPP-Module-01)
11 | - [CPP-Module-02](https://github.com/tblaase/CPP-Module-02)
12 | - [CPP-Module-03](https://github.com/tblaase/CPP-Module-03)
13 | - [CPP-Module-04](https://github.com/tblaase/CPP-Module-04)
14 | - [CPP-Module-05](https://github.com/tblaase/CPP-Module-05)
15 | - [CPP-Module-06](https://github.com/tblaase/CPP-Module-06)
16 | - [CPP-Module-07](https://github.com/tblaase/CPP-Module-07)
17 | - [CPP-Module-08](https://github.com/tblaase/CPP-Module-08)
18 |
19 |
20 | ## About this Module
21 | The Scope of this Module was to get to know C++ further and come in contact with inheritance for classes:
22 | - ex00: creation of the base class to inherit from
23 | - ex01: inherits from base class and adds new values to the attributes as well as a new function
24 | - ex02: inherits from base class and adds new values to the attributes as well as a new function
25 | - ex03: the diaond problem: inherits some attributes from ex02 and some attributes from ex01 and adds an extra function to show its relation to ex00
26 |
27 |
28 | All of those exercises are compilable with the `-std=c++98-flag`, since this was a requirement for this project.
29 | All exercises where compiled and tested on `macOS Catalina 10.15.7` and `Ubuntu 20.04.4 LTS`.
30 |
31 |
32 | All exercises can be compiled from the root of the exercise with `make`, `make all` or `make re`.
33 | All exercises have a main function to demonstrate all the abilities of the subject.
34 | After that, run the created executable like `./executable_to_run` (ie. ex00 `./ClapTrap`).
35 |
--------------------------------------------------------------------------------
/ex00/Makefile:
--------------------------------------------------------------------------------
1 | # **************************************************************************** #
2 | # #
3 | # ::: :::::::: #
4 | # Makefile :+: :+: :+: #
5 | # +:+ +:+ +:+ #
6 | # By: tblaase +#+ +:+ +#+ #
7 | # +#+#+#+#+#+ +#+ #
8 | # Created: 2022/03/25 11:24:02 by tblaase #+# #+# #
9 | # Updated: 2022/03/25 11:35:30 by tblaase ### ########.fr #
10 | # #
11 | # **************************************************************************** #
12 |
13 | NAME = ClapTrap
14 |
15 | CC = c++
16 |
17 | CFLAGS = -Wall -Werror -Wextra -std=c++98
18 |
19 | # directories
20 | SRC_DIR = src/
21 | OBJ_DIR = obj/
22 | INC_DIR = include/
23 |
24 | # controll codes
25 | RESET = \033[0m
26 | GREEN = \033[32m
27 | YELLOW = \033[33m
28 | BLUE = \033[34m
29 | RED = \033[31m
30 | UP = \033[A
31 | CUT = \033[K
32 |
33 | #source files
34 | SRC_FILES = ClapTrap.cpp main.cpp
35 |
36 | OBJ_FILES = $(SRC_FILES:.cpp=.o)
37 |
38 | #paths
39 | SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))
40 | OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
41 |
42 | #all rule
43 | all: $(NAME)
44 | @printf "\n"
45 | @printf "$(GREEN) ___ ___ ___ _____ _ _ ___ ___ \n$(RESET)"
46 | @printf "$(GREEN) ___ _ _ | | | ___| _| | _| _| |_ _| |_ | |_ |\n$(RESET)"
47 | @printf "$(GREEN)| -_|_'_| | | | | | | . | _| | |_ |_ _|_ _| | | |_ |\n$(RESET)"
48 | @printf "$(GREEN)|___|_,_| |___|___| |___|_| |_____| |_| |_| |___|___|\n$(RESET)"
49 | @printf "$(GREEN) \n$(RESET)"
50 | @printf "\n"
51 |
52 | #compile the executable
53 | $(NAME): $(OBJ)
54 | @echo "$(YELLOW)Compiling [$(NAME)]...$(RESET)"
55 | @$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
56 | @echo "$(GREEN)Finished [$(NAME)]$(RESET)"
57 |
58 | #compile objects
59 | $(OBJ_DIR)%.o:$(SRC_DIR)%.cpp
60 | @mkdir -p $(OBJ_DIR)
61 | @echo "$(YELLOW)Compiling [$@]...$(RESET)"
62 | @$(CC) $(CFLAGS) -I $(INC_DIR) -o $@ -c $<
63 | @printf "$(UP)$(CUT)"
64 | @echo "$(GREEN)Finished [$@]$(RESET)"
65 | @printf "$(UP)$(CUT)"
66 |
67 | #clean rule
68 | clean:
69 | @if [ -d "$(OBJ_DIR)" ]; then \
70 | rm -rf $(OBJ_DIR); \
71 | echo "$(BLUE)Deleting all objects from /ex00...$(RESET)"; else \
72 | echo "No objects to remove from /ex00."; \
73 | fi;
74 |
75 | #fclean rule
76 | fclean: clean
77 | @if [ -f "$(NAME)" ]; then \
78 | rm -f $(NAME); \
79 | echo "$(BLUE)Deleting $(NAME) from /ex00...$(RESET)"; else \
80 | echo "No Executable to remove from /ex00."; \
81 | fi;
82 |
83 | #re rule
84 | re: fclean all
85 |
86 | #phony
87 | .PHONY: all clean fclean re
88 |
--------------------------------------------------------------------------------
/ex00/include/ClapTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ClapTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:58 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 13:58:28 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include
18 | #include
19 |
20 | // classes
21 |
22 | class ClapTrap
23 | {
24 | private:
25 | std::string _name;
26 | unsigned int _hit_pts;
27 | unsigned int _energy_pts;
28 | unsigned int _attack_dmg;
29 |
30 | public:
31 | // Constructors
32 | ClapTrap();
33 | ClapTrap(const ClapTrap ©);
34 | ClapTrap(std::string name);
35 |
36 | // Deconstructors
37 | ~ClapTrap();
38 |
39 | // Overloaded Operators
40 | ClapTrap &operator=(const ClapTrap &src);
41 |
42 | // Public Methods
43 | void attack(const std::string &target);
44 | void takeDamage(unsigned int amount);
45 | void beRepaired(unsigned int amount);
46 | // Getter
47 |
48 | // Setter
49 |
50 | };
--------------------------------------------------------------------------------
/ex00/src/ClapTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ClapTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:50 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 14:03:42 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // #include "ClapTrap.hpp"
14 | #include "../include/ClapTrap.hpp"
15 |
16 |
17 | // Constructors
18 | ClapTrap::ClapTrap(): _name("default"), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
19 | {
20 | std::cout << "ClapTrap Default Constructor called" << std::endl;
21 | }
22 |
23 | ClapTrap::ClapTrap(const ClapTrap ©)
24 | {
25 | std::cout << "ClapTrap Copy Constructor called" << std::endl;
26 | *this = copy;
27 | }
28 |
29 | ClapTrap::ClapTrap(std::string name): _name(name), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
30 | {
31 | std::cout << "ClapTrap Constructor for the name " << _name << " called" << std::endl;
32 | }
33 |
34 | // Deconstructors
35 | ClapTrap::~ClapTrap()
36 | {
37 | std::cout << "ClapTrap Deconstructor for " << _name << " called" << std::endl;
38 | }
39 |
40 | // Overloaded Operators
41 | ClapTrap &ClapTrap::operator=(const ClapTrap &src)
42 | {
43 | std::cout << "ClapTrap Assignation operator called" << std::endl;
44 | this->_name = src._name;
45 | this->_hit_pts = src._hit_pts;
46 | this->_energy_pts = src._energy_pts;
47 | this->_attack_dmg = src._attack_dmg;
48 | return *this;
49 | }
50 |
51 | // Public Methods
52 | void ClapTrap::attack(const std::string &target)
53 | {
54 | if (this->_energy_pts > 0 && this->_hit_pts > 0)
55 | {
56 | std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " points of damage!" << std::endl;
57 | this->_energy_pts--;
58 | }
59 | else if (this->_energy_pts == 0)
60 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left.\033[0m" << std::endl;
61 | else
62 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to attack " << target << ", because he has not enough hit points.\033[0m" << std::endl;
63 | }
64 |
65 | void ClapTrap::takeDamage(unsigned int amount)
66 | {
67 | if (this->_hit_pts > amount)
68 | this->_hit_pts -= amount;
69 | else if (this->_hit_pts > 0)
70 | this->_hit_pts = 0;
71 | else
72 | {
73 | std::cout << "\033[33mClapTrap " << this->_name << " is already dead, stop beating it.\033[0m" << std::endl;
74 | return ;
75 | }
76 | std::cout << "ClapTrap " << this->_name << " was attacked and lost " << amount << " hit points, he now has " << this->_hit_pts << " hit points." << std::endl;
77 | }
78 |
79 | void ClapTrap::beRepaired(unsigned int amount)
80 | {
81 | if (this->_energy_pts > 0 && this->_hit_pts > 0 && this->_hit_pts + amount <= 10)
82 | {
83 | this->_hit_pts += amount;
84 | std::cout << "ClapTrap " << this->_name << " repaired itself and gained " << amount << " of hit points, he now has " << this->_hit_pts << "hit points." << std::endl;
85 | this->_energy_pts--;
86 | }
87 | else if (this->_energy_pts == 0)
88 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to repair itself, because he doesn't have enough energy points.\033[0m" << std::endl;
89 | else if (this->_hit_pts == 0)
90 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to repair itself, because he doesn't have enough hit points.\033[0m" << std::endl;
91 | else
92 | std::cout << "\033[33mClapTrap " << this->_name << " can't be repaired to have more than 10 hit points.\033[0m" << std::endl;
93 | }
94 | // Getter
95 |
96 | // Setter
97 |
98 |
--------------------------------------------------------------------------------
/ex00/src/main.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* main.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:55 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 12:52:17 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "../include/ClapTrap.hpp"
14 |
15 | int main()
16 | {
17 | std::cout << "### TESTING CLAPTRAP ###\n" << std::endl;
18 | {
19 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
20 | ClapTrap a;
21 | ClapTrap b("Cody");
22 |
23 | std::cout << "\033[34mTesting\033[0m" << std::endl;
24 | a.attack("some other robot");
25 | a.takeDamage(10);
26 | a.takeDamage(10);
27 | a.beRepaired(5);
28 | a.attack("some other other robot");
29 | b.beRepaired(3);
30 | for (int i = 0; i < 12; i++)
31 | b.attack("Cody-clone");
32 | b.beRepaired(3);
33 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
34 | }
35 | return (0);
36 | }
37 |
--------------------------------------------------------------------------------
/ex01/Makefile:
--------------------------------------------------------------------------------
1 | # **************************************************************************** #
2 | # #
3 | # ::: :::::::: #
4 | # Makefile :+: :+: :+: #
5 | # +:+ +:+ +:+ #
6 | # By: tblaase +#+ +:+ +#+ #
7 | # +#+#+#+#+#+ +#+ #
8 | # Created: 2022/03/16 14:46:31 by tblaase #+# #+# #
9 | # Updated: 2022/03/27 19:51:39 by tblaase ### ########.fr #
10 | # #
11 | # **************************************************************************** #
12 |
13 | NAME = ScavTrap
14 |
15 | CC = c++
16 |
17 | CFLAGS = -Wall -Werror -Wextra -std=c++98 -g
18 |
19 | # directories
20 | SRC_DIR = src/
21 | OBJ_DIR = obj/
22 | INC_DIR = include/
23 |
24 | # controll codes
25 | RESET = \033[0m
26 | GREEN = \033[32m
27 | YELLOW = \033[33m
28 | BLUE = \033[34m
29 | RED = \033[31m
30 | UP = \033[A
31 | CUT = \033[K
32 |
33 | #source files
34 | SRC_FILES = ClapTrap.cpp main.cpp ScavTrap.cpp
35 |
36 | OBJ_FILES = $(SRC_FILES:.cpp=.o)
37 |
38 | #paths
39 | SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))
40 | OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
41 |
42 | #all rule
43 | all: $(NAME)
44 | @printf "\n"
45 | @printf "$(GREEN) ___ ___ ___ _____ _ _ ___ ___ \n$(RESET)"
46 | @printf "$(GREEN) ___ _ _ | |_ | ___| _| | _| _| |_ _| |_ | |_ |\n$(RESET)"
47 | @printf "$(GREEN)| -_|_'_| | | |_| |_ | . | _| | |_ |_ _|_ _| | | |_ |\n$(RESET)"
48 | @printf "$(GREEN)|___|_,_| |___|_____| |___|_| |_____| |_| |_| |___|___|\n$(RESET)"
49 | @printf "$(GREEN) \n$(RESET)"
50 | @printf "\n"
51 |
52 | #compile the executable
53 | $(NAME): $(OBJ)
54 | @echo "$(YELLOW)Compiling [$(NAME)]...$(RESET)"
55 | @$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
56 | @echo "$(GREEN)Finished [$(NAME)]$(RESET)"
57 |
58 | #compile objects
59 | $(OBJ_DIR)%.o:$(SRC_DIR)%.cpp
60 | @mkdir -p $(OBJ_DIR)
61 | @echo "$(YELLOW)Compiling [$@]...$(RESET)"
62 | @$(CC) $(CFLAGS) -I $(INC_DIR) -o $@ -c $<
63 | @printf "$(UP)$(CUT)"
64 | @echo "$(GREEN)Finished [$@]$(RESET)"
65 | @printf "$(UP)$(CUT)"
66 |
67 | #clean rule
68 | clean:
69 | @if [ -d "$(OBJ_DIR)" ]; then \
70 | rm -rf $(OBJ_DIR); \
71 | echo "$(BLUE)Deleting all objects from /ex01...$(RESET)"; else \
72 | echo "No objects to remove from /ex01."; \
73 | fi;
74 |
75 | #fclean rule
76 | fclean: clean
77 | @if [ -f "$(NAME)" ]; then \
78 | rm -f $(NAME); \
79 | echo "$(BLUE)Deleting $(NAME) from /ex01...$(RESET)"; else \
80 | echo "No Executable to remove from /ex01."; \
81 | fi;
82 |
83 | #re rule
84 | re: fclean all
85 |
86 | #phony
87 | .PHONY: all clean fclean re
88 |
--------------------------------------------------------------------------------
/ex01/include/ClapTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ClapTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:58 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 13:30:08 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include
18 | #include
19 |
20 | // classes
21 |
22 | class ClapTrap
23 | {
24 | protected:
25 | std::string _name;
26 | unsigned int _hit_pts;
27 | unsigned int _energy_pts;
28 | unsigned int _attack_dmg;
29 |
30 | public:
31 | // Constructors
32 | ClapTrap();
33 | ClapTrap(const ClapTrap ©);
34 | ClapTrap(std::string name);
35 |
36 | // Deconstructors
37 | virtual ~ClapTrap();
38 |
39 | // Overloaded Operators
40 | ClapTrap &operator=(const ClapTrap &src);
41 |
42 | // Public Methods
43 | void attack(const std::string &target);
44 | void takeDamage(unsigned int amount);
45 | void beRepaired(unsigned int amount);
46 | // Getter
47 |
48 | // Setter
49 |
50 | };
--------------------------------------------------------------------------------
/ex01/include/ScavTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ScavTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 11:49:02 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:44:46 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include "ClapTrap.hpp"
18 |
19 | // classes
20 |
21 | class ScavTrap: public ClapTrap
22 | {
23 | private:
24 | bool _guarding_gate;
25 | public:
26 | // Constructors
27 | ScavTrap();
28 | ScavTrap(const ScavTrap ©);
29 | ScavTrap(std::string name);
30 |
31 | // Deconstructors
32 | virtual ~ScavTrap();
33 |
34 | // Overloaded Operators
35 | ScavTrap &operator=(const ScavTrap &src);
36 |
37 | // Public Methods
38 | void attack(const std::string &target);
39 | void guardGate(void);
40 | // Getter
41 |
42 | // Setter
43 |
44 | };
45 |
--------------------------------------------------------------------------------
/ex01/src/ClapTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ClapTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:50 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 14:03:35 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // #include "ClapTrap.hpp"
14 | #include "../include/ClapTrap.hpp"
15 |
16 |
17 | // Constructors
18 | ClapTrap::ClapTrap(): _name("default"), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
19 | {
20 | std::cout << "ClapTrap Default Constructor called" << std::endl;
21 | }
22 |
23 | ClapTrap::ClapTrap(const ClapTrap ©)
24 | {
25 | std::cout << "ClapTrap Copy Constructor called" << std::endl;
26 | *this = copy;
27 | }
28 |
29 | ClapTrap::ClapTrap(std::string name): _name(name), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
30 | {
31 | std::cout << "ClapTrap Constructor for the name " << this->_name << " called" << std::endl;
32 | }
33 |
34 | // Deconstructors
35 | ClapTrap::~ClapTrap()
36 | {
37 | std::cout << "ClapTrap Deconstructor for " << this->_name << " called" << std::endl;
38 | }
39 |
40 | // Overloaded Operators
41 | ClapTrap &ClapTrap::operator=(const ClapTrap &src)
42 | {
43 | std::cout << "ClapTrap Assignation operator called" << std::endl;
44 | this->_name = src._name;
45 | this->_hit_pts = src._hit_pts;
46 | this->_energy_pts = src._energy_pts;
47 | this->_attack_dmg = src._attack_dmg;
48 | return *this;
49 | }
50 |
51 | // Public Methods
52 | void ClapTrap::attack(const std::string &target)
53 | {
54 | if (this->_energy_pts > 0 && this->_hit_pts > 0)
55 | {
56 | std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " points of damage!" << std::endl;
57 | this->_energy_pts--;
58 | }
59 | else if (this->_energy_pts == 0)
60 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left.\033[0m" << std::endl;
61 | else
62 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to attack " << target << ", because he has not enough hit points.\033[0m" << std::endl;
63 | }
64 |
65 | void ClapTrap::takeDamage(unsigned int amount)
66 | {
67 | if (this->_hit_pts > amount)
68 | this->_hit_pts -= amount;
69 | else if (this->_hit_pts > 0)
70 | this->_hit_pts = 0;
71 | else
72 | {
73 | std::cout << "\033[33mClapTrap " << this->_name << " is already dead, stop beating it.\033[0m" << std::endl;
74 | return ;
75 | }
76 | std::cout << "ClapTrap " << this->_name << " was attacked and lost " << amount << " hit points, he now has " << this->_hit_pts << " hit points." << std::endl;
77 | }
78 |
79 | void ClapTrap::beRepaired(unsigned int amount)
80 | {
81 | if (this->_energy_pts > 0 && this->_hit_pts > 0 && this->_hit_pts + amount <= 10)
82 | {
83 | this->_hit_pts += amount;
84 | std::cout << "ClapTrap " << this->_name << " repaired itself and gained " << amount << " of hit points, he now has " << this->_hit_pts << "hit points." << std::endl;
85 | this->_energy_pts--;
86 | }
87 | else if (this->_energy_pts == 0)
88 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to repair itself, because he doesn't have enough energy points.\033[0m" << std::endl;
89 | else if (this->_hit_pts == 0)
90 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to repair itself, because he doesn't have enough hit points.\033[0m" << std::endl;
91 | else
92 | std::cout << "\033[33mClapTrap " << this->_name << " can't be repaired to have more than 10 hit points.\033[0m" << std::endl;
93 | }
94 | // Getter
95 |
96 | // Setter
97 |
98 |
--------------------------------------------------------------------------------
/ex01/src/ScavTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ScavTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 12:19:54 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:45:12 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ScavTrap.hpp"
14 |
15 | // Constructors
16 | ScavTrap::ScavTrap(): ClapTrap()
17 | {
18 | this->_hit_pts = 100;
19 | this->_energy_pts = 50;
20 | this->_attack_dmg = 20;
21 | this->_guarding_gate = false;
22 | std::cout << "ScavTrap Default Constructor called" << std::endl;
23 | }
24 |
25 | ScavTrap::ScavTrap(const ScavTrap ©): ClapTrap(copy)
26 | {
27 | this->_guarding_gate = copy._guarding_gate;
28 | std::cout << "ScavTrap Copy Constructor called" << std::endl;
29 | }
30 |
31 | ScavTrap::ScavTrap(std::string name): ClapTrap(name)
32 | {
33 | this->_hit_pts = 100;
34 | this->_energy_pts = 50;
35 | this->_attack_dmg = 20;
36 | this->_guarding_gate = false;
37 | std::cout << "ScavTrap Constructor for the name " << this->_name << " called" << std::endl;
38 | }
39 |
40 | // Deconstructors
41 | ScavTrap::~ScavTrap()
42 | {
43 | std::cout << "ScavTrap Deconstructor for " << this->_name << " called" << std::endl;
44 | }
45 |
46 | // Overloaded Operators
47 | ScavTrap &ScavTrap::operator=(const ScavTrap &src)
48 | {
49 | std::cout << "ScavTrap Assignation operator called" << std::endl;
50 | this->_name = src._name;
51 | this->_hit_pts = src._hit_pts;
52 | this->_energy_pts = src._energy_pts;
53 | this->_attack_dmg = src._attack_dmg;
54 | return *this;
55 | }
56 |
57 | // Public Methods
58 | void ScavTrap::attack(const std::string &target)
59 | {
60 | if (this->_energy_pts > 0 && this->_hit_pts > 0)
61 | {
62 | std::cout << "ScavTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " points of damage!" << std::endl;
63 | this->_energy_pts--;
64 | }
65 | else if (this->_energy_pts == 0)
66 | std::cout << "\033[31mScavTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left.\033[0m" << std::endl;
67 | else
68 | std::cout << "\033[31mScavTrap " << this->_name << " is not able to attack " << target << ", because he has not enough hit points.\033[0m" << std::endl;
69 | }
70 |
71 | void ScavTrap::guardGate(void)
72 | {
73 | if (this->_guarding_gate == false)
74 | {
75 | this->_guarding_gate = true;
76 | std::cout << "ScavTrap " << this->_name << " is now guarding the gate." << std::endl;
77 | }
78 | else
79 | std::cout << "\033[33mScavTrap " << this->_name << " is already guarding the gate.\033[0m" << std::endl;
80 | }
81 |
82 | // Getter
83 |
84 | // Setter
85 |
86 |
--------------------------------------------------------------------------------
/ex01/src/main.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* main.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:55 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 12:52:23 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // #include "../include/ScavTrap.hpp"
14 | #include "ScavTrap.hpp"
15 |
16 | int main()
17 | {
18 | std::cout << "### TESTING CLAPTRAP ###\n" << std::endl;
19 | {
20 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
21 | ClapTrap a;
22 | ClapTrap b("Cody");
23 |
24 | std::cout << "\033[34mTesting\033[0m" << std::endl;
25 | a.attack("some other robot");
26 | a.takeDamage(10);
27 | a.takeDamage(10);
28 | a.beRepaired(5);
29 | a.attack("some other other robot");
30 | b.beRepaired(3);
31 | for (int i = 0; i < 12; i++)
32 | b.attack("Cody-clone");
33 | b.beRepaired(3);
34 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
35 | }
36 | std::cout << "\n\n### TESTING SCAVTRAP ###\n" << std::endl;
37 | {
38 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
39 | ScavTrap c;
40 | ScavTrap d("Savage");
41 |
42 | std::cout << "\033[34mTesting\033[0m" << std::endl;
43 | c.attack("CloneTrap");
44 | // for (int i = 0; i < 50; i++)
45 | // c.attack("CloneTrap");
46 | c.beRepaired(22);
47 | c.takeDamage(21);
48 | c.beRepaired(22);
49 | c.guardGate();
50 | c.guardGate();
51 | d.attack("Savage-clone");
52 | d.takeDamage(101);
53 | d.takeDamage(15);
54 | d.attack("ScavTrap-clone");
55 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
56 | }
57 | return (0);
58 | }
59 |
--------------------------------------------------------------------------------
/ex02/Makefile:
--------------------------------------------------------------------------------
1 | # **************************************************************************** #
2 | # #
3 | # ::: :::::::: #
4 | # Makefile :+: :+: :+: #
5 | # +:+ +:+ +:+ #
6 | # By: tblaase +#+ +:+ +#+ #
7 | # +#+#+#+#+#+ +#+ #
8 | # Created: 2022/03/16 14:46:31 by tblaase #+# #+# #
9 | # Updated: 2022/03/28 16:55:01 by tblaase ### ########.fr #
10 | # #
11 | # **************************************************************************** #
12 |
13 | NAME = FragTrap
14 |
15 | CC = c++
16 |
17 | CFLAGS = -Wall -Werror -Wextra -std=c++98
18 |
19 | # directories
20 | SRC_DIR = src/
21 | OBJ_DIR = obj/
22 | INC_DIR = include/
23 |
24 | # controll codes
25 | RESET = \033[0m
26 | GREEN = \033[32m
27 | YELLOW = \033[33m
28 | BLUE = \033[34m
29 | RED = \033[31m
30 | UP = \033[A
31 | CUT = \033[K
32 |
33 | #source files
34 | SRC_FILES = ClapTrap.cpp FragTrap.cpp main.cpp ScavTrap.cpp
35 |
36 | OBJ_FILES = $(SRC_FILES:.cpp=.o)
37 |
38 | #paths
39 | SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))
40 | OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
41 |
42 | #all rule
43 | all: $(NAME)
44 | @printf "\n"
45 | @printf "$(GREEN) ___ ___ ___ _____ _ _ ___ ___ \n$(RESET)"
46 | @printf "$(GREEN) ___ _ _ | |_ | ___| _| | _| _| |_ _| |_ | |_ |\n$(RESET)"
47 | @printf "$(GREEN)| -_|_'_| | | | _| | . | _| | |_ |_ _|_ _| | | |_ |\n$(RESET)"
48 | @printf "$(GREEN)|___|_,_| |___|___| |___|_| |_____| |_| |_| |___|___|\n$(RESET)"
49 | @printf "$(GREEN) \n$(RESET)"
50 | @printf "\n"
51 |
52 | #compile the executable
53 | $(NAME): $(OBJ)
54 | @echo "$(YELLOW)Compiling [$(NAME)]...$(RESET)"
55 | @$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
56 | @echo "$(GREEN)Finished [$(NAME)]$(RESET)"
57 |
58 | #compile objects
59 | $(OBJ_DIR)%.o:$(SRC_DIR)%.cpp
60 | @mkdir -p $(OBJ_DIR)
61 | @echo "$(YELLOW)Compiling [$@]...$(RESET)"
62 | @$(CC) $(CFLAGS) -I $(INC_DIR) -o $@ -c $<
63 | @printf "$(UP)$(CUT)"
64 | @echo "$(GREEN)Finished [$@]$(RESET)"
65 | @printf "$(UP)$(CUT)"
66 |
67 | #clean rule
68 | clean:
69 | @if [ -d "$(OBJ_DIR)" ]; then \
70 | rm -rf $(OBJ_DIR); \
71 | echo "$(BLUE)Deleting all objects from /ex02...$(RESET)"; else \
72 | echo "No objects to remove from /ex02."; \
73 | fi;
74 |
75 | #fclean rule
76 | fclean: clean
77 | @if [ -f "$(NAME)" ]; then \
78 | rm -f $(NAME); \
79 | echo "$(BLUE)Deleting $(NAME) from /ex02...$(RESET)"; else \
80 | echo "No Executable to remove from /ex02."; \
81 | fi;
82 |
83 | #re rule
84 | re: fclean all
85 |
86 | #phony
87 | .PHONY: all clean fclean re
88 |
--------------------------------------------------------------------------------
/ex02/include/ClapTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ClapTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:58 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 12:01:24 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include
18 | #include
19 |
20 | // classes
21 |
22 | class ClapTrap
23 | {
24 | protected:
25 | std::string _name;
26 | unsigned int _hit_pts;
27 | unsigned int _energy_pts;
28 | unsigned int _attack_dmg;
29 |
30 | public:
31 | // Constructors
32 | ClapTrap();
33 | ClapTrap(const ClapTrap ©);
34 | ClapTrap(std::string name);
35 |
36 | // Deconstructors
37 | virtual ~ClapTrap();
38 |
39 | // Overloaded Operators
40 | ClapTrap &operator=(const ClapTrap &src);
41 |
42 | // Public Methods
43 | void attack(const std::string &target);
44 | void takeDamage(unsigned int amount);
45 | void beRepaired(unsigned int amount);
46 | // Getter
47 |
48 | // Setter
49 |
50 | };
--------------------------------------------------------------------------------
/ex02/include/FragTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* FragTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/28 15:27:18 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:51:57 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include "ClapTrap.hpp"
18 |
19 | // classes
20 |
21 | class FragTrap: public ClapTrap
22 | {
23 | private:
24 |
25 | public:
26 | // Constructors
27 | FragTrap();
28 | FragTrap(const FragTrap ©);
29 | FragTrap(std::string name);
30 |
31 | // Deconstructors
32 | virtual ~FragTrap();
33 |
34 | // Overloaded Operators
35 | FragTrap &operator=(const FragTrap &src);
36 |
37 | // Public Methods
38 | void highFiveGuys(void);
39 | // Getter
40 |
41 | // Setter
42 |
43 | };
44 |
--------------------------------------------------------------------------------
/ex02/include/ScavTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ScavTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 11:49:02 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:47:27 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include "ClapTrap.hpp"
18 |
19 | // classes
20 |
21 | class ScavTrap: public ClapTrap
22 | {
23 | private:
24 | bool _guarding_gate;
25 | public:
26 | // Constructors
27 | ScavTrap();
28 | ScavTrap(const ScavTrap ©);
29 | ScavTrap(std::string name);
30 |
31 | // Deconstructors
32 | virtual ~ScavTrap();
33 |
34 | // Overloaded Operators
35 | ScavTrap &operator=(const ScavTrap &src);
36 |
37 | // Public Methods
38 | void attack(const std::string &target);
39 | void guardGate(void);
40 | // Getter
41 |
42 | // Setter
43 |
44 | };
45 |
--------------------------------------------------------------------------------
/ex02/src/ClapTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ClapTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:50 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 14:03:29 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ClapTrap.hpp"
14 |
15 | // Constructors
16 | ClapTrap::ClapTrap(): _name("default"), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
17 | {
18 | std::cout << "ClapTrap Default Constructor called" << std::endl;
19 | }
20 |
21 | ClapTrap::ClapTrap(const ClapTrap ©)
22 | {
23 | std::cout << "ClapTrap Copy Constructor called" << std::endl;
24 | *this = copy;
25 | }
26 |
27 | ClapTrap::ClapTrap(std::string name): _name(name), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
28 | {
29 | std::cout << "ClapTrap Constructor for the name " << this->_name << " called" << std::endl;
30 | }
31 |
32 | // Deconstructors
33 | ClapTrap::~ClapTrap()
34 | {
35 | std::cout << "ClapTrap Deconstructor for " << this->_name << " called" << std::endl;
36 | }
37 |
38 | // Overloaded Operators
39 | ClapTrap &ClapTrap::operator=(const ClapTrap &src)
40 | {
41 | std::cout << "ClapTrap Assignation operator called" << std::endl;
42 | this->_name = src._name;
43 | this->_hit_pts = src._hit_pts;
44 | this->_energy_pts = src._energy_pts;
45 | this->_attack_dmg = src._attack_dmg;
46 | return *this;
47 | }
48 |
49 | // Public Methods
50 | void ClapTrap::attack(const std::string &target)
51 | {
52 | if (this->_energy_pts > 0 && this->_hit_pts > 0)
53 | {
54 | std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " points of damage!" << std::endl;
55 | this->_energy_pts--;
56 | }
57 | else if (this->_energy_pts == 0)
58 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left.\033[0m" << std::endl;
59 | else
60 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to attack " << target << ", because he has not enough hit points.\033[0m" << std::endl;
61 | }
62 |
63 | void ClapTrap::takeDamage(unsigned int amount)
64 | {
65 | if (this->_hit_pts > amount)
66 | this->_hit_pts -= amount;
67 | else if (this->_hit_pts > 0)
68 | this->_hit_pts = 0;
69 | else
70 | {
71 | std::cout << "\033[33mClapTrap " << this->_name << " is already dead, stop beating it.\033[0m" << std::endl;
72 | return ;
73 | }
74 | std::cout << "ClapTrap " << this->_name << " was attacked and lost " << amount << " hit points, he now has " << this->_hit_pts << " hit points." << std::endl;
75 | }
76 |
77 | void ClapTrap::beRepaired(unsigned int amount)
78 | {
79 | if (this->_energy_pts > 0 && this->_hit_pts > 0 && this->_hit_pts + amount <= 10)
80 | {
81 | this->_hit_pts += amount;
82 | std::cout << "ClapTrap " << this->_name << " repaired itself and gained " << amount << " of hit points, he now has " << this->_hit_pts << "hit points." << std::endl;
83 | this->_energy_pts--;
84 | }
85 | else if (this->_energy_pts == 0)
86 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to repair itself, because he doesn't have enough energy points.\033[0m" << std::endl;
87 | else if (this->_hit_pts == 0)
88 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to repair itself, because he doesn't have enough hit points.\033[0m" << std::endl;
89 | else
90 | std::cout << "\033[33mClapTrap " << this->_name << " can't be repaired to have more than 10 hit points.\033[0m" << std::endl;
91 | }
92 | // Getter
93 |
94 | // Setter
95 |
96 |
--------------------------------------------------------------------------------
/ex02/src/FragTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* FragTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/28 15:26:49 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:52:08 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "FragTrap.hpp"
14 |
15 | // Constructors
16 | FragTrap::FragTrap(): ClapTrap()
17 | {
18 | this->_hit_pts = 100;
19 | this->_energy_pts = 100;
20 | this->_attack_dmg = 30;
21 | std::cout << "FragTrap Default Constructor called" << std::endl;
22 | }
23 |
24 | FragTrap::FragTrap(const FragTrap ©): ClapTrap(copy)
25 | {
26 | std::cout << "FragTrap Copy Constructor called" << std::endl;
27 | }
28 |
29 | FragTrap::FragTrap(std::string name): ClapTrap(name)
30 | {
31 | this->_hit_pts = 100;
32 | this->_energy_pts = 100;
33 | this->_attack_dmg = 30;
34 | std::cout << "FragTrap Constructor for the name " << this->_name << " called" << std::endl;
35 | }
36 |
37 | // Deconstructors
38 | FragTrap::~FragTrap()
39 | {
40 | std::cout << "FragTrap Deconstructor for " << this->_name << " called" << std::endl;
41 | }
42 |
43 | // Overloaded Operators
44 | FragTrap &FragTrap::operator=(const FragTrap &src)
45 | {
46 | std::cout << "FragTrap Assignation operator called" << std::endl;
47 | this->_name = src._name;
48 | this->_hit_pts = src._hit_pts;
49 | this->_energy_pts = src._energy_pts;
50 | this->_attack_dmg = src._attack_dmg;
51 | return *this;
52 | }
53 |
54 | // Public Methods
55 |
56 | void FragTrap::highFiveGuys(void)
57 | {
58 | std::cout << "FragTrap " << this->_name << ": You want a high five?\n\t*WHAMM*\nHere you go." << std::endl;
59 | }
60 |
61 | // Getter
62 |
63 | // Setter
64 |
65 |
--------------------------------------------------------------------------------
/ex02/src/ScavTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ScavTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 12:19:54 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:47:39 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ScavTrap.hpp"
14 |
15 | // Constructors
16 | ScavTrap::ScavTrap(): ClapTrap()
17 | {
18 | this->_hit_pts = 100;
19 | this->_energy_pts = 50;
20 | this->_attack_dmg = 20;
21 | this->_guarding_gate = false;
22 | std::cout << "ScavTrap Default Constructor called" << std::endl;
23 | }
24 |
25 | ScavTrap::ScavTrap(const ScavTrap ©): ClapTrap(copy)
26 | {
27 | this->_guarding_gate = copy._guarding_gate;
28 | std::cout << "ScavTrap Copy Constructor called" << std::endl;
29 | }
30 |
31 | ScavTrap::ScavTrap(std::string name): ClapTrap(name)
32 | {
33 | this->_hit_pts = 100;
34 | this->_energy_pts = 50;
35 | this->_attack_dmg = 20;
36 | this->_guarding_gate = false;
37 | std::cout << "ScavTrap Constructor for the name " << this->_name << " called" << std::endl;
38 | }
39 |
40 | // Deconstructors
41 | ScavTrap::~ScavTrap()
42 | {
43 | std::cout << "ScavTrap Deconstructor for " << this->_name << " called" << std::endl;
44 | }
45 |
46 | // Overloaded Operators
47 | ScavTrap &ScavTrap::operator=(const ScavTrap &src)
48 | {
49 | std::cout << "ScavTrap Assignation operator called" << std::endl;
50 | this->_name = src._name;
51 | this->_hit_pts = src._hit_pts;
52 | this->_energy_pts = src._energy_pts;
53 | this->_attack_dmg = src._attack_dmg;
54 | return *this;
55 | }
56 |
57 | // Public Methods
58 | void ScavTrap::attack(const std::string &target)
59 | {
60 | if (this->_energy_pts > 0 && this->_hit_pts > 0)
61 | {
62 | std::cout << "ScavTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " points of damage!" << std::endl;
63 | this->_energy_pts--;
64 | }
65 | else if (this->_energy_pts == 0)
66 | std::cout << "\033[31mScavTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left.\033[0m" << std::endl;
67 | else
68 | std::cout << "\033[31mScavTrap " << this->_name << " is not able to attack " << target << ", because he has not enough hit points.\033[0m" << std::endl;
69 | }
70 |
71 | void ScavTrap::guardGate(void)
72 | {
73 | if (this->_guarding_gate == false)
74 | {
75 | this->_guarding_gate = true;
76 | std::cout << "ScavTrap " << this->_name << " is now guarding the gate." << std::endl;
77 | }
78 | else
79 | std::cout << "\033[33mScavTrap " << this->_name << " is already guarding the gate.\033[0m" << std::endl;
80 | }
81 |
82 | // Getter
83 |
84 | // Setter
85 |
86 |
--------------------------------------------------------------------------------
/ex02/src/main.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* main.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:55 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:54:37 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "FragTrap.hpp"
14 | #include "ScavTrap.hpp"
15 |
16 | int main()
17 | {
18 | std::cout << "### TESTING CLAPTRAP ###\n" << std::endl;
19 | {
20 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
21 | ClapTrap a;
22 | ClapTrap b("Cody");
23 |
24 | std::cout << "\033[34mTesting\033[0m" << std::endl;
25 | a.attack("some other robot");
26 | a.takeDamage(10);
27 | a.takeDamage(10);
28 | a.beRepaired(5);
29 | a.attack("some other other robot");
30 | b.beRepaired(3);
31 | for (int i = 0; i < 12; i++)
32 | b.attack("Cody-clone");
33 | b.beRepaired(3);
34 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
35 | }
36 | std::cout << "\n\n### TESTING SCAVTRAP ###\n" << std::endl;
37 | {
38 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
39 | ScavTrap c;
40 | ScavTrap d("Savage");
41 |
42 | std::cout << "\033[34mTesting\033[0m" << std::endl;
43 | c.attack("CloneTrap");
44 | // for (int i = 0; i < 50; i++)
45 | // c.attack("CloneTrap");
46 | c.beRepaired(22);
47 | c.takeDamage(21);
48 | c.beRepaired(22);
49 | c.guardGate();
50 | c.guardGate();
51 | d.attack("Savage-clone");
52 | d.takeDamage(101);
53 | d.takeDamage(15);
54 | d.attack("ScavTrap-clone");
55 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
56 | }
57 | std::cout << "\n\n### TESTING FRAGTRAP ###\n" << std::endl;
58 | {
59 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
60 | FragTrap e;
61 | FragTrap f("Chadd");
62 |
63 | std::cout << "\033[34mTesting\033[0m" << std::endl;
64 | e.highFiveGuys();
65 | e.attack("some random dude");
66 | e.takeDamage(101);
67 | e.takeDamage(1);
68 | e.attack("some random dude");
69 | f.highFiveGuys();
70 | // for(int i = 0; i < 101; i++)
71 | // f.attack("FragTrap-clone");
72 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
73 | }
74 | return (0);
75 | }
76 |
--------------------------------------------------------------------------------
/ex03/Makefile:
--------------------------------------------------------------------------------
1 | # **************************************************************************** #
2 | # #
3 | # ::: :::::::: #
4 | # Makefile :+: :+: :+: #
5 | # +:+ +:+ +:+ #
6 | # By: tblaase +#+ +:+ +#+ #
7 | # +#+#+#+#+#+ +#+ #
8 | # Created: 2022/03/16 14:46:31 by tblaase #+# #+# #
9 | # Updated: 2022/03/29 12:30:17 by tblaase ### ########.fr #
10 | # #
11 | # **************************************************************************** #
12 |
13 | NAME = DiamondTrap
14 |
15 | CC = c++
16 |
17 | CFLAGS = -Wall -Werror -Wextra -std=c++98
18 |
19 | # directories
20 | SRC_DIR = src/
21 | OBJ_DIR = obj/
22 | INC_DIR = include/
23 |
24 | # controll codes
25 | RESET = \033[0m
26 | GREEN = \033[32m
27 | YELLOW = \033[33m
28 | BLUE = \033[34m
29 | RED = \033[31m
30 | UP = \033[A
31 | CUT = \033[K
32 |
33 | #source files
34 | SRC_FILES = ClapTrap.cpp DiamondTrap.cpp FragTrap.cpp main.cpp ScavTrap.cpp
35 |
36 | OBJ_FILES = $(SRC_FILES:.cpp=.o)
37 |
38 | #paths
39 | SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))
40 | OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
41 |
42 | #all rule
43 | all: $(NAME)
44 | @printf "\n"
45 | @printf "$(GREEN) ___ ___ ___ _____ _ _ ___ ___ \n$(RESET)"
46 | @printf "$(GREEN) ___ _ _ | |_ | ___| _| | _| _| |_ _| |_ | |_ |\n$(RESET)"
47 | @printf "$(GREEN)| -_|_'_| | | |_ | | . | _| | |_ |_ _|_ _| | | |_ |\n$(RESET)"
48 | @printf "$(GREEN)|___|_,_| |___|___| |___|_| |_____| |_| |_| |___|___|\n$(RESET)"
49 | @printf "$(GREEN) \n$(RESET)"
50 | @printf "\n"
51 |
52 | #compile the executable
53 | $(NAME): $(OBJ)
54 | @echo "$(YELLOW)Compiling [$(NAME)]...$(RESET)"
55 | @$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
56 | @echo "$(GREEN)Finished [$(NAME)]$(RESET)"
57 |
58 | #compile objects
59 | $(OBJ_DIR)%.o:$(SRC_DIR)%.cpp
60 | @mkdir -p $(OBJ_DIR)
61 | @echo "$(YELLOW)Compiling [$@]...$(RESET)"
62 | @$(CC) $(CFLAGS) -I $(INC_DIR) -o $@ -c $<
63 | @printf "$(UP)$(CUT)"
64 | @echo "$(GREEN)Finished [$@]$(RESET)"
65 | @printf "$(UP)$(CUT)"
66 |
67 | #clean rule
68 | clean:
69 | @if [ -d "$(OBJ_DIR)" ]; then \
70 | rm -rf $(OBJ_DIR); \
71 | echo "$(BLUE)Deleting all objects from /ex03...$(RESET)"; else \
72 | echo "No objects to remove from /ex03."; \
73 | fi;
74 |
75 | #fclean rule
76 | fclean: clean
77 | @if [ -f "$(NAME)" ]; then \
78 | rm -f $(NAME); \
79 | echo "$(BLUE)Deleting $(NAME) from /ex03...$(RESET)"; else \
80 | echo "No Executable to remove from /ex03."; \
81 | fi;
82 |
83 | #re rule
84 | re: fclean all
85 |
86 | #phony
87 | .PHONY: all clean fclean re
88 |
--------------------------------------------------------------------------------
/ex03/include/ClapTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ClapTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:58 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 12:01:16 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include
18 | #include
19 |
20 | // classes
21 |
22 | class ClapTrap
23 | {
24 | protected:
25 | std::string _name;
26 | unsigned int _hit_pts;
27 | unsigned int _energy_pts;
28 | unsigned int _attack_dmg;
29 |
30 | public:
31 | // Constructors
32 | ClapTrap();
33 | ClapTrap(const ClapTrap ©);
34 | ClapTrap(std::string name);
35 |
36 | // Deconstructors
37 | virtual ~ClapTrap();
38 |
39 | // Overloaded Operators
40 | ClapTrap &operator=(const ClapTrap &src);
41 |
42 | // Public Methods
43 | void attack(const std::string &target);
44 | void takeDamage(unsigned int amount);
45 | void beRepaired(unsigned int amount);
46 | // Getter
47 |
48 | // Setter
49 |
50 | };
--------------------------------------------------------------------------------
/ex03/include/DiamondTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* DiamondTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/29 11:23:19 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 13:33:55 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include "ScavTrap.hpp"
18 | #include "FragTrap.hpp"
19 |
20 | // classes
21 |
22 | class DiamondTrap: public ScavTrap, public FragTrap
23 | {
24 | private:
25 | std::string _name;
26 | public:
27 | // Constructors
28 | DiamondTrap();
29 | DiamondTrap(const DiamondTrap ©);
30 | DiamondTrap(std::string name);
31 |
32 | // Deconstructors
33 | virtual ~DiamondTrap();
34 |
35 | // Overloaded Operators
36 | DiamondTrap &operator=(const DiamondTrap &src);
37 |
38 | // Public Methods
39 | void attack(const std::string &target);
40 | void whoAmI(void);
41 | // Getter
42 |
43 | // Setter
44 |
45 | };
--------------------------------------------------------------------------------
/ex03/include/FragTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* FragTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/28 15:27:18 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:51:52 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include "ClapTrap.hpp"
18 |
19 | // classes
20 |
21 | class FragTrap: virtual public ClapTrap
22 | {
23 | private:
24 |
25 | public:
26 | // Constructors
27 | FragTrap();
28 | FragTrap(const FragTrap ©);
29 | FragTrap(std::string name);
30 |
31 | // Deconstructors
32 | ~FragTrap();
33 |
34 | // Overloaded Operators
35 | FragTrap &operator=(const FragTrap &src);
36 |
37 | // Public Methods
38 | void highFiveGuys(void);
39 | // Getter
40 |
41 | // Setter
42 |
43 | };
44 |
--------------------------------------------------------------------------------
/ex03/include/ScavTrap.hpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ScavTrap.hpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 11:49:02 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:49:04 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // Header-protection
14 | #pragma once
15 |
16 | // Includes
17 | #include "ClapTrap.hpp"
18 |
19 | // classes
20 |
21 | class ScavTrap: virtual public ClapTrap
22 | {
23 | private:
24 | bool _guarding_gate;
25 | public:
26 | // Constructors
27 | ScavTrap();
28 | ScavTrap(const ScavTrap ©);
29 | ScavTrap(std::string name);
30 |
31 | // Deconstructors
32 | ~ScavTrap();
33 |
34 | // Overloaded Operators
35 | ScavTrap &operator=(const ScavTrap &src);
36 |
37 | // Public Methods
38 | void attack(const std::string &target);
39 | void guardGate(void);
40 | // Getter
41 |
42 | // Setter
43 |
44 | };
45 |
--------------------------------------------------------------------------------
/ex03/src/ClapTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ClapTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:50 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 14:03:14 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // #include "ClapTrap.hpp"
14 | #include "../include/ClapTrap.hpp"
15 |
16 |
17 | // Constructors
18 | ClapTrap::ClapTrap(): _name("default"), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
19 | {
20 | std::cout << "ClapTrap Default Constructor called" << std::endl;
21 | }
22 |
23 | ClapTrap::ClapTrap(const ClapTrap ©)
24 | {
25 | std::cout << "ClapTrap Copy Constructor called" << std::endl;
26 | *this = copy;
27 | }
28 |
29 | ClapTrap::ClapTrap(std::string name): _name(name), _hit_pts(10), _energy_pts(10), _attack_dmg(0)
30 | {
31 | std::cout << "ClapTrap Constructor for the name " << this->_name << " called" << std::endl;
32 | }
33 |
34 | // Deconstructors
35 | ClapTrap::~ClapTrap()
36 | {
37 | std::cout << "ClapTrap Deconstructor for " << this->_name << " called" << std::endl;
38 | }
39 |
40 | // Overloaded Operators
41 | ClapTrap &ClapTrap::operator=(const ClapTrap &src)
42 | {
43 | std::cout << "ClapTrap Assignation operator called" << std::endl;
44 | this->_name = src._name;
45 | this->_hit_pts = src._hit_pts;
46 | this->_energy_pts = src._energy_pts;
47 | this->_attack_dmg = src._attack_dmg;
48 | return *this;
49 | }
50 |
51 | // Public Methods
52 | void ClapTrap::attack(const std::string &target)
53 | {
54 | if (this->_energy_pts > 0 && this->_hit_pts > 0)
55 | {
56 | std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " points of damage!" << std::endl;
57 | this->_energy_pts--;
58 | }
59 | else if (this->_energy_pts == 0)
60 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left.\033[0m" << std::endl;
61 | else
62 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to attack " << target << ", because he has not enough hit points.\033[0m" << std::endl;
63 | }
64 |
65 | void ClapTrap::takeDamage(unsigned int amount)
66 | {
67 | if (this->_hit_pts > amount)
68 | this->_hit_pts -= amount;
69 | else if (this->_hit_pts > 0)
70 | this->_hit_pts = 0;
71 | else
72 | {
73 | std::cout << "\033[33mClapTrap " << this->_name << " is already dead, stop beating it.\033[0m" << std::endl;
74 | return ;
75 | }
76 | std::cout << "ClapTrap " << this->_name << " was attacked and lost " << amount << " hit points, he now has " << this->_hit_pts << " hit points." << std::endl;
77 | }
78 |
79 | void ClapTrap::beRepaired(unsigned int amount)
80 | {
81 | if (this->_energy_pts > 0 && this->_hit_pts > 0 && this->_hit_pts + amount <= 10)
82 | {
83 | this->_hit_pts += amount;
84 | std::cout << "ClapTrap " << this->_name << " repaired itself and gained " << amount << " of hit points, he now has " << this->_hit_pts << "hit points." << std::endl;
85 | this->_energy_pts--;
86 | }
87 | else if (this->_energy_pts == 0)
88 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to repair itself, because he doesn't have enough energy points.\033[0m" << std::endl;
89 | else if (this->_hit_pts == 0)
90 | std::cout << "\033[31mClapTrap " << this->_name << " is not able to repair itself, because he doesn't have enough hit points.\033[0m" << std::endl;
91 | else
92 | std::cout << "\033[33mClapTrap " << this->_name << " can't be repaired to have more than 10 hit points.\033[0m" << std::endl;
93 | }
94 | // Getter
95 |
96 | // Setter
97 |
98 |
--------------------------------------------------------------------------------
/ex03/src/DiamondTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* DiamondTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/29 11:22:15 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 12:23:31 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | // #include "DiamondTrap.hpp"
14 | #include "../include/DiamondTrap.hpp"
15 |
16 | // Constructors
17 | DiamondTrap::DiamondTrap(): ClapTrap("defaultDT_clap_trap")
18 | {
19 | this->_name = "defaultDT";
20 | this->_hit_pts = FragTrap::_hit_pts;
21 | this->_energy_pts = ScavTrap::_energy_pts;
22 | this->_attack_dmg = FragTrap::_attack_dmg;
23 | std::cout << "DiamondTrap Default Constructor called" << std::endl;
24 | }
25 |
26 | DiamondTrap::DiamondTrap(const DiamondTrap ©): ClapTrap(copy), ScavTrap(copy), FragTrap(copy)
27 | {
28 | *this = copy;
29 | std::cout << "DiamondTrap Copy Constructor called" << std::endl;
30 | }
31 |
32 | DiamondTrap::DiamondTrap(std::string name): ClapTrap(name + "_clap_trap")
33 | {
34 | this->_name = name;
35 | this->_hit_pts = FragTrap::_hit_pts;
36 | this->_energy_pts = ScavTrap::_energy_pts;
37 | this->_attack_dmg = FragTrap::_attack_dmg;
38 | std::cout << "DiamondTrap Constructor for the name " << this->_name << " called" << std::endl;
39 | }
40 |
41 | // Deconstructors
42 | DiamondTrap::~DiamondTrap()
43 | {
44 | std::cout << "DiamondTrap Deconstructor for " << this->_name << " called" << std::endl;
45 | }
46 |
47 | // Overloaded Operators
48 | DiamondTrap &DiamondTrap::operator=(const DiamondTrap &src)
49 | {
50 | std::cout << "DiamondTrap Assignation operator called" << std::endl;
51 | this->_name = src._name + "_clap_trap";
52 | this->_hit_pts = src._hit_pts;
53 | this->_energy_pts = src._energy_pts;
54 | this->_attack_dmg = src._attack_dmg;
55 | return *this;
56 | }
57 |
58 | // Public Methods
59 | void DiamondTrap::attack(const std::string &target)
60 | {
61 | ScavTrap::attack(target);
62 | }
63 |
64 | void DiamondTrap::whoAmI(void)
65 | {
66 | std::cout << "Hello i am a DiamondTrap named " << this->_name <<
67 | " and i am originated from the ClapTrap named " << ClapTrap::_name << "." <<
68 | std::endl;
69 | }
70 |
71 | // Getter
72 |
73 | // Setter
74 |
--------------------------------------------------------------------------------
/ex03/src/FragTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* FragTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/28 15:26:49 by tblaase #+# #+# */
9 | /* Updated: 2022/07/05 17:38:49 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "FragTrap.hpp"
14 |
15 | // Constructors
16 | FragTrap::FragTrap(): ClapTrap()
17 | {
18 | if (this->_hit_pts == 10)
19 | this->_hit_pts = 100;
20 | this->_energy_pts = 100;
21 | // if (this->_attack_dmg == 0)
22 | this->_attack_dmg = 30;
23 | std::cout << "FragTrap Default Constructor for " << this->_name << " called" << std::endl;
24 | }
25 |
26 | FragTrap::FragTrap(const FragTrap ©): ClapTrap(copy)
27 | {
28 | std::cout << "FragTrap Copy Constructor called" << std::endl;
29 | }
30 |
31 | FragTrap::FragTrap(std::string name): ClapTrap(name)
32 | {
33 | // this->_name = name;
34 | if (this->_hit_pts == 10)
35 | this->_hit_pts = 100;
36 | this->_energy_pts = 100;
37 | if (this->_attack_dmg == 0)
38 | this->_attack_dmg = 30;
39 | std::cout << "FragTrap Constructor for the name " << this->_name << " called" << std::endl;
40 | }
41 |
42 | // Deconstructors
43 | FragTrap::~FragTrap()
44 | {
45 | std::cout << "FragTrap Deconstructor for " << this->_name << " called" << std::endl;
46 | }
47 |
48 | // Overloaded Operators
49 | FragTrap &FragTrap::operator=(const FragTrap &src)
50 | {
51 | std::cout << "FragTrap Assignation operator called" << std::endl;
52 | this->_name = src._name;
53 | this->_hit_pts = src._hit_pts;
54 | this->_energy_pts = src._energy_pts;
55 | this->_attack_dmg = src._attack_dmg;
56 | return *this;
57 | }
58 |
59 | // Public Methods
60 |
61 | void FragTrap::highFiveGuys(void)
62 | {
63 | std::cout << "FragTrap " << this->_name << ": You want a high five?\n\t*WHAMM*\nHere you go." << std::endl;
64 | }
65 |
66 | // Getter
67 |
68 | // Setter
69 |
70 |
--------------------------------------------------------------------------------
/ex03/src/ScavTrap.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ScavTrap.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 12:19:54 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:49:30 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ScavTrap.hpp"
14 |
15 | // Constructors
16 | ScavTrap::ScavTrap(): ClapTrap()
17 | {
18 | this->_hit_pts = 100;
19 | this->_energy_pts = 50;
20 | this->_attack_dmg = 20;
21 | this->_guarding_gate = false;
22 | std::cout << "ScavTrap Default Constructor for " << this->_name << " called" << std::endl;
23 | }
24 |
25 | ScavTrap::ScavTrap(const ScavTrap ©): ClapTrap(copy)
26 | {
27 | this->_guarding_gate = copy._guarding_gate;
28 | std::cout << "ScavTrap Copy Constructor called" << std::endl;
29 | }
30 |
31 | ScavTrap::ScavTrap(std::string name): ClapTrap(name)
32 | {
33 | // this->_name = name;
34 | this->_hit_pts = 100;
35 | this->_energy_pts = 50;
36 | this->_attack_dmg = 20;
37 | this->_guarding_gate = false;
38 | std::cout << "ScavTrap Constructor for the name " << this->_name << " called" << std::endl;
39 | }
40 |
41 | // Deconstructors
42 | ScavTrap::~ScavTrap()
43 | {
44 | std::cout << "ScavTrap Deconstructor for " << this->_name << " called" << std::endl;
45 | }
46 |
47 | // Overloaded Operators
48 | ScavTrap &ScavTrap::operator=(const ScavTrap &src)
49 | {
50 | std::cout << "ScavTrap Assignation operator called" << std::endl;
51 | this->_name = src._name;
52 | this->_hit_pts = src._hit_pts;
53 | this->_energy_pts = src._energy_pts;
54 | this->_attack_dmg = src._attack_dmg;
55 | return *this;
56 | }
57 |
58 | // Public Methods
59 | void ScavTrap::attack(const std::string &target)
60 | {
61 | if (this->_energy_pts > 0 && this->_hit_pts > 0)
62 | {
63 | std::cout << "ScavTrap " << this->_name << " attacks " << target << ", causing " << this->_attack_dmg << " points of damage!" << std::endl;
64 | this->_energy_pts--;
65 | }
66 | else if (this->_energy_pts == 0)
67 | std::cout << "\033[31mScavTrap " << this->_name << " is not able to attack " << target << ", because he has no energy points left.\033[0m" << std::endl;
68 | else
69 | std::cout << "\033[31mScavTrap " << this->_name << " is not able to attack " << target << ", because he has not enough hit points.\033[0m" << std::endl;
70 | }
71 |
72 | void ScavTrap::guardGate(void)
73 | {
74 | if (this->_guarding_gate == false)
75 | {
76 | this->_guarding_gate = true;
77 | std::cout << "ScavTrap " << this->_name << " is now guarding the gate." << std::endl;
78 | }
79 | else
80 | std::cout << "\033[33mScavTrap " << this->_name << " is already guarding the gate.\033[0m" << std::endl;
81 | }
82 |
83 | // Getter
84 |
85 | // Setter
86 |
87 |
--------------------------------------------------------------------------------
/ex03/src/main.cpp:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* main.cpp :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: tblaase +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2022/03/25 10:33:55 by tblaase #+# #+# */
9 | /* Updated: 2022/03/29 16:54:11 by tblaase ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "DiamondTrap.hpp"
14 |
15 | int main()
16 | {
17 | std::cout << "### TESTING CLAPTRAP ###\n" << std::endl;
18 | {
19 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
20 | ClapTrap a;
21 | ClapTrap b("Cody");
22 |
23 | std::cout << "\033[34mTesting\033[0m" << std::endl;
24 | a.attack("some other robot");
25 | a.takeDamage(10);
26 | a.takeDamage(10);
27 | a.beRepaired(5);
28 | a.attack("some other other robot");
29 | b.beRepaired(3);
30 | for (int i = 0; i < 12; i++)
31 | b.attack("Cody-clone");
32 | b.beRepaired(3);
33 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
34 | }
35 | std::cout << "\n\n### TESTING SCAVTRAP ###\n" << std::endl;
36 | {
37 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
38 | ScavTrap a;
39 | ScavTrap b("Savage");
40 |
41 | std::cout << "\033[34mTesting\033[0m" << std::endl;
42 | a.attack("CloneTrap");
43 | // for (int i = 0; i < 50; i++)
44 | // a.attack("CloneTrap");
45 | a.beRepaired(22);
46 | a.takeDamage(21);
47 | a.beRepaired(22);
48 | a.guardGate();
49 | a.guardGate();
50 | b.attack("Savage-clone");
51 | b.takeDamage(101);
52 | b.takeDamage(15);
53 | b.attack("ScavTrap-clone");
54 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
55 | }
56 | std::cout << "\n\n### TESTING FRAGTRAP ###\n" << std::endl;
57 | {
58 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
59 | FragTrap a;
60 | FragTrap b("Chadd");
61 |
62 | std::cout << "\033[34mTesting\033[0m" << std::endl;
63 | a.highFiveGuys();
64 | a.attack("some random dude");
65 | a.takeDamage(101);
66 | a.takeDamage(1);
67 | a.attack("some random dude");
68 | b.highFiveGuys();
69 | // for(int i = 0; i < 101; i++)
70 | // b.attack("FragTrap-clone");
71 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
72 | }
73 | std::cout << "\n\n### TESTING DIAMONDTRAP ###\n" << std::endl;
74 | {
75 | std::cout << "\033[34mConstructing\033[0m" << std::endl;
76 | DiamondTrap a;
77 | DiamondTrap b("Giga Chadd");
78 | DiamondTrap c(a);
79 |
80 | std::cout << "\033[34mTesting\033[0m" << std::endl;
81 | a.whoAmI();
82 | a.attack("some super random dude");
83 | b.whoAmI();
84 | b.attack("Chadd-clone");
85 | c.whoAmI();
86 | std::cout << "\033[34mDeconstructing\033[0m" << std::endl;
87 | }
88 | return (0);
89 | }
90 |
--------------------------------------------------------------------------------
/readme_additions/result.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tblaase/CPP-Module-03/e08c6c7e086c39e58021d51ab85dc098c803b356/readme_additions/result.png
--------------------------------------------------------------------------------