From 048f0edf6d4e3cae99ae9bb9ac6c16d9caf8ee88 Mon Sep 17 00:00:00 2001 From: sehl Date: Sun, 29 Mar 2026 14:52:59 +0200 Subject: [PATCH] ajustement controleur + finPartieActivity --- src/main/java/sae/chuzzle/Controleur.java | 100 ++++++++++++------ .../java/sae/chuzzle/FinPartieActivity.java | 47 ++++++-- 2 files changed, 105 insertions(+), 42 deletions(-) diff --git a/src/main/java/sae/chuzzle/Controleur.java b/src/main/java/sae/chuzzle/Controleur.java index 8715204..ae9d8f7 100644 --- a/src/main/java/sae/chuzzle/Controleur.java +++ b/src/main/java/sae/chuzzle/Controleur.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; +import android.widget.Button; import android.widget.TextView; import android.widget.Toast; @@ -23,6 +24,9 @@ public class Controleur { private Objectif objectifActuel; private TextView tvObjectif; private TextView tvNbObjectifs; + private TextView tvCles; + private Button btnReinitialisationObjectif; + private View layoutCles; public Controleur(Activity activite, EtatJeu etatJeu, VueGrille vueGrille, long graine, TextView tvScore, TextView tvCoups, @@ -40,11 +44,20 @@ public class Controleur { if (hardMode) { this.gestionnaireObjectifs = new GestionnaireObjectifs(graine); - this.tvObjectif = activite.findViewById(R.id.tvObjectif); + this.tvObjectif = activite.findViewById(R.id.tvObjectif); this.tvNbObjectifs = activite.findViewById(R.id.tvNbObjectifs); - - if (tvObjectif != null) tvObjectif.setVisibility(View.VISIBLE); + this.tvCles = activite.findViewById(R.id.tvCles); + this.layoutCles = activite.findViewById(R.id.layoutCles); + this.btnReinitialisationObjectif = activite.findViewById(R.id.btnReinitialisationObjectif); + + if (tvObjectif != null) tvObjectif.setVisibility(View.VISIBLE); if (tvNbObjectifs != null) tvNbObjectifs.setVisibility(View.VISIBLE); + if (layoutCles != null) layoutCles.setVisibility(View.VISIBLE); + + // Listener du bouton de réinitialisation + if (btnReinitialisationObjectif != null) { + btnReinitialisationObjectif.setOnClickListener(v -> reinitialiserObjectif()); + } } @@ -73,6 +86,11 @@ public class Controleur { * Centralise la logique pour être appelé par le bouton ET par le tactile. */ public void gererFinDeCoup() { + + // 1 clé tous les 5 coups + if (hardMode && etatJeu.obtenirNbCoups() % 5 == 0) { + gestionnaireObjectifs.ajouterCle(); + } // --- Logique Hard Mode if (hardMode && objectifActuel != null) { // 1. Décrémenter les coups de l'objectif @@ -102,38 +120,37 @@ public class Controleur { verifierFinDePartie(); } + /** + * Tente de réinitialiser l'objectif en cours en consommant 3 clés. + * Affiche un Toast si le joueur n'a pas assez de clés. + */ + public void reinitialiserObjectif() { + if (!hardMode || gestionnaireObjectifs == null) return; + if (gestionnaireObjectifs.consommerCles()) { + objectifActuel = gestionnaireObjectifs.genererObjectif(); + rafraichirAffichage(); + } else { + Toast.makeText(activite, "Pas assez de clés !", Toast.LENGTH_SHORT).show(); + } + } + + + /** + * Lancer l'écran de fin. + */ public void verifierFinDePartie() { if (etatJeu.estTerminee()) { - Intent intent = new Intent(activite, FinPartieActivity.class); - intent.putExtra("score", etatJeu.obtenirScore()); - intent.putExtra("nbCoups", etatJeu.obtenirNbCoups()); - intent.putExtra("graine", graine); - intent.putExtra("objectifsReussis", hardMode ? gestionnaireObjectifs.getNbObjectifsRealises() : 0); - if (hardMode && objectifActuel != null) - intent.putExtra("objectifDescription", objectifActuel.getDescription()); - - // Passer la grille finale (flatten) - int[][] grille = etatJeu.obtenirGrille(); - int nbLignes = grille.length; - int nbCols = grille[0].length; - int[] grillePlat = new int[nbLignes * nbCols]; - for (int l = 0; l < nbLignes; l++) - for (int c = 0; c < nbCols; c++) - grillePlat[l * nbCols + c] = grille[l][c]; - intent.putExtra("grille", grillePlat); - intent.putExtra("nbLignes", nbLignes); - intent.putExtra("nbCols", nbCols); - - // Passer les verrous (flatten) - boolean[][] verrous = etatJeu.obtenirVerrous(); - boolean[] verrouxPlat = new boolean[nbLignes * nbCols]; - for (int l = 0; l < nbLignes; l++) - for (int c = 0; c < nbCols; c++) - verrouxPlat[l * nbCols + c] = verrous[l][c]; - intent.putExtra("verrous", verrouxPlat); - - activite.startActivity(intent); + FinPartieActivity.demarrer( + activite, + etatJeu.obtenirScore(), + etatJeu.obtenirNbCoups(), + graine, + hardMode ? gestionnaireObjectifs.getNbObjectifsRealises() : 0, + (hardMode && objectifActuel != null) ? objectifActuel.getDescription() : null, + etatJeu.obtenirGrille(), + etatJeu.obtenirVerrous() + ); } } @@ -144,11 +161,24 @@ public class Controleur { vueGrille.definirVerrous(etatJeu.obtenirVerrous()); if (hardMode && objectifActuel != null) { - if (tvObjectif != null) tvObjectif.setText(objectifActuel.getDescription()); + if (tvObjectif != null) tvObjectif.setText(objectifActuel.getDescription()); if (tvNbObjectifs != null) tvNbObjectifs.setText("Objectifs réussis : " + gestionnaireObjectifs.getNbObjectifsRealises()); + + int nbCles = gestionnaireObjectifs.getNbCles(); + if (tvCles != null) { + tvCles.setText("🗝️ Clés : " + nbCles); + } + if (btnReinitialisationObjectif != null) { + // Grisé si 0 clé + float alpha; + if (nbCles >= 1) { + alpha = 1f; // visible + } else { + alpha = 0.4f; // grisé + } + btnReinitialisationObjectif.setAlpha(alpha); + } } - - } public void sauvegarderEtat(Bundle out) { diff --git a/src/main/java/sae/chuzzle/FinPartieActivity.java b/src/main/java/sae/chuzzle/FinPartieActivity.java index 2334df5..b7d66dd 100644 --- a/src/main/java/sae/chuzzle/FinPartieActivity.java +++ b/src/main/java/sae/chuzzle/FinPartieActivity.java @@ -8,12 +8,48 @@ import android.widget.TextView; public class FinPartieActivity extends Activity { + /** + * Méthode statique pour démarrer l'activité avec toutes les données nécessaires. + */ + public static void demarrer(Activity source, int score, int nbCoups, long graine, + int objectifsReussis, String objectifDescription, + int[][] grille, boolean[][] verrous) { + Intent intent = new Intent(source, FinPartieActivity.class); + intent.putExtra("score", score); + intent.putExtra("nbCoups", nbCoups); + intent.putExtra("graine", graine); + intent.putExtra("objectifsReussis", objectifsReussis); + intent.putExtra("objectifDescription", objectifDescription); + + // Aplatissement de la grille + int nbLignes = grille.length; + int nbCols = grille[0].length; + int[] grillePlat = new int[nbLignes * nbCols]; + for (int l = 0; l < nbLignes; l++) + for (int c = 0; c < nbCols; c++) + grillePlat[l * nbCols + c] = grille[l][c]; + + intent.putExtra("grille", grillePlat); + intent.putExtra("nbLignes", nbLignes); + intent.putExtra("nbCols", nbCols); + + // Aplatissement des verrous + boolean[] verrousPlat = new boolean[nbLignes * nbCols]; + for (int l = 0; l < nbLignes; l++) + for (int c = 0; c < nbCols; c++) + verrousPlat[l * nbCols + c] = verrous[l][c]; + + intent.putExtra("verrous", verrousPlat); + + source.startActivity(intent); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fin_partie); - // Récupérer les données passées par le Controleur + // Récupérer les données passées int score = getIntent().getIntExtra("score", 0); int nbCoups = getIntent().getIntExtra("nbCoups", 0); long graine = getIntent().getLongExtra("graine", 0L); @@ -41,7 +77,7 @@ public class FinPartieActivity extends Activity { int nbLignes = getIntent().getIntExtra("nbLignes", 6); int nbCols = getIntent().getIntExtra("nbCols", 6); int[] grillePlat = getIntent().getIntArrayExtra("grille"); - boolean[] verrouxPlat = getIntent().getBooleanArrayExtra("verrous"); + boolean[] verrousPlat = getIntent().getBooleanArrayExtra("verrous"); VueGrille vueGrilleFinale = findViewById(R.id.vueGrilleFinale); @@ -53,11 +89,11 @@ public class FinPartieActivity extends Activity { vueGrilleFinale.definirGrille(grille); } - if (verrouxPlat != null && verrouxPlat.length == nbLignes * nbCols) { + if (verrousPlat != null && verrousPlat.length == nbLignes * nbCols) { boolean[][] verrous = new boolean[nbLignes][nbCols]; for (int l = 0; l < nbLignes; l++) for (int c = 0; c < nbCols; c++) - verrous[l][c] = verrouxPlat[l * nbCols + c]; + verrous[l][c] = verrousPlat[l * nbCols + c]; vueGrilleFinale.definirVerrous(verrous); } @@ -71,9 +107,6 @@ public class FinPartieActivity extends Activity { }); } - // - - // RETOUR ARRIERE = menu principal - @Override public void onBackPressed() { Intent intent = new Intent(this, MenuActivity.class);