From 957053e7bf25ae9cfda0243d3cbcad1e9abac60a Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 12 Oct 2024 16:26:26 +0200 Subject: implement linked list --- .gitignore | 4 +++ backend/ArmorObject.h | 10 +++++++ backend/ConsumableObject.h | 7 +++++ backend/Enemy.h | 7 +++++ backend/EnemyFactory.cpp | 6 ++++ backend/EnemyFactory.h | 12 ++++++++ backend/GoldObject.h | 8 ++++++ backend/List.hpp | 67 +++++++++++++++++++++++++++++++++++++++++++++ backend/Location.h | 9 ++++++ backend/LocationFactory.cpp | 6 ++++ backend/LocationFactory.h | 12 ++++++++ backend/Object.h | 14 ++++++++++ backend/ObjectFactory.cpp | 6 ++++ backend/ObjectFactory.h | 12 ++++++++ backend/WeaponObject.h | 11 ++++++++ frontend/Player.h | 0 main.cpp | 7 +++++ makefile | 13 +++++++++ 18 files changed, 211 insertions(+) create mode 100644 .gitignore create mode 100644 backend/ArmorObject.h create mode 100644 backend/ConsumableObject.h create mode 100644 backend/Enemy.h create mode 100644 backend/EnemyFactory.cpp create mode 100644 backend/EnemyFactory.h create mode 100644 backend/GoldObject.h create mode 100644 backend/List.hpp create mode 100644 backend/Location.h create mode 100644 backend/LocationFactory.cpp create mode 100644 backend/LocationFactory.h create mode 100644 backend/Object.h create mode 100644 backend/ObjectFactory.cpp create mode 100644 backend/ObjectFactory.h create mode 100644 backend/WeaponObject.h create mode 100644 frontend/Player.h create mode 100644 main.cpp create mode 100644 makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b24328f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +main +*.o +compile_commands.json +.cache diff --git a/backend/ArmorObject.h b/backend/ArmorObject.h new file mode 100644 index 0000000..c8223bc --- /dev/null +++ b/backend/ArmorObject.h @@ -0,0 +1,10 @@ +#pragma once + +#include "Object.h" + +class ArmorObject : public Object { +private: + int protection; + +}; + diff --git a/backend/ConsumableObject.h b/backend/ConsumableObject.h new file mode 100644 index 0000000..0e98674 --- /dev/null +++ b/backend/ConsumableObject.h @@ -0,0 +1,7 @@ +#pragma once + +#include "Object.h" + +class ConsumableObject : public Object { +}; + diff --git a/backend/Enemy.h b/backend/Enemy.h new file mode 100644 index 0000000..036c5df --- /dev/null +++ b/backend/Enemy.h @@ -0,0 +1,7 @@ +#pragma once + +class Enemy { +private: + +}; + diff --git a/backend/EnemyFactory.cpp b/backend/EnemyFactory.cpp new file mode 100644 index 0000000..9c92c0b --- /dev/null +++ b/backend/EnemyFactory.cpp @@ -0,0 +1,6 @@ +#include "EnemyFactory.h" + +Enemy * EnemyFactory::create_enemy() { + return new Enemy(); +} + diff --git a/backend/EnemyFactory.h b/backend/EnemyFactory.h new file mode 100644 index 0000000..ebb956b --- /dev/null +++ b/backend/EnemyFactory.h @@ -0,0 +1,12 @@ +#pragma once + +#include "Enemy.h" + +class EnemyFactory { +public: + static Enemy * create_enemy(); + +private: + EnemyFactory() = delete; +}; + diff --git a/backend/GoldObject.h b/backend/GoldObject.h new file mode 100644 index 0000000..0f64ce2 --- /dev/null +++ b/backend/GoldObject.h @@ -0,0 +1,8 @@ +#pragma once + +#include "Object.h" + +class GoldObject : public Object { +private: + int count; +}; diff --git a/backend/List.hpp b/backend/List.hpp new file mode 100644 index 0000000..df44f7a --- /dev/null +++ b/backend/List.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include +#include + +template +struct ListLink { + ListLink * prev; + ListLink * next; + T value; +}; + +template +class List { +public: + size_t size() { return this->length; } + + void push_back(T el) { + ListLink * link = static_cast *>(malloc(sizeof(ListLink))); + link->prev = this->head; + link->next = nullptr; + link->value = el; + if (this->head != nullptr) this->head->next = link; + if (this->head == nullptr) this->tail = link; + this->head = link; + this->length++; + } + + void pop_back() { + if (this->head == nullptr) return; // empty list + + ListLink * secondlast = this->head->prev; + if (secondlast != nullptr) + secondlast->next = nullptr; + free(this->head); + if (this->tail == this->head) + this->tail = nullptr; + this->head = secondlast; + + this->length--; + } + + void clear() { + ListLink * link = this->tail; + while (link != nullptr) { + ListLink * next = link->next; + free(link); + link = next; + } + this->head = nullptr; + this->tail = nullptr; + this->length = 0; + } + + T & operator [] (size_t index) { + ListLink * link = this->tail; + for (size_t i = 0; i < index; i++) + link = link->next; + return link->value; + } + +private: + ListLink * head = nullptr; + ListLink * tail = nullptr; + size_t length = 0; +}; + diff --git a/backend/Location.h b/backend/Location.h new file mode 100644 index 0000000..94c3eac --- /dev/null +++ b/backend/Location.h @@ -0,0 +1,9 @@ +#pragma once + +class Location { +protected: + Location(); + virtual ~Location(); + friend class LocationFactory; + +}; diff --git a/backend/LocationFactory.cpp b/backend/LocationFactory.cpp new file mode 100644 index 0000000..98a0cd8 --- /dev/null +++ b/backend/LocationFactory.cpp @@ -0,0 +1,6 @@ +#include "LocationFactory.h" + +Location * LocationFactory::create_location() { + return new Location(); +} + diff --git a/backend/LocationFactory.h b/backend/LocationFactory.h new file mode 100644 index 0000000..e9b1bc1 --- /dev/null +++ b/backend/LocationFactory.h @@ -0,0 +1,12 @@ +#pragma once + +#include "Location.h" + +class LocationFactory { +public: + static Location * create_location(); + +private: + LocationFactory() = delete; +}; + diff --git a/backend/Object.h b/backend/Object.h new file mode 100644 index 0000000..8127f08 --- /dev/null +++ b/backend/Object.h @@ -0,0 +1,14 @@ +#pragma once + +class Object { +private: + const char * name; + const char * description; + +protected: + Object(); + virtual ~Object(); + friend class ObjectFactory; + +}; + diff --git a/backend/ObjectFactory.cpp b/backend/ObjectFactory.cpp new file mode 100644 index 0000000..efb7742 --- /dev/null +++ b/backend/ObjectFactory.cpp @@ -0,0 +1,6 @@ +#include "ObjectFactory.h" + +Object * ObjectFactory::create_object() { + return new Object(); +} + diff --git a/backend/ObjectFactory.h b/backend/ObjectFactory.h new file mode 100644 index 0000000..f9a7301 --- /dev/null +++ b/backend/ObjectFactory.h @@ -0,0 +1,12 @@ +#pragma once + +#include "Object.h" + +class ObjectFactory { +public: + static Object * create_object(); + +private: + ObjectFactory() = delete; +}; + diff --git a/backend/WeaponObject.h b/backend/WeaponObject.h new file mode 100644 index 0000000..e9ce261 --- /dev/null +++ b/backend/WeaponObject.h @@ -0,0 +1,11 @@ +#pragma once + +#include "Object.h" + +class WeaponObject : public Object { +private: + int damage_min; + int damage_max; + +}; + diff --git a/frontend/Player.h b/frontend/Player.h new file mode 100644 index 0000000..e69de29 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..acaf028 --- /dev/null +++ b/main.cpp @@ -0,0 +1,7 @@ +#include +#include + +int main() { + return EXIT_SUCCESS; +} + diff --git a/makefile b/makefile new file mode 100644 index 0000000..396d7b3 --- /dev/null +++ b/makefile @@ -0,0 +1,13 @@ +.PHONY: FORCE + +OBJS += main.o +OBJS += backend/List.o + +main: $(OBJS) + +memtest: main FORCE + valgrind ./$< + +compile_commands.json: + compiledb make -Bn + -- cgit v1.2.3