summaryrefslogtreecommitdiff
path: root/src/check_for_valid_path.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-05-29 16:34:17 +0200
committerDominik Kaiser2024-05-29 16:34:17 +0200
commitfc074e311232e90d997aa4062c63225380190bd3 (patch)
tree0dab1b550b76d0f27ff969c76c4da09c8a8b43e8 /src/check_for_valid_path.c
parent9e62e1d25ed48263ad54ea236df1907a3a96790d (diff)
downloadso_long-fc074e311232e90d997aa4062c63225380190bd3.tar.gz
so_long-fc074e311232e90d997aa4062c63225380190bd3.zip
Add map checking
Diffstat (limited to 'src/check_for_valid_path.c')
-rw-r--r--src/check_for_valid_path.c54
1 files changed, 54 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..b93dc74
--- /dev/null
+++ b/src/check_for_valid_path.c
@@ -0,0 +1,54 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* check_for_valid_path.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/29 15:54:52 by dkaiser #+# #+# */
+/* Updated: 2024/05/29 16:32:26 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)
+ 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);
+}