Files
TD3_DEV51_brigitte_aissi/PenduWIlfried/main.java
Aissi Jude Christ 7270ba1a20 Exo 3 appliqué
2025-10-08 14:57:57 +02:00

193 lines
7.1 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class main {
public static GameState gameState;
public static JLabel wordLabel;
public static HangmanPanel hangmanPanel;
public static JTextField inputField;
public static JLabel messageLabel;
public static JLabel scoreLabel;
public static JLabel timeLabel;
public static Timer timer;
/*Fonction main*/
public static void main(String[] args) {
showDifficultySelection();
}
/*Fonction pour sélectionner la difficulté*/
public static void showDifficultySelection() {
JFrame selectionFrame = new JFrame("Sélection de Difficulté");
selectionFrame.setSize(400, 300);
selectionFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
selectionFrame.setLayout(new GridLayout(4, 1));
JLabel titleLabel = new JLabel("Choisissez la difficulté:", SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
JButton easyButton = new JButton("FACILE - Mots courts (< 8 lettres)");
JButton mediumButton = new JButton("MOYEN - Mots longs (≥ 8 lettres)");
JButton hardButton = new JButton("DIFFICILE - Phrases (2 mots)");
easyButton.addActionListener(e -> startGame("facile", selectionFrame));
mediumButton.addActionListener(e -> startGame("moyen", selectionFrame));
hardButton.addActionListener(e -> startGame("difficile", selectionFrame));
selectionFrame.add(titleLabel);
selectionFrame.add(easyButton);
selectionFrame.add(mediumButton);
selectionFrame.add(hardButton);
selectionFrame.setVisible(true);
}
/*Fonction pour démarrer le jeu*/
public static void startGame(String difficulty, JFrame selectionFrame) {
selectionFrame.dispose();
String selectedWord = ChooseWord.chooseTheWord(difficulty);
gameState = new GameState(selectedWord, difficulty);
createInterface();
startTimer();
}
/*Fonction pour créer l'interface*/
public static void createInterface() {
JFrame window = new JFrame("HangmanGame - Difficulté: " + gameState.getDifficulty());
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
// Panel d'information (score et temps)
JPanel infoPanel = new JPanel(new GridLayout(1, 3));
JLabel difficultyLabel = new JLabel("Difficulté: " + gameState.getDifficulty(), SwingConstants.CENTER);
difficultyLabel.setFont(new Font("Arial", Font.BOLD, 16));
scoreLabel = new JLabel("Score: 0", SwingConstants.CENTER);
scoreLabel.setFont(new Font("Arial", Font.BOLD, 16));
timeLabel = new JLabel("Temps: 0s", SwingConstants.CENTER);
timeLabel.setFont(new Font("Arial", Font.BOLD, 16));
infoPanel.add(difficultyLabel);
infoPanel.add(scoreLabel);
infoPanel.add(timeLabel);
wordLabel = new JLabel(gameState.getHiddenWord(), SwingConstants.CENTER);
wordLabel.setFont(new Font("Arial", Font.BOLD, 40));
hangmanPanel = new HangmanPanel();
JPanel inputPanel = new JPanel();
inputField = new JTextField(2);
inputField.setFont(new Font("Arial", Font.BOLD, 20));
JButton submitButton = new JButton("Essayer la lettre");
messageLabel = new JLabel("Entrez une lettre:");
inputPanel.add(messageLabel);
inputPanel.add(inputField);
inputPanel.add(submitButton);
window.add(infoPanel, BorderLayout.NORTH);
window.add(wordLabel, BorderLayout.CENTER);
window.add(hangmanPanel, BorderLayout.WEST);
window.add(inputPanel, BorderLayout.SOUTH);
/*événement du bouton*/
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleLetterInput();
}
});
/*événement de la touche Entrée*/
inputField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleLetterInput();
}
});
window.setVisible(true);
inputField.requestFocus();
}
/*Fonction pour démarrer le chronomètre*/
public static void startTimer() {
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateDisplay();
}
});
timer.start();
}
/*Fonction pour mettre à jour l'affichage*/
public static void updateDisplay() {
timeLabel.setText("Temps: " + gameState.getTimeElapsed() + "s");
scoreLabel.setText("Score: " + gameState.getFinalScore());
}
/*Fonction pour gérer la saisie de lettre*/
public static void handleLetterInput() {
String input = inputField.getText().toLowerCase();
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
messageLabel.setText("Entrez une seule lettre valide.");
return;
}
char letter = input.charAt(0);
inputField.setText("");
if (gameState.hasTriedLetter(letter)) {
messageLabel.setText("Lettre déjà essayée.");
return;
}
gameState.tryLetter(letter);
wordLabel.setText(gameState.getHiddenWord());
hangmanPanel.setErrors(gameState.getErrors());
updateDisplay();
if (gameState.isWon()) {
timer.stop();
int finalScore = gameState.getFinalScore();
messageLabel.setText("Félicitations ! Vous avez gagné ! Score: " + finalScore);
inputField.setEditable(false);
showEndGameMessage(true, finalScore);
} else if (gameState.isLost()) {
timer.stop();
messageLabel.setText("Perdu ! Le mot était: " + gameState.getWord());
inputField.setEditable(false);
showEndGameMessage(false, 0);
} else {
messageLabel.setText("Continuez... Erreurs: " + gameState.getErrors() + "/" + 9);
}
}
/*Fonction pour afficher le message de fin de jeu*/
public static void showEndGameMessage(boolean won, int score) {
String message = won ?
"FÉLICITATIONS !\nVous avez gagné !\nScore final: " + score + "\nTemps: " + gameState.getTimeElapsed() + "s" :
"DOMAGE !\nVous avez perdu.\nLe mot était: " + gameState.getWord() + "\nVoulez-vous réessayer ?";
int optionType = won ? JOptionPane.INFORMATION_MESSAGE : JOptionPane.QUESTION_MESSAGE;
int choice = JOptionPane.showConfirmDialog(null,
message,
"Partie Terminée",
JOptionPane.DEFAULT_OPTION,
optionType);
if (!won || choice == JOptionPane.OK_OPTION) {
// Retour au menu principal
SwingUtilities.invokeLater(() -> {
JFrame currentFrame = (JFrame) SwingUtilities.getWindowAncestor(inputField);
currentFrame.dispose();
showDifficultySelection();
});
}
}
}