-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_parsing.c
More file actions
103 lines (95 loc) · 2.59 KB
/
map_parsing.c
File metadata and controls
103 lines (95 loc) · 2.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nosahimi <nosahimi@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/13 15:27:52 by nosahimi #+# #+# */
/* Updated: 2025/03/28 20:59:30 by nosahimi ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void is_valid_elements_helper(t_game *game, char **map, int i, int j)
{
if (map[i][j] == 'P')
{
game->player_x = j;
game->player_y = i;
game->p++;
}
else if (map[i][j] == 'C')
game->c++;
else if (map[i][j] == 'E')
{
game->exit_x = j;
game->exit_y = i;
game->e++;
}
else if (map[i][j] != '0' && map[i][j] != '1')
(fd_putstr("ERROR\nINVALID ELEMENTS\n", 2),
free_all_maps(game), exit(1));
}
t_game *is_valid_elements(t_game *game, char **map)
{
int i;
int j;
if (!map)
return (game);
i = 0;
while (map[i])
{
j = 0;
while (map[i][j])
{
is_valid_elements_helper(game, map, i, j);
j++;
}
i++;
}
return (game);
}
int check_elements(t_game *game)
{
if (game->c < 1)
fd_putstr("ERROR\nNOT ENOUGH COLLECTIBALES\n", 2);
else if (game->p > 1)
fd_putstr("ERROR\nMORE THAN 1 PLAYER\n", 2);
else if (game->p == 0)
fd_putstr("ERROR\nNO PLAYER\n", 2);
else if (game->e > 1)
fd_putstr("ERROR\nMORE THAN 1 EXIT\n", 2);
else if (game->e == 0)
fd_putstr("ERROR\nNO EXIT\n", 2);
else
return (0);
return (1);
}
int is_closed_by_wall(t_game *game)
{
int i;
i = 0;
if (!game->map)
return (0);
while (game->map[i])
{
if ((game->map[i][0] != '1'
|| game->map[i][game->map_width - 1] != '1'))
return (0);
i++;
}
if (is_all_one(game->map[0]) == 0 || is_all_one(game->map[i - 1]) == 0)
return (0);
return (1);
}
void parsing_func(t_game *game)
{
if (is_closed_by_wall(game) == 0)
(fd_putstr("ERROR\nMAP IS NOT CLOSED BY WALLS\n", 2),
free_all_maps(game), exit(1));
if (check_elements(game) == 1)
(free_all_maps(game), exit(1));
if (is_elements_rechable(game->map_c) == 0)
(fd_putstr("ERROR\nELEMENTS IS NOT RECHABLE\n", 2),
free_all_maps(game), exit(1));
}