-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps_utils.c
114 lines (105 loc) · 2.64 KB
/
maps_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* maps_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/02 18:09:47 by jbadaire #+# #+# */
/* Updated: 2023/10/14 16:01:26 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include <fcntl.h>
int map_size(int fd)
{
char *tmp;
int index;
index = 0;
tmp = get_next_line(fd);
while (tmp != NULL)
{
free(tmp);
tmp = get_next_line(fd);
index++;
}
close(fd);
return (index);
}
t_world *load_map(int fd, char *path, t_world *world)
{
int y;
int y_index;
char *s;
y = 0;
y_index = map_size(fd);
world->map = malloc((y_index + 1) * sizeof(char *));
if (!world->map)
return (world);
world->map[y_index] = NULL;
fd = open(path, O_RDONLY);
while (y <= y_index)
{
s = get_next_line(fd);
if (s != NULL)
{
world->map[y] = ft_strtrim(s, "\n");
free(s);
}
y++;
}
return (world->length_y = y_index, world);
}
int valid_elements(t_world world)
{
int players;
int exits;
int collectibles;
players = count_element(world, 'P');
if (players > 1)
return (-1);
if (players < 1)
return (-2);
exits = count_element(world, 'E');
if (exits > 1)
return (-3);
if (exits < 1)
return (-4);
collectibles = count_element(world, 'C');
if (collectibles < 1)
return (-5);
return (1);
}
void is_solvable(char **map, int x, int y, int length_y)
{
char character;
if (map[y] == NULL || y > length_y)
return ;
character = map[y][x];
if (character == 'O')
return ;
if (character == '0' || character == 'C' || \
character == 'P' || character == 'E')
{
if (character == 'E')
{
map[y][x] = 'L';
return ;
}
else
map[y][x] = 'O';
is_solvable(map, x - 1, y, length_y);
is_solvable(map, x + 1, y, length_y);
is_solvable(map, x, y + 1, length_y);
is_solvable(map, x, y - 1, length_y);
}
return ;
}
t_boolean is_inside_world(int y, int x, t_world world)
{
if (y < 0 || y > world.length_y)
return (_false);
if (x < 0 || x > (int) ft_strlen(world.map[0]))
return (_false);
return (_true);
}