This commit is contained in:
2025-10-08 17:55:54 +02:00
parent 82b4e0ffd1
commit 584e8dc3c6
14 changed files with 60 additions and 21 deletions

View File

@@ -15,16 +15,22 @@ JCFLAGS = -encoding UTF-8 -implicit:none -cp $(OUT) -d $(OUT)
CLASSFILES = Pendu.class \ CLASSFILES = Pendu.class \
Partie.class \ Partie.class \
Fenetre.class \ Fenetre.class \
Dessin.class Dessin.class \
Mots.class \
Event.class \
LetterInputFilter.class \
MenuDifficulte.class \
Chronometre.class \
Score.class
# Dépendances # Dépendances
$(OUT)Pendu.class : $(IN)Pendu.java $(OUT)Partie.class $(OUT)Fenetre.class $(OUT)Pendu.class : $(IN)Pendu.java $(OUT)Partie.class $(OUT)Fenetre.class $(OUT)Event.class $(OUT)MenuDifficulte.class $(OUT)Score.class
$(JC) $(JCFLAGS) $< $(JC) $(JCFLAGS) $<
$(OUT)Partie.class : $(IN)Partie.java $(OUT)Mots.class $(OUT)Partie.class : $(IN)Partie.java $(OUT)Mots.class
$(JC) $(JCFLAGS) $< $(JC) $(JCFLAGS) $<
$(OUT)Fenetre.class : $(IN)Fenetre.java $(OUT)Partie.class $(OUT)Dessin.class $(OUT)Fenetre.class : $(IN)Fenetre.java $(OUT)Partie.class $(OUT)Dessin.class $(OUT)Chronometre.class $(OUT)Score.class
$(JC) $(JCFLAGS) $< $(JC) $(JCFLAGS) $<
$(OUT)Dessin.class : $(IN)Dessin.java $(OUT)Dessin.class : $(IN)Dessin.java
@@ -33,6 +39,21 @@ $(OUT)Dessin.class : $(IN)Dessin.java
$(OUT)Mots.class : $(IN)Mots.java $(OUT)Mots.class : $(IN)Mots.java
$(JC) $(JCFLAGS) $< $(JC) $(JCFLAGS) $<
$(OUT)Event.class : $(IN)Event.java $(OUT)Fenetre.class $(OUT)LetterInputFilter.class
$(JC) $(JCFLAGS) $<
$(OUT)LetterInputFilter.class : $(IN)LetterInputFilter.java $(OUT)Fenetre.class
$(JC) $(JCFLAGS) $<
$(OUT)MenuDifficulte.class : $(IN)MenuDifficulte.java
$(JC) $(JCFLAGS) $<
$(OUT)Chronometre.class : $(IN)Chronometre.java
$(JC) $(JCFLAGS) $<
$(OUT)Score.class : $(IN)Score.java
$(JC) $(JCFLAGS) $<
# Commandes # Commandes
Pendu : $(OUT)Pendu.class Pendu : $(OUT)Pendu.class

BIN
bin/Chronometre.class Normal file

Binary file not shown.

BIN
bin/Dessin.class Normal file

Binary file not shown.

BIN
bin/Event.class Normal file

Binary file not shown.

BIN
bin/Fenetre.class Normal file

Binary file not shown.

BIN
bin/LetterInputFilter.class Normal file

Binary file not shown.

BIN
bin/MenuDifficulte.class Normal file

Binary file not shown.

BIN
bin/Mots.class Normal file

Binary file not shown.

BIN
bin/Partie.class Normal file

Binary file not shown.

Binary file not shown.

BIN
bin/Pendu.class Normal file

Binary file not shown.

BIN
bin/Score.class Normal file

Binary file not shown.

View File

@@ -10,7 +10,7 @@ import java.util.Random;
*/ */
public class Partie { public class Partie {
//Contantes //Contantes
private static final byte REMAININGTRY = 11 ; private static final byte REMAININGTRY = 6 ;
private static final byte CARACTERCODESHIFT = 65 ; //Décalage ASCI > 'A' private static final byte CARACTERCODESHIFT = 65 ; //Décalage ASCI > 'A'
//Attributs //Attributs

View File

@@ -6,7 +6,7 @@ import java.util.function.Consumer;
* Lie Fenetre (vue) et Partie (logique) via un handler. * Lie Fenetre (vue) et Partie (logique) via un handler.
* Met à jour le dessin du pendu à chaque erreur. * Met à jour le dessin du pendu à chaque erreur.
* *
* @version 1.3 * @version 1.4
* author Adrien * author Adrien
* Date : 08-10-2025 * Date : 08-10-2025
* Licence : * Licence :
@@ -27,36 +27,54 @@ public class Pendu {
Partie partie = new Partie(); Partie partie = new Partie();
// Affichage initial du mot masqué (via Partie) // Affichage initial du mot masqué (construit ici)
fenetre.getWordLabel().setText(partie.getMaskedWord()); fenetre.getWordLabel().setText(buildMaskedWord(partie));
// Stage initial (0 erreur) // Stage initial (0 erreur)
if (fenetre.getDrawZone() instanceof Dessin) { if (fenetre.getDrawZone() instanceof Dessin) {
((Dessin) fenetre.getDrawZone()).setStage(0); ((Dessin) fenetre.getDrawZone()).setStage(0);
} }
// Handler : applique Partie puis met à jour l'UI (mot + dessin) // On mémorise les essais initiaux pour calculer les erreurs = initialTries - remainingTry
Consumer<Character> handler = new GameLetterHandler(fenetre, partie); final int initialTries = partie.getRemainingTry();
// Handler : applique Partie puis met à jour l'UI (mot + dessin + score)
Consumer<Character> handler = new GameLetterHandler(fenetre, partie, initialTries);
// Branchement des événements clavier/bouton // Branchement des événements clavier/bouton
new Event(fenetre, handler); new Event(fenetre, handler);
} }
/** Construit la chaîne "_ _ A _ _" à partir de l'état de Partie (sans modifier Partie). */
private static String buildMaskedWord(Partie partie) {
char[] word = partie.getSecretWord();
boolean[] found = partie.getFoundLetters();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < word.length; i++) {
sb.append(found[i] ? word[i] : '_');
if (i < word.length - 1) sb.append(' ');
}
return sb.toString();
}
/** /**
* Handler de lettres : * Handler de lettres :
* - applique Partie.isAlreadyEntries * - applique Partie.isAlreadyEntries
* - met à jour le mot affiché (Partie#getMaskedWord) * - met à jour le mot affiché
* - calcule le stage = MAX_STAGE - remainingTry et met à jour le Dessin * - calcule errors = initialTries - remainingTry, puis stage = min(errors, Dessin.MAX_STAGE)
* - met à jour le score
* - gère la fin de partie * - gère la fin de partie
*/ */
private static class GameLetterHandler implements Consumer<Character> { private static class GameLetterHandler implements Consumer<Character> {
private final Fenetre fenetre; private final Fenetre fenetre;
private final Partie partie; private final Partie partie;
private final int initialTries; // essais au démarrage (peut être 11 avec ta Partie)
private final Dessin dessinPanel; private final Dessin dessinPanel;
GameLetterHandler(Fenetre fenetre, Partie partie) { GameLetterHandler(Fenetre fenetre, Partie partie, int initialTries) {
this.fenetre = fenetre; this.fenetre = fenetre;
this.partie = partie; this.partie = partie;
this.initialTries = initialTries;
this.dessinPanel = (fenetre.getDrawZone() instanceof Dessin) this.dessinPanel = (fenetre.getDrawZone() instanceof Dessin)
? (Dessin) fenetre.getDrawZone() : null; ? (Dessin) fenetre.getDrawZone() : null;
} }
@@ -66,20 +84,20 @@ public class Pendu {
boolean alreadyPlayed = partie.isAlreadyEntries(ch); boolean alreadyPlayed = partie.isAlreadyEntries(ch);
// Mise à jour du mot // Mise à jour du mot
fenetre.getWordLabel().setText(partie.getMaskedWord()); fenetre.getWordLabel().setText(buildMaskedWord(partie));
// Erreurs -> stage pour le dessin // Erreurs -> stage pour le dessin (borné à MAX_STAGE)
int errors = Math.max(0, initialTries - partie.getRemainingTry());
if (dessinPanel != null) { if (dessinPanel != null) {
int stage = Dessin.MAX_STAGE - partie.getRemainingTry(); int stage = Math.min(errors, Dessin.MAX_STAGE);
dessinPanel.setStage(stage); dessinPanel.setStage(stage);
} }
// Mise à jour du score courant (erreurs + temps actuel) // Mise à jour du score courant (si tu utilises Score + Chronometre)
long elapsed = (fenetre.getChronometre() != null) long elapsed = (fenetre.getChronometre() != null)
? fenetre.getChronometre().getElapsedMillis() : 0L; ? fenetre.getChronometre().getElapsedMillis() : 0L;
int errors = Dessin.MAX_STAGE - partie.getRemainingTry();
int score = Score.compute(errors, elapsed);
if (fenetre.getScoreLabel() != null) { if (fenetre.getScoreLabel() != null) {
int score = Score.compute(errors, elapsed);
fenetre.getScoreLabel().setText("Score : " + score); fenetre.getScoreLabel().setText("Score : " + score);
} }
@@ -94,10 +112,10 @@ public class Pendu {
if (fenetre.getChronometre() != null) { if (fenetre.getChronometre() != null) {
fenetre.getChronometre().stop(); fenetre.getChronometre().stop();
} }
// Score final recalculé (au cas où une seconde vient de passer) // Score final (si Score/Chronometre présents)
long finalElapsed = (fenetre.getChronometre() != null) long finalElapsed = (fenetre.getChronometre() != null)
? fenetre.getChronometre().getElapsedMillis() : elapsed; ? fenetre.getChronometre().getElapsedMillis() : elapsed;
int finalErrors = Dessin.MAX_STAGE - partie.getRemainingTry(); int finalErrors = Math.max(0, initialTries - partie.getRemainingTry());
int finalScore = Score.compute(finalErrors, finalElapsed); int finalScore = Score.compute(finalErrors, finalElapsed);
boolean win = !fenetre.getWordLabel().getText().contains("_"); boolean win = !fenetre.getWordLabel().getText().contains("_");
@@ -105,7 +123,7 @@ public class Pendu {
? "Bravo ! Mot trouvé : " ? "Bravo ! Mot trouvé : "
: "Perdu ! Le mot était : ") : "Perdu ! Le mot était : ")
+ String.valueOf(partie.getSecretWord()) + String.valueOf(partie.getSecretWord())
+ "\nScore : " + finalScore; + (fenetre.getScoreLabel() != null ? "\nScore : " + finalScore : "");
JOptionPane.showMessageDialog(fenetre.getWindow(), msg); JOptionPane.showMessageDialog(fenetre.getWindow(), msg);