mirror of
https://github.com/Ladebeze66/cpp-partie-1.git
synced 2026-11-09 13:17:15 +01:00
ccp
This commit is contained in:
18
cpp03/ex01/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp03/ex01/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "linux-gcc-x64",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "${default}",
|
||||
"cppStandard": "${default}",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"compilerArgs": [
|
||||
""
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
24
cpp03/ex01/.vscode/launch.json
vendored
Normal file
24
cpp03/ex01/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "C/C++ Runner: Debug Session",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"externalConsole": false,
|
||||
"cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex01",
|
||||
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex01/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
59
cpp03/ex01/.vscode/settings.json
vendored
Normal file
59
cpp03/ex01/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||
"C_Cpp_Runner.cppCompilerPath": "g++",
|
||||
"C_Cpp_Runner.debuggerPath": "gdb",
|
||||
"C_Cpp_Runner.cStandard": "",
|
||||
"C_Cpp_Runner.cppStandard": "",
|
||||
"C_Cpp_Runner.msvcBatchPath": "",
|
||||
"C_Cpp_Runner.useMsvc": false,
|
||||
"C_Cpp_Runner.warnings": [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wpedantic",
|
||||
"-Wshadow",
|
||||
"-Wformat=2",
|
||||
"-Wcast-align",
|
||||
"-Wconversion",
|
||||
"-Wsign-conversion",
|
||||
"-Wnull-dereference"
|
||||
],
|
||||
"C_Cpp_Runner.msvcWarnings": [
|
||||
"/W4",
|
||||
"/permissive-",
|
||||
"/w14242",
|
||||
"/w14287",
|
||||
"/w14296",
|
||||
"/w14311",
|
||||
"/w14826",
|
||||
"/w44062",
|
||||
"/w44242",
|
||||
"/w14905",
|
||||
"/w14906",
|
||||
"/w14263",
|
||||
"/w44265",
|
||||
"/w14928"
|
||||
],
|
||||
"C_Cpp_Runner.enableWarnings": true,
|
||||
"C_Cpp_Runner.warningsAsError": false,
|
||||
"C_Cpp_Runner.compilerArgs": [],
|
||||
"C_Cpp_Runner.linkerArgs": [],
|
||||
"C_Cpp_Runner.includePaths": [],
|
||||
"C_Cpp_Runner.includeSearch": [
|
||||
"*",
|
||||
"**/*"
|
||||
],
|
||||
"C_Cpp_Runner.excludeSearch": [
|
||||
"**/build",
|
||||
"**/build/**",
|
||||
"**/.*",
|
||||
"**/.*/**",
|
||||
"**/.vscode",
|
||||
"**/.vscode/**"
|
||||
],
|
||||
"C_Cpp_Runner.useAddressSanitizer": false,
|
||||
"C_Cpp_Runner.useUndefinedSanitizer": false,
|
||||
"C_Cpp_Runner.useLeakSanitizer": false,
|
||||
"C_Cpp_Runner.showCompilationTime": false,
|
||||
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||
"C_Cpp_Runner.msvcSecureNoWarnings": false
|
||||
}
|
||||
114
cpp03/ex01/ClapTrap.cpp
Normal file
114
cpp03/ex01/ClapTrap.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ClapTrap.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 13:59:36 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/30 22:28:06 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
ClapTrap::ClapTrap():Name(""), HitPoints(10), EnergyPoints(10), AttackDamage(0) {}
|
||||
|
||||
ClapTrap::ClapTrap(std::string name) : Name(name), HitPoints(10), EnergyPoints(10), AttackDamage(0)
|
||||
{
|
||||
std::cout << GREEN << "ClapTrap contructor " << this->Name << " is created!\n" << RESET << std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(const ClapTrap ©)
|
||||
{
|
||||
*this = copy;
|
||||
std::cout << CYAN << "Copy constructor called for ClapTrap " << this->Name << RESET << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
ClapTrap &ClapTrap::operator = (const ClapTrap &operator_aff)
|
||||
{
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
this->Name = operator_aff.Name;
|
||||
this->HitPoints = operator_aff.HitPoints;
|
||||
this->EnergyPoints = operator_aff.EnergyPoints;
|
||||
this->AttackDamage = operator_aff.AttackDamage;
|
||||
}
|
||||
std::cout << MAGENTA << "Assignment operator called for ClapTrap " << this->Name << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
ClapTrap::~ClapTrap()
|
||||
{
|
||||
std::cout << RED << "\nClapTrap " << this->Name << " is detroyed!" << RESET << std::endl;
|
||||
}
|
||||
|
||||
void ClapTrap::attack(const std::string& target)
|
||||
{
|
||||
if (EnergyPoints > 0 && HitPoints > 0)
|
||||
{
|
||||
std::cout << MAGENTA << "ClapTrap " << Name << " attacks " << target << ", causing " << AttackDamage << " points of damage!" << RESET << std::endl;
|
||||
EnergyPoints--;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "ClapTrap " << Name << " has no energy or hit points to attack!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ClapTrap::takeDamage(unsigned int amount)
|
||||
{
|
||||
if (HitPoints > amount)
|
||||
{
|
||||
HitPoints -= amount;
|
||||
std::cout << RED << "ClapTrap " << Name << " takes " << amount << " points of damage!" << RESET << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
HitPoints = 0;
|
||||
std::cout << "ClapTrap " << Name << " is out of hit points and can't take more damage!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ClapTrap::beRepaired(unsigned int amount)
|
||||
{
|
||||
if (EnergyPoints > 0 && HitPoints > 0)
|
||||
{
|
||||
HitPoints += amount;
|
||||
EnergyPoints--;
|
||||
std::cout << BLUE << "ClapTrap " << Name << " repairs itself, recovering " << amount << " hit points!" << RESET << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "ClapTrap " << Name << " has no energy or hit points to repair itself!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ClapTrap::getName() const
|
||||
{
|
||||
return (this->Name);
|
||||
}
|
||||
|
||||
unsigned int ClapTrap::getHitPoints() const
|
||||
{
|
||||
return (this->HitPoints);
|
||||
}
|
||||
|
||||
unsigned int ClapTrap::getEnergyPoints() const
|
||||
{
|
||||
return (this->EnergyPoints);
|
||||
}
|
||||
|
||||
unsigned int ClapTrap::getAttackDamage() const
|
||||
{
|
||||
return (this->AttackDamage);
|
||||
}
|
||||
|
||||
void ClapTrap::printStats() const
|
||||
{
|
||||
std::cout << GREEN << "\nStatistiques pour " << Name << ":" << RESET << std::endl;
|
||||
std::cout << "Points de vie: " << HitPoints << std::endl;
|
||||
std::cout << "Points d'énergie: " << EnergyPoints << std::endl;
|
||||
std::cout << "Dégâts d'attaque: " << AttackDamage << std::endl;
|
||||
}
|
||||
55
cpp03/ex01/ClapTrap.hpp
Normal file
55
cpp03/ex01/ClapTrap.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ClapTrap.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 13:53:03 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/30 22:24:32 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef CLAPTRAP_HPP
|
||||
#define CLAPTRAP_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
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 & operator = (const ClapTrap &operator_aff);
|
||||
~ClapTrap();
|
||||
|
||||
void attack(const std::string& target);
|
||||
void takeDamage(unsigned int amount);
|
||||
void beRepaired(unsigned int amount);
|
||||
|
||||
std::string getName() const;
|
||||
unsigned int getHitPoints() const;
|
||||
unsigned int getEnergyPoints() const;
|
||||
unsigned int getAttackDamage() const;
|
||||
void printStats() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
63
cpp03/ex01/Makefile
Normal file
63
cpp03/ex01/Makefile
Normal file
@@ -0,0 +1,63 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2023/12/30 16:04:30 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\
|
||||
echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\
|
||||
echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\
|
||||
echo "( ( ( ( ( ( ( ("
|
||||
|
||||
DEF_COLOR = \033[0m
|
||||
GRAY = \033[0;90m
|
||||
RED = \033[0;91m
|
||||
GREEN = \033[0;92m
|
||||
YELLOW = \033[0;93m
|
||||
BLUE = \033[0;94m
|
||||
MAGENTA = \033[0;95m
|
||||
CYAN = \033[0;96m
|
||||
WHITE = \033[0;97m
|
||||
ORANGE = \033[38;5;214m
|
||||
|
||||
NAME = scavtrap
|
||||
|
||||
SRC = ClapTrap.cpp \
|
||||
ScavTrap.cpp \
|
||||
main.cpp \
|
||||
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./scavtrap" to execute the program!!$(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
clean :
|
||||
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
|
||||
$(RM) $(OBJS)
|
||||
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
fclean : clean
|
||||
@echo "$(RED)Delete program name... $(DEF_COLOR)"
|
||||
$(RM) $(NAME)
|
||||
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
||||
70
cpp03/ex01/ScavTrap.cpp
Normal file
70
cpp03/ex01/ScavTrap.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ScavTrap.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 15:42:16 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/30 22:28:16 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ScavTrap.hpp"
|
||||
|
||||
ScavTrap::ScavTrap() : ClapTrap()
|
||||
{
|
||||
this->HitPoints = 100;
|
||||
this->EnergyPoints = 50;
|
||||
this->AttackDamage = 20;
|
||||
}
|
||||
|
||||
ScavTrap::ScavTrap(std::string name) : ClapTrap(name)
|
||||
{
|
||||
this->HitPoints = 100;
|
||||
this->EnergyPoints = 50;
|
||||
this->AttackDamage = 20;
|
||||
std::cout << GREEN << "ScavTrap constructor " << this->Name << " has been created." << RESET << std::endl;
|
||||
}
|
||||
|
||||
ScavTrap::ScavTrap(const ScavTrap ©) : ClapTrap(copy)
|
||||
{
|
||||
*this = copy;
|
||||
std::cout << CYAN << "Copy constructor called for ScavTrap " << this->Name << RESET << std::endl;
|
||||
}
|
||||
|
||||
ScavTrap &ScavTrap::operator=(const ScavTrap &operator_aff)
|
||||
{
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
this->Name = operator_aff.Name;
|
||||
this->HitPoints = operator_aff.HitPoints;
|
||||
this->EnergyPoints = operator_aff.EnergyPoints;
|
||||
this->AttackDamage = operator_aff.AttackDamage;
|
||||
}
|
||||
std::cout << MAGENTA << "Assignment operator called for ScavTrap " << this->Name << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
ScavTrap::~ScavTrap()
|
||||
{
|
||||
std::cout << RED << "ScavTrap " << this->Name << " has been destroyed." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void ScavTrap::attack(const std::string& target)
|
||||
{
|
||||
if (this->EnergyPoints > 0 && this->HitPoints > 0)
|
||||
{
|
||||
std::cout << CYAN << "ScavTrap " << this->Name << " attacks " << target << ", causing " << this->AttackDamage << " points of damage!" << RESET << std::endl;
|
||||
this->EnergyPoints--;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "ScavTrap " << this->Name << " has no energy or hit points to attack!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ScavTrap::guardGate()
|
||||
{
|
||||
std::cout << "ScavTrap " << this->Name << " is now in Gate keeper mode." << std::endl;
|
||||
}
|
||||
31
cpp03/ex01/ScavTrap.hpp
Normal file
31
cpp03/ex01/ScavTrap.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ScavTrap.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 15:34:27 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/30 22:24:07 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SCAVTRAP_HPP
|
||||
#define SCAVTRAP_HPP
|
||||
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
class ScavTrap : public ClapTrap
|
||||
{
|
||||
public:
|
||||
ScavTrap();
|
||||
ScavTrap(std::string name);
|
||||
ScavTrap(const ScavTrap ©);
|
||||
ScavTrap & operator = (const ScavTrap &operator_aff);
|
||||
~ScavTrap();
|
||||
|
||||
void attack(const std::string& target);
|
||||
void guardGate();
|
||||
};
|
||||
|
||||
#endif
|
||||
59
cpp03/ex01/main.cpp
Normal file
59
cpp03/ex01/main.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 15:59:39 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/30 20:40:16 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ClapTrap.hpp"
|
||||
#include "ScavTrap.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
ClapTrap claptrap1("Clappy");
|
||||
ScavTrap scavtrap1("Serena");
|
||||
|
||||
claptrap1.printStats();
|
||||
scavtrap1.printStats();
|
||||
|
||||
std::cout << "\n--- Tests ClapTrap ---" << std::endl;
|
||||
claptrap1.attack("un ennemi");
|
||||
claptrap1.takeDamage(3);
|
||||
claptrap1.beRepaired(2);
|
||||
|
||||
std::cout << "\n--- Tests ScavTrap ---" << std::endl;
|
||||
scavtrap1.attack("un autre ennemi");
|
||||
scavtrap1.takeDamage(15);
|
||||
scavtrap1.beRepaired(10);
|
||||
scavtrap1.guardGate();
|
||||
|
||||
std::cout << "\n--- Tests avancés : Interactions entre ClapTrap et ScavTrap ---" << std::endl;
|
||||
claptrap1.attack(scavtrap1.getName());
|
||||
scavtrap1.takeDamage(claptrap1.getAttackDamage());
|
||||
|
||||
scavtrap1.attack(claptrap1.getName());
|
||||
claptrap1.takeDamage(scavtrap1.getAttackDamage());
|
||||
|
||||
std::cout << "\n--- Tests des limites ---" << std::endl;
|
||||
while (scavtrap1.getEnergyPoints() > 0)
|
||||
{
|
||||
scavtrap1.attack(claptrap1.getName());
|
||||
claptrap1.takeDamage(scavtrap1.getAttackDamage());
|
||||
}
|
||||
|
||||
std::cout << "\n--- ScavTrap essaie d'attaquer sans points d'énergie ---" << std::endl;
|
||||
scavtrap1.attack(claptrap1.getName());
|
||||
|
||||
std::cout << "\n--- ClapTrap essaie de se réparer avec des points de vie au maximum ---" << std::endl;
|
||||
claptrap1.beRepaired(1);
|
||||
|
||||
claptrap1.printStats();
|
||||
scavtrap1.printStats();
|
||||
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user