-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsl_finder.c
95 lines (89 loc) · 2.58 KB
/
sl_finder.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sl_finder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: julmuntz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/14 23:09:21 by julmuntz #+# #+# */
/* Updated: 2022/09/18 20:22:39 by julmuntz ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void sl_find(t_data *data, int line, int i)
{
if (data->map_copy[line][i] == 'E')
data->e_count--;
if (data->map_copy[line][i] == 'C')
data->c_count_t--;
if (data->map_copy[line][i] != '1')
data->map_copy[line][i] = 'P';
}
void sl_scan_2(t_data *data, int line, int i, int i_start)
{
if (i_start == EAST)
i = 0;
if (i_start == WEST)
i = data->nb_char - 1;
while (i >= 0 && data->map_copy[line][i])
{
data->loop = 0;
if (data->map_copy[line][i] == 'P')
{
sl_find(data, line + 1, i);
sl_find(data, line - 1, i);
sl_find(data, line, i + 1);
sl_find(data, line, i - 1);
data->loop = 1;
}
if (i_start == EAST)
i++;
if (i_start == WEST)
i--;
}
}
void sl_scan(t_data *data, int line_start, int i_start)
{
int line;
int i;
data->loop = 1;
while (data->loop)
{
if (line_start == SOUTH)
line = 0;
if (line_start == NORTH)
line = data->nb_line - 1;
while (line >= 0 && data->map_copy[line])
{
i = 0;
sl_scan_2(data, line, i, i_start);
if (line_start == SOUTH)
line++;
if (line_start == NORTH)
line--;
}
}
}
int sl_invalid_path(t_data *data)
{
sl_scan(data, SOUTH, WEST);
sl_scan(data, NORTH, WEST);
sl_scan(data, SOUTH, EAST);
sl_scan(data, NORTH, EAST);
sl_scan(data, SOUTH, WEST);
sl_scan(data, SOUTH, EAST);
sl_scan(data, NORTH, WEST);
sl_scan(data, NORTH, EAST);
sl_scan(data, NORTH, WEST);
sl_scan(data, SOUTH, WEST);
sl_scan(data, NORTH, EAST);
sl_scan(data, SOUTH, EAST);
sl_scan(data, SOUTH, EAST);
sl_scan(data, SOUTH, WEST);
sl_scan(data, NORTH, EAST);
sl_scan(data, NORTH, WEST);
if (data->e_count != 0 || data->c_count_t != 0)
return (ft_printf("Error\nInvalid path.\n"), TRUE);
else
return (FALSE);
}