-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_validate_map.c
100 lines (91 loc) · 2.35 KB
/
ft_validate_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_validate_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrhyhorn <mrhyhorn@student.21-school.ru +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/28 23:29:16 by mrhyhorn #+# #+# */
/* Updated: 2022/04/05 15:51:24 by mrhyhorn ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static int ft_check_chars(t_data *data)
{
int h;
int w;
h = 0;
while (h < data->game->height)
{
w = 0;
while (data->game->map[h][w] != '\n' &&
data->game->map[h][w] != '\0')
{
if (ft_strchr(CHARS, data->game->map[h][w]) == NULL)
return (0);
w++;
}
h++;
}
return (1);
}
static int ft_check_char(t_data *data, char ch, int *count)
{
int h;
int w;
h = 0;
*count = 0;
while (h < data->game->height)
{
w = 0;
while (data->game->map[h][w] != '\0')
{
if (data->game->map[h][w] == ch)
*count += 1;
w += 1;
}
h += 1;
}
if (ch == 'P' && (*count > 1 || *count == 0))
return (0);
else if (*count == 0)
return (0);
return (1);
}
static int ft_check_every_char(t_data *data)
{
int count;
if (!(ft_check_chars(data)))
return (INVALID_CHARS);
if (!(ft_check_char(data, 'P', &count)))
return (PLAYER_ERROR);
if (!(ft_check_char(data, 'E', &count)))
return (EXIT_ERROR);
if (!(ft_check_char(data, 'C', &count)))
return (COLLECT_ERROR);
else
data->game->collect = count;
return (VALID);
}
int ft_validate_map(t_data *data)
{
int h;
int w;
size_t len;
int out;
len = ft_strlen_nl(data->game->map[0]);
h = 0;
while (h < data->game->height)
{
if (len != (ft_strlen_nl(data->game->map[h])))
return (INVALID_RECT);
h++;
}
data->game->width = (int)len;
if (!(ft_validate_walls(data, &h, &w, len)))
return (INVALID_WALLS);
out = ft_check_every_char(data);
if (out > 0)
return (out);
return (VALID);
}