summaryrefslogtreecommitdiff
path: root/src/check_for_valid_path.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-06-10 16:40:28 +0200
committerGitHub2024-06-10 16:40:28 +0200
commit568827dc74eb4e2a7719d7cfbd48083facf85cc3 (patch)
treedd896d064a49bb399b7c942fde49ae0bf892263b /src/check_for_valid_path.c
parent7b230503231e154c5400413b4f9b87dd6fcd8304 (diff)
parentff7d60ad044e98e1e2e14170b5804d141856a949 (diff)
downloadso_long-master.tar.gz
so_long-master.zip
Merge map-checker into masterHEADmaster
Map checker
Diffstat (limited to 'src/check_for_valid_path.c')
-rw-r--r--src/check_for_valid_path.c59
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);
+}