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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include "grid.h"
#include "maze.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 = NULL;
unsigned int *start_y = NULL;
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);
if (maze_width == 0)
optarg_error(arg, "It should not be 0");
break;
case 'h':
maze_height = str_to_uint(optarg, &err);
if (err != NULL)
optarg_error(arg, err);
if (maze_height == 0)
optarg_error(arg, "It should not be 0");
break;
case 'x':
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 = malloc_s(sizeof(unsigned int *));
*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);
if (*seed == 0)
optarg_error(arg, "It should not be 0");
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: random)\n"
" -y, --start-y Y The y coordinate for the start position "
"(Default: random)\n"
" -W, --wall WALL Single character used as maze walls "
"(Default: '█')\n"
" -s, --seed SEED The randomization seed used for maze "
"generation\n"
" --help Displays usage information\n",
argv[0]);
exit(0);
case '?':
printf("\nTry '%s --help' for more information\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (seed == NULL)
{
seed = malloc_s(sizeof(unsigned int *));
get_seed(seed);
}
srand(*seed);
free(seed);
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 * 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);
}
|