summaryrefslogtreecommitdiff
path: root/src/check_map.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_map.c
parent7b230503231e154c5400413b4f9b87dd6fcd8304 (diff)
parentff7d60ad044e98e1e2e14170b5804d141856a949 (diff)
downloadso_long-568827dc74eb4e2a7719d7cfbd48083facf85cc3.tar.gz
so_long-568827dc74eb4e2a7719d7cfbd48083facf85cc3.zip
Merge map-checker into masterHEADmaster
Map checker
Diffstat (limited to 'src/check_map.c')
-rw-r--r--src/check_map.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/check_map.c b/src/check_map.c
new file mode 100644
index 0000000..af7a97d
--- /dev/null
+++ b/src/check_map.c
@@ -0,0 +1,88 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* check_map.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/29 13:23:01 by dkaiser #+# #+# */
+/* Updated: 2024/05/29 15:58:10 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "so_long.h"
+
+static int check_borders(t_tilemap *map);
+static int check_exactly_one(t_tilemap *map, char tile);
+static int check_collectibles(t_tilemap *map);
+
+int check_map(t_tilemap *map)
+{
+ if (check_borders(map))
+ return (1);
+ if (check_exactly_one(map, PLAYER_START))
+ return (1);
+ if (check_exactly_one(map, EXIT))
+ return (1);
+ if (check_collectibles(map))
+ return (1);
+ if (check_for_valid_path(map))
+ return (1);
+ return (0);
+}
+
+static int check_borders(t_tilemap *map)
+{
+ int i;
+
+ i = 0;
+ while (i < map->grid_size.x)
+ {
+ if (get_tile(map, i, 0) != WALL || get_tile(map, i, map->grid_size.y
+ - 1) != WALL)
+ return (1);
+ i++;
+ }
+ i = 0;
+ while (i < map->grid_size.y)
+ {
+ if (get_tile(map, 0, i) != WALL || get_tile(map, map->grid_size.x - 1,
+ i) != WALL)
+ return (1);
+ i++;
+ }
+ return (0);
+}
+
+static int check_exactly_one(t_tilemap *map, char tile)
+{
+ int i;
+ int found;
+
+ found = 0;
+ i = map->grid_size.x * map->grid_size.y - 1;
+ while (i >= 0)
+ {
+ if (map->tiles[i] == tile)
+ found++;
+ i--;
+ }
+ if (found != 1)
+ return (1);
+ return (0);
+}
+
+static int check_collectibles(t_tilemap *map)
+{
+ int i;
+
+ i = map->grid_size.x * map->grid_size.y - 1;
+ while (i >= 0)
+ {
+ if (map->tiles[i] == COLLECTIBLE)
+ return (0);
+ i--;
+ }
+ ft_printf("\nFound no collectibles\n");
+ return (1);
+}