ajout TP1 presque finis

This commit is contained in:
James Boutaric
2025-09-11 11:37:24 +02:00
parent 001ee76228
commit bf9189502a
10 changed files with 246 additions and 44 deletions

View File

@@ -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;
}
}

BIN
TP1/MinimaxMemo.class Normal file

Binary file not shown.

43
TP1/MinimaxMemo.java Normal file
View File

@@ -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);
}
}

BIN
TP1/MinimaxProfondeur.class Normal file

Binary file not shown.

View File

@@ -0,0 +1,41 @@
public class MinimaxProfondeur {
// Minimax avec profondeur fixe
public static int PROFONDEUR_MAX = 4;
public static int exploreMax(int n, int profondeur) {
if (n <= 0) return -1;
if (profondeur == 0) return evaluer(n);
int meilleur = Integer.MIN_VALUE;
for (int coups = 1; coups <= 3; coups++) {
int res = exploreMin(n - coups, profondeur - 1);
meilleur = Math.max(meilleur, res);
}
return meilleur;
}
public static int exploreMin(int n, int profondeur) {
if (n <= 0) return +1;
if (profondeur == 0) return evaluer(n);
int pire = Integer.MAX_VALUE;
for (int coups = 1; coups <= 3; coups++) {
int res = exploreMax(n - coups, profondeur - 1);
pire = Math.min(pire, res);
}
return pire;
}
// Fonction hsimple : perdant si n % 4 == 0
public static int evaluer(int n) {
return (n % 4 == 0) ? -1 : +1;
}
public static void main(String[] args) {
int n = 8;
int profondeur = PROFONDEUR_MAX;
int resultat = exploreMax(n, profondeur);
System.out.println("Résultat (profondeur fixe) pour n=" + n + "" + resultat);
}
}

BIN
TP1/MinimaxSimple.class Normal file

Binary file not shown.

33
TP1/MinimaxSimple.java Normal file
View 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);
}
}

BIN
TP1/MinimaxStop.class Normal file

Binary file not shown.

34
TP1/MinimaxStop.java Normal file
View File

@@ -0,0 +1,34 @@
public class MinimaxStop {
// Minimax avec détection de victoire immédiate
public static int exploreMax(int n) {
if (n <= 0) return -1;
int meilleur = Integer.MIN_VALUE;
for (int coups = 1; coups <= 3; coups++) {
int res = exploreMin(n - coups);
if (res == +1) return +1; // victoire immédiate
meilleur = Math.max(meilleur, res);
}
return meilleur;
}
public static int exploreMin(int n) {
if (n <= 0) return +1;
int pire = Integer.MAX_VALUE;
for (int coups = 1; coups <= 3; coups++) {
int res = exploreMax(n - coups);
if (res == -1) return -1; // défaite immédiate pour Max
pire = Math.min(pire, res);
}
return pire;
}
public static void main(String[] args) {
int n = 7;
int resultat = exploreMax(n);
System.out.println("Résultat pour n=" + n + "" + resultat);
}
}