34 lines
943 B
Java
34 lines
943 B
Java
public class MinimaxSimple {
|
|
|
|
// Max = moi, Min = adversaire
|
|
// Retourne +1 si Max gagne, -1 si Max perd
|
|
|
|
public static int exploreMax(int n) {
|
|
if (n <= 0) return -1; // défaite pour Max
|
|
int meilleur = Integer.MIN_VALUE;
|
|
|
|
for (int coups = 1; coups <= 3; coups++) {
|
|
int res = exploreMin(n - coups);
|
|
meilleur = Math.max(meilleur, res);
|
|
}
|
|
return meilleur;
|
|
}
|
|
|
|
public static int exploreMin(int n) {
|
|
if (n <= 0) return +1; // victoire pour Max
|
|
int pire = Integer.MAX_VALUE;
|
|
|
|
for (int coups = 1; coups <= 3; coups++) {
|
|
int res = exploreMax(n - coups);
|
|
pire = Math.min(pire, res);
|
|
}
|
|
return pire;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int n = 8; // tas initial
|
|
int resultat = exploreMax(n);
|
|
System.out.println("Résultat pour n=" + n + " → " + resultat);
|
|
}
|
|
}
|