diff --git a/include/dataStructures/list.hpp b/include/dataStructures/list.hpp index 469a344..cfe655d 100644 --- a/include/dataStructures/list.hpp +++ b/include/dataStructures/list.hpp @@ -55,9 +55,9 @@ class LinkedList */ void push_front(const TData& value) { - auto nuevo = new ListNode(value); - nuevo->next = head; - head = nuevo; + auto node = new ListNode(value); + node->next = head; + head = node; } /** @@ -67,40 +67,31 @@ class LinkedList */ void remove_at(size_t position) { - if (head == nullptr) - { - return; - } + if (head == nullptr){return;} //si esta vacia no hace nada - if (position == 0) + int contador = 0; + + if (position == 0) //caso de posicion 0 { - auto temp = head; + auto temporalNode = head; head = head->next; - delete temp; + delete temporalNode; return; } - - // Para cualquier otra posicion necesitamos mantener un puntero al nodo anterior - auto prev = head; - auto current = head->next; - size_t current_position = 1; - - // Avanzamos hasta encontrar la posicion o llegar al final de la lista - while (current != nullptr && current_position < position) + auto current = head; + ListNode previous = nullptr; + while (current != nullptr && contador < position) { - prev = current; + previous = current; current = current->next; - current_position++; + contador++; + } + if (current) + { + previous->next = current->next; + delete current; } - // Si current es nullptr quiere decir que la posicion especifica es mas grande que la lista, - // no se encuentra la posicion - if (current == nullptr) return; - - // Hacemos que el nodo anterior apunte al nodo siguiente actual - prev->next = current->next; - - delete current; } /** @@ -109,47 +100,49 @@ class LinkedList */ ListNode* take(size_t startPosition, size_t nElements) { - // Si la lista esta vacia retornamos nullptr - if (head == nullptr) - { - return nullptr; - } + if (head == nullptr) {return nullptr;} - // Buscamos el nodo de inicio - ListNode* current = head; - size_t current_position = 0; + auto current = head; + int contador=0; - // Avanzamos hasta la posicion de inicio - while (current != nullptr && current_position < startPosition) + while (current!=nullptr && contador < startPosition) { current = current->next; - current_position++; + contador++; } + if (current==nullptr) {return nullptr;} - // Si current es nullptr, la posicion esta fuera de rango - if (current == nullptr) return nullptr; - - // Creamos el primer nodo de la nueva lista - ListNode* new_head = new ListNode(current->data); - ListNode* new_current = new_head; - - // Avanzamos al siguiente nodo en la lista original - current = current->next; - - // Copiamos los siguientes mElements-1 nodos (ya copiamos 1) - for (size_t i = 1; i < nElements && current != nullptr; i++) + //punteros controladores + ListNode newhead = nullptr, newtail = nullptr; + for (size_t i = 0; i < nElements; i++) { - // Creamos un nuevo nodo con el valor actual - new_current->next = new ListNode(current->data); - - // Avanzamos en ambas listas - new_current = new_current->next; - current = current->next; + auto copia = new ListNode(current->data); + if (newhead == nullptr) //primer nodo,es cabeza y cola + { + newhead = copia; + newtail = copia; + } else //si no es el primer nodo lo mandamos al final de la lista + { + newtail->next = copia; + newtail = copia; + } + current = current->next; //avanzamos al siguiente nodo } - return new_head; + return newhead; } + // Devuelve el tamaño de la lista + /* size_t size() const { + size_t count = 0; + auto current = head; + while (current) { + count++; + current = current->next; + } + return count; + }*/ + /** * @brief Imprime todos los elementos de la lista */ @@ -229,9 +222,16 @@ class DoublyLinkedList */ void push_front(const TData& value) { - auto nuevo = new DoublyListNode(value); - nuevo->next = head; - head = nuevo; + auto node = new DoublyListNode(value); + if (head == nullptr) + { + head = node; + return; + } + node->next = head; + head->prev = node; + node->prev = nullptr; + head = node; } /** @@ -241,28 +241,17 @@ class DoublyLinkedList */ void push_back(const TData& value) { - auto nuevo = new DoublyListNode(value); - // nuevo->next = nullptr; No haria falta pq cuando se crea un DoublyListNode next y prev se inicilizan en nullptr - - // Si la lista esta vacia - if (!head) + auto node = new DoublyListNode(value); + if (head == nullptr){head=node; return;} + node->next = nullptr; //apuntamos el nodo al nullptr + auto current = head; + while ( current->next != nullptr) //vamos al final de la lista { - head = nuevo; + current =current->next; } - else - { - // Si la lista tiene elementos, recorrer hasta el ultimo nodo - // DoublyListNode* temp = get_head(); - auto temp = get_head(); - while (temp->next != nullptr) // Buscamos el ultimo nodo - { - temp = temp->next; - } + current->next = node; //que el siguiente elemento del ultimo sea el nodo + node->prev = current; // que el previo al nodo sea el current - // temp es el ultimo nodo, se conecta con el nuevo ultimo nodo - temp->next = nuevo; - nuevo->prev = temp; // El prev de nuevo apunta al antiguo ultimo nodo, osea temp - } } /** @@ -272,57 +261,29 @@ class DoublyLinkedList */ void remove_at(size_t position) { - // Si la lista esta vacia no hay nada que eliminar - if (head == nullptr) - { - return; - } - - // Caso que el nodo a eliminar sea el primero (nodo 0) - if (position == 0) + if (head == nullptr){return;} //si esta vacia no hace nada + int contador = 0; + if (position == 0) //caso de posicion 0 { - DoublyListNode* temp = head; + auto temporalNode = head; head = head->next; - - // Si hay mas nodos despues del primero actualizamos el prev del nuevo head - if (head != nullptr) - { - head->prev = nullptr; - } - - delete temp; + delete temporalNode; return; } - - // Para cualquier otra posicion recorremos la lista, hacemos un contador para llevar track de - // la posicion en la lista - DoublyListNode* current = head; - size_t current_position = 0; - - while (current != nullptr && current_position < position) + auto current = head; + // DoublyListNode previous = nullptr; ya no hace falta guardar un previous + while (current != nullptr && contador < position) { + // previous = current; current = current->next; - current_position++; - } - - // Si current es nullptr quiere decir que la posicion especifica es mas grande que la lista, - // no se encuentra la posicion - if (current == nullptr) return; - - // current es el nodo que queremos eliminar, conectamos sus - // nodos adyacentes - if (current->prev != nullptr) - { - current->prev->next = current->next; + contador++; } - - if (current->next != nullptr) + if (current) { - current->next->prev = current->prev; - } - - delete current; - + (current->prev)->next = current->next; + (current->next)->prev = current->prev; + delete current; + }else {return;} //si no se encuntra el nodo retornar } /** @@ -332,20 +293,20 @@ class DoublyLinkedList */ void copy_list(const DoublyLinkedList& other) { - // PREGUNTAR: Borramos los nodos actuales? - while (head != nullptr) + //caso donde no hay elementos en la lista nueva + if (other.head == nullptr){std::cout<<"no hay elementos que copiar"<next; - delete temporalNode; + delete current; } - - // Copiamos los nodos de la otra lista con push_bakc() - auto current = other.get_head(); - while (current != nullptr) + //hacemos un pushback en la lista vieja con los elementos de la lista nueva + auto current2 = other.head; + while (current2 != nullptr) { - push_back(current->data); - current = current->next; + push_back(current2->data); + current2 = current2->next; } } diff --git a/include/dataStructures/wrapperVector.hpp b/include/dataStructures/wrapperVector.hpp index 374067d..b14e249 100644 --- a/include/dataStructures/wrapperVector.hpp +++ b/include/dataStructures/wrapperVector.hpp @@ -5,9 +5,11 @@ #ifndef WRAPPERVECTOR_HPP #define WRAPPERVECTOR_HPP +#include #include +#include +#include #include -#include auto constexpr INIT_CAPACITY {100}; //< Capacidad inicial del vector @@ -114,6 +116,10 @@ class wrapperVector */ TData& operator[](size_t index) { + if (index >= m_size) + { + std::cout << "Indice fuera de limites" << std::endl; + } return m_data[index]; } @@ -122,6 +128,10 @@ class wrapperVector */ const TData& at(size_t index) const { + if (index >= m_size) + { + std::cout << "Indice fuera de limites" << std::endl; + } return m_data[index]; } diff --git a/include/inventarioRefugio.hpp b/include/inventarioRefugio.hpp new file mode 100644 index 0000000..d385d79 --- /dev/null +++ b/include/inventarioRefugio.hpp @@ -0,0 +1,147 @@ +/* + * Inventario del Refugio 33 + * Copyright (C) 2025, AyED COM-VIRTUAL + * Marzo 13, 2025. + */ + +#ifndef _INVENTARIO_REFUGIO_HPP +#define _INVENTARIO_REFUGIO_HPP + +#include +#include + +/** + * @brief Clase que representa un objeto de tipo WrapperVectorInventario. Una implementación de un vector para el + * inventario del refugio. + */ +class WrapperVectorInventario +{ +private: + std::string* m_data; //< Puntero a un string que representa el inventario del refugio. + size_t m_size; //< Tamaño del inventario del refugio. + size_t m_capacity; //< Capacidad del inventario del refugio. + + /** + * @brief Método que se encarga de redimensionar el inventario del refugio. + */ + void resize(){ //es como el metodo expandir + m_capacity = (m_capacity == 0) ? 100 : m_capacity * 2; + auto* nuevo_data = new std::string[m_capacity]; + for (std::string* ptr = m_data; ptr < m_data + m_size; ++ptr) { + nuevo_data[ptr - m_data] = *ptr; + } + delete[] m_data; + m_data = nuevo_data; + }; + +public: + /** + * @brief Constructor de la clase WrapperVectorInventario. + */ + WrapperVectorInventario() + : m_data(nullptr) + , m_size(0) + , m_capacity(0) {}; + + /** + * @brief Destructor de la clase WrapperVectorInventario. + */ + ~WrapperVectorInventario() + { + delete[] m_data; + } + + /** + * @brief Método que se encarga de agregar un elemento al inventario del refugio. + * + * @param item Elemento a agregar al inventario del refugio. + */ + void pushBack(const std::string& item) + { + if (m_size == m_capacity) + { + resize(); + } + m_data[m_size++] = item; + } + + /** + * @brief Método que se encarga de obtener un elemento del inventario del refugio. + */ + std::string& getAt(size_t index) //el metodo devuelve una referencia a un string, no devuelve una copia + //devuelve un acceso directo al objeto guardado en m_data + { + return m_data[index]; //con el podemos modificar directamente el objeto dentro + } //por ej: getAt(2) = "nuevo valor"; + //la funcion getAt que toma como parametro un indice size_t(entero sin signo) que se usa para representar + // tamaños o indices en contenedores estandar + /** + * @brief Método que se encarga de obtener el tamaño del inventario del refugio. + */ + size_t size() const{ + return m_size; + }; + + /** + * @brief Método que se encarga de obtener la capacidad del inventario del refugio. + */ + size_t capacity() const{ + return m_capacity; + }; + + /** + * @brief Método que se encarga de imprimir el inventario del refugio. + */ + void print() const{ + if (m_size == 0){ + std::cout<<"Inventario sin elementos"< +#include +#include + + +class Ghoul : public EntidadGenerica{ + protected : + float m_health; //defino atributos genericos de personajes + float m_strength; + float m_energy; +public: + /** + * @brief Constructor de personaje + * + * @param name Nombre del personaje + * @param strength Nivel de fuerza inicial del personaje + * @param energy energia inicial del personaje + */ + Ghoul(const std::string& name, float strength, float energy); + //muestra la informacion del gohul + void showInfo() const override; + //accion especifica del gohul + void doAction() const; +}; +#endif //GOHULS_H diff --git a/include/personajes/Saqueadores.hpp b/include/personajes/Saqueadores.hpp new file mode 100644 index 0000000..5f78eb5 --- /dev/null +++ b/include/personajes/Saqueadores.hpp @@ -0,0 +1,8 @@ +// +// Created by Rocco on 3/4/2025. +// + +#ifndef SAQUEADORES_H +#define SAQUEADORES_H + +#endif //SAQUEADORES_H diff --git a/include/personajes/Vaul-tec.hpp b/include/personajes/Vaul-tec.hpp new file mode 100644 index 0000000..aa495ea --- /dev/null +++ b/include/personajes/Vaul-tec.hpp @@ -0,0 +1,39 @@ +// +// Created by Rocco on 3/4/2025. +// + +#ifndef VAUL_TEC_H +#define VAUL_TEC_H + +#include "engine.hpp" +#include "engineData.hpp" +#include "entidadGenerica.hpp" +#include "list.hpp" +#include "wrapperVector.hpp" +#include +#include +#include + + +class Vaul_tec : public EntidadGenerica{ + protected : + float m_health; //defino atributos genericos de personajes + float m_strength; + float m_energy; + int m_number; +public: + /** + * @brief Constructor de vaul-tec + * + * @param name Nombre del refugiado + * @param strength Nivel de fuerza inicial del refugiado + * @param energy energia inicial del refugiado + * @param numero del refugiado. + */ + Vaul_tec(const std::string& name, float strength, float energy, int number); + void showInfo() const override; + void doAction() const; +}; + + +#endif //VAUL_TEC_H diff --git a/src/refugio.cpp b/src/refugio.cpp index 39258be..b929ad4 100644 --- a/src/refugio.cpp +++ b/src/refugio.cpp @@ -27,26 +27,44 @@ void Refugio::doAction() const void Refugio::addRefugee(const std::string& refugee) { - throw std::runtime_error("Not implemented yet"); + m_refugees.push_back(refugee); + std::cout<< "se ha añadido el morador" << refugee << "al refugio 33" <first == resource) + { + if (it->second >= amount) + { + it->second -= amount; + std::cout << "Se ha consumido "<* mNode) std::cout << "Fin del registro!" << std::endl; return; } + std::cout<<"visitante: "<data.nombre; + std::cout<<"\n"; + printRecursive(mNode->next); +} +bool Refugio::hasFactionVisited(EngineData::Faction faccion) const +{ + auto it=m_visitants->get_head(); + while (it != nullptr) + { + if (it->data.faccion == faccion){return true;} + if (it == nullptr){return false;} + it = it->next; + } } diff --git a/tash list b/tash list new file mode 100644 index 0000000..abf1cee --- /dev/null +++ b/tash list @@ -0,0 +1,439 @@ +diff --git a/include/Personajes.hpp b/include/Personajes.hpp +new file mode 100644 +index 0000000..40c90a7 +--- /dev/null ++++ b/include/Personajes.hpp +@@ -0,0 +1,40 @@ ++// ++// Created by Rocco on 27/3/2025. ++// ++ ++#ifndef PERSONAJES_H ++#define PERSONAJES_H ++ ++#include "entidadGenerica.hpp" ++#include "wrapperVector.hpp" ++#include  ++#include  ++#include  ++#include "list.hpp" ++ ++/** ++ * @class Personaje ++ * @brief Representa un personaje ++ * ++ * ++ * ++ */ ++class Personaje : public EntidadGenerica{ ++protected : ++ float m_health; //defino atributos genericos de personajes ++ float m_strength; ++ float m_energy; ++public: ++ /** ++ * @brief Constructor de personaje ++ * ++ * @param name Nombre del personaje ++ * @param strength Nivel de fuerza inicial del personaje ++ * @param energy energia inicial del personaje ++ */ ++ Personaje(const std::string& name, float strength, float energy); ++ void showInfo() const override; ++ void doAction() const override; ++}; ++ ++#endif //PERSONAJES_H +diff --git a/include/dataStructures/list.hpp b/include/dataStructures/list.hpp +index ee9ad7f..802c8d6 100644 +--- a/include/dataStructures/list.hpp ++++ b/include/dataStructures/list.hpp +@@ -40,7 +40,12 @@ public: +  + ~LinkedList() + { +- throw std::runtime_error("Not implemented yet"); ++ while(head != nullptr) ++ { ++ auto temporalNode = head; ++ head = head->next; ++ delete temporalNode; ++ } + } +  + /** +@@ -50,7 +55,14 @@ public: + */ + void push_front(const TData& value) + { +- throw std::runtime_error("Not implemented yet"); ++ auto node = new ListNode(value); ++ if (head == nullptr) ++ { ++ node->next = head; ++ head = node; ++ } ++ head = head->next; ++ head = node; + } +  + /** +@@ -60,7 +72,29 @@ public: + */ + void remove_at(size_t position) + { +- throw std::runtime_error("Not implemented yet"); ++ if (head == nullptr){return;} //si esta vacia no hace nada ++ int contador = 0; ++ if (position == 0) //caso de posicion 0 ++ { ++ auto temporalNode = head; ++ head = head->next; ++ delete temporalNode; ++ return; ++ } ++ auto current = head; ++ ListNode previous = nullptr; ++ while (current != nullptr && contador < position) ++ { ++ previous = current; ++ current = current->next; ++ contador++; ++ } ++ if (current) ++ { ++ previous->next = current->next; ++ delete current; ++ } ++ + } +  + /** +@@ -69,7 +103,45 @@ public: + */ + ListNode* take(size_t startPosition, size_t nElements) + { +- throw std::runtime_error("Not implemented yet"); ++ if (head == nullptr) return nullptr; ++ if (startPosition + nElements > size()){return nullptr;} ++ auto current = head; ++ int contador=0; ++ while (contador < startPosition) ++ { ++ current = current->next; ++ contador++; ++ } ++ auto nuevo = new LinkedList; ++ //punteros controladores ++ ListNode newhead = nullptr, newtail = nullptr; ++ for (size_t i = 0; i < nElements; i++) ++ { ++ auto copia = new ListNode(current->data); ++ if (newhead == nullptr) //primer nodo ++ { ++ newhead = copia; ++ newtail = copia; ++ } else //lo mandamos al final de la lista ++ { ++ newtail->next = copia; ++ newtail = copia; ++ } ++ current = current->next; //avanzamos al siguiente nodo ++ } ++ nuevo->head = newhead; //enlazamos el head de la nueva al primer nodo ++ return nuevo; ++ } ++ ++ // Devuelve el tamaño de la lista ++ size_t size() const { ++ size_t count = 0; ++ auto current = head; ++ while (current) { ++ count++; ++ current = current->next; ++ } ++ return count; + } +  + /** +@@ -151,7 +223,12 @@ public: + */ + void push_front(const TData& value) + { +- throw std::runtime_error("Not implemented yet"); ++ auto node = new ListNode(value); ++ if (head == nullptr) ++ { ++ node->next = head; ++ head = node; ++ } + } +  + /** +diff --git a/include/dataStructures/wrapperVector.hpp b/include/dataStructures/wrapperVector.hpp +index c864053..137d899 100644 +--- a/include/dataStructures/wrapperVector.hpp ++++ b/include/dataStructures/wrapperVector.hpp +@@ -5,9 +5,11 @@ + #ifndef WRAPPERVECTOR_HPP + #define WRAPPERVECTOR_HPP +  ++#include  + #include  ++#include  ++#include  + #include  +-#include  +  + auto constexpr INIT_CAPACITY {100}; //< Capacidad inicial del vector +  +@@ -113,6 +115,10 @@ public: + */ + TData& operator[](size_t index) + { ++ if (index >= m_size) ++ { ++ std::cout << "Indice fuera de limites" << std::endl; ++ } + return m_data[index]; + } +  +@@ -121,6 +127,10 @@ public: + */ + const TData& at(size_t index) const + { ++ if (index >= m_size) ++ { ++ std::cout << "Indice fuera de limites" << std::endl; ++ } + return m_data[index]; + } +  +diff --git a/include/inventarioRefugio.hpp b/include/inventarioRefugio.hpp +new file mode 100644 +index 0000000..d385d79 +--- /dev/null ++++ b/include/inventarioRefugio.hpp +@@ -0,0 +1,147 @@ ++/* ++ * Inventario del Refugio 33 ++ * Copyright (C) 2025, AyED COM-VIRTUAL ++ * Marzo 13, 2025. ++ */ ++ ++#ifndef _INVENTARIO_REFUGIO_HPP ++#define _INVENTARIO_REFUGIO_HPP ++ ++#include  ++#include  ++ ++/** ++ * @brief Clase que representa un objeto de tipo WrapperVectorInventario. Una implementación de un vector para el ++ * inventario del refugio. ++ */ ++class WrapperVectorInventario ++{ ++private: ++ std::string* m_data; //< Puntero a un string que representa el inventario del refugio. ++ size_t m_size; //< Tamaño del inventario del refugio. ++ size_t m_capacity; //< Capacidad del inventario del refugio. ++ ++ /** ++ * @brief Método que se encarga de redimensionar el inventario del refugio. ++ */ ++ void resize(){ //es como el metodo expandir ++ m_capacity = (m_capacity == 0) ? 100 : m_capacity * 2; ++ auto* nuevo_data = new std::string[m_capacity]; ++ for (std::string* ptr = m_data; ptr < m_data + m_size; ++ptr) { ++ nuevo_data[ptr - m_data] = *ptr; ++ } ++ delete[] m_data; ++ m_data = nuevo_data; ++ }; ++ ++public: ++ /** ++ * @brief Constructor de la clase WrapperVectorInventario. ++ */ ++ WrapperVectorInventario() ++ : m_data(nullptr) ++ , m_size(0) ++ , m_capacity(0) {}; ++ ++ /** ++ * @brief Destructor de la clase WrapperVectorInventario. ++ */ ++ ~WrapperVectorInventario() ++ { ++ delete[] m_data; ++ } ++ ++ /** ++ * @brief Método que se encarga de agregar un elemento al inventario del refugio. ++ * ++ * @param item Elemento a agregar al inventario del refugio. ++ */ ++ void pushBack(const std::string& item) ++ { ++ if (m_size == m_capacity) ++ { ++ resize(); ++ } ++ m_data[m_size++] = item; ++ } ++ ++ /** ++ * @brief Método que se encarga de obtener un elemento del inventario del refugio. ++ */ ++ std::string& getAt(size_t index) //el metodo devuelve una referencia a un string, no devuelve una copia ++ //devuelve un acceso directo al objeto guardado en m_data ++ { ++ return m_data[index]; //con el podemos modificar directamente el objeto dentro ++ } //por ej: getAt(2) = "nuevo valor"; ++ //la funcion getAt que toma como parametro un indice size_t(entero sin signo) que se usa para representar ++ // tamaños o indices en contenedores estandar ++ /** ++ * @brief Método que se encarga de obtener el tamaño del inventario del refugio. ++ */ ++ size_t size() const{ ++ return m_size; ++ }; ++ ++ /** ++ * @brief Método que se encarga de obtener la capacidad del inventario del refugio. ++ */ ++ size_t capacity() const{ ++ return m_capacity; ++ }; ++ ++ /** ++ * @brief Método que se encarga de imprimir el inventario del refugio. ++ */ ++ void print() const{ ++ if (m_size == 0){ ++ std::cout<<"Inventario sin elementos"< ++ + Refugio::Refugio(const std::string& name, float defense, float attack) + : EntidadGenerica(name) + , m_defense(defense) +@@ -28,24 +30,43 @@ void Refugio::doAction() const +  + void Refugio::addRefugee(const std::string& refugee) + { +- throw std::runtime_error("Not implemented yet"); ++ m_refugees.push_back(refugee); ++ std::cout<< "se ha añadido el morador" << refugee << "al refugio 33" <first == resource) ++ { ++ if (it->second >= amount) ++ { ++ it->second -= amount; ++ std::cout << "Se ha consumido "<();} ++ m_visitants->push_front(visitante); + // } + } +  +@@ -69,5 +90,11 @@ void Refugio::printRecursive(DoublyListNode* mNode) +  + bool Refugio::hasFactionVisited(const std::string& faccion) const + { +- throw std::runtime_error("Not implemented yet"); ++ auto it=m_visitants->get_head(); ++ while (it != nullptr) ++ { ++ if (it->data.faccion == faccion){return true;} ++ if (it == nullptr){return false;} ++ it = it->next; ++ } + }