-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_solution.c
88 lines (79 loc) · 2.11 KB
/
save_solution.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* save_solution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mle-roy <mle-roy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/03/06 15:52:37 by mle-roy #+# #+# */
/* Updated: 2014/03/06 15:56:16 by mle-roy ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "lemmin.h"
#include "libft.h"
static t_room *init_room_path(t_mz *room)
{
t_room *new;
if ((new = (t_room *)malloc(sizeof(*new))) == NULL)
ft_exit("malloc", 1);
new->name = ft_strdup(room->name);
if (room->start == 1)
new->start = 1;
else
new->start = 0;
if (room->end == 1)
new->end = 1;
else
new->end = 0;
new->next = NULL;
new->prev = NULL;
return (new);
}
static void add_path(t_res *res, t_mz *room)
{
t_room *add;
if (res->path == NULL)
res->path = init_room_path(room);
else
{
add = init_room_path(room);
res->path->prev = add;
add->next = res->path;
res->path = add;
}
}
static t_mz *find_next_room(t_mz *room, t_env *maze)
{
t_connex *connex;
t_mz *browse;
connex = room->list;
while (connex)
{
browse = maze->rooms;
while (browse)
{
if (!ft_strcmp(browse->name, connex->name))
{
if (browse->path == (room->path - 1))
return (browse);
}
browse = browse->next;
}
connex = connex->next;
}
return (NULL);
}
void save_solution(t_res *res, t_env *maze)
{
t_mz *room;
room = maze->end_ptr;
add_path(res, room);
while (room->path != 1)
{
room = find_next_room(room, maze);
if (room == NULL)
error_lemmin();
add_path(res, room);
}
}