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 long startTime; public static long endTime; public static int score; /* Fonction main */ public static void main(String[] args) { String difficulty = chooseDifficulty(); String selectedWord = ChooseWord.chooseTheWordByDifficulty(difficulty); gameState = new GameState(selectedWord); startTime = System.currentTimeMillis(); createInterface(); } /* Fonction pour choisir la difficulté */ public static String chooseDifficulty() { String[] options = {"Easy", "Medium", "Hard"}; int choice = JOptionPane.showOptionDialog(null, "Choose difficulty level:", "Difficulty Selection", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); if (choice == 0) return "easy"; else if (choice == 1) return "medium"; else if (choice == 2) return "hard"; else return "easy"; // défaut } /* Fonction pour créer l'interface */ public static void createInterface() { JFrame window = new JFrame("Hangman Game"); window.setSize(800, 600); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setLayout(new BorderLayout()); wordLabel = new JLabel(gameState.getHiddenWord(), SwingConstants.CENTER); wordLabel.setFont(new Font("Arial", Font.BOLD, 40)); window.add(wordLabel, BorderLayout.NORTH); hangmanPanel = new HangmanPanel(); window.add(hangmanPanel, BorderLayout.CENTER); JPanel inputPanel = new JPanel(); inputField = new JTextField(2); JButton submitButton = new JButton("Try Letter"); messageLabel = new JLabel("Enter a letter:"); inputPanel.add(messageLabel); inputPanel.add(inputField); inputPanel.add(submitButton); window.add(inputPanel, BorderLayout.SOUTH); submitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { handleLetterInput(); } }); window.setVisible(true); } /* Fonction pour gérer la saisie d'une lettre */ public static void handleLetterInput() { String input = inputField.getText().toLowerCase(); if (input.length() != 1 || !Character.isLetter(input.charAt(0))) { messageLabel.setText("Enter a single valid letter."); return; } char letter = input.charAt(0); inputField.setText(""); if (gameState.hasTriedLetter(letter)) { messageLabel.setText("Letter already tried."); return; } gameState.tryLetter(letter); wordLabel.setText(gameState.getHiddenWord()); hangmanPanel.setErrors(gameState.getErrors()); if (gameState.isWon()) { endTime = System.currentTimeMillis(); long elapsedSeconds = (endTime - startTime) / 1000; score = 1000 - (gameState.getErrors() * 50) - ((int) elapsedSeconds * 10); if (score < 0) score = 0; messageLabel.setText("Congrats! You won! Score: " + score + " Time: " + elapsedSeconds + "s"); inputField.setEditable(false); } else if (gameState.isLost()) { messageLabel.setText("You lost! Word was: " + gameState.getWord()); inputField.setEditable(false); } else { messageLabel.setText("Keep guessing..."); } } }