-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_map.c
79 lines (73 loc) · 2.06 KB
/
valid_map.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valid_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbiodies <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/21 18:21:23 by rbiodies #+# #+# */
/* Updated: 2021/11/24 11:26:36 by rbiodies ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void check_char(t_check *check, char ch)
{
if (ch != '1' && ch != 'C' && ch != 'E' && ch != 'P' && ch != 'A')
{
write(1, "Error\nUndefined character\n", 26);
exit(EXIT_FAILURE);
}
else if (ch == 'P')
{
if (check->player == 1)
{
write(1, "Error\nToo many players\n", 23);
exit(EXIT_FAILURE);
}
else
check->player = 1;
}
else if (ch == 'C')
check->collect = 1;
else if (ch == 'E')
check->exit = 1;
}
static void player_place(t_map *map, int i, int j, char ch)
{
if (ch == 'P')
{
map->x = i;
map->y = j;
}
}
void wall_error(void)
{
write(1, "Error\nNo wall\n", 14);
exit(EXIT_FAILURE);
}
void valid_map(t_map *map)
{
int i;
int j;
t_check check;
check = (t_check){0, 0, 0};
i = -1;
while (++i < map->height)
{
j = -1;
while (++j < map->width)
{
if (i == 0 || j == 0 || i == map->height - 1 || j == map->width - 1)
if (map->map[i][j] != '1')
wall_error();
if (map->map[i][j] != '0')
check_char(&check, map->map[i][j]);
player_place(map, i, j, map->map[i][j]);
}
}
if (check.collect == 0 || check.exit == 0 || check.player == 0)
{
write(1, "Error\nNo exit, collectible or player\n", 37);
exit(EXIT_FAILURE);
}
}