Added flood fill algorithm finally, to check for holes in map

This commit is contained in:
Theo Champion 2025-06-24 10:41:50 +02:00
parent 321ef3db7c
commit f9825feeea
3 changed files with 44 additions and 5 deletions

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */
/* Updated: 2025/06/22 00:17:39 by tchampio ### ########.fr */
/* Updated: 2025/06/24 10:40:02 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -85,12 +85,12 @@ void draw_map(t_mapdata *map, t_mlx_data *data)
int j;
i = 0;
while (map->map[i])
while (map->mapflood[i])
{
j = 0;
while (map->map[i][j])
while (map->mapflood[i][j])
{
if (map->map[i][j] == '1')
if (map->mapflood[i][j] == '1')
draw_2d_wall(map->f_color, data, 10, 10 * j, 10 * i);
else if (ft_strchr("NSEW", map->map[i][j]))
draw_2d_wall(0x00FF0000, data, 10, 10 * j, 10 * i);

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
/* Updated: 2025/06/21 19:40:13 by tchampio ### ########.fr */
/* Updated: 2025/06/24 10:40:30 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -53,6 +53,20 @@ void free_tab_length(char **tab, int length)
free(tab);
}
void flood_fill(t_mapdata *map, int x, int y)
{
if (map->mapflood[y][x] == '1')
return ;
if (map->mapflood[y][x] == ' ' || map->mapflood[y][x] == '\n')
map->isvalid = false;
map->mapflood[y][x] = '1';
flood_fill(map, x + 1, y);
flood_fill(map, x, y + 1);
flood_fill(map, x - 1, y);
flood_fill(map, x, y - 1);
}
bool check_cubfile(char *file, t_mapdata *map)
{
int fd;
@ -75,5 +89,8 @@ bool check_cubfile(char *file, t_mapdata *map)
if (!check_bare_minimum(map))
return (close(fd), false);
map->isvalid = true;
flood_fill(map, map->startx, map->starty);
if (!map->isvalid)
return (close(fd), ft_strlcpy(map->error, "Map has holes!", 16), false);
return (true);
}