-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
192 lines (177 loc) · 3.82 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
** main.c for lemin in /home/franck_r/rendu/lemin
**
** Made by Romain Franck
** Login <franck_r@epitech.net>
**
** Started on Tue Mar 25 14:22:50 2014 Romain Franck
** Last update Sun May 4 18:16:38 2014 root
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "lemin.h"
int create_tree(t_frm *farm, t_sln *links)
{
if (init_tree(farm, links) == EXIT_FAILURE)
{
printf("Failed to create tree\n");
return (EXIT_FAILURE);
}
if (link_everything(farm, links) == EXIT_FAILURE)
return (EXIT_FAILURE);
return (0);
}
int is_alphanum(char c)
{
if ((c >= '0' && c <= '9') || is_alphaz(c))
return (1);
return (0);
}
int valid_link_input(char *line)
{
int i;
i = 0;
if (line == 0)
return (1);
if (strncmp(line, "#", 1) == 0)
return (1);
while (line[i] && is_alphanum(line[i]))
i++;
if (line[i] == '-')
{
i++;
while (is_alphanum(line[i]) && line[i])
i++;
if (line[i] == 0)
return (1);
}
printf("Invalid link input\n");
return (0);
}
int valid_node_input(char *line)
{
int i;
int space;
i = -1;
space ^= space;
if (line == 0)
return (1);
if (strncmp(line, "#", 1) == 0)
return (1);
while (line[++i] && (is_alphanum(line[i]) || line[i] == ' '))
if (line[i] == ' ')
space++;
return ((space == 2) ? 1 : 0);
}
void manage_link(char *line, t_sln *links, int *kill)
{
if (!valid_link_input(line) || line == 0)
{
*kill = 0;
return ;
}
if (strncmp(line, "#", 1) == 0)
{
free(line);
line = getnextline(0);
}
if (!valid_link_input(line))
*kill = 0;
else
add_link_to_list(links, line);
free(line);
}
void add_valid_node(t_frm *farm, char *line, int *kill, int type)
{
if (valid_node_input(line))
add_node_to_list(farm, type, line);
else if (valid_link_input(line))
*kill = 0;
else
{
printf("Invalid room definition : (usage)-->[NAME X_POS Y_POS]\n");
*kill = 0;
}
}
void comment(t_frm *farm, char *line, int *kill)
{
if (strncmp(line, "##start", 7) == 0)
{
if ((line = getnextline(0)) != 0)
add_valid_node(farm, line, kill, 2);
}
else if (strncmp(line, "##end", 5) == 0)
{
if ((line = getnextline(0)) != 0)
add_valid_node(farm, line, kill, 1);
}
}
void manage_rooms(char *line, int *kill, t_frm *farm)
{
if (!valid_node_input(line) || line == 0)
{
*kill = 0;
return ;
}
if (strncmp(line, "#", 1) == 0)
{
comment(farm, line, kill);
free(line);
while (strncmp(line, "#", 1) == '#' && (line = getnextline(0)) != 0)
{
comment(farm, line, kill);
free(line);
}
}
else
add_valid_node(farm, line, kill, 0);
}
int create_node_list(t_frm *farm, t_sln *links)
{
char *line;
int kill;
kill = 1;
if ((line = getnextline(0)) != 0)
if ((farm->ants = my_getnbr(line)) < 1)
return (printf("Ant number must be > 0 (input : %d)\n", farm->ants));
free(line);
while (kill > 0 && (line = getnextline(0)) != 0)
manage_rooms(line, &kill, farm);
if (valid_link_input(line))
{
add_link_to_list(links, line);
free(line);
}
kill = 1;
while (kill && (line = getnextline(0)) != 0)
{
if (line[0] != '#')
manage_link(line, links, &kill);
}
return (0);
}
int main()
{
t_sln links;
t_frm farm;
t_ant *queen;
memset(&farm, 0, sizeof(t_frm));
memset(&links, 0, sizeof(t_sln));
links.size = 0;
if (create_node_list(&farm, &links) != 0)
return (EXIT_FAILURE);
if (create_tree(&farm, &links) == EXIT_FAILURE)
return (EXIT_FAILURE);
if (links.size <= 0 || farm.size <= 0)
return (EXIT_FAILURE);
backtrack(&farm, farm.exit, 0);
queen = releaseTheAnts(farm.ants, farm.start);
while (antAction(queen, move))
{
cleaningWoman(&farm);
antAction(queen, soil);
}
smashTheAnts(queen);
return (0);
}