From fc074e311232e90d997aa4062c63225380190bd3 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 29 May 2024 16:34:17 +0200 Subject: Add map checking --- src/check_for_valid_path.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/check_for_valid_path.c (limited to 'src/check_for_valid_path.c') diff --git a/src/check_for_valid_path.c b/src/check_for_valid_path.c new file mode 100644 index 0000000..b93dc74 --- /dev/null +++ b/src/check_for_valid_path.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_for_valid_path.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser grid_size.x * map->grid_size.y); + if (!tiles) + return (1); // TODO: Error + 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 || 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); +} -- cgit v1.2.3