Bon voila ma source, je cherche a mettre en pratique l'algo minmax, mais je suis confronté a des erreurs que je ne comprend pas ! A l execution lors du tour de jeux de l IA l ordinateur joue tout d un coup au lieu d y aller coup par coup, pourqoui, je traine ce probleme depuis deja deux semaines, je craque ... merci d avance !!
#include <stdio.h>
#include <stdlib.h>
#define CROIX 'X'
#define ROND 'O'
#define VIDE ' '
#define MATCH_NUL 'N'
#define NOBODY_WIN 'M'
#define MAX_X -100
#define MAX_O 100
int coupX = 0, coupY = 0;
int nbr_coups = 0;
int ctrl_prof = 0;
int init_grille(char tab[3][3])
{
int i = 0, j = 0;
for(i = 0 ; i < 3 ; i++)
{
for(j = 0 ; j < 3 ; j++)
{
tab[i][j] = VIDE;
}
}
return 0;
}
int display_grille(char tab[3][3])
{
int i = 0, j = 0;
printf(" 1 2 3\n");
for(i = 0 ; i < 3 ; i++)
{
printf("%d ", i+1);
for(j = 0 ; j < 3 ; j++)
{
printf("%c ", tab[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
char gagne_grille(char tab[3][3])
{
int i = 0;
//LIGNES && COLONES
for(i = 0 ; i < 3 ; i++)
{
if(tab[i][0] == CROIX && tab[i][1] == CROIX && tab[i][2] == CROIX)
return CROIX;
else if(tab[0][i] == CROIX && tab[1][i] == CROIX && tab[2][i] == CROIX)
return CROIX;
else if(tab[i][0] == ROND && tab[i][1] == ROND && tab[i][2] == ROND)
return ROND;
else if(tab[0][i] == ROND && tab[1][i] == ROND && tab[2][i] == ROND)
return ROND;
}
//DIAGONALES
if(tab[0][0] == CROIX && tab[1][1] == CROIX && tab[2][2] == CROIX)
return CROIX;
else if(tab[0][2] == CROIX && tab[1][1] == CROIX && tab[2][0] == CROIX)
return CROIX;
else if(tab[0][0] == ROND && tab[1][1] == ROND && tab[2][2] == ROND)
return ROND;
else if(tab[0][2] == ROND && tab[1][1] == ROND && tab[2][0] == ROND)
return ROND;
if(nbr_coups == 9)
return MATCH_NUL;
else
return NOBODY_WIN;
}
int minmax(char tab[3][3], char gamer)
{
int i, j, max, score = 0;
if(gamer == CROIX)
{
max = MAX_X;
}
else if(gamer == ROND)
{
max = MAX_O;
}
for(i = 0 ; i < 3 ; i++)
{
for(j = 0 ; j < 3 ; j++)
{
if(tab[i][j] == VIDE)
{
tab[i][j] = gamer;
if((gagne_grille(tab)) == gamer)
{
return max;
}
else
{
if(gamer == ROND)
{
score = minmax(tab, CROIX);
if(score == MAX_X)
{
return 0;
}
else if(score == MAX_O)
{
return max;
}
}
else if(gamer == CROIX)
{
score = minmax(tab, ROND);
if(score == MAX_X)
{
return 0;
}
else if(score == MAX_O)
{
return max;
}
}
}
tab[i][j] = VIDE;
}
}
}
return 0;
}
int ia(char tab[3][3])
{
int i, j, score = 0;
for(i = 0 ; i < 3 ; i++)
{
for(j = 0 ; j < 3 ; j++)
{
if(tab[i][j] == VIDE)
{
tab[i][j] = ROND;
if((gagne_grille(tab)) == ROND)
{
coupY = i;
coupX = j;
return 0;
}
else
{
score = minmax(tab, ROND);
if(score == MAX_O)
{
coupY = i;
coupX = j;
return 0;
}
}
tab[i][j] = VIDE;
}
}
}
return 0;
}
int main(int argc, char* *argv)
{
char control;
char tour = CROIX;
int col = 0, lig = 0;
char tab[3][3];
init_grille(tab);
do
{
if(tour == CROIX)
{
A:
system("clear");
display_grille(tab);
printf("ligne = ");
scanf("%d", &lig);
printf("colone = ");
scanf("%d", &col);
col--;
lig--;
if(tab[lig][col] == ' ')
{
tab[lig][col] = CROIX;
tour = ROND;
nbr_coups++;
}
else
goto A;
}
else if(tour == ROND)
{
ia(tab);
tab[coupY][coupX] = ROND;
tour = CROIX;
nbr_coups++;
}
}while((control = gagne_grille(tab)) == NOBODY_WIN);
system("clear");
display_grille(tab);
if(control == MATCH_NUL)
printf("Match nulle !!!\n");
else if(control == ROND)
printf("Vous avez perdu\n");
else if(control == CROIX)
printf("Vous avez gagné\n");
return 0;
}