This commit is contained in:
2025-10-08 12:28:34 +02:00
parent 86537ff528
commit c14e481572
9 changed files with 40 additions and 85 deletions

View File

@@ -2,6 +2,7 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class main {
public static GameState gameState;
@@ -10,36 +11,17 @@ public class main {
public static JTextField inputField;
public static JLabel messageLabel;
public static long startTime;
public static long endTime;
public static int score;
/* Fonction main */
/*Fonction main*/
public static void main(String[] args) {
String difficulty = chooseDifficulty();
String selectedWord = ChooseWord.chooseTheWordByDifficulty(difficulty);
String selectedWord = ChooseWord.chooseTheWord();
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 */
/*Fonction pour créer l'interface*/
public static void createInterface() {
JFrame window = new JFrame("Hangman Game");
JFrame window = new JFrame("HangmanGame");
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
@@ -62,6 +44,7 @@ public class main {
window.add(inputPanel, BorderLayout.SOUTH);
/*evenement du bouton*/
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleLetterInput();
@@ -71,8 +54,12 @@ public class main {
window.setVisible(true);
}
/* Fonction pour gérer la saisie d'une lettre */
/*Fonction pour mettre à jour le pendu*/
public static void handleLetterInput() {
System.out.println(gameState.getWord());
System.out.println(gameState.getWord().length());
String input = inputField.getText().toLowerCase();
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
messageLabel.setText("Enter a single valid letter.");
@@ -92,12 +79,7 @@ public class main {
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");
messageLabel.setText("Congratulations! You've won!");
inputField.setEditable(false);
} else if (gameState.isLost()) {
messageLabel.setText("You lost! Word was: " + gameState.getWord());