aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
Diffstat (limited to 'backend')
-rw-r--r--backend/CMakeLists.txt1
-rw-r--r--backend/Dungeon.cpp8
-rw-r--r--backend/Dungeon.h1
-rw-r--r--backend/Location.h1
-rw-r--r--backend/RNG.cpp32
-rw-r--r--backend/RNG.h23
6 files changed, 66 insertions, 0 deletions
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt
index 9b1e251..b508654 100644
--- a/backend/CMakeLists.txt
+++ b/backend/CMakeLists.txt
@@ -6,5 +6,6 @@ target_sources(main PUBLIC
Dungeon.cpp
Location.cpp
util.cpp
+ RNG.cpp
)
diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp
index 55dccec..3de2f11 100644
--- a/backend/Dungeon.cpp
+++ b/backend/Dungeon.cpp
@@ -1,4 +1,5 @@
#include "Dungeon.h"
+#include "RNG.h"
void Dungeon::update() {
@@ -8,3 +9,10 @@ void Dungeon::add_location(Location * location) {
this->locations.push_back(location);
}
+Location * Dungeon::get_start_location() {
+ size_t size = this->locations.size();
+ if (size == 0) return nullptr;
+ size_t index = RNG::get().rand_int(size);
+ return this->locations[index];
+}
+
diff --git a/backend/Dungeon.h b/backend/Dungeon.h
index 88328c8..2d5fa61 100644
--- a/backend/Dungeon.h
+++ b/backend/Dungeon.h
@@ -8,6 +8,7 @@ class Dungeon {
public:
void update();
void add_location(Location *);
+ Location * get_start_location();
private:
List<Location *> locations;
diff --git a/backend/Location.h b/backend/Location.h
index 6068a99..f102728 100644
--- a/backend/Location.h
+++ b/backend/Location.h
@@ -11,6 +11,7 @@ enum Direction {
SOUTH = 2,
WEST = 3,
};
+static constexpr const Direction DIRECTIONS[] = { NORTH, EAST, SOUTH, WEST };
class Location {
public:
diff --git a/backend/RNG.cpp b/backend/RNG.cpp
new file mode 100644
index 0000000..bc3e57b
--- /dev/null
+++ b/backend/RNG.cpp
@@ -0,0 +1,32 @@
+#include "RNG.h"
+
+using std::uniform_int_distribution;
+using std::uniform_real_distribution;
+
+RNG::RNG() : dev(), rng(dev()) { }
+
+RNG & RNG::get() {
+ static RNG instance;
+ return instance;
+}
+
+int RNG::rand_int(const int upper) {
+ return this->rand_int(0, upper);
+}
+int RNG::rand_int(const int lower, const int upper) {
+ uniform_int_distribution<int> random_dist(lower, upper);
+ return random_dist(rng);
+}
+
+double RNG::rand_double() {
+ return this->rand_double(0.f, 1.f);
+}
+double RNG::rand_double(const double lower, const double upper) {
+ uniform_real_distribution<double> random_dist(lower, upper);
+ return random_dist(rng);
+}
+
+bool RNG::rand_bool() {
+ return rand_int(0, 1);
+}
+
diff --git a/backend/RNG.h b/backend/RNG.h
new file mode 100644
index 0000000..ded337c
--- /dev/null
+++ b/backend/RNG.h
@@ -0,0 +1,23 @@
+#pragma once
+
+#include <random>
+
+class RNG {
+public:
+ static RNG & get();
+
+public:
+ int rand_int(const int upper);
+ int rand_int(const int lower, const int upper);
+ double rand_double();
+ double rand_double(const double lower, const double upper);
+ bool rand_bool();
+
+private:
+ RNG();
+
+private:
+ std::random_device dev;
+ std::mt19937 rng;
+};
+