more minimax stuff

This commit is contained in:
Mylloon 2022-11-19 15:50:58 +01:00
parent 573f35d246
commit 9d3e8b0914
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 64 additions and 23 deletions

View file

@ -2,7 +2,6 @@
#define OTHELLO_MINIMAX_H 1
#include <limits.h>
#include <string.h>
#include "joueur.h"
@ -12,14 +11,10 @@ typedef struct jeu Jeu;
void action_joueur_minimax(Jeu *jeu, int couleur);
/* Implémentation de Minimax */
int minimax(Jeton *plateau[LONGEUR][LARGEUR], int profondeur, Coups *positions,
Jeton coup_choisi, int joueur, int joueur_gagnant);
int minimax(Jeu *jeu, int profondeur, Coups *positions, Jeton coup_choisi,
int joueur, int joueur_gagnant);
/* Calcule la valeur heuristique pour un coup */
int heuristique(Jeton *plateau[LONGEUR][LARGEUR], Jeton coup_choisi);
/* Copie le plateau */
void copie_plateau(Jeton *plateau_source[LONGEUR][LARGEUR],
Jeton *plateau_destination[LONGEUR][LARGEUR]);
int heuristique(Jeu *jeu, Jeton coup_choisi);
#endif

View file

@ -4,15 +4,13 @@ void action_joueur_minimax(Jeu *jeu, int couleur) {
Coups *possibilites = action_possible_joueur(jeu->plateau, couleur);
// On fait une copie du plateau actuel
Jeton *plateau[LONGEUR][LARGEUR];
copie_plateau(jeu->plateau, plateau);
int rep_minimax = minimax(plateau, 3, possibilites,
int rep_minimax = minimax(jeu, 3, possibilites,
*possibilites->coups->premier->jeton, couleur, 1);
free_coups(possibilites);
printf("rep_minimax: %d\n", rep_minimax);
printf("-> %d\n", rep_minimax);
free_coups(possibilites);
// Crash temporaire
exit(1);
@ -22,10 +20,10 @@ void action_joueur_minimax(Jeu *jeu, int couleur) {
jeu_joueur(jeu, ligne, colonne, couleur);
}
int minimax(Jeton *plateau[LONGEUR][LARGEUR], int profondeur, Coups *positions,
Jeton coup_choisi, int joueur, int joueur_gagnant) {
int minimax(Jeu *jeu, int profondeur, Coups *positions, Jeton coup_choisi,
int joueur, int joueur_gagnant) {
if (profondeur == 0) {
return heuristique(plateau, coup_choisi);
return heuristique(jeu, coup_choisi);
}
int value;
@ -33,18 +31,30 @@ int minimax(Jeton *plateau[LONGEUR][LARGEUR], int profondeur, Coups *positions,
value = INT_MIN;
Element *tmp = positions->coups->premier;
while (tmp) {
value = max(value, minimax(plateau, profondeur - 1, positions,
Jeu *jeu_copie = copie_jeu(jeu);
Coups *possibilites =
action_possible_joueur(jeu_copie->plateau, joueur);
value = max(value, minimax(jeu_copie, profondeur - 1, possibilites,
*tmp->jeton, joueur, 0));
free_coups(possibilites);
free_jeu(jeu_copie);
tmp = tmp->suivant;
}
} else {
value = INT_MAX;
Element *tmp = positions->coups->premier;
while (tmp) {
value = max(value, minimax(plateau, profondeur - 1, positions,
Jeu *jeu_copie = copie_jeu(jeu);
Coups *possibilites =
action_possible_joueur(jeu_copie->plateau, joueur);
value = max(value, minimax(jeu_copie, profondeur - 1, possibilites,
*tmp->jeton, joueur, 1));
free_coups(possibilites);
free_jeu(jeu_copie);
tmp = tmp->suivant;
}
}
@ -52,11 +62,47 @@ int minimax(Jeton *plateau[LONGEUR][LARGEUR], int profondeur, Coups *positions,
return value;
}
int heuristique(Jeton *plateau[LONGEUR][LARGEUR], Jeton coup_choisi) {
int heuristique(Jeu *jeu, Jeton coup_choisi) {
if (jeu_joueur(jeu, coup_choisi.pos_i, coup_choisi.pos_j,
coup_choisi.couleur)) {
return 0;
}
fprintf(stderr, "Minimax veut jouer un coup illégal (");
affiche_jeton(stderr, &coup_choisi);
fprintf(stderr, ").\n");
exit(EXIT_FAILURE);
}
void copie_plateau(Jeton *source[LONGEUR][LARGEUR],
Jeton *destination[LONGEUR][LARGEUR]) {
memcpy(destination, source, sizeof(*source));
Jeu *copie_jeu(Jeu *source) {
Jeu *res = malloc(sizeof(Jeu));
// Copie J1
res->j1 = malloc(sizeof(Joueur));
res->j1->nom = source->j1->nom;
res->j1->couleur = source->j1->couleur;
res->j1->liste_jeton = copie_liste(source->j1->liste_jeton);
res->j1->nb_jeton = source->j1->nb_jeton;
// Copie J2
res->j2 = malloc(sizeof(Joueur));
res->j1->nom = source->j1->nom;
res->j2->couleur = source->j2->couleur;
res->j2->liste_jeton = copie_liste(source->j2->liste_jeton);
res->j2->nb_jeton = source->j2->nb_jeton;
// Copie plateau
for (int i = 0; i < LONGEUR; ++i) {
for (int j = 0; j < LARGEUR; ++j) {
Jeton *jeton = malloc(sizeof(Jeton));
jeton->couleur = source->plateau[i][j]->couleur;
jeton->pos_i = source->plateau[i][j]->pos_i;
jeton->pos_j = source->plateau[i][j]->pos_j;
res->plateau[i][j] = jeton;
}
}
return res;
}