Browse Source

finished ex03, more test to do

bastien 1 month ago
parent
commit
b41079a1b4

+ 2 - 2
Module_04/ex03/Makefile

@@ -6,7 +6,7 @@
 #    By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+         #
 #                                                 +#+#+#+#+#+   +#+            #
 #    Created: 2016/07/24 00:00:08 by bchanot           #+#    #+#              #
-#    Updated: 2025/12/18 17:07:12 by bchanot          ###   ########.fr        #
+#    Updated: 2025/12/25 01:34:11 by bchanot          ###   ########.fr        #
 #                                                                              #
 # **************************************************************************** #
 
@@ -16,7 +16,7 @@ SRCS_DIR = srcs/
 OBJS_DIR = .objects/
 CC = g++ -Wall -Wextra -Werror -g
 INC = -I./includes
-FILES = main Animal Dog Cat AnimalWrong CatWrong
+FILES = main AMateria Character Cure Ice MateriaSource
 SRCS = $(FILES)
 OBJS = $(addprefix $(OBJS_DIR), $(addsuffix .o, $(SRCS)))
 RED = \033[1;31m

+ 2 - 2
Module_04/ex03/includes/AMateria.hpp

@@ -6,7 +6,7 @@
 /*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/18 19:23:39 by bchanot           #+#    #+#             */
-/*   Updated: 2025/12/19 15:29:08 by bchanot          ###   ########.fr       */
+/*   Updated: 2025/12/26 00:02:09 by bchanot          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -34,7 +34,7 @@ class AMateria
 		virtual void 			use(ICharacter& target);
 
 	protected:
-		std::string const		&_type;
+		std::string		_type;
 
 };
 

+ 2 - 2
Module_04/ex03/includes/Character.hpp

@@ -6,7 +6,7 @@
 /*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/19 16:40:56 by bchanot           #+#    #+#             */
-/*   Updated: 2025/12/19 16:48:33 by bchanot          ###   ########.fr       */
+/*   Updated: 2025/12/26 00:27:46 by bchanot          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -36,7 +36,7 @@ class Character : public ICharacter
 		virtual void use(int idx, ICharacter& target);
 
 	private:
-		std::string	const &	_name;
+		std::string		 	_name;
 		AMateria			*_items[4];
 
 };

+ 3 - 3
Module_04/ex03/includes/ICharacter.hpp

@@ -6,7 +6,7 @@
 /*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/18 19:26:25 by bchanot           #+#    #+#             */
-/*   Updated: 2025/12/19 16:48:15 by bchanot          ###   ########.fr       */
+/*   Updated: 2025/12/25 23:44:28 by bchanot          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -16,7 +16,7 @@
 # include <iostream>
 # include <string>
 
-#include "AMateria.hpp"
+class AMateria;
 
 class ICharacter
 {
@@ -27,7 +27,7 @@ class ICharacter
 
 
 		virtual std::string const & 	getName() const = 0;
-		virtual void 					equip(AMateria* m) = 0;
+		virtual void 					equip(AMateria *m) = 0;
 		virtual void 					unequip(int idx) = 0;
 		virtual void 					use(int idx, ICharacter& target) = 0;
 

+ 30 - 0
Module_04/ex03/includes/IMateriaSource.hpp

@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   IMateriaSource.hpp                                 :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/24 15:35:03 by bchanot           #+#    #+#             */
+/*   Updated: 2025/12/24 15:35:58 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef IMATERIASOURCE_HPP
+# define IMATERIASOURCE_HPP
+
+# include <iostream>
+# include <string>
+#include "AMateria.hpp"
+
+class IMateriaSource
+{
+	public:
+		virtual ~IMateriaSource() {}
+		virtual void learnMateria(AMateria*) = 0;
+		virtual AMateria* createMateria(std::string const & type) = 0;
+};
+
+std::ostream &			operator<<( std::ostream & o, IMateriaSource const & i );
+
+#endif /* ************************************************** IMATERIASOURCE_H */

+ 42 - 0
Module_04/ex03/includes/MateriaSource.hpp

@@ -0,0 +1,42 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   MateriaSource.hpp                                  :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/24 15:47:01 by bchanot           #+#    #+#             */
+/*   Updated: 2025/12/25 01:28:03 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef MATERIASOURCE_HPP
+# define MATERIASOURCE_HPP
+
+# include <iostream>
+# include <string>
+#include "AMateria.hpp"
+#include "IMateriaSource.hpp"
+
+class MateriaSource : public IMateriaSource
+{
+
+	public:
+
+		MateriaSource();
+		MateriaSource( MateriaSource const & src );
+		~MateriaSource();
+
+		virtual void learnMateria(AMateria* source);
+		virtual AMateria* createMateria(std::string const & type);
+
+		MateriaSource &		operator=( MateriaSource const & rhs );
+
+	private:
+		AMateria		*_items[4];
+
+};
+
+std::ostream &			operator<<( std::ostream & o, MateriaSource const & i );
+
+#endif /* **************************************************** MATERIASOURCE_H */

+ 2 - 6
Module_04/ex03/srcs/AMateria.cpp

@@ -6,7 +6,7 @@
 /*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/18 19:23:39 by bchanot           #+#    #+#             */
-/*   Updated: 2025/12/19 15:29:23 by bchanot          ###   ########.fr       */
+/*   Updated: 2025/12/26 01:09:08 by bchanot          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,17 +18,14 @@
 
 AMateria::AMateria() : _type("Punch")
 { 
-	std::cout << this->_type << " AMateria constructed" << std::endl;
 }
 
 AMateria::AMateria(std::string const & type) : _type(type)
 {
-	std::cout << this->_type << " AMateria constructed" << std::endl;
 }
 
 AMateria::AMateria( const AMateria & src ) : _type(src._type)
 {
-	std::cout << this->_type << " AMateria constructed" << std::endl;
 }
 
 
@@ -38,7 +35,6 @@ AMateria::AMateria( const AMateria & src ) : _type(src._type)
 
 AMateria::~AMateria()
 {
-	std::cout << this->_type << "AMateria destroyed" << std::endl;
 }
 
 
@@ -52,7 +48,7 @@ AMateria::~AMateria()
 */
 
 void AMateria::use(ICharacter& target) {
-	std::cout << this->_type << " In tour face ! Or mine ? Should i use on mine ... ?" << std::endl;
+	std::cout << "Use " << this->_type << " on " << target.getName() << "." << std::endl;
 }
 
 /*

+ 23 - 7
Module_04/ex03/srcs/Character.cpp

@@ -6,11 +6,12 @@
 /*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/19 16:40:56 by bchanot           #+#    #+#             */
-/*   Updated: 2025/12/20 04:06:23 by bchanot          ###   ########.fr       */
+/*   Updated: 2025/12/26 01:10:15 by bchanot          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "Character.hpp"
+#include "AMateria.hpp"
 
 /*
 ** ------------------------------- CONSTRUCTOR --------------------------------
@@ -22,6 +23,12 @@ Character::Character() : _name("Noname")
         this->_items[i] = NULL;
 }
 
+Character::Character( std::string const & name ) : _name(name)
+{
+	for (int i = 0; i < 4; i++)
+        this->_items[i] = NULL;
+}
+
 Character::Character( const Character & src ) : _name(src._name)
 {
 	for (int i = 0; i < 4; i++)
@@ -35,6 +42,8 @@ Character::Character( const Character & src ) : _name(src._name)
 
 Character::~Character()
 {
+	for (int i = 0; i < 4; i++)
+ 	       this->unequip(i);
 }
 
 
@@ -47,8 +56,11 @@ Character &				Character::operator=( Character const & rhs )
 {
 	if ( this != &rhs )
 	{
-		for (int i = 0; i < 4; i++)
+		this->_name = rhs._name;
+		for (int i = 0; i < 4; i++) {
+ 	       this->unequip(i);
  	       this->_items[i] = rhs._items[i]->clone();
+		}
 	}
 	return *this;
 }
@@ -66,22 +78,26 @@ std::ostream &			operator<<( std::ostream & o, Character const & i )
 
 
 void Character::equip(AMateria* m) {
-	for (int i = 0; i < 4; i++)
-		if (!this->_items[i]) {
-			this->_items[i] = m;
+	if (m) {
+		for (int i = 0; i < 4; i++) {
+			if (!this->_items[i]) {
+				this->_items[i] = m;
+				return ;
+			}
 		}
+	}
 }
 
 void Character::unequip(int idx) {
 	if (this->_items[idx]) {
-		delete this->_items[idx];
+//		delete this->_items[idx];
 		this->_items[idx] = NULL;
 	}
 }
 
 void Character::use(int idx, ICharacter& target) {
 	if (this->_items[idx]) {
-		this->_items[idx]->AMateria::use(target);
+		this->_items[idx]->use(target);
 	}
 
 }

+ 3 - 6
Module_04/ex03/srcs/Cure.cpp

@@ -6,7 +6,7 @@
 /*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/19 15:29:40 by bchanot           #+#    #+#             */
-/*   Updated: 2025/12/19 16:20:46 by bchanot          ###   ########.fr       */
+/*   Updated: 2025/12/26 01:48:42 by bchanot          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -16,14 +16,12 @@
 ** ------------------------------- CONSTRUCTOR --------------------------------
 */
 
-Cure::Cure() : AMateria("ice")
+Cure::Cure() : AMateria("cure")
 {
-	std::cout << "ice AMateria construct" << std::endl;
 }
 
 Cure::Cure( const Cure & src ) : AMateria(src._type)
 {
-	std::cout << "ice AMateria construct" << std::endl;
 }
 
 
@@ -33,7 +31,6 @@ Cure::Cure( const Cure & src ) : AMateria(src._type)
 
 Cure::~Cure()
 {
-	std::cout << "ice AMateria destructed" << std::endl;
 }
 
 
@@ -58,7 +55,7 @@ AMateria* Cure::clone() const {
 }
 
 void Cure::use(ICharacter& target) {
-	std::cout << "* heals " << target << "'s wounds *" << std::endl;
+	std::cout << "* heals " << target.getName() << "'s wounds *" << std::endl;
 }
 
 

+ 2 - 5
Module_04/ex03/srcs/Ice.cpp

@@ -6,7 +6,7 @@
 /*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/19 15:29:40 by bchanot           #+#    #+#             */
-/*   Updated: 2025/12/19 16:11:17 by bchanot          ###   ########.fr       */
+/*   Updated: 2025/12/26 00:39:28 by bchanot          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,12 +18,10 @@
 
 Ice::Ice() : AMateria("ice")
 {
-	std::cout << "ice AMateria construct" << std::endl;
 }
 
 Ice::Ice( const Ice & src ) : AMateria(src._type)
 {
-	std::cout << "ice AMateria construct" << std::endl;
 }
 
 
@@ -33,7 +31,6 @@ Ice::Ice( const Ice & src ) : AMateria(src._type)
 
 Ice::~Ice()
 {
-	std::cout << "ice AMateria destructed" << std::endl;
 }
 
 
@@ -58,7 +55,7 @@ AMateria* Ice::clone() const {
 }
 
 void Ice::use(ICharacter& target) {
-	std::cout << "* shoots an ice bolt at " << target << " *" << std::endl;
+	std::cout << "* shoots an ice bolt at " << target.getName() << " *" << std::endl;
 }
 
 

+ 96 - 0
Module_04/ex03/srcs/MateriaSource.cpp

@@ -0,0 +1,96 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   MateriaSource.cpp                                   :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/24 15:47:01 by bchanot           #+#    #+#             */
+/*   Updated: 2025/12/24 15:49:20 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "MateriaSource.hpp"
+#include <string.h>
+
+/*
+** ------------------------------- CONSTRUCTOR --------------------------------
+*/
+
+MateriaSource::MateriaSource()
+{
+	for (int i = 0; i < 4; i++)
+        this->_items[i] = NULL;
+}
+
+MateriaSource::MateriaSource( const MateriaSource & src )
+{
+	for (int i = 0; i < 4; i++)
+        this->_items[i] = src._items[i]->clone();
+}
+
+
+/*
+** -------------------------------- DESTRUCTOR --------------------------------
+*/
+
+MateriaSource::~MateriaSource()
+{
+}
+
+
+/*
+** --------------------------------- OVERLOAD ---------------------------------
+*/
+
+MateriaSource &				MateriaSource::operator=( MateriaSource const & rhs )
+{
+	if ( this != &rhs )
+	{
+		for (int i = 0; i < 4; i++) {
+ 	       this->_items[i] = rhs._items[i]->clone();
+		}
+	}
+	return *this;
+}
+
+std::ostream &			operator<<( std::ostream & o, MateriaSource const & i )
+{
+	(void)i;
+	//o << "Value = " << i.getValue();
+	return o;
+}
+
+
+/*
+** --------------------------------- METHODS ----------------------------------
+*/
+
+void MateriaSource::learnMateria(AMateria* source) {
+	if (source) {
+		for (int i = 0; i < 4; i++) {
+			if (!this->_items[i]) {
+				this->_items[i] = source->clone();
+				return ;
+			}
+		}
+	}
+}
+
+
+AMateria* MateriaSource::createMateria(std::string const & type){
+	for (int i = 0; i < 4; i++) {
+		if (this->_items[i] && !type.compare(this->_items[i]->getType())) {
+			return this->_items[i]->clone();
+		}
+	}
+	return NULL;
+}
+
+
+/*
+** --------------------------------- ACCESSOR ---------------------------------
+*/
+
+
+/* ************************************************************************** */

+ 21 - 14
Module_04/ex03/srcs/main.cpp

@@ -6,27 +6,34 @@
 /*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/07/17 15:10:40 by bchanot           #+#    #+#             */
-/*   Updated: 2025/12/18 17:05:50 by bchanot          ###   ########.fr       */
+/*   Updated: 2025/12/26 01:10:43 by bchanot          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-#include "Animal.hpp"
-#include "Dog.hpp"
-#include "Cat.hpp"
-#include "CatWrong.hpp"
-#include "AnimalWrong.hpp"
+#include "MateriaSource.hpp"
+#include "Ice.hpp"
+#include "Cure.hpp"
+#include "Character.hpp"
 
 #include <iostream>
 #include <limits>
 
 int main( void ) {
-	const AnimalWrong* meta = new AnimalWrong();
-const Animal* j = new Dog();
-const AnimalWrong* i = new CatWrong();
-std::cout << j->getType() << " " << std::endl;
-std::cout << i->getType() << " " << std::endl;
-i->makeSound(); //will output the cat sound!
-j->makeSound();
-meta->makeSound();
+	IMateriaSource* src = new MateriaSource();
+src->learnMateria(new Ice());
+src->learnMateria(new Cure());
+ICharacter* me = new Character("me");
+AMateria* tmp;
+tmp = src->createMateria("ice");
+me->equip(tmp);
+tmp = src->createMateria("cure");
+
+me->equip(tmp);
+ICharacter* bob = new Character("bob");
+me->use(0, *bob);
+me->use(1, *bob);
+delete bob;
+delete me;
+delete src;
 return 0;
 }