diff options
| author | Dominik Kaiser | 2024-06-10 16:40:28 +0200 |
|---|---|---|
| committer | GitHub | 2024-06-10 16:40:28 +0200 |
| commit | 568827dc74eb4e2a7719d7cfbd48083facf85cc3 (patch) | |
| tree | dd896d064a49bb399b7c942fde49ae0bf892263b /src/check_for_valid_path.c | |
| parent | 7b230503231e154c5400413b4f9b87dd6fcd8304 (diff) | |
| parent | ff7d60ad044e98e1e2e14170b5804d141856a949 (diff) | |
| download | so_long-568827dc74eb4e2a7719d7cfbd48083facf85cc3.tar.gz so_long-568827dc74eb4e2a7719d7cfbd48083facf85cc3.zip | |
Map checker
Diffstat (limited to 'src/check_for_valid_path.c')
| -rw-r--r-- | src/check_for_valid_path.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/check_for_valid_path.c b/src/check_for_valid_path.c new file mode 100644 index 0000000..ef75bc2 --- /dev/null +++ b/src/check_for_valid_path.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_for_valid_path.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/29 15:54:52 by dkaiser #+# #+# */ +/* Updated: 2024/06/10 16:03:06 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "so_long.h" + +static void floodfill(char *tiles, t_ivector size, t_ivector pos); +static int check_tiles(char *tiles, int size); + +int check_for_valid_path(t_tilemap *map) +{ + char *tiles; + + tiles = malloc(map->grid_size.x * map->grid_size.y); + if (!tiles) + { + ft_putstr_fd("Allocation error\n", 1); + return (1); + } + ft_strlcpy(tiles, map->tiles, map->grid_size.x * map->grid_size.y + 1); + floodfill(tiles, map->grid_size, map->player_start_tile); + return (check_tiles(tiles, map->grid_size.x * map->grid_size.y)); +} + +static void floodfill(char *tiles, t_ivector size, t_ivector pos) +{ + if (tiles[pos.y * size.x + pos.x] == WALL) + return ; + if (tiles[pos.y * size.x + pos.x] == 'X') + return ; + tiles[pos.y * size.x + pos.x] = 'X'; + floodfill(tiles, size, (t_ivector){pos.x - 1, pos.y}); + floodfill(tiles, size, (t_ivector){pos.x + 1, pos.y}); + floodfill(tiles, size, (t_ivector){pos.x, pos.y - 1}); + floodfill(tiles, size, (t_ivector){pos.x, pos.y + 1}); +} + +static int check_tiles(char *tiles, int size) +{ + int i; + + i = 0; + while (i < size) + { + if (tiles[i] != WALL && tiles[i] != 'X') + return (1); + i++; + } + return (0); +} |
