forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
7 Commits
87909bbe35
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e70bcbb63 | |||
| e649d30b33 | |||
| 584e8dc3c6 | |||
| 82b4e0ffd1 | |||
| 9cb9088b81 | |||
| 77b3158262 | |||
| 0f7c1f6b71 |
32
Makefile
32
Makefile
@@ -15,21 +15,45 @@ JCFLAGS = -encoding UTF-8 -implicit:none -cp $(OUT) -d $(OUT)
|
||||
CLASSFILES = Pendu.class \
|
||||
Partie.class \
|
||||
Fenetre.class \
|
||||
Dessin.class
|
||||
Dessin.class \
|
||||
Mots.class \
|
||||
Event.class \
|
||||
LetterInputFilter.class \
|
||||
MenuDifficulte.class \
|
||||
Chronometre.class \
|
||||
Score.class
|
||||
|
||||
# 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) $<
|
||||
|
||||
$(OUT)Partie.class : $(IN)Partie.java
|
||||
$(OUT)Partie.class : $(IN)Partie.java $(OUT)Mots.class
|
||||
$(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) $<
|
||||
|
||||
$(OUT)Dessin.class : $(IN)Dessin.java
|
||||
$(JC) $(JCFLAGS) $<
|
||||
|
||||
$(OUT)Mots.class : $(IN)Mots.java
|
||||
$(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
|
||||
Pendu : $(OUT)Pendu.class
|
||||
|
||||
|
||||
58
src/Mots.java
Normal file
58
src/Mots.java
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
/**
|
||||
* La classe <code>Mots</code>
|
||||
*
|
||||
* @version 1.0
|
||||
* @author Aurélien
|
||||
* Date : 08-10-25
|
||||
* Licence :
|
||||
*/
|
||||
public final class Mots {
|
||||
//Attributs
|
||||
public static final short dictionarysize = 32 ;
|
||||
public static final String[] dictionary = {
|
||||
"Magnifique",
|
||||
"Etoile",
|
||||
"Voyage",
|
||||
"Biscuit",
|
||||
"Refrigerateur",
|
||||
"Courage",
|
||||
"Avion",
|
||||
"Explorateur",
|
||||
"Montagne",
|
||||
"Philosophie",
|
||||
"Lumiere",
|
||||
"Ethernet",
|
||||
"Architecture",
|
||||
"Ocean",
|
||||
"Liberte",
|
||||
"Aventure",
|
||||
"Cerise",
|
||||
"Harmonieux",
|
||||
"Informatique",
|
||||
"Pluie",
|
||||
"Equilibriste",
|
||||
"Papillon",
|
||||
"Saisons",
|
||||
"Liberte",
|
||||
"Alphabet",
|
||||
"Musique",
|
||||
"Translucent",
|
||||
"Passion",
|
||||
"Etreindre",
|
||||
"Poetique",
|
||||
"Serenite",
|
||||
"Révolution"
|
||||
};
|
||||
|
||||
//Constructeur
|
||||
private Mots() { //N'a pas pour but d'être instanciée
|
||||
throw new UnsupportedOperationException("The \"Fichier\" class cannot be instanced !");
|
||||
}
|
||||
//Méthodes
|
||||
|
||||
//Affichage
|
||||
public String toString() {
|
||||
return "" ;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* La classe <code>Partie</code>
|
||||
*
|
||||
@@ -24,23 +26,25 @@ public class Partie {
|
||||
this.wordsize = (byte) secretword.length ;
|
||||
this.foundletters = new boolean[wordsize] ;
|
||||
}
|
||||
|
||||
// Getters
|
||||
public char[] getSecretWord() { return this.secretword ; }
|
||||
public boolean[] getFoundLetters() { return this.foundletters ; }
|
||||
public byte getRemainingTry() { return this.remainingtry ; }
|
||||
|
||||
/** Représentation masquée du mot, ex: "_ _ A _ _" */
|
||||
public String getMaskedWord() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < secretword.length; i++) {
|
||||
sb.append(foundletters[i] ? secretword[i] : '_');
|
||||
if (i < secretword.length - 1) sb.append(' ');
|
||||
}
|
||||
return sb.toString();
|
||||
//Méthodes
|
||||
public char[] getSecretWord() {
|
||||
return this.secretword ;
|
||||
}
|
||||
|
||||
/** Vérifie l'état de la partie en cours. */
|
||||
public boolean[] getFoundLetters() {
|
||||
return this.foundletters ;
|
||||
}
|
||||
|
||||
public byte getRemainingTry() {
|
||||
return this.remainingtry ;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Vérifie l'état de la partie en cours.
|
||||
*
|
||||
* @return true si le jeu est fini.
|
||||
*/
|
||||
public boolean gameIsEnding() {
|
||||
if(this.remainingtry <= 0){
|
||||
return true ;
|
||||
@@ -52,9 +56,10 @@ public class Partie {
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si la lettre reçue a déjà été jouée puis met à jour
|
||||
* entriesletters / foundletters / remainingtry.
|
||||
* @return true si la lettre était déjà présente (doublon).
|
||||
* Vérifie si la lettre reçu n'a pas déjà été joué puis, met à jour le tableau "entriesletters" et
|
||||
* "foundletters" le cas échéant.
|
||||
*
|
||||
* @return true si la lettre était déjà présente.
|
||||
*/
|
||||
public boolean isAlreadyEntries(char letter) {
|
||||
short caractercode = (short) letter ; //Récupération du code du caractère
|
||||
@@ -77,21 +82,55 @@ public class Partie {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère un mot à partir d'un grand dictionnaire (enfin en principe).
|
||||
*
|
||||
* @return le mot généré.
|
||||
*/
|
||||
private char[] generateSecretWord() {
|
||||
char[] word = {'P','I','Z','Z','A'};
|
||||
//À implémenter plus tard
|
||||
Random random = new Random();
|
||||
byte grain = (byte) random.nextInt(Mots.dictionarysize);
|
||||
char[] word = Mots.dictionary[grain].toUpperCase().toCharArray();
|
||||
return word ;
|
||||
}
|
||||
|
||||
private boolean wordIsFound() {
|
||||
for(byte i = 0 ; i < this.wordsize ; i++){
|
||||
if(!this.foundletters[i]){ //Si une lettre n'est pas trouvée
|
||||
for(byte i = 0 ; i < this.wordsize ; i++){ //Parcours du "secretword"
|
||||
if(!this.foundletters[i]){ //Si une lettre n'est pas trouvé
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
return true ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() { return "" ; }
|
||||
|
||||
//Affichage
|
||||
public String toString() {
|
||||
return "" ;
|
||||
}
|
||||
|
||||
//Tests
|
||||
public static void main(String[] args){
|
||||
char[] test = {'E','O','M','I','E','D','A','Z','N','L','C','R','P','H','T','S'};
|
||||
byte size = (byte) test.length ;
|
||||
boolean status ;
|
||||
|
||||
Partie game = new Partie();
|
||||
System.out.println("Trick > " + String.valueOf(game.secretword) + "\n");
|
||||
for(byte i = 0 ; i < size && !game.gameIsEnding() ; i++){
|
||||
System.out.println("Essais restants : " + game.getRemainingTry());
|
||||
status = game.isAlreadyEntries(test[i]);
|
||||
for(byte l = 0 ; l < game.wordsize ; l++){ //Parcours du "secretword"
|
||||
if(game.foundletters[l] == true){
|
||||
System.out.print(game.getSecretWord()[l] + " ");
|
||||
}else{
|
||||
System.out.print("_ ");
|
||||
}
|
||||
}
|
||||
System.out.println(""); //Lisibilité
|
||||
//System.out.println("Lettres : " + game.entriesletters);
|
||||
}
|
||||
System.out.println("Essais restants : " + game.getRemainingTry());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.function.Consumer;
|
||||
* Lie Fenetre (vue) et Partie (logique) via un handler.
|
||||
* Met à jour le dessin du pendu à chaque erreur.
|
||||
*
|
||||
* @version 1.3
|
||||
* @version 1.4
|
||||
* author Adrien
|
||||
* Date : 08-10-2025
|
||||
* Licence :
|
||||
@@ -27,36 +27,54 @@ public class Pendu {
|
||||
|
||||
Partie partie = new Partie();
|
||||
|
||||
// Affichage initial du mot masqué (via Partie)
|
||||
fenetre.getWordLabel().setText(partie.getMaskedWord());
|
||||
// Affichage initial du mot masqué (construit ici)
|
||||
fenetre.getWordLabel().setText(buildMaskedWord(partie));
|
||||
|
||||
// Stage initial (0 erreur)
|
||||
if (fenetre.getDrawZone() instanceof Dessin) {
|
||||
((Dessin) fenetre.getDrawZone()).setStage(0);
|
||||
}
|
||||
|
||||
// Handler : applique Partie puis met à jour l'UI (mot + dessin)
|
||||
Consumer<Character> handler = new GameLetterHandler(fenetre, partie);
|
||||
// On mémorise les essais initiaux pour calculer les erreurs = initialTries - remainingTry
|
||||
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
|
||||
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 :
|
||||
* - applique Partie.isAlreadyEntries
|
||||
* - met à jour le mot affiché (Partie#getMaskedWord)
|
||||
* - calcule le stage = MAX_STAGE - remainingTry et met à jour le Dessin
|
||||
* - met à jour le mot affiché
|
||||
* - calcule errors = initialTries - remainingTry, puis stage = min(errors, Dessin.MAX_STAGE)
|
||||
* - met à jour le score
|
||||
* - gère la fin de partie
|
||||
*/
|
||||
private static class GameLetterHandler implements Consumer<Character> {
|
||||
private final Fenetre fenetre;
|
||||
private final Partie partie;
|
||||
private final int initialTries; // essais au démarrage (peut être 11 avec ta Partie)
|
||||
private final Dessin dessinPanel;
|
||||
|
||||
GameLetterHandler(Fenetre fenetre, Partie partie) {
|
||||
GameLetterHandler(Fenetre fenetre, Partie partie, int initialTries) {
|
||||
this.fenetre = fenetre;
|
||||
this.partie = partie;
|
||||
this.initialTries = initialTries;
|
||||
this.dessinPanel = (fenetre.getDrawZone() instanceof Dessin)
|
||||
? (Dessin) fenetre.getDrawZone() : null;
|
||||
}
|
||||
@@ -66,20 +84,20 @@ public class Pendu {
|
||||
boolean alreadyPlayed = partie.isAlreadyEntries(ch);
|
||||
|
||||
// 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) {
|
||||
int stage = Dessin.MAX_STAGE - partie.getRemainingTry();
|
||||
int stage = Math.min(errors, Dessin.MAX_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)
|
||||
? fenetre.getChronometre().getElapsedMillis() : 0L;
|
||||
int errors = Dessin.MAX_STAGE - partie.getRemainingTry();
|
||||
int score = Score.compute(errors, elapsed);
|
||||
if (fenetre.getScoreLabel() != null) {
|
||||
int score = Score.compute(errors, elapsed);
|
||||
fenetre.getScoreLabel().setText("Score : " + score);
|
||||
}
|
||||
|
||||
@@ -94,10 +112,10 @@ public class Pendu {
|
||||
if (fenetre.getChronometre() != null) {
|
||||
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)
|
||||
? 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);
|
||||
|
||||
boolean win = !fenetre.getWordLabel().getText().contains("_");
|
||||
@@ -105,7 +123,7 @@ public class Pendu {
|
||||
? "Bravo ! Mot trouvé : "
|
||||
: "Perdu ! Le mot était : ")
|
||||
+ String.valueOf(partie.getSecretWord())
|
||||
+ "\nScore : " + finalScore;
|
||||
+ (fenetre.getScoreLabel() != null ? "\nScore : " + finalScore : "");
|
||||
|
||||
JOptionPane.showMessageDialog(fenetre.getWindow(), msg);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user