forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
8 Commits
6a8bc81e01
...
Hugo
| Author | SHA1 | Date | |
|---|---|---|---|
| 7d2a0a12f9 | |||
| aef5c7f70c | |||
| dcac91d944 | |||
| b60bda7ae0 | |||
| 1037a9ff92 | |||
| 0d074ad1b6 | |||
| 3ab03c6b3e | |||
| 3bc6530305 |
6
Makefile
6
Makefile
@@ -2,7 +2,7 @@
|
|||||||
SRC_DIR = src
|
SRC_DIR = src
|
||||||
OUT_DIR = out
|
OUT_DIR = out
|
||||||
PACKAGE = fr/iut/Projet
|
PACKAGE = fr/iut/Projet
|
||||||
MAIN_CLASS = fr.iut.Projet.Random_word
|
MAIN_CLASS = fr.iut.Projet.Display
|
||||||
|
|
||||||
# === Règle principale ===
|
# === Règle principale ===
|
||||||
all: compile run
|
all: compile run
|
||||||
@@ -11,7 +11,9 @@ all: compile run
|
|||||||
compile:
|
compile:
|
||||||
@echo "Compilation du projet..."
|
@echo "Compilation du projet..."
|
||||||
@mkdir -p $(OUT_DIR)
|
@mkdir -p $(OUT_DIR)
|
||||||
@javac -d $(OUT_DIR) $(SRC_DIR)/$(PACKAGE)/Random_word.java
|
# Compilation de tous les fichiers Java du package
|
||||||
|
@javac -d $(OUT_DIR) $(SRC_DIR)/$(PACKAGE)/*.java
|
||||||
|
# Copier Word.txt dans le dossier de sortie
|
||||||
@cp $(SRC_DIR)/$(PACKAGE)/Word.txt $(OUT_DIR)/$(PACKAGE)/
|
@cp $(SRC_DIR)/$(PACKAGE)/Word.txt $(OUT_DIR)/$(PACKAGE)/
|
||||||
@echo "Compilation terminée."
|
@echo "Compilation terminée."
|
||||||
|
|
||||||
|
|||||||
BIN
out/Affiche.class
Normal file
BIN
out/Affiche.class
Normal file
Binary file not shown.
BIN
out/Mouse.class
Normal file
BIN
out/Mouse.class
Normal file
Binary file not shown.
BIN
out/fr/iut/Projet/Action$1.class
Normal file
BIN
out/fr/iut/Projet/Action$1.class
Normal file
Binary file not shown.
BIN
out/fr/iut/Projet/Action.class
Normal file
BIN
out/fr/iut/Projet/Action.class
Normal file
Binary file not shown.
BIN
out/fr/iut/Projet/Affiche.class
Normal file
BIN
out/fr/iut/Projet/Affiche.class
Normal file
Binary file not shown.
BIN
out/fr/iut/Projet/Display.class
Normal file
BIN
out/fr/iut/Projet/Display.class
Normal file
Binary file not shown.
BIN
out/fr/iut/Projet/PlayButtonListener.class
Normal file
BIN
out/fr/iut/Projet/PlayButtonListener.class
Normal file
Binary file not shown.
Binary file not shown.
192
src/fr/iut/Projet/Action.java
Normal file
192
src/fr/iut/Projet/Action.java
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
package fr.iut.Projet;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe principale qui gère l'interface graphique du jeu du pendu.
|
||||||
|
* <p>
|
||||||
|
* Cette classe crée la fenêtre du jeu, affiche le mot caché, les lettres incorrectes,
|
||||||
|
* le dessin du pendu (via {@link Affiche}), et permet à l'utilisateur de saisir des lettres.
|
||||||
|
* Elle gère également la logique de mise à jour et la fin de partie.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public class Action {
|
||||||
|
|
||||||
|
/** Fenêtre principale du jeu. */
|
||||||
|
private JFrame gameFrame;
|
||||||
|
|
||||||
|
/** Label affichant le mot caché avec les lettres découvertes. */
|
||||||
|
private JLabel wordLabel;
|
||||||
|
|
||||||
|
/** Label affichant la liste des lettres incorrectes. */
|
||||||
|
private JLabel incorrectLettersLabel;
|
||||||
|
|
||||||
|
/** Champ de texte permettant à l'utilisateur d'entrer une lettre. */
|
||||||
|
private JTextField letterInputField;
|
||||||
|
|
||||||
|
/** Instance du jeu, contenant la logique (mot, vies, lettres, etc.). */
|
||||||
|
private Random_word game;
|
||||||
|
|
||||||
|
/** Composant graphique qui affiche le dessin du pendu. */
|
||||||
|
private Affiche affiche;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur de la classe {@code Action}.
|
||||||
|
* <p>
|
||||||
|
* Initialise le jeu, crée les composants graphiques, met en place la mise en page
|
||||||
|
* et affiche la fenêtre du jeu.
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
public Action() {
|
||||||
|
game = new Random_word();
|
||||||
|
initializeComponents();
|
||||||
|
layoutComponents();
|
||||||
|
letterInputField.addActionListener(e -> handleGuess());
|
||||||
|
gameFrame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== Initialisation ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise tous les composants graphiques (labels, champs, boutons...).
|
||||||
|
*/
|
||||||
|
private void initializeComponents() {
|
||||||
|
gameFrame = new JFrame("Hanging Man");
|
||||||
|
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
gameFrame.setSize(700, 500);
|
||||||
|
|
||||||
|
affiche = new Affiche();
|
||||||
|
affiche.setPreferredSize(new Dimension(350, 400));
|
||||||
|
affiche.setBackground(Color.WHITE);
|
||||||
|
affiche.setOpaque(true);
|
||||||
|
|
||||||
|
wordLabel = new JLabel(game.getHiddenWord());
|
||||||
|
wordLabel.setFont(new Font("Arial", Font.BOLD, 32));
|
||||||
|
wordLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
|
||||||
|
incorrectLettersLabel = new JLabel("Incorrect letters: " + game.getIncorrectLetters());
|
||||||
|
incorrectLettersLabel.setFont(new Font("Arial", Font.PLAIN, 20));
|
||||||
|
incorrectLettersLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
|
||||||
|
letterInputField = new JTextField(3);
|
||||||
|
letterInputField.setFont(new Font("Arial", Font.PLAIN, 24));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== Mise en page ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispose les composants graphiques dans la fenêtre selon un {@link BorderLayout}.
|
||||||
|
*/
|
||||||
|
private void layoutComponents() {
|
||||||
|
JPanel mainPanel = new JPanel(new BorderLayout());
|
||||||
|
|
||||||
|
// --- panneau gauche : dessin du pendu ---
|
||||||
|
JPanel leftPanel = new JPanel(new BorderLayout());
|
||||||
|
leftPanel.add(affiche, BorderLayout.CENTER);
|
||||||
|
mainPanel.add(leftPanel, BorderLayout.WEST);
|
||||||
|
|
||||||
|
// --- panneau droit : interface du jeu ---
|
||||||
|
JPanel rightPanel = new JPanel(new GridBagLayout());
|
||||||
|
GridBagConstraints gbc = new GridBagConstraints();
|
||||||
|
gbc.gridx = 0;
|
||||||
|
gbc.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc.insets = new Insets(5, 5, 5, 5);
|
||||||
|
|
||||||
|
// Mot caché
|
||||||
|
gbc.gridy = 0;
|
||||||
|
gbc.weighty = 1.0;
|
||||||
|
rightPanel.add(wordLabel, gbc);
|
||||||
|
|
||||||
|
// Lettres incorrectes
|
||||||
|
gbc.gridy = 1;
|
||||||
|
rightPanel.add(incorrectLettersLabel, gbc);
|
||||||
|
|
||||||
|
// Champ de saisie
|
||||||
|
gbc.gridy = 2;
|
||||||
|
JPanel inputRow = new JPanel();
|
||||||
|
inputRow.add(new JLabel("Enter a letter:"));
|
||||||
|
inputRow.add(letterInputField);
|
||||||
|
rightPanel.add(inputRow, gbc);
|
||||||
|
|
||||||
|
// Bouton de redémarrage
|
||||||
|
gbc.gridy = 3;
|
||||||
|
JButton restartButton = new JButton("Restart");
|
||||||
|
restartButton.addActionListener(new PlayButtonListener(gameFrame));
|
||||||
|
rightPanel.add(restartButton, gbc);
|
||||||
|
|
||||||
|
mainPanel.add(rightPanel, BorderLayout.CENTER);
|
||||||
|
gameFrame.add(mainPanel);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== Gestion du jeu ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gère la saisie d'une lettre par le joueur.
|
||||||
|
* <p>
|
||||||
|
* Vérifie la validité de la lettre, met à jour le jeu, l'affichage
|
||||||
|
* et teste si la partie est terminée.
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
private void handleGuess() {
|
||||||
|
String inputText = letterInputField.getText();
|
||||||
|
letterInputField.setText("");
|
||||||
|
|
||||||
|
if (!isValidInput(inputText)) return;
|
||||||
|
|
||||||
|
char guessedLetter = inputText.charAt(0);
|
||||||
|
String message = game.guessLetter(guessedLetter);
|
||||||
|
|
||||||
|
updateUI();
|
||||||
|
|
||||||
|
if (game.isGameOver()) endGame(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vérifie que la saisie de l'utilisateur est une seule lettre valide.
|
||||||
|
*
|
||||||
|
* @param inputText texte saisi dans le champ de saisie
|
||||||
|
* @return {@code true} si la saisie est valide, {@code false} sinon
|
||||||
|
*/
|
||||||
|
private boolean isValidInput(String inputText) {
|
||||||
|
if (inputText.length() != 1 || !Character.isLetter(inputText.charAt(0))) {
|
||||||
|
JOptionPane.showMessageDialog(gameFrame, "Please enter a single letter!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Met à jour les éléments de l'interface graphique :
|
||||||
|
* <ul>
|
||||||
|
* <li>le mot affiché,</li>
|
||||||
|
* <li>les lettres incorrectes,</li>
|
||||||
|
* <li>et le dessin du pendu selon les vies restantes.</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
private void updateUI() {
|
||||||
|
wordLabel.setText(game.getHiddenWord());
|
||||||
|
incorrectLettersLabel.setText("Incorrect letters: " + game.getIncorrectLetters());
|
||||||
|
affiche.setLives(game.getLives());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Termine la partie en affichant un message selon le résultat (victoire ou défaite)
|
||||||
|
* et empêche de nouvelles saisies.
|
||||||
|
*
|
||||||
|
* @param message message à afficher dans une boîte de dialogue
|
||||||
|
*/
|
||||||
|
private void endGame(String message) {
|
||||||
|
if (game.isWon()) {
|
||||||
|
affiche.setYouWin(true);
|
||||||
|
} else {
|
||||||
|
affiche.setLives(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
JOptionPane.showMessageDialog(gameFrame, message);
|
||||||
|
letterInputField.setEditable(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
177
src/fr/iut/Projet/Affiche.java
Normal file
177
src/fr/iut/Projet/Affiche.java
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
package fr.iut.Projet;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe graphique responsable du dessin du pendu dans le jeu.
|
||||||
|
* <p>
|
||||||
|
* Cette classe hérite de {@link JComponent} et redéfinit la méthode
|
||||||
|
* {@link #paintComponent(Graphics)} pour afficher le pendu selon le nombre
|
||||||
|
* de vies restantes. Elle affiche également un message de victoire ou de défaite.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
class Affiche extends JComponent {
|
||||||
|
|
||||||
|
/** Nombre de vies restantes (entre 0 et 8). */
|
||||||
|
private int lives = 8;
|
||||||
|
|
||||||
|
/** Indique si le joueur a gagné. */
|
||||||
|
private boolean youWin = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Définit le nombre de vies restantes et redessine le composant.
|
||||||
|
*
|
||||||
|
* @param lives nombre de vies restantes
|
||||||
|
*/
|
||||||
|
public void setLives(int lives) {
|
||||||
|
this.lives = lives;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Définit si le joueur a gagné et redessine le composant.
|
||||||
|
*
|
||||||
|
* @param value {@code true} si le joueur a gagné, sinon {@code false}
|
||||||
|
*/
|
||||||
|
public void setYouWin(boolean value) {
|
||||||
|
this.youWin = value;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redessine le pendu en fonction du nombre de vies restantes.
|
||||||
|
* <ul>
|
||||||
|
* <li>Affiche la potence dès la première erreur</li>
|
||||||
|
* <li>Ajoute progressivement les membres du pendu</li>
|
||||||
|
* <li>Affiche une tête de mort si toutes les vies sont perdues</li>
|
||||||
|
* <li>Affiche un message "YOU WIN" si la partie est gagnée</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @param g contexte graphique utilisé pour dessiner
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
|
||||||
|
if (isOpaque()) {
|
||||||
|
g.setColor(getBackground());
|
||||||
|
g.fillRect(0, 0, getWidth(), getHeight());
|
||||||
|
}
|
||||||
|
|
||||||
|
Graphics2D g2 = (Graphics2D) g.create();
|
||||||
|
g2.setStroke(new BasicStroke(3));
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
|
||||||
|
if (lives <= 7) drawGallows(g2);
|
||||||
|
if (lives <= 6) drawHead(g2);
|
||||||
|
if (lives <= 5) drawBody(g2);
|
||||||
|
if (lives <= 4) drawLeftArm(g2);
|
||||||
|
if (lives <= 3) drawRightArm(g2);
|
||||||
|
if (lives <= 2) drawLeftLeg(g2);
|
||||||
|
if (lives <= 1) drawRightLeg(g2);
|
||||||
|
if (lives == 1) drawAura(g2);
|
||||||
|
if (lives == 0) drawSkull(g2);
|
||||||
|
if (youWin) drawYouWinMessage(g2);
|
||||||
|
|
||||||
|
g2.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== Méthodes de dessin ====================
|
||||||
|
|
||||||
|
/** Dessine la potence. */
|
||||||
|
private void drawGallows(Graphics2D g2) {
|
||||||
|
g2.drawLine(50, 350, 200, 350);
|
||||||
|
g2.drawLine(100, 350, 100, 50);
|
||||||
|
g2.drawLine(100, 50, 250, 50);
|
||||||
|
g2.drawLine(250, 50, 250, 100);
|
||||||
|
g2.drawLine(100, 100, 180, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Coordonnées et tailles du pendu
|
||||||
|
private final int headX = 225, headY = 100, headDiam = 50;
|
||||||
|
private final int bodyX = headX + headDiam / 2;
|
||||||
|
private final int bodyYStart = headY + headDiam;
|
||||||
|
private final int bodyYEnd = bodyYStart + 100;
|
||||||
|
private final int armLength = 60;
|
||||||
|
private final int legLength = 70;
|
||||||
|
|
||||||
|
/** Dessine la tête du pendu. */
|
||||||
|
private void drawHead(Graphics2D g2) { g2.drawOval(headX, headY, headDiam, headDiam); }
|
||||||
|
|
||||||
|
/** Dessine le corps du pendu. */
|
||||||
|
private void drawBody(Graphics2D g2) { g2.drawLine(bodyX, bodyYStart, bodyX, bodyYEnd); }
|
||||||
|
|
||||||
|
/** Dessine le bras gauche. */
|
||||||
|
private void drawLeftArm(Graphics2D g2) {
|
||||||
|
g2.drawLine(bodyX, bodyYStart + 20, bodyX - armLength, bodyYStart + 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Dessine le bras droit. */
|
||||||
|
private void drawRightArm(Graphics2D g2) {
|
||||||
|
g2.drawLine(bodyX, bodyYStart + 20, bodyX + armLength, bodyYStart + 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Dessine la jambe gauche. */
|
||||||
|
private void drawLeftLeg(Graphics2D g2) {
|
||||||
|
g2.drawLine(bodyX, bodyYEnd, bodyX - 5, bodyYEnd + legLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Dessine la jambe droite. */
|
||||||
|
private void drawRightLeg(Graphics2D g2) {
|
||||||
|
g2.drawLine(bodyX, bodyYEnd, bodyX + 5, bodyYEnd + legLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Dessine une aura bleue lorsque le joueur est sur sa dernière vie. */
|
||||||
|
private void drawAura(Graphics2D g2) {
|
||||||
|
g2.setColor(new Color(0, 0, 255, 100));
|
||||||
|
g2.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
||||||
|
g2.drawOval(headX, headY, headDiam, headDiam);
|
||||||
|
g2.drawLine(bodyX, bodyYStart, bodyX, bodyYEnd);
|
||||||
|
g2.drawLine(bodyX, bodyYStart + 20, bodyX - armLength, bodyYStart + 20);
|
||||||
|
g2.drawLine(bodyX, bodyYStart + 20, bodyX + armLength, bodyYStart + 20);
|
||||||
|
g2.drawLine(bodyX, bodyYEnd, bodyX - 5, bodyYEnd + legLength);
|
||||||
|
g2.drawLine(bodyX, bodyYEnd, bodyX + 5, bodyYEnd + legLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Dessine la tête de mort et le message "GAME OVER". */
|
||||||
|
private void drawSkull(Graphics2D g2) {
|
||||||
|
int skullX = 225, skullY = 100, skullDiam = 50;
|
||||||
|
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
g2.fillOval(skullX, skullY, skullDiam, skullDiam);
|
||||||
|
|
||||||
|
g2.setColor(Color.WHITE);
|
||||||
|
g2.fillOval(skullX + skullDiam / 5, skullY + skullDiam / 6, skullDiam / 5, skullDiam / 5);
|
||||||
|
g2.fillOval(skullX + 3 * skullDiam / 5, skullY + skullDiam / 6, skullDiam / 5, skullDiam / 5);
|
||||||
|
g2.fillOval(skullX + skullDiam / 2 - skullDiam / 12, skullY + skullDiam / 2 - skullDiam / 12, skullDiam / 6, skullDiam / 6);
|
||||||
|
|
||||||
|
g2.setStroke(new BasicStroke(2));
|
||||||
|
g2.drawLine(skullX + skullDiam / 5, skullY + 2 * skullDiam / 3,
|
||||||
|
skullX + 4 * skullDiam / 5, skullY + 2 * skullDiam / 3);
|
||||||
|
|
||||||
|
g2.setColor(Color.RED);
|
||||||
|
g2.setFont(new Font("Arial", Font.BOLD, 36));
|
||||||
|
String message = "GAME OVER";
|
||||||
|
FontMetrics fm = g2.getFontMetrics();
|
||||||
|
int textWidth = fm.stringWidth(message);
|
||||||
|
int xText = (getWidth() - textWidth) / 2;
|
||||||
|
int yText = skullY - 20;
|
||||||
|
g2.drawString(message, xText, yText);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Dessine le message "YOU WIN" en vert en cas de victoire. */
|
||||||
|
private void drawYouWinMessage(Graphics2D g2) {
|
||||||
|
g2.setColor(Color.GREEN);
|
||||||
|
g2.setFont(new Font("Arial", Font.BOLD, 36));
|
||||||
|
String message = "YOU WIN";
|
||||||
|
FontMetrics fm = g2.getFontMetrics();
|
||||||
|
int textWidth = fm.stringWidth(message);
|
||||||
|
int xText = (getWidth() - textWidth) / 2;
|
||||||
|
int yText = 50;
|
||||||
|
g2.drawString(message, xText, yText);
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/fr/iut/Projet/Display.java
Normal file
48
src/fr/iut/Projet/Display.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package fr.iut.Projet;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe Display représentant le menu principal du jeu "Hanging Man".
|
||||||
|
* Elle affiche le titre et le bouton Play.
|
||||||
|
*/
|
||||||
|
public class Display {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Méthode principale. Crée et affiche la fenêtre du menu principal.
|
||||||
|
* @param args Arguments de la ligne de commande (non utilisés)
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Création de la fenêtre principale
|
||||||
|
JFrame frame = new JFrame("Hanging Man");
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
frame.setSize(1040, 1000);
|
||||||
|
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
|
||||||
|
|
||||||
|
// Titre du jeu
|
||||||
|
JLabel text = new JLabel("Hanging Man");
|
||||||
|
text.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
text.setFont(new Font("Arial", Font.BOLD, 70));
|
||||||
|
|
||||||
|
// Bouton Play
|
||||||
|
JButton play = new JButton("Play");
|
||||||
|
play.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
play.setPreferredSize(new Dimension(300, 100));
|
||||||
|
play.setMaximumSize(new Dimension(300, 150));
|
||||||
|
play.setFont(new Font("Arial", Font.PLAIN, 36));
|
||||||
|
|
||||||
|
// Listener séparé pour gérer le clic sur Play
|
||||||
|
play.addActionListener(new PlayButtonListener(frame));
|
||||||
|
|
||||||
|
// Ajouter les composants avec de l'espace
|
||||||
|
frame.add(Box.createVerticalGlue());
|
||||||
|
frame.add(text);
|
||||||
|
frame.add(Box.createVerticalStrut(30));
|
||||||
|
frame.add(play);
|
||||||
|
frame.add(Box.createVerticalGlue());
|
||||||
|
|
||||||
|
// Affichage de la fenêtre
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
35
src/fr/iut/Projet/PlayButtonListener.java
Normal file
35
src/fr/iut/Projet/PlayButtonListener.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package fr.iut.Projet;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe PlayButtonListener qui gère le clic sur le bouton "Play" du menu.
|
||||||
|
* Lorsqu'on clique sur le bouton, cette classe ouvre la fenêtre du jeu
|
||||||
|
* et ferme la fenêtre principale.
|
||||||
|
*/
|
||||||
|
public class PlayButtonListener implements ActionListener {
|
||||||
|
|
||||||
|
/** Référence à la fenêtre principale du menu */
|
||||||
|
private JFrame parentFrame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur de PlayButtonListener.
|
||||||
|
* @param frame La fenêtre principale à fermer après ouverture du jeu
|
||||||
|
*/
|
||||||
|
public PlayButtonListener(JFrame frame) {
|
||||||
|
this.parentFrame = frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Méthode appelée lorsque le bouton est cliqué.
|
||||||
|
* Ouvre la fenêtre du jeu et ferme la fenêtre principale.
|
||||||
|
* @param e L'événement déclenché par le clic
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
new Action(); // Ouvre la fenêtre du jeu
|
||||||
|
parentFrame.dispose(); // Ferme la fenêtre principale
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,109 +5,63 @@ import java.util.*;
|
|||||||
|
|
||||||
public class Random_word {
|
public class Random_word {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
private String secretWord;
|
||||||
// Récupère un mot aléatoire depuis le fichier Word.txt
|
private char[] hiddenWord;
|
||||||
String randomword = getRandomWord();
|
private Set<Character> lettersGuessed;
|
||||||
|
private Set<Character> incorrectLetters;
|
||||||
|
private int lives;
|
||||||
|
|
||||||
if (randomword == null) {
|
/**
|
||||||
System.err.println("Impossible de choisir un mot aléatoire !");
|
* Constructeur : sélectionne un mot aléatoire et initialise le jeu.
|
||||||
return;
|
*/
|
||||||
|
public Random_word() {
|
||||||
|
this.secretWord = getRandomWord();
|
||||||
|
if (this.secretWord == null) {
|
||||||
|
throw new RuntimeException("Impossible de choisir un mot aléatoire !");
|
||||||
}
|
}
|
||||||
|
this.lives = 8;
|
||||||
// Démarre le jeu avec le mot choisi
|
this.hiddenWord = new char[secretWord.length()];
|
||||||
play(randomword);
|
Arrays.fill(this.hiddenWord, '_');
|
||||||
|
this.lettersGuessed = new HashSet<>();
|
||||||
|
this.incorrectLetters = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lit les mots dans "Word.txt" et retourne un mot aléatoire.
|
* Lit les mots dans "Word.txt" et retourne un mot aléatoire.
|
||||||
* @return un mot aléatoire en minuscules, ou null si le fichier est introuvable ou vide
|
|
||||||
*/
|
*/
|
||||||
public static String getRandomWord() {
|
private String getRandomWord() {
|
||||||
// Charge le fichier Word.txt depuis le package
|
|
||||||
InputStream is = Random_word.class.getResourceAsStream("Word.txt");
|
InputStream is = Random_word.class.getResourceAsStream("Word.txt");
|
||||||
|
if (is == null) return null;
|
||||||
if (is == null) {
|
|
||||||
System.err.println("Le fichier 'Word.txt' est introuvable dans le package !");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
String randomWord = null;
|
String randomWord = null;
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
|
||||||
try (BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(is))) {
|
|
||||||
String line;
|
String line;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
// Parcourt chaque ligne du fichier
|
while ((line = br.readLine()) != null) {
|
||||||
while ((line = bufferedreader.readLine()) != null) {
|
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
if (!line.isEmpty()) {
|
if (!line.isEmpty()) {
|
||||||
count++;
|
count++;
|
||||||
// Sélection aléatoire d'un mot (méthode reservoir sampling)
|
|
||||||
if (random.nextInt(count) == 0) {
|
if (random.nextInt(count) == 0) {
|
||||||
randomWord = line;
|
randomWord = line;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Erreur lors de la lecture du fichier : " + e.getMessage());
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return randomWord != null ? randomWord.toLowerCase() : null;
|
return randomWord != null ? randomWord.toLowerCase() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Affiche l'état actuel du mot et les lettres incorrectes.
|
* Tente une lettre, met à jour l'état du jeu et retourne un message.
|
||||||
* @param hiddenWord le mot masqué avec les lettres découvertes
|
|
||||||
* @param incorrectLetters les lettres déjà essayées mais incorrectes
|
|
||||||
*/
|
*/
|
||||||
private static void showwordstatus(char[] hiddenWord, Set<Character> incorrectLetters) {
|
public String guessLetter(char letter) {
|
||||||
System.out.print("\nMot actuel : ");
|
letter = Character.toLowerCase(letter);
|
||||||
for (char Character : hiddenWord) {
|
|
||||||
System.out.print(Character + " ");
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
System.out.println("Lettres incorrectes : " + incorrectLetters);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Logique principale du jeu : gère les essais, met à jour l'état du mot,
|
|
||||||
* suit le nombre de vies restantes, et détermine la victoire ou la défaite.
|
|
||||||
* @param secretWord le mot que le joueur doit deviner
|
|
||||||
*/
|
|
||||||
public static void play(String secretWord) {
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
Set<Character> lettersGuessed = new HashSet<>();
|
|
||||||
Set<Character> incorrectLetters = new HashSet<>();
|
|
||||||
int lives = 8; // nombre d'essais
|
|
||||||
|
|
||||||
char[] hiddenWord = new char[secretWord.length()];
|
|
||||||
Arrays.fill(hiddenWord, '_');
|
|
||||||
|
|
||||||
System.out.println("Bienvenue dans le jeu du mot mystère !");
|
|
||||||
System.out.println("Le mot contient " + secretWord.length() + " lettres.");
|
|
||||||
|
|
||||||
while (lives > 0) {
|
|
||||||
// Affiche le mot actuel et les lettres incorrectes
|
|
||||||
showwordstatus(hiddenWord, incorrectLetters);
|
|
||||||
|
|
||||||
System.out.print("Entrez une lettre : ");
|
|
||||||
String input = scanner.nextLine().toLowerCase();
|
|
||||||
|
|
||||||
// Vérifie que l'entrée est valide
|
|
||||||
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
|
|
||||||
System.out.println("Entrez une seule lettre !");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
char letter = input.charAt(0);
|
|
||||||
|
|
||||||
// Vérifie si la lettre a déjà été essayée
|
|
||||||
if (lettersGuessed.contains(letter) || incorrectLetters.contains(letter)) {
|
if (lettersGuessed.contains(letter) || incorrectLetters.contains(letter)) {
|
||||||
System.out.println("Vous avez déjà essayé cette lettre !");
|
return "Vous avez déjà essayé cette lettre !";
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Vérifie si la lettre fait partie du mot secret
|
|
||||||
if (secretWord.indexOf(letter) >= 0) {
|
if (secretWord.indexOf(letter) >= 0) {
|
||||||
lettersGuessed.add(letter);
|
lettersGuessed.add(letter);
|
||||||
for (int i = 0; i < secretWord.length(); i++) {
|
for (int i = 0; i < secretWord.length(); i++) {
|
||||||
@@ -115,22 +69,48 @@ public class Random_word {
|
|||||||
hiddenWord[i] = letter;
|
hiddenWord[i] = letter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Bien joué !");
|
if (isWon()) {
|
||||||
|
return "Félicitations ! Vous avez trouvé le mot : " + secretWord;
|
||||||
|
}
|
||||||
|
return "Bien joué !";
|
||||||
} else {
|
} else {
|
||||||
// Lettre incorrecte : décrémente le nombre de vies
|
|
||||||
incorrectLetters.add(letter);
|
incorrectLetters.add(letter);
|
||||||
lives--;
|
lives--;
|
||||||
System.out.println("Mauvaise lettre ! Il vous reste " + lives + " vies.");
|
if (lives <= 0) {
|
||||||
|
return "Vous avez perdu ! Le mot était : " + secretWord;
|
||||||
}
|
}
|
||||||
|
return "Mauvaise lettre ! Il vous reste " + lives + " vies.";
|
||||||
// Vérifie si le mot complet a été trouvé
|
|
||||||
if (String.valueOf(hiddenWord).equals(secretWord)) {
|
|
||||||
System.out.println("\nFélicitations ! Vous avez trouvé le mot : " + secretWord);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Le joueur a perdu
|
public String getHiddenWord() {
|
||||||
System.out.println("\nVous avez perdu ! Le mot était : " + secretWord);
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (char c : hiddenWord) sb.append(c).append(' ');
|
||||||
|
return sb.toString().trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIncorrectLetters() {
|
||||||
|
return incorrectLetters.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLives() {
|
||||||
|
return lives;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isGameOver() {
|
||||||
|
return lives <= 0 || isWon();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isWon() {
|
||||||
|
for (char c : hiddenWord) {
|
||||||
|
if (c == '_') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSecretWord() {
|
||||||
|
return secretWord;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user