138 lines
4.0 KiB
Java
138 lines
4.0 KiB
Java
package com.bernard.crapoyams;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class CrapoyamsMotor {
|
|
public static int[][] tableDesGains = {
|
|
/*Paire*/{0,100,100,200,200,300},
|
|
/*2Paire*/{100,200,300,400,500,600},
|
|
/*Brelan*/{100,300,400,600,700,900},
|
|
/*Carré*/{200,400,600,800,1000,1200},
|
|
/*Full*/{200,500,700,1000,1200,1500},
|
|
/*Petite suite*/{5000},
|
|
/*Grande suite*/{25000},
|
|
/*Quinte*/{60000,60000,60000,60000,60000,60000}
|
|
};
|
|
public static int déFaces = 6;
|
|
|
|
public static void main(String[] args) {
|
|
int nombreLancers = 100000000;
|
|
int[] vals = new int[nombreLancers];
|
|
for (int n = 1;n < nombreLancers; n++) {
|
|
int[] dés = new int[] {
|
|
(int)(Math.random()*6+1),
|
|
(int)(Math.random()*6+1),
|
|
(int)(Math.random()*6+1),
|
|
(int)(Math.random()*6+1),
|
|
(int)(Math.random()*6+1)
|
|
};
|
|
vals[n] = gain(dés);
|
|
System.out.println(n+":"+Arrays.toString(dés)+"->"+vals[n]);
|
|
}
|
|
System.out.println("==>"+moyenne(vals));
|
|
}
|
|
|
|
public static int évaluer(int[] dés) {
|
|
double[] éspérances = new double[0b100000];
|
|
for (int i = 0; i < éspérances.length; i++)
|
|
éspérances[i] = espérance(dés, i);
|
|
List<Integer> bestMélanges = new ArrayList<>();
|
|
double perfection = highest(éspérances);
|
|
int pos;
|
|
while((pos = indexOf(éspérances, perfection)) != -1) {
|
|
éspérances[pos] = Double.NaN;
|
|
bestMélanges.add(pos);
|
|
}
|
|
System.out.println(perfection);
|
|
return bestMélanges.get(0);
|
|
}
|
|
|
|
public static double espérance(int[] dés, int mélange) {
|
|
if(mélange == 0b00000) {//Aucun mélange, donc l'espérance est le gain réel
|
|
double gain = gain(dés);
|
|
//System.out.println(Arrays.toString(dés)+" -> " + gain);
|
|
return gain;
|
|
}else {
|
|
int pos = 0;
|
|
while((mélange >> pos) % 2 == 0)pos++;
|
|
double[] espérances = new double[déFaces];
|
|
mélange -= 0b1 << pos;
|
|
int[] shuffledDés = Arrays.copyOf(dés, dés.length);
|
|
for (int tirage = 1; tirage < déFaces + 1; tirage++) {
|
|
shuffledDés[pos] = tirage;
|
|
espérances[tirage-1] = espérance(shuffledDés, mélange);
|
|
}
|
|
return moyenne(espérances);
|
|
|
|
}
|
|
}
|
|
|
|
public static final int gain(int[] dés) {
|
|
Arrays.sort(dés);
|
|
int[] counts = new int[déFaces];
|
|
Arrays.fill(counts, 0);
|
|
for (int i : dés) counts[i-1]++;
|
|
int pos = -1;
|
|
|
|
/* Traitement des cas */
|
|
if(Arrays.equals(dés, new int[] {1,2,3,4,5}))
|
|
return tableDesGains[6][0];
|
|
else if(Arrays.equals(dés, new int[] {0,1,2,3,4}))
|
|
return tableDesGains[5][0];
|
|
else if((pos = lastIndexOf(counts,5)) != -1)
|
|
return tableDesGains[7][pos];
|
|
else if((pos = lastIndexOf(counts,4)) != -1)
|
|
return tableDesGains[3][pos];
|
|
else if((pos = lastIndexOf(counts,3)) != -1)
|
|
if(lastIndexOf(counts,2) == -1)
|
|
return tableDesGains[2][pos];
|
|
else
|
|
return tableDesGains[4][pos];
|
|
else if((pos = lastIndexOf(counts,2)) != -1)
|
|
if(indexOf(counts,2) == pos)
|
|
return tableDesGains[0][pos];
|
|
else
|
|
return tableDesGains[1][pos];
|
|
else
|
|
return 0;//Aucune combinaison
|
|
}
|
|
|
|
public static final int lastIndexOf(int[] array, int element) {
|
|
for (int i = array.length -1; i >= 0; i--)
|
|
if(element == array[i])return i;
|
|
return -1;
|
|
}
|
|
public static final int indexOf(int[] array, int element) {
|
|
for (int i = 0; i < array.length; i++)
|
|
if(element == array[i])return i;
|
|
return -1;
|
|
}
|
|
public static final int indexOf(double[] array, double element) {
|
|
for (int i = 0; i < array.length; i++)
|
|
if(element == array[i])return i;
|
|
return -1;
|
|
}
|
|
public static final double moyenne(double[] valeurs) {
|
|
double moyenne = 0;
|
|
for (double d : valeurs)
|
|
moyenne += d;
|
|
return moyenne/((double)valeurs.length);
|
|
}
|
|
public static final double moyenne(int[] valeurs) {
|
|
double moyenne = 0;
|
|
for (double d : valeurs)
|
|
moyenne += d;
|
|
return moyenne/((double)valeurs.length);
|
|
}
|
|
public static final double highest(double[] valeurs) {
|
|
if(valeurs.length == 0)throw new IllegalArgumentException("valeurs ne doit pas etre vide !");
|
|
double current = valeurs[0];
|
|
for (double d : valeurs)
|
|
if(d > current)
|
|
current = d;
|
|
return current;
|
|
}
|
|
}
|