forked from menault/TD3_DEV51_Qualite_Algo
Merge branch 'JANNAIRE'
This commit is contained in:
@@ -1,76 +1,155 @@
|
|||||||
package back;
|
package front;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import back.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import javax.swing.*;
|
||||||
import java.util.Set;
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logique principale du jeu du pendu (back).
|
* Interface graphique du jeu du pendu.
|
||||||
* Gère le mot, les lettres trouvées, et les conditions de victoire/défaite.
|
* (Toutes les méthodes ≤ 50 lignes)
|
||||||
*/
|
*/
|
||||||
public class Game {
|
public class GameUI {
|
||||||
private final String word;
|
private JFrame frame;
|
||||||
private final Set<Character> correct = new HashSet<>();
|
private JLabel imgLabel, wordLabel, triedLabel, scoreLabel, timeLabel;
|
||||||
private final Set<Character> all = new HashSet<>();
|
private JTextField input;
|
||||||
private final int maxErrors;
|
private JButton tryBtn, newGameBtn;
|
||||||
private int errors;
|
private Game game;
|
||||||
|
private String currentWord;
|
||||||
|
private Timer timer;
|
||||||
|
|
||||||
public Game(String word, int maxErrors) {
|
/** Lance la fenêtre et démarre une partie */
|
||||||
this.word = word.toLowerCase();
|
public void show() {
|
||||||
this.maxErrors = maxErrors;
|
setupWindow();
|
||||||
|
setupLayout();
|
||||||
|
setupActions();
|
||||||
|
startNewGame();
|
||||||
|
frame.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Tente une lettre et renvoie le résultat */
|
/** Crée la fenêtre principale */
|
||||||
public Result play(char letter) {
|
private void setupWindow() {
|
||||||
char c = Character.toLowerCase(letter);
|
frame = new JFrame("Jeu du Pendu");
|
||||||
if (all.contains(c)) return Result.ALREADY;
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
all.add(c);
|
frame.setSize(560, 560);
|
||||||
if (word.indexOf(c) >= 0) {
|
frame.setLocationRelativeTo(null);
|
||||||
correct.add(c);
|
frame.setLayout(new BorderLayout(12, 12));
|
||||||
return Result.HIT;
|
}
|
||||||
} else {
|
|
||||||
errors++;
|
/** Construit les composants et le layout */
|
||||||
return Result.MISS;
|
private void setupLayout() {
|
||||||
|
imgLabel = new JLabel("", SwingConstants.CENTER);
|
||||||
|
frame.add(imgLabel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
wordLabel = new JLabel("Mot : ");
|
||||||
|
triedLabel = new JLabel("Lettres essayées : ");
|
||||||
|
scoreLabel = new JLabel("Score : 0");
|
||||||
|
timeLabel = new JLabel("Temps : 0s");
|
||||||
|
|
||||||
|
JPanel top = new JPanel(new GridLayout(2, 1));
|
||||||
|
top.add(buildTopLine(wordLabel, scoreLabel));
|
||||||
|
top.add(buildTopLine(triedLabel, timeLabel));
|
||||||
|
frame.add(top, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
JPanel bottom = new JPanel(new BorderLayout(8, 8));
|
||||||
|
JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||||
|
input = new JTextField(5);
|
||||||
|
tryBtn = new JButton("Essayer");
|
||||||
|
newGameBtn = new JButton("Nouvelle partie");
|
||||||
|
inputPanel.add(new JLabel("Lettre :"));
|
||||||
|
inputPanel.add(input);
|
||||||
|
inputPanel.add(tryBtn);
|
||||||
|
bottom.add(inputPanel, BorderLayout.WEST);
|
||||||
|
bottom.add(newGameBtn, BorderLayout.EAST);
|
||||||
|
frame.add(bottom, BorderLayout.SOUTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Crée une ligne du haut avec 2 labels */
|
||||||
|
private JPanel buildTopLine(JLabel left, JLabel right) {
|
||||||
|
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||||
|
panel.add(left);
|
||||||
|
panel.add(Box.createHorizontalStrut(24));
|
||||||
|
panel.add(right);
|
||||||
|
return panel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Ajoute les actions et le timer */
|
||||||
|
private void setupActions() {
|
||||||
|
tryBtn.addActionListener(this::onTry);
|
||||||
|
input.addActionListener(this::onTry);
|
||||||
|
newGameBtn.addActionListener(e -> startNewGame());
|
||||||
|
timer = new Timer(1000, e -> refreshStatsOnly());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Démarre une nouvelle partie */
|
||||||
|
private void startNewGame() {
|
||||||
|
currentWord = Words.random();
|
||||||
|
game = new Game(currentWord, 7);
|
||||||
|
input.setText("");
|
||||||
|
input.requestFocusInWindow();
|
||||||
|
if (!timer.isRunning()) timer.start();
|
||||||
|
refreshUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Gère le clic ou l'appui sur Entrée */
|
||||||
|
private void onTry(ActionEvent e) {
|
||||||
|
String text = input.getText();
|
||||||
|
if (!Check.isLetter(text)) {
|
||||||
|
showMsg("Tape une seule lettre (A-Z).");
|
||||||
|
input.requestFocusInWindow();
|
||||||
|
input.selectAll();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Result res = game.play(Character.toLowerCase(text.charAt(0)));
|
||||||
|
handleResult(res);
|
||||||
|
input.setText("");
|
||||||
|
refreshUI();
|
||||||
|
checkEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Réagit selon le résultat d'une tentative */
|
||||||
|
private void handleResult(Result res) {
|
||||||
|
switch (res) {
|
||||||
|
case ALREADY:
|
||||||
|
showMsg("Lettre déjà utilisée.");
|
||||||
|
break;
|
||||||
|
case HIT:
|
||||||
|
case MISS:
|
||||||
|
break; // rien, juste refresh
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Retourne le mot masqué avec les lettres trouvées */
|
/** Vérifie si la partie est finie */
|
||||||
public String maskedWord() {
|
private void checkEnd() {
|
||||||
StringBuilder sb = new StringBuilder();
|
if (game.isWin() || game.isLose()) {
|
||||||
for (int i = 0; i < word.length(); i++) {
|
timer.stop();
|
||||||
char c = word.charAt(i);
|
game.end(game.isWin());
|
||||||
if (!Character.isLetter(c)) sb.append(c);
|
String msg = (game.isWin() ? "Bravo !" : "Perdu !")
|
||||||
else if (correct.contains(c)) sb.append(c);
|
+ " Le mot était : " + currentWord
|
||||||
else sb.append('_');
|
+ "\nScore final : " + game.getScore()
|
||||||
if (i < word.length() - 1) sb.append(' ');
|
+ "\nTemps : " + game.getElapsedSeconds() + "s";
|
||||||
|
showMsg(msg);
|
||||||
}
|
}
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Vérifie si le joueur a gagné */
|
/** Met à jour tout l'affichage */
|
||||||
public boolean isWin() {
|
private void refreshUI() {
|
||||||
for (int i = 0; i < word.length(); i++) {
|
imgLabel.setIcon(Gallows.icon(game.getErrors()));
|
||||||
char c = word.charAt(i);
|
wordLabel.setText("Mot : " + game.maskedWord());
|
||||||
if (Character.isLetter(c) && !correct.contains(c)) return false;
|
triedLabel.setText("Lettres essayées : " + String.join(", ", game.triedLetters()));
|
||||||
}
|
refreshStatsOnly();
|
||||||
return true;
|
frame.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Vérifie si le joueur a perdu */
|
/** Met à jour uniquement score + chrono */
|
||||||
public boolean isLose() {
|
private void refreshStatsOnly() {
|
||||||
return errors >= maxErrors;
|
scoreLabel.setText("Score : " + game.getScore());
|
||||||
|
timeLabel.setText("Temps : " + game.getElapsedSeconds() + "s");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Renvoie le nombre d'erreurs actuelles */
|
/** Affiche une boîte de message */
|
||||||
public int getErrors() { return errors; }
|
private void showMsg(String msg) {
|
||||||
|
JOptionPane.showMessageDialog(frame, msg);
|
||||||
/** Liste les lettres déjà essayées */
|
|
||||||
public List<String> triedLetters() {
|
|
||||||
List<Character> sorted = new ArrayList<>(all);
|
|
||||||
sorted.sort(Character::compareTo);
|
|
||||||
List<String> out = new ArrayList<>();
|
|
||||||
for (Character ch : sorted) out.add(String.valueOf(ch));
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user