-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_check.c
80 lines (72 loc) · 2.2 KB
/
path_check.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhlim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/11 17:23:18 by zhlim #+# #+# */
/* Updated: 2023/07/24 15:01:44 by zhlim ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
char **temp_grid(t_map *map)
{
char **new_grid;
int i;
i = 0;
new_grid = (char **)malloc(map->rows * sizeof(char *));
if (!new_grid)
return (NULL);
while (i < map->rows)
{
new_grid[i] = ft_strdup(map->grid[i]);
i++;
}
return (new_grid);
}
void flood_fill(t_map *temp_map, int col, int row)
{
char *current_coord;
current_coord = &temp_map->grid[row][col];
if (row >= temp_map->rows || row < 0 || col >= temp_map->columns || col < 0
|| *current_coord == WALL)
return ;
if (*current_coord == EXIT)
{
temp_map->exited = 1;
return ;
}
if (*current_coord == COLLECTIBLE)
{
temp_map->collected++;
*current_coord = EMPTY;
}
*current_coord = WALL;
flood_fill(temp_map, col, row - 1);
flood_fill(temp_map, col, row + 1);
flood_fill(temp_map, col - 1, row);
flood_fill(temp_map, col + 1, row);
}
void valid_check(t_map *map, t_map *temp_map)
{
if (temp_map->collected != temp_map->collectible_count)
{
free_map(temp_map);
free_error_exit(map, "Can't pick up all collectibles");
}
if (temp_map->exited != 1)
{
free_map(temp_map);
free_error_exit(map, "Exit is unreachable");
}
}
void path_check(t_map *map)
{
t_map temp_map;
temp_map = *map;
temp_map.grid = temp_grid(map);
flood_fill(&temp_map, map->player.x, map->player.y);
valid_check(map, &temp_map);
free_map(&temp_map);
}