aboutsummaryrefslogtreecommitdiff
path: root/src/mazerator.cpp
blob: f74f9520f1f1c2f14c652630a9370b9e2551a0e6 (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
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
#include "getopt.h"
#include "matrix.hpp"
#include "maze.hpp"
#include "utils.hpp"
#include "vector2.hpp"
#include <iostream>
#include <memory>
#include <random>
#include <string>

void optarg_error(char arg, std::string error)
{
	std::cout << "Error: Invalid option argument for -" << arg << ". " << error
			  << std::endl;
	exit(EXIT_FAILURE);
}

void validate_start_coords(unsigned int start_x, unsigned int start_y, unsigned int width,
						   unsigned int height)
{
	if (start_x >= width)
		throw "The x start coordinate cannot be higher than or equal to the maze's width";

	if (start_y >= height)
		throw "The y start coordinate cannot be higher than or equal to the maze's "
			  "height";
}

/**
 * Parses a unsigned integer command-line argument.
 *
 * @param num_dst A pointer to a place to store the result value
 * @param arg The command-line argument character
 * @param check_zero Whether or not to make sure that the result is not zero
 */
void parse_uint_arg(unsigned int *num_dst, char arg, bool check_zero = false)
{
	try
	{
		*num_dst = str_to_uint(std::string(optarg));

		if (check_zero && *num_dst == 0)
			throw "It should not be 0";
	}
	catch (const char *error)
	{
		optarg_error(arg, std::string(error));
	}
}

const struct option options[] = {{"width", required_argument, NULL, 'w'},
								 {"height", 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;

	std::shared_ptr<unsigned int> start_x = nullptr;
	std::shared_ptr<unsigned int> start_y = nullptr;

	std::unique_ptr<std::mt19937> random_gen = nullptr;

	std::string wall = "█";

	int arg;
	while ((arg = getopt_long(argc, argv, "w:h:W:s:x:y:", options, nullptr)) != -1)
	{
		switch (arg)
		{
		case 'w':
			parse_uint_arg(&maze_width, arg, true);
			break;
		case 'h':
			parse_uint_arg(&maze_height, arg, true);
			break;
		case 'x':
			start_x = std::make_shared<unsigned int>();
			parse_uint_arg(start_x.get(), arg);
			break;
		case 'y':
			start_y = std::make_shared<unsigned int>();
			parse_uint_arg(start_y.get(), arg);
			break;
		case 'W':
			wall = optarg;
			break;
		case 's':
			unsigned int seed;
			parse_uint_arg(&seed, arg, true);

			random_gen = std::make_unique<std::mt19937>(seed);
			break;
		case 0:
			std::cout
				<< "Usage: " << argv[0]
				<< " [OPTION]...\n\n"
				   "Options:\n"
				   "  -w, --width WIDTH       The width of the maze (Default: 40)\n"
				   "  -h, --height HEIGHT     The height 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"
				<< std::endl;
			return EXIT_SUCCESS;
		case '?':
			std::cout << "\nTry '" << argv[0] << " --help' for more information"
					  << std::endl;
			return EXIT_FAILURE;
		}
	}

	if (random_gen == nullptr)
	{
		std::random_device random_device;
		random_gen = std::make_unique<std::mt19937>(random_device());
	}

	if (start_x == nullptr)
	{
		auto random_dist =
			std::uniform_int_distribution<unsigned int>(0, maze_width - 1U);

		start_x = std::make_unique<unsigned int>(random_dist(*random_gen));
	}

	if (start_y == nullptr)
	{
		auto random_dist =
			std::uniform_int_distribution<unsigned int>(0, maze_height - 1U);

		start_y = std::make_unique<unsigned int>(random_dist(*random_gen));
	}

	try
	{
		validate_start_coords(*start_x, *start_y, maze_width, maze_height);
	}
	catch (const char *error)
	{
		std::cout << "Error: " << error << std::endl;
		return EXIT_FAILURE;
	}

	Matrix<std::string> matrix(maze_height * 2U + 1U, maze_width * 2U + 1U);

	matrix.fill(wall);

	auto start_pos = std::make_shared<Vector2>(*start_x * 2U + 1U, *start_y * 2U + 1U);

	matrix_to_maze<std::string>(&matrix, start_pos, " ", *random_gen);

	matrix.print();
}