-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_maze.c
109 lines (99 loc) · 2.47 KB
/
get_maze.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_maze.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mle-roy <mle-roy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/03/05 14:22:54 by mle-roy #+# #+# */
/* Updated: 2014/03/06 19:25:43 by mle-roy ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "lemmin.h"
#include "libft.h"
static int how_many_word(const char *str)
{
int i;
int ret;
i = 0;
ret = 0;
while (str[i])
{
if (!ft_isspace(str[i]) && (i == 0 || ft_isspace(str[i - 1])))
ret++;
i++;
}
return (ret);
}
void add_to_mz(t_env *maze, t_mz *room)
{
t_mz *browse;
if (maze->rooms == NULL)
maze->rooms = room;
else
{
browse = maze->rooms;
while (browse->next)
browse = browse->next;
browse->next = room;
room->prev = browse;
}
}
static t_env *init_maze(t_lex *first)
{
t_env *new;
int i;
i = 0;
if ((new = (t_env *)malloc(sizeof(*new))) == NULL)
ft_exit("malloc", 1);
if (how_many_word(first->str) > 1)
error_lemmin();
while ((first->str)[i])
{
if (!ft_isdigit((first->str)[i]))
error_lemmin();
i++;
}
new->fourmiz = ft_atoi(first->str);
new->start = NULL;
new->end = NULL;
new->rooms = NULL;
new->ants = NULL;
return (new);
}
static int check_maze(t_env *maze)
{
if (maze->start_ptr == NULL)
error_lemmin();
if (maze->end_ptr == NULL)
error_lemmin();
if (maze->rooms == NULL)
error_lemmin();
if (maze->ants == NULL)
error_lemmin();
return (0);
}
t_env *get_maze(t_lx *lex)
{
t_env *maze;
t_lex *browse;
maze = init_maze(lex->start);
browse = lex->start->next;
while (browse)
{
if ((browse->str)[0] == '#')
{
browse = browse->next;
add_start_end(maze, browse);
}
else if ((how_many_word(browse->str)) == 3)
add_room(maze, browse);
else if (ft_strchr(browse->str, '-'))
add_connex(maze, browse);
browse = browse->next;
}
make_ants(maze);
check_maze(maze);
return (maze);
}