ajout TP1 presque finis
This commit is contained in:
34
TP1/MinimaxStop.java
Normal file
34
TP1/MinimaxStop.java
Normal file
@@ -0,0 +1,34 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user