TP finis
This commit is contained in:
58
TP1/MinimaxAlphaBeta.java
Normal file
58
TP1/MinimaxAlphaBeta.java
Normal file
@@ -0,0 +1,58 @@
|
||||
import java.util.*;
|
||||
|
||||
public class MinimaxAlphaBeta {
|
||||
public static int compteur = 0; // nombre d'états visités
|
||||
public static Random random = new Random();
|
||||
|
||||
public static int exploreMax(int n, int alpha, int beta) {
|
||||
compteur++;
|
||||
if (n <= 0) return -1;
|
||||
|
||||
int meilleur = Integer.MIN_VALUE;
|
||||
|
||||
// coups possibles
|
||||
List<Integer> coups = Arrays.asList(1, 2, 3);
|
||||
Collections.shuffle(coups, random); // ordre aléatoire
|
||||
|
||||
for (int c : coups) {
|
||||
int res = exploreMin(n - c, alpha, beta);
|
||||
meilleur = Math.max(meilleur, res);
|
||||
alpha = Math.max(alpha, meilleur);
|
||||
|
||||
// coupure alpha-beta
|
||||
if (alpha >= beta) break;
|
||||
}
|
||||
return meilleur;
|
||||
}
|
||||
|
||||
public static int exploreMin(int n, int alpha, int beta) {
|
||||
compteur++;
|
||||
if (n <= 0) return +1;
|
||||
|
||||
int pire = Integer.MAX_VALUE;
|
||||
|
||||
// coups possibles
|
||||
List<Integer> coups = Arrays.asList(1, 2, 3);
|
||||
Collections.shuffle(coups, random); // ordre aléatoire
|
||||
|
||||
for (int c : coups) {
|
||||
int res = exploreMax(n - c, alpha, beta);
|
||||
pire = Math.min(pire, res);
|
||||
beta = Math.min(beta, pire);
|
||||
|
||||
// coupure alpha-beta
|
||||
if (alpha >= beta) break;
|
||||
}
|
||||
return pire;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] tests = {5, 7, 13, 19, 31};
|
||||
|
||||
for (int n : tests) {
|
||||
compteur = 0;
|
||||
int resultat = exploreMax(n, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
System.out.println("n=" + n + " → " + resultat + " | états visités=" + compteur);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user