-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_path.c
79 lines (74 loc) · 2.17 KB
/
check_path.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ddyankov <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/27 11:01:28 by ddyankov #+# #+# */
/* Updated: 2023/03/08 12:07:40 by ddyankov ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void find_path(t_game *game, int y, int x, int *collect)
{
if (game->map[y][x] == '1' || game->map[y][x] == 'c'
|| game->map[y][x] == 'o' || game->map[y][x] == 'e'
|| game->map[y][x] == 'E' || game->map[y][x] == 'X')
{
if (game->map[y][x] == 'E')
game->exit_flag = 1;
return ;
}
if (game->map[y][x] == 'C')
{
(*collect)--;
game->map[y][x] = 'c';
}
else if (game->map[y][x] == '0')
game->map[y][x] = 'o';
find_path(game, (y + 1), x, collect);
find_path(game, (y - 1), x, collect);
find_path(game, y, (x + 1), collect);
find_path(game, y, (x - 1), collect);
}
void restore_map(t_game *game)
{
int x;
int y;
y = 0;
while (y < game->height)
{
x = 0;
while (x < game->width)
{
if (game->map[y][x] == 'c')
game->map[y][x] = 'C';
else if (game->map[y][x] == 'e')
game->map[y][x] = 'E';
else if (game->map[y][x] == 'o')
game->map[y][x] = '0';
x++;
}
y++;
}
}
void valid_path(t_game *game)
{
int collect;
collect = game->collect_count;
find_path(game, game->player_y, game->player_x, &collect);
if (collect != 0)
{
ft_printf("Error\nNo valid path to a ball\n");
free_game_map(game);
exit(1);
}
restore_map(game);
if (game->exit_flag == 0)
{
ft_printf("Error\nNo valid path to Exit\n");
free_game_map(game);
exit(1);
}
}