Commit 4dc02de6 authored by TRAN Alain's avatar TRAN Alain

Upload New File

parent 7e992b95
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoecodingame;
import static java.lang.Math.max;
/**
*
* @author Alain
*/
public class AlgoRechercheMinMax {
String[][] grille = {{"","",""},{"","",""},{"","",""}};
int[] scores = {1,-1,0};
public Coup meilleur_coup_minmax(){
double inf = Double.POSITIVE_INFINITY;
double bestScore = -1 * inf;
int[] meilleur_coup = new int[2] ;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (grille[i][j] == "" ) {
grille[i][j] = "X";}
int score = minimax(grille,0,false);
grille[i][j] = "" ;
if (score > bestScore){
bestScore = score;
meilleur_coup[0] = i;
meilleur_coup[1] = j;
}
}
}
grille[meilleur_coup[0]][meilleur_coup[1]] = "X";
// changer le tour du joueur ?
}
public int minimax(grille, profondeur, isMaximizing){
Joueur gagnant = grille.vainqueur(); //Il faudrait connaitre le vainqueur
int resultat = gagnant.getIdJoueur();
if (resultat !== null) {
return scores[resultat];
}
if (isMaximizing) {
double inf = Double.POSITIVE_INFINITY;
double bestScore = -1 * inf;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (grille[i][j] == "" ) {
grille[i][j] = "X";
double score = minimax(grille,profondeur + 1,false);
grille[i][j] = "" ;
bestScore = max(score,bestScore);
}
}
}
return bestScore;
}
else {
double inf = Double.POSITIVE_INFINITY;
double bestScore = inf;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (grille[i][j] == "" ) {
grille[i][j] = "X";
double score = minimax(grille,profondeur + 1,true);
grille[i][j] = "" ;
bestScore = min(score,bestScore);
}
}
}
return bestScore;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment