-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_map.c
121 lines (109 loc) · 3.25 KB
/
parse_map.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aassaf <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 18:55:57 by aassaf #+# #+# */
/* Updated: 2024/01/23 10:13:14 by aassaf ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
char **duplicate_map(t_map *map)
{
char **tmp;
int i;
int j;
tmp = malloc(sizeof(char *) * map->row);
if(!tmp)
return NULL;
i = 0;
while(i < map->row)
{
tmp[i] = malloc(sizeof(char *) * map->col);
i++;
}
i = 0;
while(i < map->row)
{
j = 0;
while(j < map->col)
{
tmp[i][j] = map->arr_map[i][j];
j++;
}
i++;
}
return(tmp);
}
void free_map_and_road(char **tmp, t_map *map, int x, int y)
{
int i;
if(tmp[x][y] == 'E' || tmp[x][y] == 'C')
{
free_tmp(tmp, map);
i = 0;
if(i < map->row)
{
free(map->arr_map[i]);
i++;
}
free(map->arr_map);
hdl_error(map, 3);
}
}
void check_start(char **tmp, t_map *map)
{
int x;
int y;
x = 0;
// print_map(tmp, map->row, map->col);
ft_flood_fill(tmp, map);
// print_map(tmp, map->row, map->col);
while(x < map->row)
{
y = 0;
while(y < map->col)
{
free_map_and_road(tmp, map, x, y);
y++;
}
x++;
}
free_tmp(tmp, map);
}
void flood_fill(char **tmp, int x, int y)
{
if(tmp[x][y] == 'E' || tmp[x][y] == 'F')
{
if(tmp[x][y] == 'E')
tmp[x][y] = 'F';
return ;
}
else if(tmp[x][y] != '1')
{
tmp[x][y] = 'F';
flood_fill(tmp, x + 1, y);
flood_fill(tmp, x - 1, y);
flood_fill(tmp, x, y + 1);
flood_fill(tmp, x, y - 1);
}
}
void ft_flood_fill(char **tmp, t_map *map)
{
int x;
int y;
x = 0;
while(x < map->row)
{
y = 0;
while(y < map->col)
{
if(tmp[x][y] == 'P')
flood_fill(tmp, x, y);
y++;
}
x++;
}
}