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