Repository navigation
Expand file tree
/
Copy pathpath_finder.c
More file actions
109 lines (101 loc) · 2.68 KB
/
Copy pathpath_finder.c
File metadata and controls
109 lines (101 loc) · 2.68 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
99
100
101
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path_finder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebakirog <ebakirog@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 16:58:16 by ebakirog #+# #+# */
/* Updated: 2023/12/09 17:25:07 by ebakirog ### ########.fr */
/* */
/* ************************************************************************** */
#include "libraries/ft_printf/ft_printf.h"
#include "so_long.h"
#include <stdlib.h>
void map_cpy(t_path *path, t_game *data)
{
int i;
int j;
i = -1;
while (data->map[++i])
{
j = -1;
while (data->map[i][++j])
{
path->map[i][j] = data->map[i][j];
}
}
}
void recursive_path_move(t_path *path, int x, int y)
{
path->map[y][x] = 'X';
if (x > 0 && path->map[y][x - 1] != '1' && path->map[y][x - 1] != 'X')
{
recursive_path_move(path, x - 1, y);
}
if (y < path->height - 1 && path->map[y + 1][x] != '1' && path->map[y
+ 1][x] != 'X')
{
recursive_path_move(path, x, y + 1);
}
if (y > 0 && path->map[y - 1][x] != '1' && path->map[y - 1][x] != 'X')
{
recursive_path_move(path, x, y - 1);
}
if (x < path->width - 1 && path->map[y][x + 1] != '1' && path->map[y][x
+ 1] != 'X')
{
recursive_path_move(path, x + 1, y);
}
}
void path_check(t_path *path, t_game *data)
{
int i;
int j;
int ce_count;
ce_count = path->collectibles + 1;
i = -1;
while (path->map[++i])
{
j = -1;
while (path->map[i][++j])
{
if ((data->map[i][j] == 'C' || data->map[i][j] == 'E')
&& path->map[i][j] == 'X')
ce_count--;
}
}
if (ce_count == 0)
{
return ;
}
else
{
ft_printf("Error\nMap is not correct.\n");
exit(1);
}
}
void ft_path_f(t_game data)
{
t_path path;
int i;
i = -1;
path.height = get_map_height(data.map);
path.width = get_map_width(data.map);
path.map = malloc(sizeof(char *) * (path.height + 1));
if (!path.map)
malloc_error();
path.map[path.height] = NULL;
while (++i < path.height)
{
path.map[i] = malloc(sizeof(char) * (path.width + 1));
if (!path.map[i])
malloc_error();
path.map[i][path.width] = '\0';
}
map_cpy(&path, &data);
path_f_player(&path);
path_f_collectiable(&path);
recursive_path_move(&path, path.x_index, path.y_index);
path_check(&path, &data);
}