From 3608d5e8cb7fcf2a9a997b41bfdba66cddb625fa Mon Sep 17 00:00:00 2001 From: Axel PIETROIS Date: Fri, 20 Sep 2024 15:14:31 +0200 Subject: [PATCH] Update MiniMaxPetiteOpti.java --- MiniMaxPetiteOpti.java | 80 +++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/MiniMaxPetiteOpti.java b/MiniMaxPetiteOpti.java index 2d0cf9c..92e2c31 100644 --- a/MiniMaxPetiteOpti.java +++ b/MiniMaxPetiteOpti.java @@ -1,22 +1,68 @@ public class MiniMaxPetiteOpti { - public static int depart = 5; + public static int[] tests = {5,11,17,42,100}; - private static int compteur; + private static int compteur; - public static void main(String[] args) { - int n = depart; - compteur = 0; - int s = Nim(n); - if (s == 1){ - System.out.print("Gagné"); - } else { - System.out.print("Perdu"); - } - System.out.println(" Compteur = "+compteur); - } + static long debut; - public static int Nim(int n){ - return exploreMax(n); - } -} + public static void main(String[] args) { + + for(int n: tests) { + debut = System.nanoTime(); + compteur = 0; + System.out.print(n + " allumettes : "); + int s = Nim(n); + if (s == 1) { + System.out.print("Gagné ;"); + } else { + System.out.print("Perdu ;"); + } + long fin = System.nanoTime(); + System.out.print(" Temps écoulé (ms) : "+((fin-debut)/1000000)+" ;"); + System.out.println(" Compteur = " + compteur); + } + } + + public static int Nim(int n){ + return exploreMax(n); + } + + + + /* + Vérifie les issues des coups jouables par le J1 et renvoie 1 si le J1 a une opportunité de gagner a coup sur + */ + public static int exploreMax(int allumette) { + compteur++; + if ((System.nanoTime()-debut)/1000000 > 100000 ){ + System.out.println("TimeOut au bout de 100 secondes"); + System.exit(-1); + } + for (int i=0;allumette>1&&i<3;i++){ + allumette--; + int v=exploreMin(allumette); + if (v==1){ + return v; + } + + } + return -1; + } + + + /* + Vérifie les issues possibles des coups du J2 et renvoie -1 si le J2 a une opportunité de gagner a coup sur + */ + public static int exploreMin(int allumette){ + compteur++; + for (int i=0;allumette>1&&i<3;i++){ + allumette--; + int v=exploreMax(allumette); + if (v==-1){ + return v; + } + } + return 1; + } +} \ No newline at end of file