diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-12 16:26:26 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-12 16:26:26 +0200 |
commit | 957053e7bf25ae9cfda0243d3cbcad1e9abac60a (patch) | |
tree | 6ef4b9b7a846729e4c8e376297fe4bcd6a9367ce | |
parent | 3db5a4a84c5fd2c35b1bc9e77b271f18406138e9 (diff) |
implement linked list
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | backend/ArmorObject.h | 10 | ||||
-rw-r--r-- | backend/ConsumableObject.h | 7 | ||||
-rw-r--r-- | backend/Enemy.h | 7 | ||||
-rw-r--r-- | backend/EnemyFactory.cpp | 6 | ||||
-rw-r--r-- | backend/EnemyFactory.h | 12 | ||||
-rw-r--r-- | backend/GoldObject.h | 8 | ||||
-rw-r--r-- | backend/List.hpp | 67 | ||||
-rw-r--r-- | backend/Location.h | 9 | ||||
-rw-r--r-- | backend/LocationFactory.cpp | 6 | ||||
-rw-r--r-- | backend/LocationFactory.h | 12 | ||||
-rw-r--r-- | backend/Object.h | 14 | ||||
-rw-r--r-- | backend/ObjectFactory.cpp | 6 | ||||
-rw-r--r-- | backend/ObjectFactory.h | 12 | ||||
-rw-r--r-- | backend/WeaponObject.h | 11 | ||||
-rw-r--r-- | frontend/Player.h | 0 | ||||
-rw-r--r-- | main.cpp | 7 | ||||
-rw-r--r-- | makefile | 13 |
18 files changed, 211 insertions, 0 deletions
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 <cstdlib> +#include <cstdio> + +template <typename T> +struct ListLink { + ListLink<T> * prev; + ListLink<T> * next; + T value; +}; + +template<typename T> +class List { +public: + size_t size() { return this->length; } + + void push_back(T el) { + ListLink<T> * link = static_cast<ListLink<T> *>(malloc(sizeof(ListLink<T>))); + 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<T> * 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<T> * link = this->tail; + while (link != nullptr) { + ListLink<T> * next = link->next; + free(link); + link = next; + } + this->head = nullptr; + this->tail = nullptr; + this->length = 0; + } + + T & operator [] (size_t index) { + ListLink<T> * link = this->tail; + for (size_t i = 0; i < index; i++) + link = link->next; + return link->value; + } + +private: + ListLink<T> * head = nullptr; + ListLink<T> * 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 --- /dev/null +++ b/frontend/Player.h 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 <cstdlib> +#include <cstdio> + +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 + |