aboutsummaryrefslogtreecommitdiff
path: root/src/mazerator.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mazerator.c')
-rw-r--r--src/mazerator.c118
1 files changed, 60 insertions, 58 deletions
diff --git a/src/mazerator.c b/src/mazerator.c
index d231173..95c6fb6 100644
--- a/src/mazerator.c
+++ b/src/mazerator.c
@@ -1,38 +1,19 @@
#include "maze.h"
+#include "grid.h"
+#include "position.h"
#include "utils.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
-void validate_number_optarg(char *optarg, int c)
+void optarg_error(char arg, char *error)
{
- unsigned int is_invalid = 0;
-
- size_t error_length = 40;
- char *error = malloc(error_length + 1);
-
- if (!is_number(optarg))
- {
- is_invalid = 1;
- snprintf(error, error_length, "It must be a number");
- }
- else if (atoi(optarg) < 1)
- {
- is_invalid = 1;
- snprintf(error, error_length, "It must be greater than 0");
- }
-
- if (is_invalid == 1)
- {
- printf("Error: Invalid option argument for -%c. %s\n", c, error);
- free(error);
- exit(1);
- }
-
- free(error);
+ printf("Error: Invalid option argument for -%c. %s\n", arg, error);
+ exit(EXIT_FAILURE);
}
-void validate_start_coords(int start_x, int start_y, int width, int height)
+void validate_start_coords(unsigned int start_x, unsigned int start_y, unsigned int width,
+ unsigned int height)
{
char *error_format =
"Error: The %s start coordinate is not allowed to be higher than the maze's %s\n";
@@ -40,17 +21,17 @@ void validate_start_coords(int start_x, int start_y, int width, int height)
if (start_x > width)
{
printf(error_format, "x", "width");
- exit(1);
+ exit(EXIT_FAILURE);
}
if (start_y > height)
{
printf(error_format, "y", "height");
- exit(1);
+ exit(EXIT_FAILURE);
}
}
-void get_seed(int *seed_dst)
+void get_seed(unsigned int *seed_dst)
{
FILE *urandom = fopen("/dev/urandom", "r");
fread(seed_dst, sizeof(*seed_dst), 1, urandom);
@@ -68,40 +49,60 @@ const struct option options[] = {{"width", required_argument, NULL, 'w'},
int main(int argc, char *argv[])
{
- int maze_width = 40;
- int maze_height = 20;
- int start_x = 0;
- int start_y = 0;
+ unsigned int maze_width = 40U;
+ unsigned int maze_height = 20U;
+ unsigned int start_x = 1U;
+ unsigned int start_y = 1U;
+ unsigned int *seed = NULL;
+
char *wall = "█";
- int seed = -1;
- int c;
- while ((c = getopt_long(argc, argv, "w:h:W:s:x:y:", options, NULL)) != -1)
+ int arg;
+ while ((arg = getopt_long(argc, argv, "w:h:W:s:x:y:", options, NULL)) != -1)
{
- switch (c)
+ char *err = NULL;
+
+ switch (arg)
{
case 'w':
- validate_number_optarg(optarg, c);
- maze_width = atoi(optarg);
+ maze_width = str_to_uint(optarg, &err);
+
+ if (err != NULL)
+ optarg_error(arg, err);
+
break;
case 'h':
- validate_number_optarg(optarg, c);
- maze_height = atoi(optarg);
+ maze_height = str_to_uint(optarg, &err);
+
+ if (err != NULL)
+ optarg_error(arg, err);
+
break;
case 'x':
- validate_number_optarg(optarg, c);
- start_x = atoi(optarg);
+ start_x = str_to_uint(optarg, &err);
+
+ if (err != NULL)
+ optarg_error(arg, err);
+
break;
case 'y':
- validate_number_optarg(optarg, c);
- start_y = atoi(optarg);
+ start_y = str_to_uint(optarg, &err);
+
+ if (err != NULL)
+ optarg_error(arg, err);
+
break;
case 'W':
wall = optarg;
break;
case 's':
- validate_number_optarg(optarg, c);
- seed = atoi(optarg);
+ seed = malloc_s(sizeof(unsigned int *));
+
+ *seed = str_to_uint(optarg, &err);
+
+ if (err != NULL)
+ optarg_error(arg, err);
+
break;
case 0:
printf("Usage: %s [OPTION]...\n\n"
@@ -109,9 +110,9 @@ int main(int argc, char *argv[])
" -w, --width WIDTH The width of the maze (Default: 40)\n"
" -h, --heigth HEIGHT The heigth of the maze (Default: 20)\n"
" -x, --start-x X The x coordinate for the start position "
- "(Default: 0)\n"
+ "(Default: 1)\n"
" -y, --start-y Y The y coordinate for the start position "
- "(Default: 0)\n"
+ "(Default: 1)\n"
" -W, --wall WALL Single character used as maze walls "
"(Default: '█')\n"
" -s, --seed SEED The randomization seed used for maze "
@@ -127,22 +128,23 @@ int main(int argc, char *argv[])
validate_start_coords(start_x, start_y, maze_width, maze_height);
- if (seed == -1)
+ if (seed == NULL)
{
- get_seed(&seed);
+ seed = malloc_s(sizeof(unsigned int *));
+ get_seed(seed);
}
- srand(seed);
+ srand(*seed);
- Dimensions dimens = {.width = maze_width, .height = maze_height};
+ free(seed);
- Maze maze = maze_create(dimens, wall);
+ Position start_pos = position_create(start_x, start_y);
- Position start_pos = {.x = start_x, .y = start_y};
+ Grid grid = grid_create(maze_width * 2 + 1, maze_width * 2 + 1, wall);
- maze_excavate(&maze, start_pos);
+ grid_to_maze(grid, start_pos);
- maze_print(maze);
+ grid_print(grid);
- maze_destroy(&maze);
+ grid_destroy(grid);
}