From 567dc7282554aa42b7f1b49dad583612919887ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Thu, 14 Dec 2023 00:01:21 +0100 Subject: [PATCH 1/3] Init best move calculation --- Makefile | 1 + src/lemin.c | 1 + src/lemin.h | 1 + src/move.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 src/move.c diff --git a/Makefile b/Makefile index fbfa1d5..4ede955 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,7 @@ VPATH := src SRC := lemin.c SRC += linked.c SRC += free.c +SRC += move.c SRC += parser/parser.c SRC += parser/room.c diff --git a/src/lemin.c b/src/lemin.c index 8afb91a..a6b8e96 100644 --- a/src/lemin.c +++ b/src/lemin.c @@ -55,6 +55,7 @@ void handle_anthill(anthill_t *anthill) if (graph == NULL) return; DEBUG("HEAD: %s", graph->name); + move_ants(graph, anthill); } int lemin(void) diff --git a/src/lemin.h b/src/lemin.h index a5c5a3f..da96a16 100644 --- a/src/lemin.h +++ b/src/lemin.h @@ -67,5 +67,6 @@ void parse_me_baby(anthill_t *anthill); void create_linked_list(anthill_t *anthill); void free_ants(anthill_t *anthill); room_t *get_graph(anthill_t *anthill); +void move_ants(room_t *graph, anthill_t *anthill); #endif diff --git a/src/move.c b/src/move.c new file mode 100644 index 0000000..bc3b09a --- /dev/null +++ b/src/move.c @@ -0,0 +1,35 @@ +/* +** EPITECH PROJECT, 2023 +** lemin +** File description: +** move +*/ + +#include + +#include "lemin.h" + +#include "my/string.h" + +static +tunnel_t *get_shorest_path(room_t *graph, anthill_t *anthill) +{ + tunnel_t *shortest = malloc(anthill->rooms.count * sizeof(tunnel_t)); + + if (shortest == NULL) + return NULL; + my_memset(shortest, 0, anthill->rooms.count * sizeof(tunnel_t)); + return shortest; +} + +void move_ants(room_t *graph, anthill_t *anthill) +{ + tunnel_t *shortest; + + if (graph == NULL || anthill == NULL) + return; + shortest = get_shorest_path(graph, anthill); + if (shortest == NULL) + return; + free(shortest); +} From 7f502c5fefdaa5a3022b35f4411b659e2f0926b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Thu, 14 Dec 2023 14:06:59 +0100 Subject: [PATCH 2/3] Add movement calculation --- src/lemin.h | 1 + src/move.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/lemin.h b/src/lemin.h index da96a16..f3a9a77 100644 --- a/src/lemin.h +++ b/src/lemin.h @@ -30,6 +30,7 @@ typedef struct room_s { size_t x; size_t y; enum room_type_e type; + bool visited; struct { size_t n; struct room_s **arr; diff --git a/src/move.c b/src/move.c index bc3b09a..90d815f 100644 --- a/src/move.c +++ b/src/move.c @@ -7,29 +7,93 @@ #include +#include "my/std.h" +#include "my/debug.h" +#include "my/math.h" +#include "my/string.h" + #include "lemin.h" -#include "my/string.h" +static +int visit_room(room_t **shortest, size_t i, room_t *graph) +{ + if (graph == NULL || graph->visited == true) + return RET_ERROR; + graph->visited = true; + if (graph->type == ROOM_EXIT) { + shortest[i] = graph; + return RET_VALID; + } + for (size_t j = 0; j < graph->leaves.n; j++) { + if (visit_room(shortest, i + 1, graph->leaves.arr[j]) == RET_ERROR) + continue; + shortest[i] = graph; + return RET_VALID; + } + return RET_ERROR; +} static -tunnel_t *get_shorest_path(room_t *graph, anthill_t *anthill) +room_t **get_shorest_path(room_t *graph, anthill_t *anthill) { - tunnel_t *shortest = malloc(anthill->rooms.count * sizeof(tunnel_t)); + size_t size = anthill->rooms.count * sizeof(tunnel_t); + room_t **shortest = malloc(size); if (shortest == NULL) return NULL; - my_memset(shortest, 0, anthill->rooms.count * sizeof(tunnel_t)); - return shortest; + my_memset(shortest, 0, size); + if (visit_room(shortest, 0, graph) == RET_VALID) + return shortest; + free(shortest); + return NULL; +} + +static +void single_file( + anthill_t *anthill, size_t *positions, + size_t len_shortest, room_t *shortest[len_shortest]) +{ + bool space = false; + + for (size_t i = 0; i < anthill->ants; i++) { + if (positions[i] == len_shortest - 1) + continue; + positions[i] += 1; + my_printf("%sP%d-%s", (space) ? " " : "", + i + 1, shortest[positions[i]]->name); + space = true; + if (positions[i] == 1) + break; + } + my_printf("\n"); +} + +static +void move_my_children(anthill_t *anthill, room_t **shortest) +{ + size_t positions[anthill->ants]; + size_t len_shortest; + + for (size_t i = 0; i < anthill->ants; i++) + positions[i] = 0; + for (len_shortest = 0; shortest[len_shortest] != NULL; len_shortest++); + do { + single_file(anthill, positions, len_shortest, shortest); + } while (positions[anthill->ants - 1] != len_shortest - 1); } void move_ants(room_t *graph, anthill_t *anthill) { - tunnel_t *shortest; + room_t **shortest; if (graph == NULL || anthill == NULL) return; shortest = get_shorest_path(graph, anthill); if (shortest == NULL) return; + my_printf("#moves\n"); + for (size_t i = 0; shortest[i] != NULL; i++) + DEBUG("move %d: %s", i + 1, shortest[i]->name); + move_my_children(anthill, shortest); free(shortest); } From 2a8e5a95d1a5ae679a86bed31f3302afd7a99ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Thu, 14 Dec 2023 14:07:55 +0100 Subject: [PATCH 3/3] Fix dead ends --- src/move.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/move.c b/src/move.c index 90d815f..8f5b704 100644 --- a/src/move.c +++ b/src/move.c @@ -30,6 +30,7 @@ int visit_room(room_t **shortest, size_t i, room_t *graph) shortest[i] = graph; return RET_VALID; } + graph->visited = false; return RET_ERROR; }