1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#include "maze.h"
#include "grid.h"
#include "position.h"
#include "utils.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
void optarg_error(char arg, char *error)
{
printf("Error: Invalid option argument for -%c. %s\n", arg, error);
exit(EXIT_FAILURE);
}
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";
if (start_x > width)
{
printf(error_format, "x", "width");
exit(EXIT_FAILURE);
}
if (start_y > height)
{
printf(error_format, "y", "height");
exit(EXIT_FAILURE);
}
}
void get_seed(unsigned int *seed_dst)
{
FILE *urandom = fopen("/dev/urandom", "r");
fread(seed_dst, sizeof(*seed_dst), 1, urandom);
fclose(urandom);
}
const struct option options[] = {{"width", required_argument, NULL, 'w'},
{"heigth", required_argument, NULL, 'h'},
{"wall", required_argument, NULL, 'W'},
{"seed", required_argument, NULL, 's'},
{"start-x", required_argument, NULL, 'x'},
{"start-y", required_argument, NULL, 'y'},
{"help", no_argument, NULL, 0},
{NULL, 0, NULL, 0}};
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 *seed = NULL;
char *wall = "█";
int arg;
while ((arg = getopt_long(argc, argv, "w:h:W:s:x:y:", options, NULL)) != -1)
{
char *err = NULL;
switch (arg)
{
case 'w':
maze_width = str_to_uint(optarg, &err);
if (err != NULL)
optarg_error(arg, err);
break;
case 'h':
maze_height = str_to_uint(optarg, &err);
if (err != NULL)
optarg_error(arg, err);
break;
case 'x':
start_x = str_to_uint(optarg, &err);
if (err != NULL)
optarg_error(arg, err);
break;
case 'y':
start_y = str_to_uint(optarg, &err);
if (err != NULL)
optarg_error(arg, err);
break;
case 'W':
wall = optarg;
break;
case 's':
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"
"Options:\n"
" -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"
" -y, --start-y Y The y coordinate for the start position "
"(Default: 1)\n"
" -W, --wall WALL Single character used as maze walls "
"(Default: '█')\n"
" -s, --seed SEED The randomization seed used for maze "
"generation (Any number)\n"
" --help Displays usage information\n",
argv[0]);
exit(0);
case '?':
printf("\nTry '%s --help' for more information\n", argv[0]);
exit(1);
}
}
validate_start_coords(start_x, start_y, maze_width, maze_height);
if (seed == NULL)
{
seed = malloc_s(sizeof(unsigned int *));
get_seed(seed);
}
srand(*seed);
free(seed);
Position start_pos = position_create(start_x, start_y);
Grid grid = grid_create(maze_width * 2 + 1, maze_width * 2 + 1, wall);
grid_to_maze(grid, start_pos);
grid_print(grid);
grid_destroy(grid);
}
|