From 176d6141c87a180b251bacaee656808bad17498b Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 13 Feb 2022 20:54:02 +0100 Subject: refactor: add random number generation abstraction --- src/mazerator.cpp | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'src/mazerator.cpp') 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 #include -#include #include 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 start_x = nullptr; - std::shared_ptr start_y = nullptr; + std::unique_ptr start_x = nullptr; + std::unique_ptr start_y = nullptr; - std::unique_ptr random_gen = nullptr; + std::shared_ptr 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(); + start_x = std::make_unique(); parse_uint_arg(start_x.get(), arg); break; case 'y': - start_y = std::make_shared(); + start_y = std::make_unique(); 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(seed); + random_gen = std::make_shared(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(random_device()); + random_gen = std::make_shared(); } if (start_x == nullptr) { - auto random_dist = - std::uniform_int_distribution(0, maze_width - 1U); - - start_x = std::make_unique(random_dist(*random_gen)); + start_x = + std::make_unique(random_gen->in_range(0, maze_width - 1U)); } if (start_y == nullptr) { - auto random_dist = - std::uniform_int_distribution(0, maze_height - 1U); - - start_y = std::make_unique(random_dist(*random_gen)); + start_y = + std::make_unique(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(*start_x * 2U + 1U, *start_y * 2U + 1U); - matrix_to_maze(&matrix, start_pos, " ", *random_gen); + matrix_to_maze(&matrix, start_pos, " ", random_gen); matrix.print(); } -- cgit v1.2.3-18-g5258