-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.c
91 lines (82 loc) · 2.22 KB
/
window.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* window.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rmitache <rmitache@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/13 16:41:37 by rmitache #+# #+# */
/* Updated: 2023/08/06 17:57:27 by rmitache ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int destroy_win(t_game *game)
{
ft_free_all_allocated_memory(game);
exit(EXIT_FAILURE);
}
void window_size_calc(t_game *data, char **argv)
{
int fd;
fd = open(argv[1], O_RDONLY);
if (fd < 0)
{
close(fd);
alert("Failed to read the map path. Map even exist?\n");
}
data->size_x = count_width(fd) * SIZE;
data->size_y = count_height(fd) * SIZE;
close (fd);
}
int count_width(int fd)
{
char buffer[0];
int i;
int bytes;
buffer[0] = '\0';
bytes = 1;
i = 0;
while (bytes == 1)
{
bytes = read(fd, buffer, 1);
if (buffer[0] != '\n')
i++;
else
break ;
}
return (i);
}
int count_height(int fd)
{
int linecount;
char *bytes;
linecount = 1;
bytes = NULL;
while (1)
{
bytes = get_next_line(fd);
if (bytes)
linecount++;
else
break ;
free(bytes);
}
return (linecount);
}
void look_for(t_game *game, int y_axis, int local_x)
{
if ((game->w_map[y_axis][local_x] == 'v')
|| (game->w_map[y_axis][local_x] == '1'))
return ;
if (game->w_map[y_axis][local_x] == 'C')
game->c_found++;
if (game->w_map[y_axis][local_x] == 'E')
game->e_found++;
if (game->e_found > 0 && (game->c_found == game->c_c))
return ;
game->w_map[y_axis][local_x] = 'v';
look_for(game, y_axis, local_x + 1);
look_for(game, y_axis - 1, local_x);
look_for(game, y_axis, local_x - 1);
look_for(game, y_axis + 1, local_x);
}