forked from menault/TD3_DEV51_Qualite_Algo
		
	ajount de proposition de mot
This commit is contained in:
		| @@ -1,10 +1,10 @@ | |||||||
| import javax.swing.*; | import javax.swing.*; | ||||||
|  |  | ||||||
| import java.awt.*; | import java.awt.*; | ||||||
| import java.awt.event.*; | import java.awt.event.*; | ||||||
| import java.util.*; | import java.util.*; | ||||||
| import java.util.Timer; | import java.util.Timer; | ||||||
| import java.util.TimerTask; | import java.util.TimerTask; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Hangman GUI with difficulty selection, dynamic score, and timer. |  * Hangman GUI with difficulty selection, dynamic score, and timer. | ||||||
|  * Uses WordManager for words. |  * Uses WordManager for words. | ||||||
| @@ -12,12 +12,12 @@ import java.util.TimerTask; | |||||||
|  */ |  */ | ||||||
| public class HangmanGUI extends JFrame { | public class HangmanGUI extends JFrame { | ||||||
|     private WordManager manager; |     private WordManager manager; | ||||||
|  |  | ||||||
|     private String secretWord; |     private String secretWord; | ||||||
|     private final Set<Character> found = new HashSet<>(); |     private final Set<Character> found = new HashSet<>(); | ||||||
|     private final Set<Character> tried = new HashSet<>(); |     private final Set<Character> tried = new HashSet<>(); | ||||||
|     private int errors = 0; |     private int errors = 0; | ||||||
|     private static final int MAX_ERRORS = 8; |     private static final int MAX_ERRORS = 8; | ||||||
|  |     private static final int WORD_GUESS_PENALTY = 2; // pénalité (en erreurs) si le mot complet proposé est faux | ||||||
|     private boolean gameOver = false; |     private boolean gameOver = false; | ||||||
|  |  | ||||||
|     // scoring and timing |     // scoring and timing | ||||||
| @@ -29,36 +29,30 @@ public class HangmanGUI extends JFrame { | |||||||
|     // UI components |     // UI components | ||||||
|     private final JLabel wordLbl = new JLabel("", SwingConstants.CENTER); |     private final JLabel wordLbl = new JLabel("", SwingConstants.CENTER); | ||||||
|     private final JLabel triedLbl = new JLabel("", SwingConstants.CENTER); |     private final JLabel triedLbl = new JLabel("", SwingConstants.CENTER); | ||||||
|     private final JLabel infoLbl  = new JLabel("Entrez une lettre (a-z)", SwingConstants.CENTER); |     private final JLabel infoLbl  = new JLabel("Entrez une lettre ou un mot (a-z)", SwingConstants.CENTER); | ||||||
|     private final JLabel scoreLbl = new JLabel("Score: 0", SwingConstants.CENTER); |     private final JLabel scoreLbl = new JLabel("Score: 0", SwingConstants.CENTER); | ||||||
|     private final JLabel timerLbl = new JLabel("Temps: 0s", SwingConstants.CENTER); |     private final JLabel timerLbl = new JLabel("Temps: 0s", SwingConstants.CENTER); | ||||||
|     private final JTextField input = new JTextField(); |     private final JTextField input = new JTextField(); | ||||||
|     private final JButton guessBtn = new JButton("Proposer"); |     private final JButton guessBtn = new JButton("Proposer"); | ||||||
|     private final HangPanel hangPanel = new HangPanel(); |     private final HangPanel hangPanel = new HangPanel(); | ||||||
|  |  | ||||||
|     private final String[] difficulties = {"Facile", "Moyen", "Difficile"}; |     private final String[] difficulties = {"Facile", "Moyen", "Difficile"}; | ||||||
|     private final JComboBox<String> difficultyCombo = new JComboBox<>(difficulties); |     private final JComboBox<String> difficultyCombo = new JComboBox<>(difficulties); | ||||||
|  |  | ||||||
|     private Timer timer; |     private Timer timer; | ||||||
|  |  | ||||||
|     /** Drawing panel for hangman */ |     /** Drawing panel for hangman */ | ||||||
|     private static class HangPanel extends JPanel { |     private static class HangPanel extends JPanel { | ||||||
|         private int errors = 0; |         private int errors = 0; | ||||||
|  |  | ||||||
|         public void setErrors(int e) { |         public void setErrors(int e) { | ||||||
|             this.errors = Math.max(0, Math.min(e, MAX_ERRORS)); |             this.errors = Math.max(0, Math.min(e, MAX_ERRORS)); | ||||||
|             repaint(); |             repaint(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         protected void paintComponent(Graphics g) { |         protected void paintComponent(Graphics g) { | ||||||
|             super.paintComponent(g); |             super.paintComponent(g); | ||||||
|             Graphics2D gg = (Graphics2D) g; |             Graphics2D gg = (Graphics2D) g; | ||||||
|             gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |             gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | ||||||
|  |  | ||||||
|             int w = getWidth(), h = getHeight(); |             int w = getWidth(), h = getHeight(); | ||||||
|             int baseY = h - 40; |             int baseY = h - 40; | ||||||
|  |  | ||||||
|             if (errors >= 1) gg.drawLine(50, baseY, w - 50, baseY); |             if (errors >= 1) gg.drawLine(50, baseY, w - 50, baseY); | ||||||
|             if (errors >= 2) gg.drawLine(100, baseY, 100, 60); |             if (errors >= 2) gg.drawLine(100, baseY, 100, 60); | ||||||
|             if (errors >= 3) gg.drawLine(100, 60, 220, 60); |             if (errors >= 3) gg.drawLine(100, 60, 220, 60); | ||||||
| @@ -133,6 +127,7 @@ public class HangmanGUI extends JFrame { | |||||||
|         controls.add(guessBtn, BorderLayout.EAST); |         controls.add(guessBtn, BorderLayout.EAST); | ||||||
|  |  | ||||||
|         hangPanel.setPreferredSize(new Dimension(500, 300)); |         hangPanel.setPreferredSize(new Dimension(500, 300)); | ||||||
|  |  | ||||||
|         JPanel center = new JPanel(new BorderLayout()); |         JPanel center = new JPanel(new BorderLayout()); | ||||||
|         center.add(hangPanel, BorderLayout.CENTER); |         center.add(hangPanel, BorderLayout.CENTER); | ||||||
|  |  | ||||||
| @@ -141,7 +136,6 @@ public class HangmanGUI extends JFrame { | |||||||
|         main.add(top, BorderLayout.NORTH); |         main.add(top, BorderLayout.NORTH); | ||||||
|         main.add(center, BorderLayout.CENTER); |         main.add(center, BorderLayout.CENTER); | ||||||
|         main.add(controls, BorderLayout.SOUTH); |         main.add(controls, BorderLayout.SOUTH); | ||||||
|  |  | ||||||
|         setContentPane(main); |         setContentPane(main); | ||||||
|  |  | ||||||
|         guessBtn.addActionListener(e -> onGuess()); |         guessBtn.addActionListener(e -> onGuess()); | ||||||
| @@ -161,12 +155,13 @@ public class HangmanGUI extends JFrame { | |||||||
|         tried.clear(); |         tried.clear(); | ||||||
|         errors = 0; |         errors = 0; | ||||||
|         gameOver = false; |         gameOver = false; | ||||||
|         startTime = System.currentTimeMillis(); |  | ||||||
|  |  | ||||||
|  |         startTime = System.currentTimeMillis(); | ||||||
|         input.setText(""); |         input.setText(""); | ||||||
|         input.setEditable(true); |         input.setEditable(true); | ||||||
|         guessBtn.setEnabled(true); |         guessBtn.setEnabled(true); | ||||||
|         infoLbl.setText("Entrez une lettre (a-z)"); |         infoLbl.setText("Entrez une lettre ou un mot (a-z)"); | ||||||
|  |  | ||||||
|         updateUIState(); |         updateUIState(); | ||||||
|  |  | ||||||
|         if (timer != null) timer.cancel(); |         if (timer != null) timer.cancel(); | ||||||
| @@ -182,38 +177,58 @@ public class HangmanGUI extends JFrame { | |||||||
|         }, 0, 200); |         }, 0, 200); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** Handle guess */ |     /** Handle guess (lettre OU mot) */ | ||||||
|     private void onGuess() { |     private void onGuess() { | ||||||
|         if (gameOver) return; |         if (gameOver) return; | ||||||
|  |  | ||||||
|         String s = input.getText().trim().toLowerCase(Locale.ROOT); |         String s = input.getText().trim().toLowerCase(Locale.ROOT); | ||||||
|         if (s.length() != 1 || s.charAt(0) < 'a' || s.charAt(0) > 'z') { |  | ||||||
|             infoLbl.setText("Veuillez entrer une seule lettre (a-z)."); |         // autoriser uniquement les lettres a-z, mais longueur libre (1 = lettre, >1 = mot) | ||||||
|             input.selectAll(); |         if (!s.matches("[a-z]+")) { | ||||||
|             input.requestFocusInWindow(); |             infoLbl.setText("Veuillez entrer uniquement des lettres (a-z)."); | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|         char c = s.charAt(0); |  | ||||||
|         if (tried.contains(c)) { |  | ||||||
|             infoLbl.setText("Lettre déjà essayée : " + c); |  | ||||||
|             input.selectAll(); |             input.selectAll(); | ||||||
|             input.requestFocusInWindow(); |             input.requestFocusInWindow(); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         tried.add(c); |         if (s.length() == 1) { | ||||||
|         boolean hit = false; |             // === Proposition d'une lettre === | ||||||
|         for (int i = 0; i < secretWord.length(); i++) { |             char c = s.charAt(0); | ||||||
|             if (secretWord.charAt(i) == c) { |             if (tried.contains(c)) { | ||||||
|                 found.add(c); |                 infoLbl.setText("Lettre déjà essayée : " + c); | ||||||
|                 hit = true; |                 input.selectAll(); | ||||||
|  |                 input.requestFocusInWindow(); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |             tried.add(c); | ||||||
|  |  | ||||||
|  |             boolean hit = false; | ||||||
|  |             for (int i = 0; i < secretWord.length(); i++) { | ||||||
|  |                 if (secretWord.charAt(i) == c) { | ||||||
|  |                     found.add(c); | ||||||
|  |                     hit = true; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             if (!hit) { | ||||||
|  |                 errors++; | ||||||
|  |                 infoLbl.setText("Mauvaise lettre : " + c); | ||||||
|  |             } else { | ||||||
|  |                 infoLbl.setText("Bonne lettre : " + c); | ||||||
|             } |             } | ||||||
|         } |  | ||||||
|         if (!hit) { |  | ||||||
|             errors++; |  | ||||||
|             infoLbl.setText("Mauvaise lettre : " + c); |  | ||||||
|         } else { |         } else { | ||||||
|             infoLbl.setText("Bonne lettre : " + c); |             // === Proposition d'un mot entier === | ||||||
|  |             if (s.equals(secretWord)) { | ||||||
|  |                 // victoire immédiate : révéler tout | ||||||
|  |                 for (int i = 0; i < secretWord.length(); i++) { | ||||||
|  |                     char lc = secretWord.charAt(i); | ||||||
|  |                     if (Character.isLetter(lc)) found.add(lc); | ||||||
|  |                 } | ||||||
|  |                 infoLbl.setText("Bravo ! Vous avez deviné le mot en entier : " + secretWord); | ||||||
|  |             } else { | ||||||
|  |                 // pénalité si mot faux | ||||||
|  |                 errors = Math.min(errors + WORD_GUESS_PENALTY, MAX_ERRORS); | ||||||
|  |                 infoLbl.setText("Mauvaise proposition du mot."); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         updateUIState(); |         updateUIState(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user