Skip to content

Commit

Permalink
Merge pull request #8 from drawpitech/best-move
Browse files Browse the repository at this point in the history
Best move calculation
  • Loading branch information
drawbu authored Dec 14, 2023
2 parents 41e62a0 + 2a8e5a9 commit 19cab17
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/lemin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/lemin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,5 +68,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
100 changes: 100 additions & 0 deletions src/move.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
** EPITECH PROJECT, 2023
** lemin
** File description:
** move
*/

#include <stdlib.h>

#include "my/std.h"
#include "my/debug.h"
#include "my/math.h"
#include "my/string.h"

#include "lemin.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;
}
graph->visited = false;
return RET_ERROR;
}

static
room_t **get_shorest_path(room_t *graph, anthill_t *anthill)
{
size_t size = anthill->rooms.count * sizeof(tunnel_t);
room_t **shortest = malloc(size);

if (shortest == NULL)
return NULL;
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)
{
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);
}

0 comments on commit 19cab17

Please sign in to comment.