From 71380426426dffe787d1704a8fd639c4b1bbfad3 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 30 Oct 2024 16:34:29 +0100 Subject: cmd_get complete --- backend/GoldObject.cpp | 5 ++++- backend/GoldObject.h | 3 ++- backend/List.h | 3 ++- backend/List.hpp | 16 +++++++++++++++- backend/Location.cpp | 3 +++ backend/Location.h | 1 + 6 files changed, 27 insertions(+), 4 deletions(-) (limited to 'backend') diff --git a/backend/GoldObject.cpp b/backend/GoldObject.cpp index 624fdf9..cf97b2e 100644 --- a/backend/GoldObject.cpp +++ b/backend/GoldObject.cpp @@ -1,6 +1,9 @@ #include "GoldObject.h" -void GoldObject::set_count(int count) { +void GoldObject::set_count(const int & count) { this->count = count; } +int GoldObject::get_count() const { + return this->count; +} diff --git a/backend/GoldObject.h b/backend/GoldObject.h index ea473d4..03a074e 100644 --- a/backend/GoldObject.h +++ b/backend/GoldObject.h @@ -6,7 +6,8 @@ class GoldObject : public Object { using Object::Object; public: - void set_count(int count); + void set_count(const int & count); + int get_count() const; private: int count = 0; diff --git a/backend/List.h b/backend/List.h index 2d3d6b1..1d8893e 100644 --- a/backend/List.h +++ b/backend/List.h @@ -20,7 +20,8 @@ class List { public: size_t size(); - void push_back(T el); + void push_back(T & el); + void remove(const T & val); void pop_back(); diff --git a/backend/List.hpp b/backend/List.hpp index df075f9..a0d9289 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -8,7 +8,7 @@ size_t List::size() { } template -void List::push_back(T el) { +void List::push_back(T & el) { ListLink * link = static_cast *>(malloc(sizeof(ListLink))); link->prev = this->head; link->next = nullptr; @@ -19,6 +19,20 @@ void List::push_back(T el) { this->length++; } +template +void List::remove(const T & target) { + ListLink * link = nullptr; + for (link = this->tail; link != nullptr; link = link->next) { + if (link->value == target) break; + } + if (link == nullptr) return; // target not in list + + if (link->next != nullptr) link->next->prev = link->prev; + if (link->prev != nullptr) link->prev->next = link->next; + free(link); + this->length--; +} + template void List::pop_back() { if (this->head == nullptr) return; // empty list diff --git a/backend/Location.cpp b/backend/Location.cpp index a26d530..9add524 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -40,6 +40,9 @@ Location * Location::get_exit(Direction dir) { void Location::add_object(Object * object) { this->objects.push_back(object); } +void Location::remove_object(Object * object) { + this->objects.remove(object); +} ListRange Location::get_objects() { return this->objects.range(); } diff --git a/backend/Location.h b/backend/Location.h index 59a531f..fb0abfc 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -23,6 +23,7 @@ public: void set_exit(Direction dir, Location * location = nullptr); Location * get_exit(Direction dir); void add_object(Object *); + void remove_object(Object *); ListRange get_objects(); void add_enemy(Enemy *); ListRange get_enemies(); -- cgit v1.2.3