diff --git a/TP1/MinimaxAlphaBeta.java b/TP1/MinimaxAlphaBeta.java new file mode 100644 index 0000000..adadc5c --- /dev/null +++ b/TP1/MinimaxAlphaBeta.java @@ -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 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 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); + } + } +}