diff options
Diffstat (limited to 'mazerator.c')
| -rw-r--r-- | mazerator.c | 78 | 
1 files changed, 45 insertions, 33 deletions
diff --git a/mazerator.c b/mazerator.c index 82620f0..46ac9fc 100644 --- a/mazerator.c +++ b/mazerator.c @@ -168,6 +168,41 @@ void print_maze(char ***maze, int height, int width)  	}  } +void validate_number_optarg(char *optarg, int c) +{ +	char *base_error = malloc(39 * 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); +	} +} +  const struct option options[] = {  	{"width", required_argument, NULL, 'w'},  	{"heigth", required_argument, NULL, 'h'}, @@ -193,63 +228,38 @@ int main(int argc, char *argv[])  		switch (c)  		{  		case 'w': -			if (!is_number(optarg) || atoi(optarg) < 1 || atoi(optarg) > 255) -			{ -				printf("Invalid option argument for -%c!\n", c); -				exit(1); -			} - +			validate_number_optarg(optarg, c);  			maze_width = atoi(optarg);  			break;  		case 'h': -			if (!is_number(optarg) || atoi(optarg) < 1 || atoi(optarg) > 255) -			{ -				printf("Invalid option argument for -%c!\n", c); -				exit(1); -			} - +			validate_number_optarg(optarg, c);  			maze_height = atoi(optarg);  			break;  		case 'x': -			if (!is_number(optarg) || atoi(optarg) < 1) -			{ -				printf("Invalid option argument for -%c!\n", c); -				exit(1); -			} - +			validate_number_optarg(optarg, c);  			start_x = atoi(optarg);  			break;  		case 'y': -			if (!is_number(optarg) || atoi(optarg) < 1) -			{ -				printf("Invalid option argument for -%c!\n", c); -				exit(1); -			} - +			validate_number_optarg(optarg, c);  			start_y = atoi(optarg);  			break;  		case 'W':  			wall = optarg;  			break;  		case 's': -			if (!is_number(optarg)) -			{ -				printf("Invalid option argument for -s/--seed!\n"); -				exit(1); -			} - +			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. (1-255) (Default: 40)\n" -				"  -h, --heigth HEIGHT     The heigth of the maze. (1-255) (Default: 20)\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" +				"  -s, --seed SEED         The randomization seed used for maze generation (Any number)\n"  				"  --help                  Displays usage information\n",  				argv[0]);  			exit(0); @@ -259,6 +269,8 @@ int main(int argc, char *argv[])  		}  	} +	validate_start_coords(start_x, start_y, maze_width, maze_height); +  	if (seed == -1)  	{  		FILE *f = fopen("/dev/urandom", "r");  | 
