-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_validations.c
77 lines (69 loc) · 2.1 KB
/
map_validations.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
75
76
77
/* ************************************************************************** */
/* */
/* :::::::: */
/* map_validations.c :+: :+: */
/* +:+ */
/* By: cschuijt <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/11/11 16:45:17 by cschuijt #+# #+# */
/* Updated: 2022/11/11 16:45:17 by cschuijt ######## odam.nl */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include <stdlib.h>
static void validate_outer_row(char *row);
static void validate_middle_row(char *row);
// Validates that all rows of the map are the same size and
// that the map contains at least three rows and three columns
void validate_map_measurements(char **map)
{
size_t len;
size_t i;
i = 0;
len = ft_strlen(map[i]);
if (len < 3)
exit_message("map is too small");
i++;
while (map[i])
{
if (ft_strlen(map[i]) != len)
exit_message("map rows are not all the same length");
i++;
}
if (i < 3)
exit_message("map is too small");
}
// Validates that the entire map is surrounded by walls.
void validate_map_boundaries(char **map)
{
size_t i;
i = 0;
validate_outer_row(map[i]);
validate_outer_row(last_string_in_array(map));
i++;
while (map[i] && map[i + 1])
{
validate_middle_row(map[i]);
i++;
}
}
static void validate_outer_row(char *row)
{
while (*row)
{
if (*row != '1')
exit_message("map outer walls are not properly closed");
row++;
}
}
static void validate_middle_row(char *row)
{
if (*row != '1')
exit_message("map outer walls are not properly closed");
row++;
while (*row)
row++;
row--;
if (*row != '1')
exit_message("map outer walls are not properly closed");
}