From 6b1f6cf3b7685eed5fb9e24b18c479d842c4043c Mon Sep 17 00:00:00 2001 From: Hampus Date: Wed, 5 Jan 2022 21:35:35 +0100 Subject: feat: implement random start position --- src/mazerator.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/mazerator.c b/src/mazerator.c index 95c6fb6..8224fb9 100644 --- a/src/mazerator.c +++ b/src/mazerator.c @@ -51,8 +51,10 @@ int main(int argc, char *argv[]) { unsigned int maze_width = 40U; unsigned int maze_height = 20U; - unsigned int start_x = 1U; - unsigned int start_y = 1U; + + unsigned int *start_x = NULL; + unsigned int *start_y = NULL; + unsigned int *seed = NULL; char *wall = "█"; @@ -79,14 +81,16 @@ int main(int argc, char *argv[]) break; case 'x': - start_x = str_to_uint(optarg, &err); + start_x = malloc_s(sizeof(unsigned int *)); + *start_x = str_to_uint(optarg, &err); if (err != NULL) optarg_error(arg, err); break; case 'y': - start_y = str_to_uint(optarg, &err); + start_y = malloc_s(sizeof(unsigned int *)); + *start_y = str_to_uint(optarg, &err); if (err != NULL) optarg_error(arg, err); @@ -97,7 +101,6 @@ int main(int argc, char *argv[]) break; case 's': seed = malloc_s(sizeof(unsigned int *)); - *seed = str_to_uint(optarg, &err); if (err != NULL) @@ -110,9 +113,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: 1)\n" + "(Default: random)\n" " -y, --start-y Y The y coordinate for the start position " - "(Default: 1)\n" + "(Default: random)\n" " -W, --wall WALL Single character used as maze walls " "(Default: '█')\n" " -s, --seed SEED The randomization seed used for maze " @@ -122,12 +125,10 @@ int main(int argc, char *argv[]) exit(0); case '?': printf("\nTry '%s --help' for more information\n", argv[0]); - exit(1); + exit(EXIT_FAILURE); } } - validate_start_coords(start_x, start_y, maze_width, maze_height); - if (seed == NULL) { seed = malloc_s(sizeof(unsigned int *)); @@ -138,13 +139,30 @@ int main(int argc, char *argv[]) free(seed); - Position start_pos = position_create(start_x, start_y); + if (start_x == NULL) + { + start_x = malloc_s(sizeof(unsigned int *)); + *start_x = rand() % maze_width; + } + + if (start_y == NULL) + { + start_y = malloc_s(sizeof(unsigned int *)); + *start_y = rand() % maze_height; + } + + validate_start_coords(*start_x, *start_y, maze_width, maze_height); + + Position start_pos = position_create(*start_x * 2U + 1U, *start_y * 2U + 1U); - Grid grid = grid_create(maze_width * 2 + 1, maze_width * 2 + 1, wall); + Grid grid = grid_create(maze_width * 2U + 1U, maze_height * 2U + 1U, wall); grid_to_maze(grid, start_pos); grid_print(grid); grid_destroy(grid); + + free(start_x); + free(start_y); } -- cgit v1.2.3-18-g5258