-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparce_bonus.c
137 lines (127 loc) · 2.74 KB
/
parce_bonus.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parce_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: het-taja <het-taja@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 20:46:17 by het-taja #+# #+# */
/* Updated: 2024/07/15 21:22:46 by het-taja ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long_bonus.h"
int parcing(t_game *game, char *map)
{
if (map_name(map) == -1)
return (-1);
if (read_map(game, map) == -1)
{
clear_map(game->map);
return (-1);
}
if (parce_map(game->map) == -1 || map_walls(game) == -1)
{
clear_map (game->map);
return (-1);
}
player_pos(game);
exit_pos(game);
game->c = ft_count_char(game->map, 'C');
return (0);
}
int read_map(t_game *game, char *map)
{
int fd;
char *buff;
int readed;
fd = open(map, O_RDONLY);
if (fd == -1)
exit(error(game));
buff = malloc(1001);
readed = read(fd, buff, 1000);
buff[readed] = '\0';
if (read(fd, buff, 10))
{
free(buff);
return (-1);
}
close (fd);
game->map = ft_split(buff, '\n');
if (count_newline(buff, game->map) == -1)
{
free(buff);
return (-1);
}
free(buff);
return (0);
}
int map_walls(t_game *game)
{
int x;
int y;
int hight;
int len;
y = 0;
len = ft_strlen(game->map[y]);
hight = -1;
while (game->map && game->map[y])
{
if (game->map[y][0] != '1' || game->map[y][len - 1] != '1')
return (-1);
y++;
hight++;
}
x = 0;
while (game->map[hight][x] && game->map[0][x])
{
if (game->map[hight][x] != '1' || game->map[0][x] != '1')
return (-1);
x++;
}
return (0);
}
int is_wrong_char(char **str)
{
int i;
int j;
i = 0;
j = 0;
while (str && str[i])
{
j = 0;
while (str[i] && str[i][j])
{
if (str[i][j] != 'P' && str[i][j] != 'E' && str[i][j] != 'C'
&& str[i][j] != '1' && str[i][j] != '0')
{
return (1);
}
j++;
}
i++;
}
return (0);
}
int parce_map(char **str)
{
int y;
int len;
y = 0;
if (!str)
return (-1);
if (ft_count_char(str, 'P') != 1 || ft_count_char(str, 'E') != 1
|| ft_count_char(str, 'C') < 1)
return (-1);
if (is_wrong_char(str) != 0)
return (-1);
len = ft_strlen(str[0]);
while (str && str[y])
{
if (ft_strlen(str[y]) != len)
{
return (-1);
}
y++;
}
return (0);
}