-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloodfill.c
More file actions
98 lines (90 loc) · 2.43 KB
/
floodfill.c
File metadata and controls
98 lines (90 loc) · 2.43 KB
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
96
97
98
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* floodfill.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mamir <mamir@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:34:32 by mamir #+# #+# */
/* Updated: 2024/05/21 10:10:10 by mamir ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void ft_copie(t_data *data, char *path, char **copie)
{
int i;
int j;
int fd;
char *str;
fd = open(path, O_RDONLY);
str = get_next_line(fd);
i = 0;
while (i < data->map_dim[0])
{
j = 0;
copie[i] = (char *)malloc(sizeof(char) * (data->map_dim[1] + 1));
if (!copie[i])
ft_error("malloc !\n");
while (j < data->map_dim[1])
{
copie[i][j] = str[j];
j++;
}
free(str);
str = get_next_line(fd);
copie[i][j] = '\0';
i++;
}
}
void ft_free(t_data *data, char **str)
{
int i;
i = 0;
while (i < data->map_dim[0])
{
free(str[i]);
i++;
}
free(str);
}
void ft_duplicate(t_data *data, char *path)
{
int i;
int j;
char **copie;
copie = (char **)malloc(sizeof(char *) * (data->map_dim[0]));
if (!copie)
ft_error("Copied map!\n");
ft_copie(data, path, copie);
ft_player_position(data);
flood_fill(data, copie, data->py, data->px);
i = 0;
while (i < data->map_dim[0])
{
j = 0;
while (j < data->map_dim[1])
{
if (copie[i][j] == 'E' || copie[i][j] == 'C')
ft_error("Flood Fill!\n");
j++;
}
i++;
}
ft_free(data, copie);
}
void flood_fill(t_data *data, char **copie, int x, int y)
{
if (x < 0 || y < 0 || x >= data->map_dim[0] || y >= data->map_dim[1])
ft_error("out of borders\n");
if (copie[x][y] == 'E')
copie[x][y] = '1';
if (copie[x][y] == 'C' || copie[x][y] == 'E' || copie[x][y] == '0'
|| copie[x][y] == 'P')
{
copie[x][y] = '.';
flood_fill(data, copie, x + 1, y);
flood_fill(data, copie, x - 1, y);
flood_fill(data, copie, x, y + 1);
flood_fill(data, copie, x, y - 1);
}
}