-
Notifications
You must be signed in to change notification settings - Fork 0
/
so_long.c
116 lines (108 loc) · 2.73 KB
/
so_long.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
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* so_long.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mjong <mjong@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 16:20:51 by mjong #+# #+# */
/* Updated: 2024/05/22 18:10:47 by mjong ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int ft_rect_check(t_game *game, char *argv[])
{
int temp;
int fd;
char *line;
temp = 0;
fd = open(argv[1], O_RDONLY);
line = get_next_line(fd);
temp = ft_linelen(line);
while (line != NULL)
{
game->width = ft_linelen(line);
if (game->width != temp)
{
ft_free2(&line);
return (1);
}
game->num += ft_strlen(line);
ft_free2(&line);
line = get_next_line(fd);
game->height++;
}
ft_free2(&line);
close(fd);
return (0);
}
char *ft_filetomap(t_game *game, char *argv[])
{
int fd;
char *map;
char *temp;
fd = open(argv[1], O_RDONLY);
map = NULL;
if (fd < 0 || ft_rect_check(game, argv) == 1)
{
ft_printf("Error\nINVALID MAP\n");
exit(EXIT_FAILURE);
}
while (1)
{
temp = get_next_line(fd);
if (!temp)
break ;
game->ylength++;
map = ft_strjoin2(map, temp);
free(temp);
}
close(fd);
return (map);
}
void init(t_game *game)
{
game->width = 0;
game->height = 0;
game->colnum = 0;
game->exinum = 0;
game->planum = 0;
game->flonum = 0;
game->ylength = 0;
game->movecount = 0;
game->countc = 0;
game->num = 0;
game->mlx = NULL;
game->collectible = NULL;
game->eexit = NULL;
game->ffloor = NULL;
game->player = NULL;
game->wall = NULL;
game->two_d_map = NULL;
game->two_d_mapcheck = NULL;
}
int32_t main(int argc, char *argv[])
{
t_game game;
char *map;
if (!argv[1] || argc > 2 || ft_bercheck(argv[1]))
{
ft_printf("Error, wrong input\n");
ft_printf("Correct input: ./so_long maps/map.ber");
exit(1);
}
init(&game);
map = ft_filetomap(&game, argv);
game.two_d_map = ft_split(map, '\n');
game.two_d_mapcheck = ft_split(map, '\n');
free(map);
flood_fill(&game, 1, 1);
elementcheck1(&game);
if (ft_mapcheck(&game) == 1)
ft_exitgame(&game, "fail");
display_map(&game);
mlx_key_hook(game.mlx, (void *)&ft_hooks, &game);
mlx_loop(game.mlx);
mlx_terminate(game.mlx);
return (0);
}