-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
88 lines (81 loc) · 2.27 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mkiflema <mkiflema@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/13 10:44:55 by mkiflema #+# #+# */
/* Updated: 2023/04/19 12:08:15 by mkiflema ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void allocate_space(t_data *data, char ***container)
{
int i;
(*container) = malloc(sizeof(char *) * (data->height + 1));
if (!(*container))
{
free_array((*container));
return ;
}
i = -1;
while (++i < data->height)
{
(*container)[i] = malloc(sizeof(char) * (data->width + 1));
if (!(*container)[i])
{
free((*container)[i]);
return ;
}
}
}
void fill_container(t_data **data, char *storage, int i)
{
int j;
int width;
width = -1;
if (!(*data)->storage || !(*data)->storage[0])
return ;
while (++i < (*data)->height)
{
j = 0;
while (storage[++width] && storage[width] != '\n')
{
if (storage[width] == 'P')
{
(*data)->start[0] = i;
(*data)->start[1] = j;
(*data)->player_y = i;
(*data)->player_x = j;
}
else if (storage[width] == 'C')
(*data)->collectables += 1;
(*data)->storage[i][j++] = storage[width];
}
(*data)->storage[i][j] = '\0';
}
(*data)->storage[i] = 0;
}
void store_map(char *storage, t_data *data)
{
int width;
int height;
int i;
i = -1;
height = 1;
while (storage[++i])
if (storage[i] == '\n' && storage[i + 1] != '\0')
height++;
i = -1;
width = 0;
while (storage[++i] && storage[i] != '\n')
width++;
data->height = height;
data->width = width;
data->collectables = 0;
data->storage = NULL;
allocate_space(data, &data->storage);
fill_container(&data, storage, -1);
data->numofmoves = 0;
}