aboutsummaryrefslogtreecommitdiff
path: root/mazerator.c
blob: 33c47cea3bfbd893b938580721fec15782d97575 (plain)
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
#include "maze.h"
#include "utils.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

void validate_number_optarg(char *optarg, int c)
{
	char *base_error = malloc((38 + 1) * sizeof(char));
	sprintf(base_error, "Error: Invalid option argument for -%c.", c);

	if (!is_number(optarg))
	{
		printf("%s It must be a number\n", base_error);
		exit(1);
	}

	if (atoi(optarg) < 1)
	{
		printf("%s It must be greater than 0\n", base_error);
		exit(1);
	}
}

void validate_start_coords(int start_x, int start_y, int width, 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(1);
	}

	if (start_y > height)
	{
		printf(error_format, "y", "height");
		exit(1);
	}
}

void get_seed(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[])
{
	int maze_width = 40;
	int maze_height = 20;
	int start_x = 0;
	int start_y = 0;
	char *wall = "█";
	int seed = -1;

	int c;
	while ((c = getopt_long(argc, argv, "w:h:W:s:x:y:", options, NULL)) != -1)
	{
		switch (c)
		{
		case 'w':
			validate_number_optarg(optarg, c);
			maze_width = atoi(optarg);
			break;
		case 'h':
			validate_number_optarg(optarg, c);
			maze_height = atoi(optarg);
			break;
		case 'x':
			validate_number_optarg(optarg, c);
			start_x = atoi(optarg);
			break;
		case 'y':
			validate_number_optarg(optarg, c);
			start_y = atoi(optarg);
			break;
		case 'W':
			wall = optarg;
			break;
		case 's':
			validate_number_optarg(optarg, c);
			seed = atoi(optarg);
			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: 0)\n"
				"  -y, --start-y Y         The y coordinate for the start position (Default: 0)\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 == -1)
	{
		get_seed(&seed);
	}

	srand(seed);

	int full_maze_height = maze_height * 2 + 1;
	int full_maze_width = maze_width * 2 + 1;

	struct MazeSize maze_size = {.width = maze_width, .height = maze_height};

	struct Maze maze = maze_create(maze_size, wall);

	struct Position start_pos = {.x = start_x, .y = start_y};

	maze_excavate(maze, start_pos);

	maze_print(maze.grid, full_maze_width, full_maze_height);
}