game_strategies.c
Ajout pour le système de bfs
This commit is contained in:
85
Ethan et Lucien/game_strategies.c
Normal file
85
Ethan et Lucien/game_strategies.c
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "game_strategies.h"
|
||||
|
||||
#include "data_queue.h"
|
||||
#include "maze.h"
|
||||
|
||||
const char* str_names[STR_SIZE] = {"Immobile", "Aléatoire", "Opposée",
|
||||
"Agressive", "BFS"};
|
||||
void (*str_funs[STR_SIZE])(maze*, move, move*) = {
|
||||
&minotaurs_still, &minotaurs_random, &minotaurs_reverse, &minotaurs_closein,
|
||||
&minotaurs_bfs};
|
||||
|
||||
void minotaurs_still(maze*, move, move*) { return; }
|
||||
|
||||
void minotaurs_random(maze* m, move, move* mv_minotaurs) {
|
||||
if (m == NULL || mv_minotaurs == NULL) return;
|
||||
for (int i = 0; i < m->nb_minotaurs; i++) {
|
||||
mv_minotaurs[i] = rand() % M_WAIT;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void minotaurs_reverse(maze* m, move mv_player, move* mv_minotaurs) {
|
||||
if (m == NULL || mv_minotaurs == NULL) return;
|
||||
for (int i = 0; i < m->nb_minotaurs; i++) {
|
||||
mv_minotaurs[i] = (mv_player + 2) % M_WAIT;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void minotaurs_closein(maze* m, move, move* mv_minotaurs) {
|
||||
if (m == NULL || mv_minotaurs == NULL) return;
|
||||
int x_player = m->player % m->hsize;
|
||||
int y_player = m->player / m->hsize;
|
||||
for (int i = 0; i < m->nb_minotaurs; i++) {
|
||||
int x_minotaur = m->minotaurs[i] % m->hsize;
|
||||
int y_minotaur = m->minotaurs[i] / m->hsize;
|
||||
int mv[2] = {x_player - x_minotaur, y_player - y_minotaur};
|
||||
bool changed = false;
|
||||
for (int j = rand() % 2; j < 2; j++) {
|
||||
if (!changed) {
|
||||
if (j % 2 == 0) {
|
||||
if (mv[j] > 0) {
|
||||
mv_minotaurs[i] = M_EAST;
|
||||
} else if (mv[j] < 0) {
|
||||
mv_minotaurs[i] = M_WEST;
|
||||
}
|
||||
} else {
|
||||
if (mv[j] > 0) {
|
||||
mv_minotaurs[i] = M_SOUTH;
|
||||
} else if (mv[j] < 0) {
|
||||
mv_minotaurs[i] = M_NORTH;
|
||||
}
|
||||
}
|
||||
if (mv[j] != 0) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void minotaurs_bfs(maze* m, move, move* mv_minotaurs) {
|
||||
if (m == NULL || mv_minotaurs == NULL) return;
|
||||
queue* q = create_queue();
|
||||
enqueue(m->player, q);
|
||||
m->props[m->player] |= (1 << 7);
|
||||
while (!is_empty_queue(q)) {
|
||||
uint pos = dequeue(q);
|
||||
for (cardinal c = NORTH; c <= WEST; c++) {
|
||||
int adj = get_adj_maze(m, pos, c);
|
||||
if (adj != -1 && !has_wall_maze(m, pos, c) &&
|
||||
!(m->props[adj] & (1 << 7))) {
|
||||
enqueue(adj, q);
|
||||
m->props[adj] |= (1 << 7);
|
||||
for (int i = 0; i < m->nb_minotaurs; i++) {
|
||||
if (m->minotaurs[i] == adj)
|
||||
mv_minotaurs[i] = (move)((c + 2) % (WEST + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < m->hsize * m->vsize; i++) m->props[i] &= ~(1 << 7);
|
||||
delete_queue(q);
|
||||
return;
|
||||
}
|
||||
Reference in New Issue
Block a user