Compare commits
15 Commits
master
...
a7d9f17ba8
| Author | SHA1 | Date | |
|---|---|---|---|
| a7d9f17ba8 | |||
| 9b7ed446b2 | |||
| b707e37787 | |||
| 6f74af39d9 | |||
| 972ff65f63 | |||
| d74e119af3 | |||
| d79aec8248 | |||
| b4c8b1d156 | |||
| e0f727d76b | |||
| bd4a204d25 | |||
| fae2ecf2c8 | |||
| 7679f1f35b | |||
| 93e31b0aac | |||
| fd393bfc85 | |||
| 89aad809a1 |
13
Jeu_pendu/Back/Check.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package back;
|
||||
|
||||
/**
|
||||
* Vérifie la validité des entrées utilisateur.
|
||||
*/
|
||||
public class Check {
|
||||
/** Retourne vrai si la saisie est une seule lettre alphabétique */
|
||||
public static boolean isLetter(String value) {
|
||||
if (value == null) return false;
|
||||
String trimmed = value.trim();
|
||||
return trimmed.length() == 1 && Character.isLetter(trimmed.charAt(0));
|
||||
}
|
||||
}
|
||||
76
Jeu_pendu/Back/Game.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package back;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Logique principale du jeu du pendu (back).
|
||||
* Gère le mot, les lettres trouvées, et les conditions de victoire/défaite.
|
||||
*/
|
||||
public class Game {
|
||||
private final String word;
|
||||
private final Set<Character> correct = new HashSet<>();
|
||||
private final Set<Character> all = new HashSet<>();
|
||||
private final int maxErrors;
|
||||
private int errors;
|
||||
|
||||
public Game(String word, int maxErrors) {
|
||||
this.word = word.toLowerCase();
|
||||
this.maxErrors = maxErrors;
|
||||
}
|
||||
|
||||
/** Tente une lettre et renvoie le résultat */
|
||||
public Result play(char letter) {
|
||||
char c = Character.toLowerCase(letter);
|
||||
if (all.contains(c)) return Result.ALREADY;
|
||||
all.add(c);
|
||||
if (word.indexOf(c) >= 0) {
|
||||
correct.add(c);
|
||||
return Result.HIT;
|
||||
} else {
|
||||
errors++;
|
||||
return Result.MISS;
|
||||
}
|
||||
}
|
||||
|
||||
/** Retourne le mot masqué avec les lettres trouvées */
|
||||
public String maskedWord() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char c = word.charAt(i);
|
||||
if (!Character.isLetter(c)) sb.append(c);
|
||||
else if (correct.contains(c)) sb.append(c);
|
||||
else sb.append('_');
|
||||
if (i < word.length() - 1) sb.append(' ');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** Vérifie si le joueur a gagné */
|
||||
public boolean isWin() {
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char c = word.charAt(i);
|
||||
if (Character.isLetter(c) && !correct.contains(c)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Vérifie si le joueur a perdu */
|
||||
public boolean isLose() {
|
||||
return errors >= maxErrors;
|
||||
}
|
||||
|
||||
/** Renvoie le nombre d'erreurs actuelles */
|
||||
public int getErrors() { return errors; }
|
||||
|
||||
/** Liste les lettres déjà essayées */
|
||||
public List<String> triedLetters() {
|
||||
List<Character> sorted = new ArrayList<>(all);
|
||||
sorted.sort(Character::compareTo);
|
||||
List<String> out = new ArrayList<>();
|
||||
for (Character ch : sorted) out.add(String.valueOf(ch));
|
||||
return out;
|
||||
}
|
||||
}
|
||||
6
Jeu_pendu/Back/Result.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package back;
|
||||
|
||||
/**
|
||||
* Résultat possible d'une tentative.
|
||||
*/
|
||||
public enum Result { HIT, MISS, ALREADY }
|
||||
85
Jeu_pendu/Back/Words.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package back;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Fournit les mots pour le jeu.
|
||||
* - Lit d'abord "bibliothèque/mots.txt" (UTF-8), 1 mot par ligne.
|
||||
* - Ignore les lignes vides et celles qui commencent par '#'.
|
||||
* - Si le fichier est introuvable ou vide, bascule sur une liste par défaut.
|
||||
*/
|
||||
public class Words {
|
||||
|
||||
/** Chemin du fichier de mots (relatif à la racine du projet). */
|
||||
private static final Path WORDS_PATH = Paths.get("Bibliotheque", "mots.txt");
|
||||
|
||||
/** Liste de secours si le fichier n'est pas disponible. */
|
||||
private static final List<String> DEFAULT = List.of(
|
||||
"algorithm", "variable", "function", "interface", "inheritance",
|
||||
"exception", "compiler", "database", "network", "architecture",
|
||||
"iteration", "recursion", "encryption", "framework", "protocol"
|
||||
);
|
||||
|
||||
/** RNG partagé et cache des mots chargés. */
|
||||
private static final SecureRandom RNG = new SecureRandom();
|
||||
private static volatile List<String> CACHE = null;
|
||||
|
||||
/**
|
||||
* Retourne un mot choisi au hasard depuis le fichier ou la liste par défaut.
|
||||
* Déclenche un chargement paresseux (lazy-load) si nécessaire.
|
||||
*/
|
||||
public static String random() {
|
||||
ensureLoaded();
|
||||
return CACHE.get(RNG.nextInt(CACHE.size()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Recharge les mots depuis le fichier. Utile si modification de mots.txt à chaud.
|
||||
*/
|
||||
public static synchronized void reload() {
|
||||
CACHE = loadFromFileOrDefault();
|
||||
}
|
||||
|
||||
/** Garantit que le cache est initialisé. */
|
||||
private static void ensureLoaded() {
|
||||
if (CACHE == null) {
|
||||
synchronized (Words.class) {
|
||||
if (CACHE == null) {
|
||||
CACHE = loadFromFileOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Tente de charger depuis le fichier, sinon renvoie la liste par défaut. */
|
||||
private static List<String> loadFromFileOrDefault() {
|
||||
List<String> fromFile = readUtf8Lines(WORDS_PATH);
|
||||
if (fromFile.isEmpty()) return DEFAULT;
|
||||
return fromFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit toutes les lignes UTF-8 depuis le chemin fourni,
|
||||
* en filtrant vides et commentaires (# ...).
|
||||
*/
|
||||
private static List<String> readUtf8Lines(Path path) {
|
||||
List<String> result = new ArrayList<>();
|
||||
try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
|
||||
lines.map(String::trim)
|
||||
.filter(s -> !s.isEmpty())
|
||||
.filter(s -> !s.startsWith("#"))
|
||||
.forEach(result::add);
|
||||
} catch (IOException e) {
|
||||
// Silencieux : on basculera sur DEFAULT
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
835
Jeu_pendu/Bibliotheque/mots.txt
Normal file
62
Jeu_pendu/Front/Gallows.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package front;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Gère les images du pendu (une par étape).
|
||||
* - Charge les images depuis assets
|
||||
*/
|
||||
public class Gallows {
|
||||
/** Dossier d'assets relatif à la racine du projet */
|
||||
private static final String ASSETS_DIR = "assets" + File.separator + "images";
|
||||
/** Nombre d'étapes (0 = aucun membre, 7 = étape finale) */
|
||||
private static final int MAX_STAGE = 6;
|
||||
|
||||
private static final ImageIcon[] ICONS = new ImageIcon[MAX_STAGE + 1];
|
||||
|
||||
static {
|
||||
for (int i = 0; i <= MAX_STAGE; i++) {
|
||||
String path = ASSETS_DIR + File.separator + i + ".png";
|
||||
File f = new File(path);
|
||||
if (f.exists()) {
|
||||
ICONS[i] = new ImageIcon(path);
|
||||
} else {
|
||||
// Placeholder si fichier manquant
|
||||
ICONS[i] = placeholder("Missing: " + i + ".png");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne l'icône correspondant au nombre d'erreurs.
|
||||
*/
|
||||
public static ImageIcon icon(int errors) {
|
||||
int idx = Math.max(0, Math.min(errors, MAX_STAGE));
|
||||
return ICONS[idx];
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée une petite image placeholder (si un asset est manquant).
|
||||
*/
|
||||
private static ImageIcon placeholder(String text) {
|
||||
int w = 320, h = 240;
|
||||
Image img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g = (Graphics2D) img.getGraphics();
|
||||
g.setColor(Color.LIGHT_GRAY);
|
||||
g.fillRect(0, 0, w, h);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.drawRect(0, 0, w - 1, h - 1);
|
||||
g.drawString(text, 10, h / 2);
|
||||
g.dispose();
|
||||
return new ImageIcon(img);
|
||||
}
|
||||
|
||||
// Import manquant pour BufferedImage
|
||||
private static class BufferedImage extends java.awt.image.BufferedImage {
|
||||
public BufferedImage(int width, int height, int imageType) {
|
||||
super(width, height, imageType);
|
||||
}
|
||||
}
|
||||
}
|
||||
125
Jeu_pendu/Front/GameUI.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package front;
|
||||
|
||||
import back.*;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
/**
|
||||
* Interface graphique (Swing) du jeu du pendu.
|
||||
* - Affiche une image différente à chaque erreur.
|
||||
* - Champ de saisie pour entrer une lettre.
|
||||
* - Texte pour le mot masqué et les lettres essayées.
|
||||
*/
|
||||
public class GameUI {
|
||||
private JFrame frame;
|
||||
private JLabel imgLabel;
|
||||
private JLabel wordLabel;
|
||||
private JLabel triedLabel;
|
||||
private JTextField input;
|
||||
private JButton tryBtn;
|
||||
private JButton newGameBtn;
|
||||
|
||||
private Game game;
|
||||
private String currentWord;
|
||||
|
||||
/** Affiche la fenêtre principale */
|
||||
public void show() {
|
||||
setupUI();
|
||||
startNewGame();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
/** Initialise les composants Swing */
|
||||
private void setupUI() {
|
||||
frame = new JFrame("Jeu du Pendu");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(520, 520);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setLayout(new BorderLayout(12, 12));
|
||||
|
||||
// Image du pendu
|
||||
imgLabel = new JLabel("", SwingConstants.CENTER);
|
||||
frame.add(imgLabel, BorderLayout.CENTER);
|
||||
|
||||
// Panneau bas: saisie + actions
|
||||
JPanel bottom = new JPanel(new BorderLayout(8, 8));
|
||||
JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||
input = new JTextField(5);
|
||||
tryBtn = new JButton("Essayer");
|
||||
inputPanel.add(new JLabel("Lettre :"));
|
||||
inputPanel.add(input);
|
||||
inputPanel.add(tryBtn);
|
||||
|
||||
newGameBtn = new JButton("Nouvelle partie");
|
||||
bottom.add(inputPanel, BorderLayout.WEST);
|
||||
bottom.add(newGameBtn, BorderLayout.EAST);
|
||||
frame.add(bottom, BorderLayout.SOUTH);
|
||||
|
||||
// Panneau haut: mot + lettres essayées
|
||||
JPanel top = new JPanel();
|
||||
top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
|
||||
wordLabel = new JLabel("Mot : ");
|
||||
triedLabel = new JLabel("Lettres essayées : ");
|
||||
wordLabel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
|
||||
triedLabel.setBorder(BorderFactory.createEmptyBorder(0, 8, 8, 8));
|
||||
top.add(wordLabel);
|
||||
top.add(triedLabel);
|
||||
frame.add(top, BorderLayout.NORTH);
|
||||
|
||||
// Actions
|
||||
tryBtn.addActionListener(this::onTry);
|
||||
input.addActionListener(this::onTry);
|
||||
newGameBtn.addActionListener(e -> startNewGame());
|
||||
}
|
||||
|
||||
/** Démarre une nouvelle partie */
|
||||
private void startNewGame() {
|
||||
currentWord = Words.random();
|
||||
game = new Game(currentWord, 7);
|
||||
input.setText("");
|
||||
input.requestFocusInWindow();
|
||||
refreshUI();
|
||||
}
|
||||
|
||||
/** Gère le clic sur "Essayer" ou Entrée dans le champ */
|
||||
private void onTry(ActionEvent e) {
|
||||
String text = input.getText();
|
||||
if (!Check.isLetter(text)) {
|
||||
JOptionPane.showMessageDialog(frame, "Tape une seule lettre (A-Z).");
|
||||
input.requestFocusInWindow();
|
||||
input.selectAll();
|
||||
return;
|
||||
}
|
||||
char c = Character.toLowerCase(text.charAt(0));
|
||||
Result res = game.play(c);
|
||||
|
||||
switch (res) {
|
||||
case ALREADY: JOptionPane.showMessageDialog(frame, "Lettre déjà utilisée.");
|
||||
break;
|
||||
case HIT:
|
||||
break;
|
||||
case MISS:
|
||||
break;
|
||||
}
|
||||
|
||||
input.setText("");
|
||||
refreshUI();
|
||||
|
||||
if (game.isWin()) {
|
||||
refreshUI();
|
||||
JOptionPane.showMessageDialog(frame, "Bravo ! Le mot était : " + currentWord);
|
||||
} else if (game.isLose()) {
|
||||
refreshUI();
|
||||
JOptionPane.showMessageDialog(frame, "Perdu ! Le mot était : " + currentWord);
|
||||
}
|
||||
}
|
||||
|
||||
/** Met à jour l'image et les textes selon l'état courant */
|
||||
private void refreshUI() {
|
||||
imgLabel.setIcon(Gallows.icon(game.getErrors()));
|
||||
wordLabel.setText("Mot : " + game.maskedWord());
|
||||
triedLabel.setText("Lettres essayées : " + String.join(", ", game.triedLetters()));
|
||||
frame.repaint();
|
||||
}
|
||||
}
|
||||
17
Jeu_pendu/Main/Main.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package main;
|
||||
|
||||
import front.GameUI;
|
||||
|
||||
/**
|
||||
* Point d'entrée du programme.
|
||||
* Lance l'interface graphique du jeu du pendu.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Démarre l'UI Swing sur le thread de l'EDT
|
||||
javax.swing.SwingUtilities.invokeLater(() -> {
|
||||
GameUI ui = new GameUI();
|
||||
ui.show();
|
||||
});
|
||||
}
|
||||
}
|
||||
BIN
Jeu_pendu/assets/images/0.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
Jeu_pendu/assets/images/1.png
Normal file
|
After Width: | Height: | Size: 3.3 KiB |
BIN
Jeu_pendu/assets/images/2.png
Normal file
|
After Width: | Height: | Size: 3.2 KiB |
BIN
Jeu_pendu/assets/images/3.png
Normal file
|
After Width: | Height: | Size: 3.8 KiB |
BIN
Jeu_pendu/assets/images/4.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
Jeu_pendu/assets/images/5.png
Normal file
|
After Width: | Height: | Size: 4.4 KiB |
BIN
Jeu_pendu/assets/images/6.png
Normal file
|
After Width: | Height: | Size: 5.4 KiB |
27
README.md
Normal file
@@ -0,0 +1,27 @@
|
||||
- **bibliothèque/mots.txt** → liste des mots à deviner (1 par ligne)
|
||||
- **assets/images/** → images du pendu (de 0.png à 6.png)
|
||||
|
||||
---
|
||||
|
||||
## Fonctionnalités
|
||||
|
||||
Affichage d’une **image différente du pendu** à chaque erreur
|
||||
Lecture de mots depuis un **fichier externe** (`bibliothèque/mots.txt`)
|
||||
Validation des entrées (une seule LETTRE à la fois)
|
||||
Bouton **“Nouvelle partie”** pour rejouer sans relancer le programme
|
||||
Messages de victoire / défaite
|
||||
|
||||
---
|
||||
|
||||
## Compilation et exécution
|
||||
Se placer préalablement dans Jeu_pendu
|
||||
|
||||
### Compilation
|
||||
```javac -d out $(find -name "*.java")```
|
||||
|
||||
### Exécution
|
||||
```java -cp out main.Main```
|
||||
|
||||
---
|
||||
|
||||
Mini projet effectuer par Clément JANNAIRE et Clémence DUCREUX.
|
||||