]> git.dkaiser.de - 42/so_long.git/commitdiff
Add map checking
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Wed, 29 May 2024 14:34:17 +0000 (16:34 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Wed, 29 May 2024 14:34:17 +0000 (16:34 +0200)
Makefile
include/so_long.h
src/check_for_valid_path.c [new file with mode: 0644]
src/check_map.c [new file with mode: 0644]
src/main.c

index 51de30048731c70d0235122ecd4b14a8f9594bb8..6bfaf36ac1492d0ac45076f3e756b46a5c1fceda 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ LIBS    =       -Llibft -lft -lm -LMLX42/build -lmlx42 -ldl -lglfw -pthread
 
 VPATH  :=      src
 SRC            =       main.c init.c loop.c input.c draw.c tilemap.c player_process.c \
-                       collision.c map_utils.c
+                       collision.c map_utils.c check_map.c check_for_valid_path.c
 
 OBJ_DIR        :=      obj
 OBJ            :=      $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
index 6f3800dbcc37fb6559823175aa0eb42995684785..109035838a635a1bd33cc8d7cc507aa72a527425 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/08 14:14:02 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/20 20:07:16 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/29 15:57:34 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -69,10 +69,10 @@ typedef struct s_tilemap
 {
        t_ivector       grid_size;
        t_ivector       tile_size;
-       char            *tiles;
+       char  *tiles;
        t_ivector       player_start_tile;
        t_ivector       exit_tile;
-       mlx_image_t *collectible_img;
+       mlx_image_t     *collectible_img;
 }                              t_tilemap;
 
 typedef struct s_game
@@ -85,6 +85,8 @@ typedef struct s_game
 }                              t_game;
 
 int                            load_map_from_file(t_tilemap *tilemap, char *filename);
+int                            check_map(t_tilemap *map);
+int check_for_valid_path(t_tilemap *map);
 
 int                            init(t_game *game);
 void                   loop(void *params);
diff --git a/src/check_for_valid_path.c b/src/check_for_valid_path.c
new file mode 100644 (file)
index 0000000..b93dc74
--- /dev/null
@@ -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);
+}
diff --git a/src/check_map.c b/src/check_map.c
new file mode 100644 (file)
index 0000000..af7a97d
--- /dev/null
@@ -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);
+}
index fca5cf00cf95ffd20b39bd754455d8ce0ad28984..75b58d24c6552a095c961bfdd37822824a6cf592 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/08 14:14:13 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/15 17:31:46 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/29 14:58:22 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -20,6 +20,8 @@ int   main(int argc, char *argv[])
                return (1);
        if (load_map_from_file(&game.map, argv[1]))
                return (1);
+       if (check_map(&game.map))
+               return (1);
        init(&game);
        draw_walls(&game);
        draw_exit(&game);