-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_check_2.c
89 lines (81 loc) · 2.32 KB
/
error_check_2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_error_check_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alimotta <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 10:34:08 by alimotta #+# #+# */
/* Updated: 2024/01/12 10:34:12 by alimotta ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
static void ft_find_position(t_vars *v, int *x, int *y, char componets)
{
int i;
int j;
i = 0;
while (i < v -> height)
{
j = 0;
while (v -> arr[i][j] != '\0')
{
if (v -> arr[i][j] == componets)
{
*x = i;
*y = j;
}
j++;
}
i++;
}
}
static int ft_is_safe(char **arr, int i, int j)
{
int lines;
int colums;
lines = 0;
colums = ft_strlen(arr[lines]);
while (arr[lines])
lines++;
if (i > 0 && i < lines && j > 0 && j < colums)
return (0);
return (-1);
}
static int ft_check_path(t_vars *v, char **visited, int i, int j)
{
if (ft_is_safe(v->arr, i, j) == 0
&& v->arr[i][j] != '1' && visited[i][j] != 'x')
{
if (visited[i][j] == 'C')
v ->collectable--;
if (visited[i][j] == 'E')
v -> exit -= 1;
visited[i][j] = 'x';
if (ft_check_path(v, visited, i - 1, j) == 0)
return (0);
if (ft_check_path(v, visited, i, j - 1) == 0)
return (0);
if (ft_check_path(v, visited, i + 1, j) == 0)
return (0);
if (ft_check_path(v, visited, i, j + 1) == 0)
return (0);
}
return (-1);
}
int ft_check_valid_map(t_vars *v, char **visited)
{
int coll;
ft_find_position(v, &v -> x_player, &v -> y_player, 'P');
ft_find_position(v, &v -> x_exit, &v -> y_exit, 'E');
coll = v -> collectable;
while (v ->collectable > 0)
{
ft_check_path(v, visited, v -> x_player, v -> y_player);
if (v->collectable > 0 || v -> exit != 0)
return (-1);
}
v->collectable = coll;
v->exit = 1;
return (0);
}