Compare commits

8 Commits

10 changed files with 2091 additions and 0 deletions

27
Makefile Normal file
View File

@@ -0,0 +1,27 @@
# === Configuration ===
SRC_DIR = src
OUT_DIR = out
PACKAGE = fr/iut/Projet
MAIN_CLASS = fr.iut.Projet.Random_word
# === Règle principale ===
all: compile run
# === Compilation ===
compile:
@echo "Compilation du projet..."
@mkdir -p $(OUT_DIR)
@javac -d $(OUT_DIR) $(SRC_DIR)/$(PACKAGE)/Random_word.java
@cp $(SRC_DIR)/$(PACKAGE)/Word.txt $(OUT_DIR)/$(PACKAGE)/
@echo "Compilation terminée."
# === Exécution ===
run:
@echo "Exécution du programme..."
@java -cp $(OUT_DIR) $(MAIN_CLASS)
# === Nettoyage ===
clean:
@echo "Suppression des fichiers compilés..."
@rm -rf $(OUT_DIR)
@echo "Nettoyage terminé."

Binary file not shown.

835
out/fr/iut/Projet/Word.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,31 @@
package fr.iut.Projet;
import javax.swing.*;
import java.awt.*;
/**
* Classe Action représentant la fenêtre principale du jeu "Hanging Man".
* Affiche un message de bienvenue.
*/
public class Action {
/**
* Constructeur. Crée et affiche la fenêtre du jeu avec un message.
*/
public Action() {
// Création de la fenêtre du jeu
JFrame gameFrame = new JFrame("Hanging Man");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setSize(1040, 1000);
gameFrame.setLayout(new BorderLayout());
// Label de bienvenue
JLabel label = new JLabel("Bienvenue dans le jeu !");
label.setFont(new Font("Arial", Font.BOLD, 28));
label.setHorizontalAlignment(SwingConstants.CENTER);
// Ajouter le label et afficher la fenêtre
gameFrame.add(label, BorderLayout.CENTER);
gameFrame.setVisible(true);
}
}

View File

@@ -0,0 +1,119 @@
import javax.swing.*;
import java.awt.*;
class Affiche extends JComponent {
private int step = 0;
private boolean youWin = false;
public void setStep(int step) {
this.step = step;
repaint();
}
public void setYouWin(boolean value) {
this.youWin = value;
repaint();
}
@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);
drawGallows(g2);
drawHangman(g2);
if (step >= 7) drawSkull(g2);
if (youWin) drawYouWinMessage(g2);
g2.dispose();
}
private void drawGallows(Graphics2D g2) {
// Base and vertical post
g2.drawLine(50, 350, 200, 350);
g2.drawLine(100, 350, 100, 50);
// Horizontal beam and rope
g2.drawLine(100, 50, 250, 50);
g2.drawLine(250, 50, 250, 100);
// Diagonal support
g2.drawLine(100, 100, 180, 50);
}
private void drawHangman(Graphics2D g2) {
int headX = 225, headY = 100, headDiam = 50;
int bodyX = headX + headDiam / 2, bodyYStart = headY + headDiam, bodyYEnd = bodyYStart + 100;
int armLength = 60;
int legLength = 70;
if (step >= 1) g2.drawOval(headX, headY, headDiam, headDiam); // head
if (step >= 2) g2.drawLine(bodyX, bodyYStart, bodyX, bodyYEnd); // body
if (step >= 3) g2.drawLine(bodyX, bodyYStart + 20, bodyX - armLength, bodyYStart + 20); // left arm
if (step >= 4) g2.drawLine(bodyX, bodyYStart + 20, bodyX + armLength, bodyYStart + 20); // right arm
if (step >= 5) g2.drawLine(bodyX, bodyYEnd, bodyX - 5, bodyYEnd + legLength); // left leg
if (step >= 6) g2.drawLine(bodyX, bodyYEnd, bodyX + 5, bodyYEnd + legLength); // right leg
if (step == 6) drawAura(g2, headX, headY, headDiam, bodyX, bodyYStart, bodyYEnd, armLength, legLength);
}
private void drawAura(Graphics2D g2, int headX, int headY, int headDiam, int bodyX, int bodyYStart, int bodyYEnd, int armLength, int legLength) {
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);
}
private void drawSkull(Graphics2D g2) {
int skullX = 225, skullY = 100, skullDiam = 50;
// Skull
g2.setColor(Color.BLACK);
g2.fillOval(skullX, skullY, skullDiam, skullDiam);
// Eyes
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);
// Nose
g2.fillOval(skullX + skullDiam / 2 - skullDiam / 12, skullY + skullDiam / 2 - skullDiam / 12, skullDiam / 6, skullDiam / 6);
// Mouth
g2.setStroke(new BasicStroke(2));
g2.drawLine(skullX + skullDiam / 5, skullY + 2 * skullDiam / 3,
skullX + 4 * skullDiam / 5, skullY + 2 * skullDiam / 3);
// GAME OVER message
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);
}
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);
}
}

View 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);
}
}

View File

@@ -0,0 +1,25 @@
import java.awt.event.*;
import javax.swing.SwingUtilities;
class Mouse extends MouseAdapter {
private Affiche aff;
private int step = 0; // correspond à Affiche.step
public Mouse(Affiche aff) {
this.aff = aff;
}
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
// clic droit -> gagne
aff.setYouWin(true);
} else {
// clic gauche -> incrémente étape
if (step < 7) {
step++;
aff.setStep(step); // <-- utiliser setStep
}
}
}
}

View 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
}
}

View File

@@ -0,0 +1,136 @@
package fr.iut.Projet;
import java.io.*;
import java.util.*;
public class Random_word {
public static void main(String[] args) {
// Récupère un mot aléatoire depuis le fichier Word.txt
String randomword = getRandomWord();
if (randomword == null) {
System.err.println("Impossible de choisir un mot aléatoire !");
return;
}
// Démarre le jeu avec le mot choisi
play(randomword);
}
/**
* 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() {
// Charge le fichier Word.txt depuis le package
InputStream is = Random_word.class.getResourceAsStream("Word.txt");
if (is == null) {
System.err.println("Le fichier 'Word.txt' est introuvable dans le package !");
return null;
}
String randomWord = null;
Random random = new Random();
try (BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(is))) {
String line;
int count = 0;
// Parcourt chaque ligne du fichier
while ((line = bufferedreader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty()) {
count++;
// Sélection aléatoire d'un mot (méthode reservoir sampling)
if (random.nextInt(count) == 0) {
randomWord = line;
}
}
}
} catch (IOException e) {
System.err.println("Erreur lors de la lecture du fichier : " + e.getMessage());
}
return randomWord != null ? randomWord.toLowerCase() : null;
}
/**
* Affiche l'état actuel du mot et les lettres incorrectes.
* @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) {
System.out.print("\nMot actuel : ");
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)) {
System.out.println("Vous avez déjà essayé cette lettre !");
continue;
}
// Vérifie si la lettre fait partie du mot secret
if (secretWord.indexOf(letter) >= 0) {
lettersGuessed.add(letter);
for (int i = 0; i < secretWord.length(); i++) {
if (secretWord.charAt(i) == letter) {
hiddenWord[i] = letter;
}
}
System.out.println("Bien joué !");
} else {
// Lettre incorrecte : décrémente le nombre de vies
incorrectLetters.add(letter);
lives--;
System.out.println("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
System.out.println("\nVous avez perdu ! Le mot était : " + secretWord);
}
}

835
src/fr/iut/Projet/Word.txt Normal file

File diff suppressed because it is too large Load Diff