core: check map surround/close

This commit is contained in:
Camille Chauvet 2023-05-04 11:47:18 +00:00
parent c4d953acb5
commit b323b98dc0

View File

@ -75,36 +75,14 @@ static int map_is_in_one_part(const char **body)
return (0); return (0);
} }
static int map_surround_check(char **body, size_t x, size_t y) static char **map_cpy(const char **body)
{
if (y == 0 || body[y] == NULL)
return (1);
if (x == 0 || body[y][x] == '\0')
return (1);
if (ft_is_in("21", body[y][x]))
return (0);
if (ft_is_in(" ", body[y][x]))
return (1);
body[y][x] = '2';
if (map_surround_check(body, x + 1, y))
return (1);
if (map_surround_check(body, x - 1, y))
return (1);
if (map_surround_check(body, x, y + 1))
return (1);
if (map_surround_check(body, x, y - 1))
return (1);
return (0);
}
static int map_surround(const char **body, t_map *map)
{ {
size_t i; size_t i;
char **copy; char **copy;
copy = malloc((ft_tablen((const void **) body) + 1) * sizeof(char *)); copy = malloc((ft_tablen((const void **) body) + 1) * sizeof(char *));
if (copy == NULL) if (copy == NULL)
return (1); return (NULL);
i = 0; i = 0;
while (body[i] != NULL) while (body[i] != NULL)
{ {
@ -112,15 +90,42 @@ static int map_surround(const char **body, t_map *map)
if (copy[i] == NULL) if (copy[i] == NULL)
{ {
ft_cancel((void **) copy, i); ft_cancel((void **) copy, i);
return (1); return (NULL);
} }
i++; i++;
} }
copy[i] = NULL; copy[i] = NULL;
if (map_surround_check(copy, map->spawn_x, map->spawn_y)) return (copy);
{ }
ft_freer_tab_ultimate(1, copy);
static int map_surround(const char **body)
{
char **copy;
size_t y;
size_t x;
copy = map_cpy(body);
if (copy == NULL)
return (1); return (1);
y = 0;
while (body[y] != NULL)
{
x = 0;
while (body[y][x] != '\0')
{
if (body[y][x] == '0')
{
if (body[y + 1] == NULL || (y == 0)
|| (body[y][x + 1] == '\0') || (x == 0)
|| (body[y + 1][x] == ' ' || body[y + 1][x] == '\0')
|| (body[y - 1][x] == ' ' || body[y - 1][x] == '\0')
|| (body[y][x - 1] == ' ' || body[y][x - 1] == '\0')
|| (body[y][x + 1] == ' ' || body[y][x + 1] == '\0'))
return (1);
}
x++;
}
y++;
} }
ft_freer_tab_ultimate(1, copy); ft_freer_tab_ultimate(1, copy);
return (0); return (0);
@ -166,7 +171,7 @@ int body_is_valid(const char **body, t_map *map)
ft_eprintf("map: spawn position: not define"); ft_eprintf("map: spawn position: not define");
return (0); return (0);
} }
error = map_surround(body, map); error = map_surround(body);
if (error) if (error)
{ {
ft_eprintf("map: not surrounded"); ft_eprintf("map: not surrounded");