From bf9189502a3febe8ff15066a1e8050bf98324921 Mon Sep 17 00:00:00 2001 From: James Boutaric Date: Thu, 11 Sep 2025 11:37:24 +0200 Subject: [PATCH] ajout TP1 presque finis --- README.md | 95 ++++++++++++++++++++++++++++++++++++ TP1/Minimax.java | 44 ----------------- TP1/MinimaxMemo.class | Bin 0 -> 1542 bytes TP1/MinimaxMemo.java | 43 ++++++++++++++++ TP1/MinimaxProfondeur.class | Bin 0 -> 1530 bytes TP1/MinimaxProfondeur.java | 41 ++++++++++++++++ TP1/MinimaxSimple.class | Bin 0 -> 1283 bytes TP1/MinimaxSimple.java | 33 +++++++++++++ TP1/MinimaxStop.class | Bin 0 -> 1310 bytes TP1/MinimaxStop.java | 34 +++++++++++++ 10 files changed, 246 insertions(+), 44 deletions(-) create mode 100644 README.md delete mode 100644 TP1/Minimax.java create mode 100644 TP1/MinimaxMemo.class create mode 100644 TP1/MinimaxMemo.java create mode 100644 TP1/MinimaxProfondeur.class create mode 100644 TP1/MinimaxProfondeur.java create mode 100644 TP1/MinimaxSimple.class create mode 100644 TP1/MinimaxSimple.java create mode 100644 TP1/MinimaxStop.class create mode 100644 TP1/MinimaxStop.java diff --git a/README.md b/README.md new file mode 100644 index 0000000..c140b84 --- /dev/null +++ b/README.md @@ -0,0 +1,95 @@ +# 📘 README – Minimax pour le jeu de Nim + +## 🎯 Objectif +Ce TP a pour but d’implĂ©menter l’algorithme **Minimax** dans le jeu de **Nim** (variante Ă  1 tas oĂč l’on peut retirer 1, 2 ou 3 objets). +On dĂ©veloppe plusieurs versions de l’algorithme pour comprendre ses optimisations possibles. + +--- + +## 📂 Fichiers +Le projet contient **4 fichiers Java**, correspondant chacun Ă  une variante de Minimax : + +1. **`MinimaxSimple.java`** + - Version de base. + - Explore toutes les possibilitĂ©s jusqu’à la fin de la partie. + - Pas d’optimisation → explore parfois plus que nĂ©cessaire. + +2. **`MinimaxEarlyStop.java`** + - Optimisation par **arrĂȘt anticipĂ©** . + - Si un coup gagnant (pour Max) ou perdant (pour Min) est trouvĂ©, la recherche s’arrĂȘte immĂ©diatement. + - MĂȘme rĂ©sultat que la version simple, mais plus rapide. +```bash +boutaric@MacBook-James TP1 % time java MinimaxSimple +RĂ©sultat pour n=8 → -1 +java MinimaxSimple 0,03s user 0,03s system 49% cpu 0,136 total +``` +```bash +boutaric@MacBook-James TP1 % time java MinimaxStop +RĂ©sultat pour n=7 → 1 +java MinimaxStop 0,04s user 0,02s system 93% cpu 0,062 total +``` + + +3. **`MinimaxProfondeur.java`** + - Variante avec **profondeur maximale fixĂ©e**. + - Quand la profondeur limite est atteinte, on utilise une **fonction d’évaluation heuristique**. + - Heuristique utilisĂ©e : une position est perdante si `n % 4 == 0`, sinon gagnante. + +4. **`MinimaxMemo.java`** + - Optimisation par **mĂ©moĂŻsation**. + - Utilise deux tableaux (`memoMax` et `memoMin`) pour stocker les rĂ©sultats dĂ©jĂ  calculĂ©s. + - Évite les recalculs et accĂ©lĂšre l’algorithme pour des valeurs de `n` plus grandes. + +--- + +## ▶ ExĂ©cution +Compiler et lancer chaque fichier sĂ©parĂ©ment : + +```bash +javac MinimaxSimple.java +java MinimaxSimple +``` + +```bash +javac MinimaxEarlyStop.java +java MinimaxEarlyStop +``` + +```bash +javac MinimaxProfondeur.java +java MinimaxProfondeur +``` + +```bash +javac MinimaxMemo.java +java MinimaxMemo +``` + +Chaque programme affiche le rĂ©sultat pour une valeur donnĂ©e de `n` (taille initiale du tas). +- `+1` → Position gagnante pour Max (le joueur qui commence). +- `-1` → Position perdante pour Max. + +--- + +## 📊 Exemple de rĂ©sultats +Pour les premiĂšres positions de Nim (1 tas) : + +| n | RĂ©sultat | Commentaire | +|----|----------|-------------| +| 1 | +1 | Max peut tout prendre et gagne | +| 2 | +1 | Max peut tout prendre et gagne | +| 3 | +1 | Max peut tout prendre et gagne | +| 4 | -1 | Position perdante (Min gagne) | +| 5 | +1 | Max enlĂšve 1 → reste 4 (perdant pour Min) | +| 6 | +1 | Max enlĂšve 2 → reste 4 | +| 7 | +1 | Max enlĂšve 3 → reste 4 | +| 8 | -1 | Position perdante | +| ...| ... | ... | + +--- + +## 🧠 Conclusion +- Le **Minimax simple** permet de comprendre la logique de base. +- Le **Minimax stop** amĂ©liore la performance sans changer le rĂ©sultat. +- La **profondeur fixe + heuristique** est utile si l’arbre est trop grand. +- La **mĂ©moĂŻsation** accĂ©lĂšre Ă©normĂ©ment le calcul pour des valeurs de `n` Ă©levĂ©es. diff --git a/TP1/Minimax.java b/TP1/Minimax.java deleted file mode 100644 index aa3f45f..0000000 --- a/TP1/Minimax.java +++ /dev/null @@ -1,44 +0,0 @@ - -public class Minimax { - - public static int pire; - public static int res; - public static int meilleurRes; - - public static void main(String[] args) { - System.out.println(ExploreMin(1)); - - } - - public static int ExploreMin(int n){ - if(n <= 0) { - return -1; - } - - pire = 2; - - for (int coups = 1; coups <= 3; coups++) { - res = ExploreMax(n - coups); - if(res < pire){ - pire = res; - } - } - - return pire; - } - - public static int ExploreMax(int n){ - if(n <= 0) { - return -1; - } - - for (int coups = 1; coups <= 3; coups++) { - res = ExploreMin(n - coups); - if(res > meilleurRes){ - meilleurRes = res; - } - } - - return meilleurRes; - } -} \ No newline at end of file diff --git a/TP1/MinimaxMemo.class b/TP1/MinimaxMemo.class new file mode 100644 index 0000000000000000000000000000000000000000..4fccfaa097c002d53096d365feb2e2d330f0ca53 GIT binary patch literal 1542 zcmX^0Z`VEs1_nolAT98VB5Tnu~+{Ok+@JPd*iLW~T87%CVUI5P7}!V+^zQ^B@*GB+?VFmN$&Fo^On zh%tyWGH|6<6y)R=rTS*(F*2}dcxrlbF-S5_Nl|8Ax-}PrIfDf|gC!4x6@xV+gD^NuP%U6&U@rh!my^fHAgTdz0yx&8MrejH zGO#%3r=&76@cU%urTUfTCZ!gIBqrsgGBWT6mn0@<`z97Z`7F7Kp!g8eK#p}JgSdk8 zON)|I-7-O%1(1@A9>`^k3<6I1`6b0AMTrHzsU;ctDa9NNu8a&CL5Eism*$itmMCcC z9$uN7e|UXyVo7Fxo~A-Uerb_Ho-L!oqqa#3jQng2?pzEW44&)^UZ5cJW@JzS84O9r znR#XT*{S*v=RiWyEit(yzo?RtVFt3#p$Z{R@JP%{$w@6%@yXB6E-kRe3?L*C6pbO3 z1*uq+BZmwmV_737I;auOi8(pJnI);#j0`-Ud3mWt&N+$2#i_;Y41SCZsu+I4=2Uiu z07eEm{7M-a*dYF5P-Ng>U}9ikU}E3|WoQNl22N0lXJBApVPFK62n>u2HVh05vJ8w2 zj0_A6tXkU{7&kI7FfcOMGB7Z(fdv^EI6=7qs?~CjXQ_dmr3rVIk`#7li7~J-h%<IlGPp4?GPp4KfQwT`244n$hCl{M0LUU^Y5)KL literal 0 HcmV?d00001 diff --git a/TP1/MinimaxMemo.java b/TP1/MinimaxMemo.java new file mode 100644 index 0000000..15300b2 --- /dev/null +++ b/TP1/MinimaxMemo.java @@ -0,0 +1,43 @@ +public class MinimaxMemo { + + // Minimax avec mĂ©mo (on Ă©vite de recalculer les mĂȘmes positions) + public static Integer[] memoMax; + public static Integer[] memoMin; + + public static int exploreMax(int n) { + if (n <= 0) return -1; + if (memoMax[n] != null) return memoMax[n]; + + int meilleur = Integer.MIN_VALUE; + for (int coups = 1; coups <= 3; coups++) { + int res = exploreMin(n - coups); + meilleur = Math.max(meilleur, res); + } + + memoMax[n] = meilleur; + return meilleur; + } + + public static int exploreMin(int n) { + if (n <= 0) return +1; + if (memoMin[n] != null) return memoMin[n]; + + int pire = Integer.MAX_VALUE; + for (int coups = 1; coups <= 3; coups++) { + int res = exploreMax(n - coups); + pire = Math.min(pire, res); + } + + memoMin[n] = pire; + return pire; + } + + public static void main(String[] args) { + int n = 25; // taille du tas initial + memoMax = new Integer[n + 1]; + memoMin = new Integer[n + 1]; + + int resultat = exploreMax(n); + System.out.println("RĂ©sultat pour n=" + n + " → " + resultat); + } +} diff --git a/TP1/MinimaxProfondeur.class b/TP1/MinimaxProfondeur.class new file mode 100644 index 0000000000000000000000000000000000000000..c7e77c83850dd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