forked from menault/TD3_DEV51_Qualite_Algo
branche jude
This commit is contained in:
BIN
PenduJudeChrist/Display.class
Normal file
BIN
PenduJudeChrist/Display.class
Normal file
Binary file not shown.
43
PenduJudeChrist/Display.java
Normal file
43
PenduJudeChrist/Display.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class Display {
|
||||||
|
|
||||||
|
|
||||||
|
public static void showWord(String word, Set<Character> guessedLetters) {
|
||||||
|
System.out.print("Mot :");
|
||||||
|
for (int i = 0; i < word.length(); i++) {
|
||||||
|
char letter = word.charAt(i);
|
||||||
|
if (guessedLetters.contains(letter)) {
|
||||||
|
System.out.print(letter + " ");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.print("_ ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
public static void showLives(int lives, int maxLives) {
|
||||||
|
System.out.print("Vies : ");
|
||||||
|
for (int i = 0; i < lives; i++) {
|
||||||
|
System.out.print("♥︎ ");
|
||||||
|
}
|
||||||
|
System.out.println("(" + lives + "/" + maxLives + ")\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void showEndGame(String word, int lives, int maxLives){
|
||||||
|
|
||||||
|
if (lives <= 0) {
|
||||||
|
System.out.println(" PERDU ! ");
|
||||||
|
System.out.println(" Vous n'avez plus de vies !");
|
||||||
|
System.out.println(" Le mot etait: " + word);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println(" VICTOIRE ! ");
|
||||||
|
System.out.println(" Le mot était: " + word);
|
||||||
|
System.out.println(" Vies restantes: " + lives);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
PenduJudeChrist/GameLogic.class
Normal file
BIN
PenduJudeChrist/GameLogic.class
Normal file
Binary file not shown.
21
PenduJudeChrist/GameLogic.java
Normal file
21
PenduJudeChrist/GameLogic.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class GameLogic {
|
||||||
|
|
||||||
|
public static boolean isWordGuessed(String word, Set<Character> guessedLetters){
|
||||||
|
|
||||||
|
for (int i = 0; i < word.length(); i++ ) {
|
||||||
|
if(!guessedLetters.contains(word.charAt(i))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
BIN
PenduJudeChrist/HangedGame.class
Normal file
BIN
PenduJudeChrist/HangedGame.class
Normal file
Binary file not shown.
39
PenduJudeChrist/HangedGame.java
Normal file
39
PenduJudeChrist/HangedGame.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
||||||
|
public class HangedGame {
|
||||||
|
|
||||||
|
private static final int MAX_LIVES = 12;
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
String word = WordSelector.pickRandomWord();
|
||||||
|
Set<Character> guessedLetters = new HashSet<>();
|
||||||
|
int lives = MAX_LIVES;
|
||||||
|
|
||||||
|
System.out.println("Bienvenu dans le jeu du pendu");
|
||||||
|
|
||||||
|
while ( lives > 0 && !GameLogic.isWordGuessed(word, guessedLetters)) {
|
||||||
|
Display.showWord(word, guessedLetters);
|
||||||
|
Display.showLives(lives, MAX_LIVES);
|
||||||
|
|
||||||
|
char letter = InputHandler.getLetter(scanner, guessedLetters);
|
||||||
|
guessedLetters.add(letter);
|
||||||
|
|
||||||
|
if (!word.contains(String.valueOf(letter))) {
|
||||||
|
lives = lives -1;
|
||||||
|
System.out.println("Mauvaise lettre vous avez maintenant " + lives + " vies !");
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("Bonne lettre ! ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Display.showEndGame(word, lives, MAX_LIVES);
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
BIN
PenduJudeChrist/HangedGameGUI$1.class
Normal file
BIN
PenduJudeChrist/HangedGameGUI$1.class
Normal file
Binary file not shown.
BIN
PenduJudeChrist/HangedGameGUI$2.class
Normal file
BIN
PenduJudeChrist/HangedGameGUI$2.class
Normal file
Binary file not shown.
BIN
PenduJudeChrist/HangedGameGUI$3.class
Normal file
BIN
PenduJudeChrist/HangedGameGUI$3.class
Normal file
Binary file not shown.
BIN
PenduJudeChrist/HangedGameGUI$4.class
Normal file
BIN
PenduJudeChrist/HangedGameGUI$4.class
Normal file
Binary file not shown.
BIN
PenduJudeChrist/HangedGameGUI.class
Normal file
BIN
PenduJudeChrist/HangedGameGUI.class
Normal file
Binary file not shown.
248
PenduJudeChrist/HangedGameGUI.java
Normal file
248
PenduJudeChrist/HangedGameGUI.java
Normal file
@@ -0,0 +1,248 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class HangedGameGUI {
|
||||||
|
private static final int MAX_LIVES = 12;
|
||||||
|
|
||||||
|
private String word;
|
||||||
|
private Set<Character> guessedLetters;
|
||||||
|
private int lives;
|
||||||
|
|
||||||
|
// Composants de l'interface
|
||||||
|
private JFrame frame;
|
||||||
|
private JLabel wordLabel;
|
||||||
|
private JLabel livesLabel;
|
||||||
|
private JLabel messageLabel;
|
||||||
|
private JTextField letterField;
|
||||||
|
private JButton guessButton;
|
||||||
|
private JPanel hangmanPanel;
|
||||||
|
|
||||||
|
public HangedGameGUI() {
|
||||||
|
word = WordSelector.pickRandomWord();
|
||||||
|
guessedLetters = new HashSet<>();
|
||||||
|
lives = MAX_LIVES;
|
||||||
|
|
||||||
|
initializeGUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeGUI() {
|
||||||
|
frame = new JFrame("Jeu du Pendu");
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
frame.setLayout(new BorderLayout());
|
||||||
|
frame.setSize(500, 400);
|
||||||
|
|
||||||
|
// Panel du haut - Affichage du mot et des vies
|
||||||
|
JPanel topPanel = new JPanel(new GridLayout(3, 1));
|
||||||
|
|
||||||
|
wordLabel = new JLabel("", JLabel.CENTER);
|
||||||
|
wordLabel.setFont(new Font("Arial", Font.BOLD, 24));
|
||||||
|
updateWordDisplay();
|
||||||
|
|
||||||
|
livesLabel = new JLabel("Vies: " + lives + "/" + MAX_LIVES, JLabel.CENTER);
|
||||||
|
livesLabel.setFont(new Font("Arial", Font.PLAIN, 16));
|
||||||
|
|
||||||
|
messageLabel = new JLabel("Devinez le mot !", JLabel.CENTER);
|
||||||
|
messageLabel.setFont(new Font("Arial", Font.ITALIC, 14));
|
||||||
|
|
||||||
|
topPanel.add(wordLabel);
|
||||||
|
topPanel.add(livesLabel);
|
||||||
|
topPanel.add(messageLabel);
|
||||||
|
|
||||||
|
// Panel du pendu (visuel simple)
|
||||||
|
hangmanPanel = new JPanel() {
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
drawHangman(g);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
hangmanPanel.setPreferredSize(new Dimension(200, 200));
|
||||||
|
hangmanPanel.setBackground(Color.WHITE);
|
||||||
|
|
||||||
|
// Panel de saisie
|
||||||
|
JPanel inputPanel = new JPanel();
|
||||||
|
inputPanel.setLayout(new FlowLayout());
|
||||||
|
|
||||||
|
JLabel inputLabel = new JLabel("Entrez une lettre:");
|
||||||
|
letterField = new JTextField(2);
|
||||||
|
letterField.setFont(new Font("Arial", Font.BOLD, 18));
|
||||||
|
|
||||||
|
guessButton = new JButton("Deviner");
|
||||||
|
|
||||||
|
inputPanel.add(inputLabel);
|
||||||
|
inputPanel.add(letterField);
|
||||||
|
inputPanel.add(guessButton);
|
||||||
|
|
||||||
|
// Ajout des composants à la fenêtre
|
||||||
|
frame.add(topPanel, BorderLayout.NORTH);
|
||||||
|
frame.add(hangmanPanel, BorderLayout.CENTER);
|
||||||
|
frame.add(inputPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
// Gestion des événements
|
||||||
|
setupEventHandlers();
|
||||||
|
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawHangman(Graphics g) {
|
||||||
|
int errors = MAX_LIVES - lives;
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
|
||||||
|
// Base
|
||||||
|
if (errors >= 1) g.drawLine(50, 150, 100, 150);
|
||||||
|
// Poteau
|
||||||
|
if (errors >= 2) g.drawLine(75, 150, 75, 50);
|
||||||
|
// Traverse
|
||||||
|
if (errors >= 3) g.drawLine(75, 50, 125, 50);
|
||||||
|
// Corde
|
||||||
|
if (errors >= 4) g.drawLine(125, 50, 125, 70);
|
||||||
|
// Tête
|
||||||
|
if (errors >= 5) g.drawOval(120, 70, 10, 10);
|
||||||
|
// Corps
|
||||||
|
if (errors >= 6) g.drawLine(125, 80, 125, 110);
|
||||||
|
// Bras gauche
|
||||||
|
if (errors >= 7) g.drawLine(125, 85, 115, 95);
|
||||||
|
// Bras droit
|
||||||
|
if (errors >= 8) g.drawLine(125, 85, 135, 95);
|
||||||
|
// Jambe gauche
|
||||||
|
if (errors >= 9) g.drawLine(125, 110, 115, 125);
|
||||||
|
// Jambe droite
|
||||||
|
if (errors >= 10) g.drawLine(125, 110, 135, 125);
|
||||||
|
// Visage triste
|
||||||
|
if (errors >= 11) {
|
||||||
|
g.drawArc(122, 73, 3, 3, 0, 180); // œil gauche
|
||||||
|
g.drawArc(127, 73, 3, 3, 0, 180); // œil droit
|
||||||
|
g.drawArc(122, 78, 6, 3, 0, -180); // bouche
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateWordDisplay() {
|
||||||
|
StringBuilder display = new StringBuilder();
|
||||||
|
for (int i = 0; i < word.length(); i++) {
|
||||||
|
char letter = word.charAt(i);
|
||||||
|
if (guessedLetters.contains(letter)) {
|
||||||
|
display.append(letter).append(" ");
|
||||||
|
} else {
|
||||||
|
display.append("_ ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wordLabel.setText(display.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupEventHandlers() {
|
||||||
|
// Bouton deviner
|
||||||
|
guessButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
processGuess();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Entrée dans le champ texte
|
||||||
|
letterField.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
processGuess();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processGuess() {
|
||||||
|
String input = letterField.getText().toLowerCase().trim();
|
||||||
|
letterField.setText("");
|
||||||
|
|
||||||
|
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
|
||||||
|
messageLabel.setText("Veuillez entrer une seule lettre !");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char letter = input.charAt(0);
|
||||||
|
|
||||||
|
if (guessedLetters.contains(letter)) {
|
||||||
|
messageLabel.setText("Vous avez déjà essayé cette lettre !");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
guessedLetters.add(letter);
|
||||||
|
|
||||||
|
if (!word.contains(String.valueOf(letter))) {
|
||||||
|
lives--;
|
||||||
|
messageLabel.setText("Mauvaise lettre ! Il vous reste " + lives + " vies.");
|
||||||
|
} else {
|
||||||
|
messageLabel.setText("Bonne lettre ! Continuez !");
|
||||||
|
}
|
||||||
|
|
||||||
|
updateWordDisplay();
|
||||||
|
livesLabel.setText("Vies: " + lives + "/" + MAX_LIVES);
|
||||||
|
hangmanPanel.repaint();
|
||||||
|
|
||||||
|
checkGameStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkGameStatus() {
|
||||||
|
if (lives <= 0) {
|
||||||
|
endGame(false);
|
||||||
|
} else if (GameLogic.isWordGuessed(word, guessedLetters)) {
|
||||||
|
endGame(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void endGame(boolean won) {
|
||||||
|
guessButton.setEnabled(false);
|
||||||
|
letterField.setEnabled(false);
|
||||||
|
|
||||||
|
if (won) {
|
||||||
|
messageLabel.setText("VICTOIRE ! Le mot était: " + word);
|
||||||
|
JOptionPane.showMessageDialog(frame,
|
||||||
|
"Félicitations ! Vous avez gagné !\n" +
|
||||||
|
"Le mot était: " + word + "\n" +
|
||||||
|
"Vies restantes: " + lives,
|
||||||
|
"Victoire !",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
} else {
|
||||||
|
messageLabel.setText("PERDU ! Le mot était: " + word);
|
||||||
|
JOptionPane.showMessageDialog(frame,
|
||||||
|
"Désolé ! Vous avez perdu !\n" +
|
||||||
|
"Le mot était: " + word,
|
||||||
|
"Défaite",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bouton pour rejouer
|
||||||
|
int choice = JOptionPane.showConfirmDialog(frame,
|
||||||
|
"Voulez-vous rejouer ?",
|
||||||
|
"Nouvelle partie",
|
||||||
|
JOptionPane.YES_NO_OPTION);
|
||||||
|
|
||||||
|
if (choice == JOptionPane.YES_OPTION) {
|
||||||
|
restartGame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restartGame() {
|
||||||
|
word = WordSelector.pickRandomWord();
|
||||||
|
guessedLetters.clear();
|
||||||
|
lives = MAX_LIVES;
|
||||||
|
|
||||||
|
guessButton.setEnabled(true);
|
||||||
|
letterField.setEnabled(true);
|
||||||
|
|
||||||
|
updateWordDisplay();
|
||||||
|
livesLabel.setText("Vies: " + lives + "/" + MAX_LIVES);
|
||||||
|
messageLabel.setText("Nouvelle partie - Devinez le mot !");
|
||||||
|
hangmanPanel.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
new HangedGameGUI();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
BIN
PenduJudeChrist/InputHandler.class
Normal file
BIN
PenduJudeChrist/InputHandler.class
Normal file
Binary file not shown.
32
PenduJudeChrist/InputHandler.java
Normal file
32
PenduJudeChrist/InputHandler.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import java.util.Random;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
|
||||||
|
public class InputHandler {
|
||||||
|
|
||||||
|
public static char getLetter(Scanner scanner, Set<Character> guessedLetters) {
|
||||||
|
char letter;
|
||||||
|
while (true) {
|
||||||
|
System.out.print("Entrez une lettre: ");
|
||||||
|
String input = scanner.nextLine().toLowerCase().trim();
|
||||||
|
|
||||||
|
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
|
||||||
|
System.out.println("Veuillez entrer une seule lettre ! ");
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
letter = input.charAt(0);
|
||||||
|
if (guessedLetters.contains(letter)) {
|
||||||
|
System.out.println("Vous avez déjà essayé cette lettre ! ");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return letter;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
BIN
PenduJudeChrist/TD3 - DEV5.1.pdf
Normal file
BIN
PenduJudeChrist/TD3 - DEV5.1.pdf
Normal file
Binary file not shown.
BIN
PenduJudeChrist/WordSelector.class
Normal file
BIN
PenduJudeChrist/WordSelector.class
Normal file
Binary file not shown.
33
PenduJudeChrist/WordSelector.java
Normal file
33
PenduJudeChrist/WordSelector.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class WordSelector {
|
||||||
|
|
||||||
|
private static final String[] WORDS = {
|
||||||
|
"java", "python", "programmation", "ordinateur", "clavier",
|
||||||
|
"souris", "ecran", "internet", "logiciel", "algorithme",
|
||||||
|
"maison", "jardin", "livre", "table", "chaise", "porte",
|
||||||
|
"fenetre", "lampe", "bureau", "stylo", "crayon", "papier",
|
||||||
|
"fleur", "arbre", "oiseau", "chat", "chien", "poisson",
|
||||||
|
"soleil", "lune", "etoile", "ciel", "nuage", "pluie",
|
||||||
|
"vent", "neige", "terre", "eau", "feu", "air", "pierre",
|
||||||
|
"sable", "route", "ville", "rue", "voiture", "velo",
|
||||||
|
"train", "avion", "bateau", "musique", "chanson", "danse",
|
||||||
|
"peinture", "couleur", "forme", "image", "photo", "film",
|
||||||
|
"histoire", "conte", "reve", "idee", "pensee", "mot",
|
||||||
|
"phrase", "texte", "histoire", "nombre", "chiffre",
|
||||||
|
"calcul", "probleme", "solution", "question", "reponse",
|
||||||
|
"temps", "heure", "minute", "seconde", "jour", "nuit",
|
||||||
|
"matin", "soir", "semaine", "mois", "annee", "saison",
|
||||||
|
"printemps", "ete", "automne", "hiver", "famille", "ami",
|
||||||
|
"frere", "soeur", "parent", "enfant", "ecole", "classe",
|
||||||
|
"cours", "lecon", "devoir", "travail", "repos", "jeu"
|
||||||
|
};
|
||||||
|
|
||||||
|
public static String pickRandomWord(){
|
||||||
|
Random random = new Random();
|
||||||
|
return WORDS[random.nextInt(WORDS.length)];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user