summaryrefslogtreecommitdiff
path: root/src/check_for_valid_path.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-06-10 16:39:55 +0200
committerDominik Kaiser2024-06-10 16:39:55 +0200
commitff7d60ad044e98e1e2e14170b5804d141856a949 (patch)
treedd896d064a49bb399b7c942fde49ae0bf892263b /src/check_for_valid_path.c
parent81bfaf91dedc75b758a9057a2b7c23adc5a68e93 (diff)
downloadso_long-ff7d60ad044e98e1e2e14170b5804d141856a949.tar.gz
so_long-ff7d60ad044e98e1e2e14170b5804d141856a949.zip
Cleanup
Diffstat (limited to 'src/check_for_valid_path.c')
-rw-r--r--src/check_for_valid_path.c63
1 files changed, 34 insertions, 29 deletions
diff --git a/src/check_for_valid_path.c b/src/check_for_valid_path.c
index b93dc74..ef75bc2 100644
--- a/src/check_for_valid_path.c
+++ b/src/check_for_valid_path.c
@@ -6,49 +6,54 @@
/* 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 */
+/* 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);
+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)
+int check_for_valid_path(t_tilemap *map)
{
- char *tiles;
+ 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));
+ 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)
+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});
+ 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)
+static int check_tiles(char *tiles, int size)
{
- int i;
+ int i;
- i = 0;
- while (i < size)
- {
- if (tiles[i] != WALL && tiles[i] != 'X')
- return (1);
- i++;
- }
- return (0);
+ i = 0;
+ while (i < size)
+ {
+ if (tiles[i] != WALL && tiles[i] != 'X')
+ return (1);
+ i++;
+ }
+ return (0);
}