35 lines
988 B
Java
35 lines
988 B
Java
public class MinimaxStop {
|
|
|
|
// Minimax avec détection de victoire immédiate
|
|
|
|
public static int exploreMax(int n) {
|
|
if (n <= 0) return -1;
|
|
int meilleur = Integer.MIN_VALUE;
|
|
|
|
for (int coups = 1; coups <= 3; coups++) {
|
|
int res = exploreMin(n - coups);
|
|
if (res == +1) return +1; // victoire immédiate
|
|
meilleur = Math.max(meilleur, res);
|
|
}
|
|
return meilleur;
|
|
}
|
|
|
|
public static int exploreMin(int n) {
|
|
if (n <= 0) return +1;
|
|
int pire = Integer.MAX_VALUE;
|
|
|
|
for (int coups = 1; coups <= 3; coups++) {
|
|
int res = exploreMax(n - coups);
|
|
if (res == -1) return -1; // défaite immédiate pour Max
|
|
pire = Math.min(pire, res);
|
|
}
|
|
return pire;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int n = 7;
|
|
int resultat = exploreMax(n);
|
|
System.out.println("Résultat pour n=" + n + " → " + resultat);
|
|
}
|
|
}
|