78 lines
2.3 KiB
Java
78 lines
2.3 KiB
Java
|
|
package sae.chuzzle;
|
||
|
|
|
||
|
|
import android.os.Bundle;
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Random;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Gère la génération aléatoire et le comptage des objectifs.
|
||
|
|
*/
|
||
|
|
public class GestionnaireObjectifs {
|
||
|
|
private final Random random;
|
||
|
|
private int nbObjectifsRealises = 0;
|
||
|
|
|
||
|
|
public GestionnaireObjectifs(long graine) {
|
||
|
|
this.random = new Random(graine);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Génère un nouvel objectif en respectant les probabilités de difficulté.
|
||
|
|
*/
|
||
|
|
public Objectif genererObjectif() {
|
||
|
|
List<Objectif> pool = new ArrayList<>();
|
||
|
|
int totalPoids = 0;
|
||
|
|
|
||
|
|
// M (nbCoupsMax) entre 2 et 5.
|
||
|
|
// N (nbSeriesCible) tel que M - N >= 1, donc N <= M - 1.
|
||
|
|
for (int m = 2; m <= 5; m++) {
|
||
|
|
for (int n = 1; n <= m - 1; n++) {
|
||
|
|
// Probabilité inversement proportionnelle à la facilité (M-N).
|
||
|
|
// Plus (M-N) est petit, plus c'est dur.
|
||
|
|
// Exemple : M=5, N=4 -> diff=1 (Dur). M=5, N=1 -> diff=4 (Facile).
|
||
|
|
int diff = m - n;
|
||
|
|
int poids = diff * diff; // On utilise le carré pour accentuer la différence de probabilité
|
||
|
|
|
||
|
|
for (int color = 0; color < 7; color++) {
|
||
|
|
pool.add(new Objectif(color, n, m, poids));
|
||
|
|
totalPoids += poids;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int tirage = random.nextInt(totalPoids);
|
||
|
|
int cumul = 0;
|
||
|
|
for (Objectif obj : pool) {
|
||
|
|
cumul += obj.getPoids();
|
||
|
|
if (tirage < cumul) {
|
||
|
|
return new Objectif(obj.getCouleur(), obj.getNbSeriesCible(), obj.getNbCoupsMax(), obj.getPoids());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return pool.get(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void incrementerReussites() {
|
||
|
|
|
||
|
|
nbObjectifsRealises++;
|
||
|
|
}
|
||
|
|
|
||
|
|
public int getNbObjectifsRealises() {
|
||
|
|
|
||
|
|
return nbObjectifsRealises;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void sauvegarder(Bundle out) {
|
||
|
|
out.putInt("nb_objectifs_total", nbObjectifsRealises);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void restaurer(Bundle in) {
|
||
|
|
if (in != null) {
|
||
|
|
nbObjectifsRealises = in.getInt("nb_objectifs_total", 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Note: La classe Objectif a été mise à jour pour inclure getNbSeriesCible et getNbCoupsMax.
|
||
|
|
*/
|