-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_checker.c
106 lines (97 loc) · 2.51 KB
/
map_checker.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: majjig <majjig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/07 03:09:48 by majjig #+# #+# */
/* Updated: 2021/12/12 17:25:47 by majjig ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static void items_checker(char **map)
{
static int player_count = 0;
static int exit_count = 0;
static int coll_count = 0;
t_pos pos;
pos.y = -1;
while (map[++pos.y])
{
pos.x = -1;
while (map[pos.y][++pos.x])
{
if (map[pos.y][pos.x] == PLAYER)
if (++player_count != 1)
ft_exit(NO_PLAYER);
if (map[pos.y][pos.x] == EXIT)
exit_count++;
else if (map[pos.y][pos.x] == COLL)
coll_count++;
else if (map[pos.y][pos.x] != EMPTY && map[pos.y][pos.x] != WALL)
if (map[pos.y][pos.x] != PLAYER)
ft_exit(UNKNOWN_ELEMENT);
}
}
if (coll_count == 0 || exit_count == 0)
ft_exit(NO_COLL_EXIT);
}
static void is_closed(char *line, int len)
{
if (line[0] == WALL && line[len - 1] == WALL)
return ;
ft_exit(NOT_CLOSED);
}
static void is_all_wall(char *line)
{
while (*line)
{
if (*line != WALL)
ft_exit(NOT_CLOSED);
line++;
}
}
void check(char **map)
{
int len;
int y;
y = -1;
len = ft_strlen(map[0]);
while (map[++y])
if (len != ft_strlen(map[y]))
ft_exit(NO_RECTANGLE);
items_checker(map);
y = 0;
is_all_wall(map[y]);
while (map[++y])
is_closed(map[y], len);
is_all_wall(map[--y]);
}
int map_checker(char *file, char ***map)
{
int fd;
char *big;
int nb;
char *tmp;
fd = open(file, O_RDONLY);
big = (char *) malloc(BUFFER_LEN + 1);
nb = read(fd, big, BUFFER_LEN);
if (nb == -1)
ft_exit(NO_FILE);
if (nb == BUFFER_LEN)
return (free(big), 0);
if (nb == 0 || nb == 1)
ft_exit(EMPTY_MAP);
if (nb < BUFFER_LEN)
{
big[nb] = 0;
tmp = ft_strdup(big);
free(big);
big = tmp;
}
check_spaces(big);
*map = ft_split(big, '\n');
check(*map);
return (free(big), 1);
}