-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_bonus.c
129 lines (118 loc) · 3.57 KB
/
main_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: melkholy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/07 16:35:52 by melkholy #+# #+# */
/* Updated: 2022/10/07 16:56:30 by melkholy ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long_bonus.h"
bool ft_validate_map(t_graph *graph)
{
int count;
int c_in;
count = -1;
c_in = -1;
while (graph->map[++count])
{
c_in = -1;
while (graph->map[count][++c_in])
if (!ft_strchr("01PGCE", graph->map[count][c_in]))
return (ft_printf("Error\nInvalid map character\n"), false);
}
while (--c_in >= 0)
if (graph->map[0][c_in] != '1')
return (ft_printf("Error\nInvalid map structure\n"), false);
while (graph->map[0][++c_in])
if (graph->map[graph->row - 1][c_in] != '1')
return (ft_printf("Error\nInvalid map structure\n"), false);
while (++c_in - graph->col < graph->row - 1)
if (graph->map[c_in - graph->col][0] != '1' \
|| graph->map[c_in - graph->col][graph->col - 1] != '1')
return (ft_printf("Error\nInvalid map structure\n"), false);
if (ft_check_p_c_e(graph) != 1 || graph->gate < 1 || graph->cats < 1)
return (ft_printf("Error\nYou can have P==1, E>0, C>0 only\n"), false);
return (true);
}
bool ft_get_map(char *file, t_graph *graph)
{
char *line;
int count;
int fd;
count = 0;
fd = open(file, O_RDONLY);
line = get_next_line(fd);
graph->map[count] = ft_strtrim(line, "\n");
free(line);
while (graph->map[count])
{
count ++;
line = get_next_line(fd);
graph->map[count] = ft_strtrim(line, "\n");
free(line);
}
close(fd);
graph->row = count;
graph->col = ft_strlen(graph->map[0]);
while (--count >= 0)
if (graph->col != (int)ft_strlen(graph->map[count]))
return (ft_printf("Error\nInvalid map structure\n"), false);
return (ft_validate_map(graph));
}
bool ft_maplen(char *file, t_graph *graph)
{
char *line;
int fd;
fd = open(file, O_RDONLY);
if (fd == -1)
return (ft_printf("Error\nFile doesn't exsit.\n"), false);
line = get_next_line(fd);
if (line)
graph->col = ft_strlen(line) - 1;
else
return (ft_printf("Error\nThis is an empty map.\n"), false);
while (line)
{
graph->row ++;
free(line);
line = get_next_line(fd);
}
close(fd);
graph->map = (char **)ft_calloc(graph->row + 1, sizeof(char *));
if (!graph->map)
return (false);
graph->map[graph->row] = NULL;
return (true);
}
t_graph *ft_check_map(char *file)
{
t_graph *graph;
if (!ft_strnstr(file, ".ber", ft_strlen(file)))
{
ft_printf("Error\nPlease pass the correct Map format\n");
return (NULL);
}
graph = (t_graph *)ft_calloc(1, sizeof(t_graph));
if (!graph)
return (NULL);
if (!(ft_maplen(file, graph) && ft_get_map(file, graph) \
&& ft_ispath(graph)))
{
ft_free_graph(graph);
return (NULL);
}
return (graph);
}
int main(int argc, char *argv[])
{
t_graph *graph;
if (argc != 2)
exit(40 - write(2, "Error\nYou need one argument (Map file)\n", 39));
graph = ft_check_map(argv[1]);
if (graph)
ft_start_graphics(graph);
return (0);
}