129 lines
4.0 KiB
C
129 lines
4.0 KiB
C
#include "solve_adv.h"
|
|
|
|
cmp_search* (*cmp_funs[CMP_SIZE])(game*) = {&cmp_taketreasures, &cmp_avoidminotaurs};
|
|
const char* cmp_names[CMP_SIZE] = {"Récupération de tous les trésors", "Eviter les minotaures"};
|
|
|
|
void cmp_free(cmp_search* chm_com) {
|
|
if (chm_com == NULL) {
|
|
return;
|
|
}
|
|
free(chm_com->array);
|
|
free(chm_com);
|
|
}
|
|
|
|
cmp_search* cmp_taketreasures(game* g) {
|
|
maze* m = g->m;
|
|
cmp_search* chm_com = malloc(sizeof(cmp_search));
|
|
if (chm_com == NULL) {
|
|
ERROR("Erreur d'allocation de mémoire pour cmp_search\n");
|
|
return NULL;
|
|
}
|
|
// Tableau avec les tresors
|
|
int* goals_cell = malloc(m->hsize * m->vsize * sizeof(int));
|
|
int* goals_object = malloc(m->hsize * m->vsize * sizeof(object));
|
|
|
|
int indice = 0;
|
|
int count = 0;
|
|
for (int i = 0; i < m->hsize * m->vsize; i++) {
|
|
if (get_object_maze(m, i) == SMALLT || get_object_maze(m, i) == MEDT || get_object_maze(m, i) == LARGET) {
|
|
count++;
|
|
goals_cell[indice] = i;
|
|
goals_object[indice] = get_object_maze(m, i);
|
|
indice++;
|
|
}
|
|
}
|
|
goals_cell = realloc(goals_cell, count * sizeof(int));
|
|
goals_object = realloc(goals_object, count * sizeof(object));
|
|
chm_com->array = malloc((count + 1) * sizeof(sim_path*));
|
|
chm_com->algo = CMP_TREAS;
|
|
chm_com->size = 0;
|
|
|
|
int start = m->player;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
sim_search* chm = sim_astar(g, start, GOA_TREASURE, &astar_manhattan, false);
|
|
if (chm == NULL) {
|
|
ERROR("Erreur de calcul du chemin\n");
|
|
return NULL;
|
|
}
|
|
chm_com->array[i] = chm->path;
|
|
chm_com->size++;
|
|
start = chm->path->end;
|
|
chm_com->length += chm->path->length;
|
|
|
|
int cell_treasure = chm->path->end;
|
|
add_object_maze(m, cell_treasure, NONE);
|
|
}
|
|
sim_search* chm = sim_astar(g, start, GOA_EXIT, &astar_manhattan, false);
|
|
chm_com->array[count] = chm->path;
|
|
chm_com->size++;
|
|
chm_com->length += chm->path->length;
|
|
|
|
// Remettre les tresors
|
|
for (int i = 0; i < count; i++) {
|
|
add_object_maze(m, goals_cell[i], goals_object[i]);
|
|
}
|
|
|
|
free(goals_cell);
|
|
free(goals_object);
|
|
return chm_com;
|
|
}
|
|
|
|
cmp_search* cmp_avoidminotaurs(game* g) {
|
|
maze* m = g->m;
|
|
cmp_search* chm_com = malloc(sizeof(cmp_search));
|
|
if (chm_com == NULL) {
|
|
ERROR("Erreur d'allocation de mémoire pour cmp_search\n");
|
|
return NULL;
|
|
}
|
|
// Tableau avec les tresors
|
|
int* goals_cell = malloc(m->hsize * m->vsize * sizeof(int));
|
|
int* goals_object = malloc(m->hsize * m->vsize * sizeof(object));
|
|
|
|
int indice = 0;
|
|
int count = 0;
|
|
for (int i = 0; i < m->hsize * m->vsize; i++) {
|
|
if (get_object_maze(m, i) == SMALLT || get_object_maze(m, i) == MEDT || get_object_maze(m, i) == LARGET) {
|
|
count++;
|
|
goals_cell[indice] = i;
|
|
goals_object[indice] = get_object_maze(m, i);
|
|
indice++;
|
|
}
|
|
}
|
|
goals_cell = realloc(goals_cell, count * sizeof(int));
|
|
goals_object = realloc(goals_object, count * sizeof(object));
|
|
chm_com->array = malloc((count + 1) * sizeof(sim_path*));
|
|
chm_com->algo = CMP_TREAS;
|
|
chm_com->size = 0;
|
|
|
|
int start = m->player;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
sim_search* chm = sim_astar(g, start, GOA_TREASURE, &astar_runaway, false);
|
|
if (chm == NULL) {
|
|
ERROR("Erreur de calcul du chemin\n");
|
|
return NULL;
|
|
}
|
|
chm_com->array[i] = chm->path;
|
|
chm_com->size++;
|
|
start = chm->path->end;
|
|
chm_com->length += chm->path->length;
|
|
|
|
int cell_treasure = chm->path->end;
|
|
add_object_maze(m, cell_treasure, NONE);
|
|
}
|
|
sim_search* chm = sim_astar(g, start, GOA_EXIT, &astar_runaway, false);
|
|
chm_com->array[count] = chm->path;
|
|
chm_com->size++;
|
|
chm_com->length += chm->path->length;
|
|
|
|
// Remettre les tresors
|
|
for (int i = 0; i < count; i++) {
|
|
add_object_maze(m, goals_cell[i], goals_object[i]);
|
|
}
|
|
|
|
free(goals_cell);
|
|
free(goals_object);
|
|
return chm_com;
|
|
}
|