aboutsummaryrefslogtreecommitdiff
path: root/src/mazerator.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-02-13 20:54:02 +0100
committerHampusM <hampus@hampusmat.com>2022-02-13 20:54:02 +0100
commit176d6141c87a180b251bacaee656808bad17498b (patch)
tree9d3893014eeb8be293cf8928044925c379739d24 /src/mazerator.cpp
parentb0c265ee3d94921f55266a679d3801a4d2b4505b (diff)
refactor: add random number generation abstraction
Diffstat (limited to 'src/mazerator.cpp')
-rw-r--r--src/mazerator.cpp37
1 files changed, 19 insertions, 18 deletions
diff --git a/src/mazerator.cpp b/src/mazerator.cpp
index 33ef1b1..b30124f 100644
--- a/src/mazerator.cpp
+++ b/src/mazerator.cpp
@@ -2,10 +2,10 @@
#include "engine/vector2.hpp"
#include "getopt.h"
#include "maze.hpp"
+#include "random_generator.hpp"
#include "utils.hpp"
#include <iostream>
#include <memory>
-#include <random>
#include <string>
void optarg_error(int arg, std::string error)
@@ -19,11 +19,15 @@ void validate_start_coords(unsigned int start_x, unsigned int start_y, unsigned
unsigned int height)
{
if (start_x >= width)
+ {
throw "The x start coordinate cannot be higher than or equal to the maze's width";
+ }
if (start_y >= height)
+ {
throw "The y start coordinate cannot be higher than or equal to the maze's "
"height";
+ }
}
/**
@@ -40,7 +44,9 @@ void parse_uint_arg(unsigned int *num_dst, int arg, bool check_zero = false)
*num_dst = str_to_uint(std::string(optarg));
if (check_zero && *num_dst == 0)
+ {
throw "It should not be 0";
+ }
}
catch (const char *error)
{
@@ -62,10 +68,10 @@ int main(int argc, char *argv[])
unsigned int maze_width = 40U;
unsigned int maze_height = 20U;
- std::shared_ptr<unsigned int> start_x = nullptr;
- std::shared_ptr<unsigned int> start_y = nullptr;
+ std::unique_ptr<unsigned int> start_x = nullptr;
+ std::unique_ptr<unsigned int> start_y = nullptr;
- std::unique_ptr<std::mt19937> random_gen = nullptr;
+ std::shared_ptr<RandomNumberGenerator> random_gen = nullptr;
std::string wall = "█";
@@ -81,11 +87,11 @@ int main(int argc, char *argv[])
parse_uint_arg(&maze_height, arg, true);
break;
case 'x':
- start_x = std::make_shared<unsigned int>();
+ start_x = std::make_unique<unsigned int>();
parse_uint_arg(start_x.get(), arg);
break;
case 'y':
- start_y = std::make_shared<unsigned int>();
+ start_y = std::make_unique<unsigned int>();
parse_uint_arg(start_y.get(), arg);
break;
case 'W':
@@ -95,7 +101,7 @@ int main(int argc, char *argv[])
unsigned int seed;
parse_uint_arg(&seed, arg, true);
- random_gen = std::make_unique<std::mt19937>(seed);
+ random_gen = std::make_shared<RandomNumberGenerator>(seed);
break;
case 0:
std::cout
@@ -126,24 +132,19 @@ int main(int argc, char *argv[])
if (random_gen == nullptr)
{
- std::random_device random_device;
- random_gen = std::make_unique<std::mt19937>(random_device());
+ random_gen = std::make_shared<RandomNumberGenerator>();
}
if (start_x == nullptr)
{
- auto random_dist =
- std::uniform_int_distribution<unsigned int>(0, maze_width - 1U);
-
- start_x = std::make_unique<unsigned int>(random_dist(*random_gen));
+ start_x =
+ std::make_unique<unsigned int>(random_gen->in_range(0, maze_width - 1U));
}
if (start_y == nullptr)
{
- auto random_dist =
- std::uniform_int_distribution<unsigned int>(0, maze_height - 1U);
-
- start_y = std::make_unique<unsigned int>(random_dist(*random_gen));
+ start_y =
+ std::make_unique<unsigned int>(random_gen->in_range(0, maze_height - 1U));
}
try
@@ -162,7 +163,7 @@ int main(int argc, char *argv[])
auto start_pos = std::make_shared<Vector2>(*start_x * 2U + 1U, *start_y * 2U + 1U);
- matrix_to_maze<std::string>(&matrix, start_pos, " ", *random_gen);
+ matrix_to_maze<std::string>(&matrix, start_pos, " ", random_gen);
matrix.print();
}