-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.c
More file actions
134 lines (124 loc) · 2.61 KB
/
Copy pathvalidate.c
File metadata and controls
134 lines (124 loc) · 2.61 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* validate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mpedraza <mpedraza@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/21 16:55:23 by mpedraza #+# #+# */
/* Updated: 2026/01/23 18:51:24 by mpedraza ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int is_rectangle(t_game *g)
{
int y;
if (!g->map)
return (0);
y = 0;
while (g->map[y])
{
if (ft_strlen(g->map[y]) != (size_t)g->map_w)
return (0);
y++;
}
return (1);
}
static void set_objets(char obj, t_game *g, int x, int y)
{
if (obj == 'C' || obj == 'A')
{
if (obj == 'C')
g->i.c++;
else
g->i.a++;
}
if (obj == 'E')
{
g->e.total++;
g->e.x = x;
g->e.y = y;
}
if (obj == 'P')
{
g->p.total++;
g->p.x = x;
g->p.y = y;
}
}
int has_valid_objects(t_game *g)
{
int y;
int x;
y = 0;
while (g->map[y])
{
x = 0;
while (g->map[y][x])
{
if (!ft_strchr(MAP_MARKERS, g->map[y][x]))
{
write(2, "Error: Map contains invalid characters\n", 40);
return (0);
}
set_objets(g->map[y][x], g, x, y);
x++;
}
y++;
}
if (g->p.total != 1 || g->e.total != 1 || g->i.c < 1)
{
write(2, "Error: Wrong number of objects in map\n", 39);
return (0);
}
return (1);
}
int has_valid_border(t_game *g)
{
int y;
int x;
y = 0;
while (g->map[y])
{
x = 0;
while (g->map[y][x])
{
if (y == 0 || y == g->map_h)
{
if (g->map[y][x] != '1')
return (0);
}
if (x == 0 || x == g->map_w - 1)
{
if (g->map[y][x] != '1')
return (0);
}
x++;
}
y++;
}
return (1);
}
int is_solvable(t_game *g)
{
int y;
g->test_map = malloc(sizeof(char *) * (g->map_h + 1));
if (!g->test_map)
return (0);
y = 0;
while (g->map[y])
{
g->test_map[y] = ft_strdup(g->map[y]);
if (!g->test_map[y])
return (0);
y++;
}
g->test_map[y] = NULL;
flood_fill_items(g->test_map, g->p.x, g->p.y);
if (!has_valid_item_path(g->test_map))
return (0);
flood_fill_exit(g->test_map, g->p.x, g->p.y);
if (!has_valid_exit_path(g->test_map))
return (0);
return (1);
}