This commit is contained in:
2024-02-20 15:35:19 +01:00
parent a5816720e4
commit 93c619a3c7
267 changed files with 10113 additions and 0 deletions

View 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
cpp04/ex03/.vscode/launch.json vendored Normal file
View 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/cpp04/ex03",
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex03/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

59
cpp04/ex03/.vscode/settings.json vendored Normal file
View 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
}

27
cpp04/ex03/AMateria.cpp Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AMateria.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 16:47:27 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 16:48:56 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "AMateria.hpp"
AMateria::AMateria(std::string const & type) : type(type) {}
AMateria::~AMateria() {}
std::string const & AMateria::getType() const
{
return (this->type);
}
void AMateria::use(ICharacter& target)
{
std::cout << "* uses some unknown materia on " << target.getName() << " *" << std::endl;
}

34
cpp04/ex03/AMateria.hpp Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AMateria.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 16:44:47 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 17:09:18 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef AMATERIA_HPP
#define AMATERIA_HPP
#include "ICharacter.hpp"
#include <string>
#include <iostream>
class AMateria
{
protected:
std::string type;
public:
AMateria(std::string const & type);
virtual ~AMateria();
std::string const & getType() const; // Renvoie le type de la materia
virtual AMateria* clone() const = 0; // Doit être implémenté dans les sous-classes
virtual void use(ICharacter& target); // Peut être redéfini dans les sous-classes
};
#endif

BIN
cpp04/ex03/AMateria.o Normal file

Binary file not shown.

82
cpp04/ex03/Character.cpp Normal file
View File

@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Character.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 16:58:59 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 17:28:36 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Character.hpp"
#include "AMateria.hpp"
Character::Character(std::string name) : name(name)
{
for (int i = 0; i < 4; ++i)
{
inventory[i] = nullptr;
}
}
Character::Character(const Character& other)
{
*this = other;
}
Character& Character::operator=(const Character& other)
{
if (this != &other)
{
name = other.name;
for (int i = 0; i < 4; ++i)
{
delete inventory[i]; // Assurez-vous de libérer la mémoire de l'inventaire actuel
inventory[i] = other.inventory[i] ? other.inventory[i]->clone() : nullptr;
}
}
return (*this);
}
Character::~Character()
{
for (int i = 0; i < 4; ++i)
{
delete inventory[i];
}
}
std::string const & Character::getName() const
{
return (name);
}
void Character::equip(AMateria* m)
{
for (int i = 0; i < 4; ++i)
{
if (!inventory[i])
{
inventory[i] = m;
break;
}
}
}
void Character::unequip(int idx)
{
if (idx >= 0 && idx < 4)
{
inventory[idx] = nullptr;
}
}
void Character::use(int idx, ICharacter& target)
{
if (idx >= 0 && idx < 4 && inventory[idx])
{
inventory[idx]->use(target);
}
}

36
cpp04/ex03/Character.hpp Normal file
View File

@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Character.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 16:58:01 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 16:58:40 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CHARACTER_HPP
#define CHARACTER_HPP
#include "ICharacter.hpp"
class Character : public ICharacter
{
private:
std::string name;
AMateria* inventory[4]; // Inventaire avec 4 emplacements pour les Materias
public:
Character(std::string name);
Character(const Character& other);
Character& operator=(const Character& other);
virtual ~Character();
virtual std::string const & getName() const override;
virtual void equip(AMateria* m) override;
virtual void unequip(int idx) override;
virtual void use(int idx, ICharacter& target) override;
};
#endif

BIN
cpp04/ex03/Character.o Normal file

Binary file not shown.

35
cpp04/ex03/Cure.cpp Normal file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cure.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 17:03:56 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 17:04:37 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Cure.hpp"
Cure::Cure() : AMateria("cure") {}
Cure::Cure(const Cure& other) : AMateria(other) {}
Cure& Cure::operator=(const Cure& other)
{
AMateria::operator=(other);
return (*this);
}
Cure::~Cure() {}
AMateria* Cure::clone() const
{
return new Cure(*this);
}
void Cure::use(ICharacter& target)
{
std::cout << "* heals " << target.getName() << "’s wounds *" << std::endl;
}

30
cpp04/ex03/Cure.hpp Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cure.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 17:03:19 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 17:03:43 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CURE_HPP
#define CURE_HPP
#include "AMateria.hpp"
class Cure : public AMateria
{
public:
Cure();
Cure(const Cure& other);
Cure& operator=(const Cure& other);
virtual ~Cure();
virtual AMateria* clone() const override;
virtual void use(ICharacter& target) override;
};
#endif

BIN
cpp04/ex03/Cure.o Normal file

Binary file not shown.

31
cpp04/ex03/ICharacter.hpp Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ICharacter.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 16:52:43 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 17:25:11 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ICHARACTER_HPP
#define ICHARACTER_HPP
#include <string>
class AMateria;
class ICharacter
{
public:
virtual ~ICharacter() {}
virtual std::string const & getName() const = 0; // Doit retourner le nom du personnage
virtual void equip(AMateria* m) = 0; // Équipe une AMateria
virtual void unequip(int idx) = 0; // Déséquipe une AMateria sans la détruire
virtual void use(int idx, ICharacter& target) = 0; // Utilise une AMateria sur un ICharacter
};
#endif

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* IMateriaSource.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 17:06:19 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 17:07:06 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef IMATERIASOURCE_HPP
#define IMATERIASOURCE_HPP
#include "AMateria.hpp"
class IMateriaSource
{
public:
virtual ~IMateriaSource() {}
virtual void learnMateria(AMateria*) = 0;
virtual AMateria* createMateria(std::string const & type) = 0;
};
#endif

35
cpp04/ex03/Ice.cpp Normal file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Ice.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 16:49:57 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 16:50:52 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Ice.hpp"
Ice::Ice() : AMateria("ice") {}
Ice::Ice(const Ice& other) : AMateria(other) {}
Ice& Ice::operator=(const Ice& other)
{
AMateria::operator=(other);
return (*this);
}
Ice::~Ice() {}
AMateria* Ice::clone() const
{
return new Ice(*this);
}
void Ice::use(ICharacter& target)
{
std::cout << "* shoots an ice bolt at " << target.getName() << " *" << std::endl;
}

30
cpp04/ex03/Ice.hpp Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Ice.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 16:49:13 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 16:49:44 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ICE_HPP
#define ICE_HPP
#include "AMateria.hpp"
class Ice : public AMateria
{
public:
Ice();
Ice(const Ice& other);
Ice& operator=(const Ice& other);
virtual ~Ice();
virtual AMateria* clone() const override;
virtual void use(ICharacter& target) override;
};
#endif

BIN
cpp04/ex03/Ice.o Normal file

Binary file not shown.

72
cpp04/ex03/Makefile Normal file
View File

@@ -0,0 +1,72 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2023/12/31 17:14:41 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
LOGO = @echo " ██▓ ▄▄▄ ▓█████▄▓█████ ▄▄▄▄ ▓█████▒███████▓█████";\
echo " ▓██▒ ▒████▄ ▒██▀ ██▓█ ▀▓█████▄▓█ ▀▒ ▒ ▒ ▄▀▓█ ▀";\
echo " ▒██░ ▒██ ▀█▄ ░██ █▒███ ▒██▒ ▄█▒███ ░ ▒ ▄▀▒░▒███";\
echo " ▒██░ ░██▄▄▄▄██░▓█▄ ▒▓█ ▄▒██░█▀ ▒▓█ ▄ ▄▀▒ ▒▓█ ▄";\
echo " ░██████▓█ ▓██░▒████▓░▒████░▓█ ▀█░▒████▒███████░▒████▒";\
echo " ░ ▒░▓ ▒▒ ▓▒█░▒▒▓ ▒░░ ▒░ ░▒▓███▀░░ ▒░ ░▒▒ ▓░▒░░░ ▒░ ░";\
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 = materia
SRC = AMateria.cpp \
Character.cpp \
Cure.cpp \
Ice.cpp \
MateriaSource.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 "./materia" 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

View File

@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MateriaSource.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 17:08:37 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 17:11:59 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "MateriaSource.hpp"
MateriaSource::MateriaSource()
{
materias.fill(nullptr);
}
MateriaSource::MateriaSource(const MateriaSource& other)
{
*this = other;
}
MateriaSource& MateriaSource::operator=(const MateriaSource& other)
{
if (this != &other)
{
for (int i = 0; i < 4; ++i)
{
delete materias[i];
materias[i] = other.materias[i] ? other.materias[i]->clone() : nullptr;
}
}
return (*this);
}
MateriaSource::~MateriaSource()
{
for (auto& materia : materias)
{
delete materia;
}
}
void MateriaSource::learnMateria(AMateria* m)
{
for (auto& materia : materias)
{
if (!materia)
{
materia = m;
break;
}
}
}
AMateria* MateriaSource::createMateria(std::string const & type)
{
for (auto& materia : materias)
{
if (materia && materia->getType() == type)
{
return materia->clone();
}
}
return (nullptr);
}

View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MateriaSource.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 17:07:33 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 17:08:05 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MATERIASOURCE_HPP
#define MATERIASOURCE_HPP
#include "IMateriaSource.hpp"
#include <array>
class MateriaSource : public IMateriaSource
{
private:
std::array<AMateria*, 4> materias; // Peut contenir jusqu'à 4 types de Materias
public:
MateriaSource();
MateriaSource(const MateriaSource& other);
MateriaSource& operator=(const MateriaSource& other);
virtual ~MateriaSource();
virtual void learnMateria(AMateria* m) override;
virtual AMateria* createMateria(std::string const & type) override;
};
#endif

BIN
cpp04/ex03/MateriaSource.o Normal file

Binary file not shown.

70
cpp04/ex03/main.cpp Normal file
View File

@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/31 17:14:54 by fgras-ca #+# #+# */
/* Updated: 2023/12/31 17:36:22 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "MateriaSource.hpp"
#include "Character.hpp"
#include "Ice.hpp"
#include "Cure.hpp"
int main()
{
IMateriaSource* src = new MateriaSource();
src->learnMateria(new Ice());
src->learnMateria(new Cure());
ICharacter* me = new Character("me");
ICharacter* bob = new Character("bob");
std::string input;
while (true)
{
std::cout << "Enter command (equip, use, or quit): ";
std::cin >> input;
if (input == "equip")
{
std::string materiaType;
std::cout << "Enter materia type (ice or cure): ";
std::cin >> materiaType;
AMateria* tmp = src->createMateria(materiaType);
if (tmp)
{
me->equip(tmp);
std::cout << materiaType << " equipped." << std::endl;
}
else
{
std::cout << "Unknown materia type." << std::endl;
}
}
else if (input == "use")
{
int idx;
std::cout << "Enter inventory index (0-3): ";
std::cin >> idx;
me->use(idx, *bob);
}
else if (input == "quit")
{
break;
}
else
{
std::cout << "Unknown command." << std::endl;
}
}
delete bob;
delete me;
delete src;
return (0);
}

BIN
cpp04/ex03/main.o Normal file

Binary file not shown.

BIN
cpp04/ex03/materia Executable file

Binary file not shown.