-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_validation.c
More file actions
95 lines (88 loc) · 2.56 KB
/
map_validation.c
File metadata and controls
95 lines (88 loc) · 2.56 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_validation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apeposhi <apeposhi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/16 17:59:35 by apeposhi #+# #+# */
/* Updated: 2023/07/21 21:49:12 by apeposhi ### ########.fr */
/* */
/* ************************************************************************** */
#include "include/so_long.h"
static int ft_check_middle(char c, t_data *g_d, size_t i, size_t j)
{
if (j == 0 || j == g_d->map.l - 1)
{
if (c != '1')
return (ft_print_error("Wrong map layout given.\n", 1));
}
else
{
if (c != '1' && c != '0' && c != 'P' && \
c != 'E' && c != 'C' && c != 'A')
return (ft_print_error("Invalid map. Character not allowed!\n", 1));
if (c == 'P')
{
g_d->map.pl += 1;
g_d->map.x_p = j;
g_d->map.y_p = i;
}
if (c == 'E' && g_d->map.fnsh == 0)
g_d->map.fnsh += 1;
if (c == 'C')
g_d->map.clls += 1;
}
return (0);
}
int ft_check_map_data(t_map map)
{
if (map.clls == 0)
return (ft_print_error("Collectables are missing!\n", 1));
if (map.fnsh != 1)
return (ft_print_error("Exactly one E must be given.\n", 1));
if (map.pl != 1)
return (ft_print_error("Exactly one P must be given.\n", 1));
return (0);
}
static int ft_check_map(char **c_state, t_data *g_d)
{
size_t i;
size_t j;
i = 0;
while (c_state[i] != NULL)
{
j = 0;
while (c_state[i][j] != '\0')
{
if (g_d->map.l != ft_strlen(c_state[i]))
return (ft_print_error("Map doesn't have the right size.\n", 1));
if (i == 0 || i + 1 == g_d->map.h)
{
if (c_state[i][j] != '1')
return (ft_print_error("Wrong map layout given.\n", 1));
}
else
if (ft_check_middle(c_state[i][j], g_d, i, j))
return (1);
j++;
}
i++;
}
return (ft_check_map_data(g_d->map));
}
static void ft_init_map_data(t_map *map)
{
map->clls = 0;
map->fnsh = 0;
map->pl = 0;
}
int ft_validate_map(t_data *g_d)
{
ft_init_map_data(&g_d->map);
if (ft_check_map(g_d->c_state, g_d))
return (1);
if (ft_is_map_winnable(&g_d->map, g_d->c_state))
return (1);
return (0);
}