-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsing.c
More file actions
113 lines (104 loc) · 2.48 KB
/
parsing.c
File metadata and controls
113 lines (104 loc) · 2.48 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
102
103
104
105
106
107
108
109
110
111
112
113
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dhuss <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/06 15:31:06 by dhuss #+# #+# */
/* Updated: 2024/09/18 10:11:48 by dhuss ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static int is_valid_char_2(char c, const char *valid_chars)
{
while (*valid_chars)
{
if (c == *valid_chars)
return (1);
valid_chars++;
}
return (0);
}
void locate_positions(t_game_state *game)
{
size_t i;
char *pos;
i = 0;
while (game->map[i] != NULL)
{
pos = ft_strchr(game->map[i], 'P');
if (pos != NULL)
{
game->player.x = pos - game->map[i];
game->player.y = i;
}
pos = ft_strchr(game->map[i], 'E');
if (pos != NULL)
{
game->exit.x = pos - game->map[i];
game->exit.y = i;
}
i++;
}
}
void check_rectangle_(t_game_state *game)
{
int i;
i = 1;
while (i < game->size.y)
{
if (game->x_len[i] != game->x_len[i - 1])
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd("Not a rectangle!\n", 1);
free(game->x_len);
free_double_exit(game->map);
}
i++;
}
free(game->x_len);
}
void check_if_empty(t_game_state *game)
{
if (!game->map || !game->map[0])
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd("First row of the map is NULL\n", 1);
if (game->map)
{
free(game->x_len);
free_double_exit(game->map);
}
}
else if (game->map[0][0] == '\0')
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd("First row is an empty string\n", 1);
free(game->x_len);
free_double_exit(game->map);
}
}
void check_valid_chars(char **map)
{
size_t y;
size_t x;
size_t len;
y = 0;
len = ft_strlen(map[0]);
while (map[y] != NULL)
{
x = 0;
while (x < len)
{
if (!is_valid_char_2(map[y][x], "01PCE"))
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd("Map contains invalid characters!\n", 1);
free_double_exit(map);
}
x++;
}
y++;
}
}