forked from menault/TD3_DEV51_Qualite_Algo
ex4
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.HashSet;
|
||||
import java.awt.event.*;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class HangedGameGUI {
|
||||
private static final int MAX_LIVES = 12;
|
||||
|
||||
private String word;
|
||||
|
||||
// Game variables
|
||||
private String word;
|
||||
private Set<Character> guessedLetters;
|
||||
private int lives;
|
||||
|
||||
|
||||
// Composants de l'interface
|
||||
private JFrame frame;
|
||||
private String difficulty; // "easy", "medium", "hard"
|
||||
|
||||
// Timer and scoring
|
||||
private TimerManager timerManager;
|
||||
|
||||
// GUI components
|
||||
private JFrame frameMenu;
|
||||
private JFrame frameGame;
|
||||
private JLabel wordLabel;
|
||||
private JLabel livesLabel;
|
||||
private JLabel messageLabel;
|
||||
@@ -22,38 +26,96 @@ public class HangedGameGUI {
|
||||
private JButton guessButton;
|
||||
private JPanel hangmanPanel;
|
||||
|
||||
private JButton easyButton;
|
||||
private JButton mediumButton;
|
||||
private JButton hardButton;
|
||||
|
||||
public HangedGameGUI() {
|
||||
word = WordSelector.pickRandomWord();
|
||||
WordSelector.loadWordsForDifficulty("easy", "motsFacile.txt");
|
||||
WordSelector.loadWordsForDifficulty("medium", "motsMoyen.txt");
|
||||
WordSelector.loadWordsForDifficulty("hard", "motsDifficiles.txt");
|
||||
|
||||
guessedLetters = new HashSet<>();
|
||||
lives = MAX_LIVES;
|
||||
|
||||
initializeGUI();
|
||||
timerManager = new TimerManager();
|
||||
|
||||
initializeMenuGUI();
|
||||
}
|
||||
|
||||
private void initializeGUI() {
|
||||
frame = new JFrame("Jeu du Pendu");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLayout(new BorderLayout());
|
||||
frame.setSize(500, 400);
|
||||
|
||||
// Panel du haut - Affichage du mot et des vies
|
||||
private void initializeMenuGUI() {
|
||||
frameMenu = new JFrame("Jeu du Pendu — Choix de difficulté");
|
||||
frameMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frameMenu.setSize(400, 200);
|
||||
frameMenu.setLayout(new BorderLayout());
|
||||
|
||||
JLabel prompt = new JLabel("Sélectionnez une difficulté :", JLabel.CENTER);
|
||||
prompt.setFont(new Font("Arial", Font.BOLD, 16));
|
||||
frameMenu.add(prompt, BorderLayout.NORTH);
|
||||
|
||||
JPanel buttonPanel = new JPanel(new GridLayout(3, 1, 5, 5));
|
||||
easyButton = new JButton("Easy");
|
||||
mediumButton = new JButton("Medium");
|
||||
hardButton = new JButton("Hard");
|
||||
buttonPanel.add(easyButton);
|
||||
buttonPanel.add(mediumButton);
|
||||
buttonPanel.add(hardButton);
|
||||
frameMenu.add(buttonPanel, BorderLayout.CENTER);
|
||||
|
||||
setupMenuEvents();
|
||||
|
||||
frameMenu.setLocationRelativeTo(null);
|
||||
frameMenu.setVisible(true);
|
||||
}
|
||||
|
||||
private void setupMenuEvents() {
|
||||
easyButton.addActionListener(e -> {
|
||||
difficulty = "easy";
|
||||
startNewGame();
|
||||
});
|
||||
mediumButton.addActionListener(e -> {
|
||||
difficulty = "medium";
|
||||
startNewGame();
|
||||
});
|
||||
hardButton.addActionListener(e -> {
|
||||
difficulty = "hard";
|
||||
startNewGame();
|
||||
});
|
||||
}
|
||||
|
||||
private void startNewGame() {
|
||||
frameMenu.setVisible(false);
|
||||
|
||||
guessedLetters.clear();
|
||||
lives = MAX_LIVES;
|
||||
|
||||
word = WordSelector.pickWord(difficulty);
|
||||
|
||||
initializeGameGUI();
|
||||
|
||||
timerManager.start();
|
||||
}
|
||||
|
||||
private void initializeGameGUI() {
|
||||
frameGame = new JFrame("Jeu du Pendu — " + difficulty);
|
||||
frameGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frameGame.setSize(600, 400);
|
||||
frameGame.setLayout(new BorderLayout());
|
||||
|
||||
JPanel topPanel = new JPanel(new GridLayout(3, 1));
|
||||
|
||||
wordLabel = new JLabel("", JLabel.CENTER);
|
||||
wordLabel.setFont(new Font("Arial", Font.BOLD, 24));
|
||||
updateWordDisplay();
|
||||
|
||||
livesLabel = new JLabel("Vies: " + lives + "/" + MAX_LIVES, JLabel.CENTER);
|
||||
|
||||
livesLabel = new JLabel("Lives: " + lives + " / " + MAX_LIVES, JLabel.CENTER);
|
||||
livesLabel.setFont(new Font("Arial", Font.PLAIN, 16));
|
||||
|
||||
|
||||
messageLabel = new JLabel("Devinez le mot !", JLabel.CENTER);
|
||||
messageLabel.setFont(new Font("Arial", Font.ITALIC, 14));
|
||||
|
||||
|
||||
topPanel.add(wordLabel);
|
||||
topPanel.add(livesLabel);
|
||||
topPanel.add(messageLabel);
|
||||
|
||||
// Panel du pendu (visuel simple)
|
||||
|
||||
hangmanPanel = new JPanel() {
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
@@ -61,189 +123,134 @@ public class HangedGameGUI {
|
||||
drawHangman(g);
|
||||
}
|
||||
};
|
||||
hangmanPanel.setPreferredSize(new Dimension(200, 200));
|
||||
hangmanPanel.setPreferredSize(new Dimension(300, 300));
|
||||
hangmanPanel.setBackground(Color.WHITE);
|
||||
|
||||
// Panel de saisie
|
||||
JPanel inputPanel = new JPanel();
|
||||
inputPanel.setLayout(new FlowLayout());
|
||||
|
||||
JLabel inputLabel = new JLabel("Entrez une lettre:");
|
||||
|
||||
JPanel inputPanel = new JPanel(new FlowLayout());
|
||||
JLabel inputLabel = new JLabel("Enter a letter:");
|
||||
letterField = new JTextField(2);
|
||||
letterField.setFont(new Font("Arial", Font.BOLD, 18));
|
||||
|
||||
guessButton = new JButton("Deviner");
|
||||
|
||||
guessButton = new JButton("Guess");
|
||||
|
||||
inputPanel.add(inputLabel);
|
||||
inputPanel.add(letterField);
|
||||
inputPanel.add(guessButton);
|
||||
|
||||
// Ajout des composants à la fenêtre
|
||||
frame.add(topPanel, BorderLayout.NORTH);
|
||||
frame.add(hangmanPanel, BorderLayout.CENTER);
|
||||
frame.add(inputPanel, BorderLayout.SOUTH);
|
||||
|
||||
// Gestion des événements
|
||||
setupEventHandlers();
|
||||
|
||||
frame.setVisible(true);
|
||||
|
||||
frameGame.add(topPanel, BorderLayout.NORTH);
|
||||
frameGame.add(hangmanPanel, BorderLayout.CENTER);
|
||||
frameGame.add(inputPanel, BorderLayout.SOUTH);
|
||||
|
||||
setupGameEvents();
|
||||
|
||||
frameGame.setLocationRelativeTo(null);
|
||||
frameGame.setVisible(true);
|
||||
}
|
||||
|
||||
private void drawHangman(Graphics g) {
|
||||
int errors = MAX_LIVES - lives;
|
||||
g.setColor(Color.BLACK);
|
||||
|
||||
// Base
|
||||
if (errors >= 1) g.drawLine(50, 150, 100, 150);
|
||||
// Poteau
|
||||
if (errors >= 2) g.drawLine(75, 150, 75, 50);
|
||||
// Traverse
|
||||
if (errors >= 3) g.drawLine(75, 50, 125, 50);
|
||||
// Corde
|
||||
if (errors >= 4) g.drawLine(125, 50, 125, 70);
|
||||
// Tête
|
||||
if (errors >= 5) g.drawOval(120, 70, 10, 10);
|
||||
// Corps
|
||||
if (errors >= 6) g.drawLine(125, 80, 125, 110);
|
||||
// Bras gauche
|
||||
if (errors >= 7) g.drawLine(125, 85, 115, 95);
|
||||
// Bras droit
|
||||
if (errors >= 8) g.drawLine(125, 85, 135, 95);
|
||||
// Jambe gauche
|
||||
if (errors >= 9) g.drawLine(125, 110, 115, 125);
|
||||
// Jambe droite
|
||||
if (errors >= 10) g.drawLine(125, 110, 135, 125);
|
||||
// Visage triste
|
||||
if (errors >= 11) {
|
||||
g.drawArc(122, 73, 3, 3, 0, 180); // œil gauche
|
||||
g.drawArc(127, 73, 3, 3, 0, 180); // œil droit
|
||||
g.drawArc(122, 78, 6, 3, 0, -180); // bouche
|
||||
}
|
||||
|
||||
private void setupGameEvents() {
|
||||
guessButton.addActionListener(e -> processGuess());
|
||||
letterField.addActionListener(e -> processGuess());
|
||||
}
|
||||
|
||||
private void updateWordDisplay() {
|
||||
StringBuilder display = new StringBuilder();
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char letter = word.charAt(i);
|
||||
if (guessedLetters.contains(letter)) {
|
||||
display.append(letter).append(" ");
|
||||
} else {
|
||||
display.append("_ ");
|
||||
}
|
||||
}
|
||||
wordLabel.setText(display.toString());
|
||||
}
|
||||
|
||||
private void setupEventHandlers() {
|
||||
// Bouton deviner
|
||||
guessButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
processGuess();
|
||||
}
|
||||
});
|
||||
|
||||
// Entrée dans le champ texte
|
||||
letterField.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
processGuess();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void processGuess() {
|
||||
String input = letterField.getText().toLowerCase().trim();
|
||||
letterField.setText("");
|
||||
|
||||
|
||||
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
|
||||
messageLabel.setText("Veuillez entrer une seule lettre !");
|
||||
messageLabel.setText("Please enter a single letter.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
char letter = input.charAt(0);
|
||||
|
||||
if (guessedLetters.contains(letter)) {
|
||||
messageLabel.setText("Vous avez déjà essayé cette lettre !");
|
||||
messageLabel.setText("You already guessed that letter.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
guessedLetters.add(letter);
|
||||
|
||||
|
||||
if (!word.contains(String.valueOf(letter))) {
|
||||
lives--;
|
||||
messageLabel.setText("Mauvaise lettre ! Il vous reste " + lives + " vies.");
|
||||
messageLabel.setText("Wrong letter! Lives left: " + lives);
|
||||
} else {
|
||||
messageLabel.setText("Bonne lettre ! Continuez !");
|
||||
messageLabel.setText("Good guess!");
|
||||
}
|
||||
|
||||
|
||||
updateWordDisplay();
|
||||
livesLabel.setText("Vies: " + lives + "/" + MAX_LIVES);
|
||||
livesLabel.setText("Lives: " + lives + " / " + MAX_LIVES);
|
||||
hangmanPanel.repaint();
|
||||
|
||||
checkGameStatus();
|
||||
|
||||
checkGameEnd();
|
||||
}
|
||||
|
||||
private void checkGameStatus() {
|
||||
if (lives <= 0) {
|
||||
endGame(false);
|
||||
} else if (GameLogic.isWordGuessed(word, guessedLetters)) {
|
||||
endGame(true);
|
||||
|
||||
private void updateWordDisplay() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char c = word.charAt(i);
|
||||
if (c == ' ') {
|
||||
sb.append(" ");
|
||||
} else if (guessedLetters.contains(c)) {
|
||||
sb.append(c).append(" ");
|
||||
} else {
|
||||
sb.append("_ ");
|
||||
}
|
||||
}
|
||||
wordLabel.setText(sb.toString());
|
||||
}
|
||||
|
||||
private void drawHangman(Graphics g) {
|
||||
int errors = MAX_LIVES - lives;
|
||||
g.setColor(Color.BLACK);
|
||||
if (errors >= 1) g.drawLine(50, 250, 150, 250);
|
||||
if (errors >= 2) g.drawLine(100, 250, 100, 50);
|
||||
if (errors >= 3) g.drawLine(100, 50, 200, 50);
|
||||
if (errors >= 4) g.drawLine(200, 50, 200, 80);
|
||||
if (errors >= 5) g.drawOval(185, 80, 30, 30);
|
||||
if (errors >= 6) g.drawLine(200, 110, 200, 160);
|
||||
if (errors >= 7) g.drawLine(200, 120, 180, 140);
|
||||
if (errors >= 8) g.drawLine(200, 120, 220, 140);
|
||||
if (errors >= 9) g.drawLine(200, 160, 180, 190);
|
||||
if (errors >= 10) g.drawLine(200, 160, 220, 190);
|
||||
if (errors >= 11) {
|
||||
g.drawArc(190, 90, 5, 5, 0, 180);
|
||||
g.drawArc(205, 90, 5, 5, 0, 180);
|
||||
g.drawArc(190, 105, 15, 8, 0, -180);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void checkGameEnd() {
|
||||
boolean won = GameLogic.isWordGuessed(word, guessedLetters);
|
||||
if (lives <= 0 || won) {
|
||||
endGame(won);
|
||||
}
|
||||
}
|
||||
|
||||
private void endGame(boolean won) {
|
||||
guessButton.setEnabled(false);
|
||||
letterField.setEnabled(false);
|
||||
|
||||
|
||||
long elapsedMillis = timerManager.getElapsedTimeMillis();
|
||||
int errors = MAX_LIVES - lives;
|
||||
int score = ScoreManager.calculateScore(errors, elapsedMillis, difficulty);
|
||||
|
||||
String message;
|
||||
if (won) {
|
||||
messageLabel.setText("VICTOIRE ! Le mot était: " + word);
|
||||
JOptionPane.showMessageDialog(frame,
|
||||
"Félicitations ! Vous avez gagné !\n" +
|
||||
"Le mot était: " + word + "\n" +
|
||||
"Vies restantes: " + lives,
|
||||
"Victoire !",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
message = "You win! Word: " + word + "\nLives left: " + lives + "\nScore: " + score;
|
||||
JOptionPane.showMessageDialog(frameGame, message, "Victory", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
messageLabel.setText("PERDU ! Le mot était: " + word);
|
||||
JOptionPane.showMessageDialog(frame,
|
||||
"Désolé ! Vous avez perdu !\n" +
|
||||
"Le mot était: " + word,
|
||||
"Défaite",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
message = "You lose! Word: " + word + "\nScore: " + score;
|
||||
JOptionPane.showMessageDialog(frameGame, message, "Defeat", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
// Bouton pour rejouer
|
||||
int choice = JOptionPane.showConfirmDialog(frame,
|
||||
"Voulez-vous rejouer ?",
|
||||
"Nouvelle partie",
|
||||
JOptionPane.YES_NO_OPTION);
|
||||
|
||||
|
||||
int choice = JOptionPane.showConfirmDialog(frameGame, "Play again?", "Replay", JOptionPane.YES_NO_OPTION);
|
||||
if (choice == JOptionPane.YES_OPTION) {
|
||||
restartGame();
|
||||
frameGame.dispose();
|
||||
frameMenu.setVisible(true);
|
||||
} else {
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void restartGame() {
|
||||
word = WordSelector.pickRandomWord();
|
||||
guessedLetters.clear();
|
||||
lives = MAX_LIVES;
|
||||
|
||||
guessButton.setEnabled(true);
|
||||
letterField.setEnabled(true);
|
||||
|
||||
updateWordDisplay();
|
||||
livesLabel.setText("Vies: " + lives + "/" + MAX_LIVES);
|
||||
messageLabel.setText("Nouvelle partie - Devinez le mot !");
|
||||
hangmanPanel.repaint();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new HangedGameGUI();
|
||||
}
|
||||
});
|
||||
SwingUtilities.invokeLater(HangedGameGUI::new);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user