-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_maze.c
74 lines (63 loc) · 1.81 KB
/
main_maze.c
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
#include <graphviz/cgraph.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include "algorithms.h"
#include "utils.h"
void usage(char *command_name) {
fprintf(stderr, "usage: %s --algorithm=<value> [--seed=number]\n", command_name);
fprintf(stderr, "reads a dot grid on stdin and turns it into a maze on stdout\n");
fprintf(stderr, "available algorithms are:\n");
fprintf(stderr, " backtracker (the default)\n");
}
int main(int argc, char** argv) {
char *command_name = argv[0];
void (*algorithm)(Agraph_t *) = mazes_generate_backtracker;
long seed = 12345;
struct option options[] = {
{.name = "algorithm", .has_arg = required_argument, .flag = NULL, .val = 'a'},
{.name = "seed", .has_arg = required_argument, .flag = NULL, .val = 's'},
{.name = NULL, .has_arg = 0, .flag = NULL, .val = 0}
};
int opt;
while (-1 != (opt = getopt_long(argc, argv, "", options, NULL))) {
switch (opt) {
case 'a':
if (0 == strcmp("backtracker", optarg)) {
algorithm = mazes_generate_backtracker;
} else {
fprintf(stderr, "algorithm not recognized\n");
usage(command_name);
return 1;
}
break;
case 's':
seed = strtol(optarg, NULL, 10);
break;
default:
fprintf(stderr, "unrecognized option \'%c\'\n", opt);
usage(command_name);
return 1;
}
}
if (NULL == algorithm) {
fprintf(stderr, "algorithm is a required option\n");
usage(command_name);
return 1;
}
if (seed <= 0) {
fprintf(stderr, "seed must be a number greater than zero\n");
usage(command_name);
return 1;
}
srand(seed);
Agraph_t *maze = agread(stdin, NULL);
if (NULL == maze) {
fprintf(stderr, "could not read maze from stdin\n");
usage(command_name);
return 1;
}
algorithm(maze);
agwrite(maze, stdout);
agclose(maze);
}