Add option to set random seed for deterministic behavior

Bump version to 1.6
This commit is contained in:
2022-01-15 14:40:21 -05:00
parent 476aa4bce6
commit 0272fd726d
10 changed files with 58 additions and 12 deletions

View File

@@ -15,6 +15,7 @@ find_package(PkgConfig REQUIRED)
pkg_search_module(PNG REQUIRED libpng)
check_symbol_exists(arc4random_uniform "stdlib.h" HAVE_ARC4RANDOM)
check_symbol_exists(srand_deterministic "stdlib.h" HAVE_SRAND_DETERMINISTIC)
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/config.h.in"
"${CMAKE_CURRENT_SOURCE_DIR}/config.h" )

View File

@@ -1 +1,2 @@
#cmakedefine HAVE_ARC4RANDOM @HAVE_ARC4RANDOM@
#cmakedefine HAVE_SRAND_DETERMINISTIC @HAVE_SRAND_DETERMINISTIC@

View File

@@ -1,5 +1,6 @@
#include <stdlib.h>
#include "grid.h"
#include "options.h"
#include "config.h"
#define MAX_EDGE_WEIGHT 100
@@ -19,14 +20,25 @@
#define HORIZONTAL_HALF_LEFT "\xe2\x95\xb8"
#define HORIZONTAL_HALF_RIGHT "\xe2\x95\xba"
static edgeweight_t random_edgeweight() {
static edgeweight_t random_edgeweight(mazeoptions_t const* options) {
if (!options->seed_set) {
#ifdef HAVE_ARC4RANDOM
return arc4random_uniform(MAX_EDGE_WEIGHT);
return arc4random_uniform(MAX_EDGE_WEIGHT);
#else // HAVE_ARC4RANDOM
return random()%MAX_EDGE_WEIGHT;
return random()%MAX_EDGE_WEIGHT;
#endif // HAVE_ARC4RANDOM
} else {
// use deterministic random number generator
return rand()%MAX_EDGE_WEIGHT;
}
}
#ifdef HAVE_SRAND_DETERMINISTIC
#define SRAND(x) (srand_deterministic(x))
#else
#define SRAND(x) (srand(x))
#endif // defined(HAVE_SRAND_DETERMINISTIC)
mazegrid_t mazegrid_new(size_t width, size_t height) {
mazeedges_t** grid = calloc(height, sizeof(mazeedges_t*));
for (size_t i = 0; i < height; i++) {
@@ -72,11 +84,16 @@ edgeweight_t mazegrid_get_edge(mazegrid_t const* g, size_t x, size_t y, mazeedge
else return g->grid[y][x].up;
}
void mazegrid_randomize(mazegrid_t* g) {
void mazegrid_randomize(mazegrid_t* g, mazeoptions_t const* options) {
// initialize random system
if (options->seed_set) {
SRAND(options->seed);
}
for (size_t i = 0; i < g->height; i++) {
for (size_t j = 0; j < g->width; j++) {
if (i < g->height - 1) g->grid[i][j].up = random_edgeweight();
if (j < g->width - 1) g->grid[i][j].right = random_edgeweight();
if (i < g->height - 1) g->grid[i][j].up = random_edgeweight(options);
if (j < g->width - 1) g->grid[i][j].right = random_edgeweight(options);
}
}
}

View File

@@ -10,7 +10,7 @@ void mazegrid_free(mazegrid_t* g);
int mazegrid_set_edge(mazegrid_t* g, size_t x, size_t y, mazeedge_dir_t dir, edgeweight_t wt);
edgeweight_t mazegrid_get_edge(mazegrid_t const* g, size_t, size_t y, mazeedge_dir_t dir);
void mazegrid_randomize(mazegrid_t* g);
void mazegrid_randomize(mazegrid_t* g, mazeoptions_t const* options);
void mazegrid_uniform(mazegrid_t* g);
void mazegrid_print(mazegrid_t const* g, FILE * f);

View File

@@ -1,13 +1,21 @@
#include <mazemaker.h>
#include <stdlib.h>
#include "grid.h"
#include "prim.h"
void mazemaker_generate_maze(int width, int height, mazegrid_t* result) {
void mazemaker_generate_maze_opt(int width, int height, mazegrid_t* result, mazeoptions_t const* options) {
mazegrid_t g = mazegrid_new(width, height);
mazegrid_randomize(&g);
mazegrid_randomize(&g, options);
prim(&g, result);
mazegrid_free(&g);
}
void mazemaker_generate_maze(int width, int height, mazegrid_t* result) {
mazeoptions_t* options = mazemaker_options_new(); // use defaults
mazemaker_generate_maze_opt(width, height, result, options);
mazemaker_options_free(options);
}
void mazemaker_free_maze(mazegrid_t* maze) {
mazegrid_free(maze);
}

View File

@@ -10,6 +10,8 @@ mazeoptions_t* mazemaker_options_new() {
// set defaults
memset(result->wall_color, 0, 3);
memset(result->background_color, 0xff, 3);
result->seed = 0;
result->seed_set = false;
return result;
}
@@ -78,3 +80,7 @@ int mazemaker_options_set_background_color(mazeoptions_t* options, char const* c
return stringToColor(color_desc, options->background_color);
}
void mazemaker_options_set_seed(mazeoptions_t* options, rand_seed_t seed) {
options->seed = seed;
options->seed_set = true;
}

View File

@@ -2,12 +2,16 @@
#define _OPTIONS_H 1
#include <stdint.h>
#include <stdbool.h>
typedef uint8_t rgb_color_t[3];
typedef unsigned int rand_seed_t;
typedef struct mazeoptions {
rgb_color_t wall_color;
rgb_color_t background_color;
rand_seed_t seed;
bool seed_set;
} mazeoptions_t;
#endif // !def(_OPTIONS_H)