-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
101 lines (93 loc) · 2.86 KB
/
map.c
File metadata and controls
101 lines (93 loc) · 2.86 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sryou <sryou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/16 15:47:24 by sryou #+# #+# */
/* Updated: 2022/08/23 15:59:03 by sryou ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void read_map_loop(int fd, t_game *game)
{
char *ln;
char *temp;
int len;
while (1)
{
game->height++;
ln = get_next_line(fd);
if (ln)
{
len = ft_strlen(ln);
if (ln[len - 1] == '\n')
ln[--len] = '\0';
if (len != game->width)
throw_error("MAP ERROR | Map is not Rectangular.", game);
temp = ft_strjoin(game->map, ln);
free(game->map);
free(ln);
game->map = temp;
}
else
return ;
}
}
void read_map(char *map, t_game *game)
{
int fd;
int len;
char *ln;
fd = open(map, O_RDONLY);
ln = get_next_line(fd);
if (ln == NULL)
throw_error("FILE ERROR | Invalid File.", game);
len = ft_strlen(ln);
if (ln[len - 1] == '\n')
ln[--len] = '\0';
game->width = len;
game->map = ft_strdup(ln);
free(ln);
read_map_loop(fd, game);
}
void check_valid_map(int num_user, int num_exit, \
int invalid_wall, t_game *game)
{
if (invalid_wall)
throw_error("MAP ERROR | Map is not surrounded by Walls.", game);
if (num_user < 1)
throw_error("MAP ERROR | Map does not have User.", game);
if (num_user > 1)
throw_error("MAP ERROR | Map has too many User.", game);
if (num_exit < 1)
throw_error("MAP ERROR | Map does not have Exit.", game);
if (game->num_item < 1)
throw_error("MAP ERROR | Map does not have Item.", game);
}
void interpret_map(t_game *game)
{
int num_user;
int num_exit;
int idx;
num_user = 0;
num_exit = 0;
idx = -1;
while (++idx < game->width * game->height)
{
if (idx < game->width || idx >= game->width * (game->height - 1) || \
idx % game->width == 0 || idx % game->width == game->width - 1)
if (game->map[idx] != '1')
check_valid_map(num_user, num_exit, 1, game);
if (game->map[idx] == 'P' && (num_user++ || 1))
game->cord_user = idx;
else if (game->map[idx] == 'C')
game->num_item++;
else if (game->map[idx] == 'E')
num_exit++;
else if (game->map[idx] != '1' && game->map[idx] != '0')
throw_error("MAP ERROR | Map has Wrong Character.", game);
}
check_valid_map(num_user, num_exit, 0, game);
}