-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflood_fill_bonus.c
95 lines (87 loc) · 2.25 KB
/
flood_fill_bonus.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* flood_fill_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: het-taja <het-taja@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 13:39:57 by het-taja #+# #+# */
/* Updated: 2024/07/15 21:22:46 by het-taja ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long_bonus.h"
void error_fill(t_game *game, char **map)
{
clear_map(map);
clear_map(game->map);
map = NULL;
game->map = NULL;
}
int flood_fill(t_game *game)
{
char **tmp;
tmp = NULL;
tmp = map_dup(game->map);
check_path(game->playerx, game->playery, tmp, 'C');
if (check_fill(tmp, 'C') == -1)
{
error_fill(game, tmp);
return (-1);
}
check_path_exit(game->playerx, game->playery, tmp, 'E');
if (check_fill(tmp, 'E') == -1)
{
error_fill(game, tmp);
return (-1);
}
clear_map(tmp);
tmp = NULL;
return (0);
}
void check_path(int x, int y, char **map, char c)
{
if (map && map[y] && map[y][x])
{
if (map[y][x] == '0' || map[y][x] == c || map[y][x] == 'P')
{
map[y][x] = 'V';
check_path(x + 1, y, map, c);
check_path(x - 1, y, map, c);
check_path(x, y + 1, map, c);
check_path(x, y - 1, map, c);
}
}
}
void check_path_exit(int x, int y, char **map, char c)
{
if (map && map[y] && map[y][x])
{
if (map[y][x] == '0' || map[y][x] == c || map[y][x] == 'V')
{
map[y][x] = '1';
check_path_exit(x + 1, y, map, c);
check_path_exit(x - 1, y, map, c);
check_path_exit(x, y + 1, map, c);
check_path_exit(x, y - 1, map, c);
}
}
}
int check_fill(char **map, char c)
{
int x;
int y;
x = 0;
y = 0;
while (map && map[y])
{
while (map[y][x])
{
if (map[y][x] == c)
return (-1);
x++;
}
x = 0;
y++;
}
return (1);
}