2 Commits

Author SHA1 Message Date
c14e481572 tkt 2025-10-08 12:28:34 +02:00
86537ff528 pendu 1 2025-10-08 12:23:02 +02:00
12 changed files with 1122 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,28 @@
import java.io.*;
import java.util.*;
public class ChooseWord {
/*Fonction pour choisir le mot aléatoirement*/
public static String chooseTheWord() {
List<String> words = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("motsFacile.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.trim().isEmpty()) {
words.add(line.trim().toLowerCase());
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (words.isEmpty()) {
return "default";
}
Random rand = new Random();
return words.get(rand.nextInt(words.size()));
}
}

Binary file not shown.

View File

@@ -0,0 +1,79 @@
import java.util.*;
public class GameState {
private String word;
private char[] hiddenWord;
private int errors;
private Set<Character> triedLetters;
private static final int MAX_ERRORS = 9;
public GameState(String wordToGuess) {
this.word = wordToGuess.toLowerCase();
this.hiddenWord = new char[word.length()];
Arrays.fill(hiddenWord, '_');
this.triedLetters = new HashSet<>();
this.errors = 0;
}
/*Fonction pour essayer une lettre*/
public void tryLetter(char letter) {
letter = Character.toLowerCase(letter);
triedLetters.add(letter);
boolean found = false;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == letter) {
hiddenWord[i] = letter;
found = true;
}
}
if (!found) {
errors++;
}
}
/*Fonction pour vérifier si une lettre à déjà été essayé*/
public boolean hasTriedLetter(char letter) {
letter = Character.toLowerCase(letter);
return triedLetters.contains(letter);
}
/*Fonction pour vérifier si on a gagné*/
public boolean isWon() {
for (char c : hiddenWord) {
if (c == '_') {
return false;
}
}
return true;
}
/*Fonction pour vérifier si on a perdu*/
public boolean isLost() {
return errors >= MAX_ERRORS;
}
/*Fonction pour voir le nombre d'erreur*/
public int getErrors() {
return errors;
}
/*Fonction pour voir le mot caché*/
public String getHiddenWord() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hiddenWord.length; i++) {
sb.append(hiddenWord[i]);
if (i < hiddenWord.length - 1) {
sb.append(' ');
}
}
return sb.toString();
}
/*Fonction pour voir le mot*/
public String getWord() {
return word;
}
}

Binary file not shown.

View File

@@ -0,0 +1,33 @@
import javax.swing.*;
import java.awt.*;
public class HangmanPanel extends JPanel {
private int errors = 0;
/*mettre à jour les erreurs*/
public void setErrors(int errors) {
this.errors = errors;
repaint();
}
/*Dessiner le pendu*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(50, 300, 200, 300);
g.drawLine(125, 300, 125, 50);
g.drawLine(125, 50, 250, 50);
g.drawLine(250, 50, 250, 80);
if (errors > 0) g.drawOval(230, 80, 40, 40);
if (errors > 1) g.drawLine(250, 120, 250, 200);
if (errors > 2) g.drawLine(250, 140, 220, 170);
if (errors > 3) g.drawLine(250, 140, 280, 170);
if (errors > 4) g.drawLine(250, 200, 220, 250);
if (errors > 5) g.drawLine(250, 200, 280, 250);
if (errors > 6) g.drawLine(230, 90, 270, 90);
if (errors > 7) g.drawString("X", 240, 100);
if (errors > 8) g.drawString("X", 255, 100);
}
}

BIN
PenduWIlfried/main$1.class Normal file

Binary file not shown.

BIN
PenduWIlfried/main.class Normal file

Binary file not shown.

91
PenduWIlfried/main.java Normal file
View File

@@ -0,0 +1,91 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class main {
public static GameState gameState;
public static JLabel wordLabel;
public static HangmanPanel hangmanPanel;
public static JTextField inputField;
public static JLabel messageLabel;
/*Fonction main*/
public static void main(String[] args) {
String selectedWord = ChooseWord.chooseTheWord();
gameState = new GameState(selectedWord);
createInterface();
}
/*Fonction pour créer l'interface*/
public static void createInterface() {
JFrame window = new JFrame("HangmanGame");
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
wordLabel = new JLabel(gameState.getHiddenWord(), SwingConstants.CENTER);
wordLabel.setFont(new Font("Arial", Font.BOLD, 40));
window.add(wordLabel, BorderLayout.NORTH);
hangmanPanel = new HangmanPanel();
window.add(hangmanPanel, BorderLayout.CENTER);
JPanel inputPanel = new JPanel();
inputField = new JTextField(2);
JButton submitButton = new JButton("Try Letter");
messageLabel = new JLabel("Enter a letter:");
inputPanel.add(messageLabel);
inputPanel.add(inputField);
inputPanel.add(submitButton);
window.add(inputPanel, BorderLayout.SOUTH);
/*evenement du bouton*/
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleLetterInput();
}
});
window.setVisible(true);
}
/*Fonction pour mettre à jour le pendu*/
public static void handleLetterInput() {
System.out.println(gameState.getWord());
System.out.println(gameState.getWord().length());
String input = inputField.getText().toLowerCase();
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
messageLabel.setText("Enter a single valid letter.");
return;
}
char letter = input.charAt(0);
inputField.setText("");
if (gameState.hasTriedLetter(letter)) {
messageLabel.setText("Letter already tried.");
return;
}
gameState.tryLetter(letter);
wordLabel.setText(gameState.getHiddenWord());
hangmanPanel.setErrors(gameState.getErrors());
if (gameState.isWon()) {
messageLabel.setText("Congratulations! You've won!");
inputField.setEditable(false);
} else if (gameState.isLost()) {
messageLabel.setText("You lost! Word was: " + gameState.getWord());
inputField.setEditable(false);
} else {
messageLabel.setText("Keep guessing...");
}
}
}

View File

@@ -0,0 +1,87 @@
petit chat
grand arbre
voiture rouge
soleil brillant
fleur jaune
eau claire
montagne haute
ciel bleu
oiseau chanteur
jardin secret
livre ancien
musique douce
temps froid
rue calme
pont vieux
chien fidèle
nuit noire
lune pleine
feu rapide
vent fort
mer agitée
plage dorée
neige blanche
herbe verte
clé perdue
main droite
pied gauche
nez fin
main froide
jour gris
fête joyeuse
rire franc
sac lourd
boue épaisse
seau plein
bête sauvage
chaton joueur
fromage frais
café noir
bois dur
miel doux
âne têtu
nager vite
jouet cassé
roi puissant
reine belle
vase fragile
fleur fanée
voix douce
temps perdu
sable chaud
neuf brillant
souris rapide
pont solide
coeur tendre
lait frais
pied nu
faux plat
rare gemme
tôt matin
lent pas
fin courte
rue étroite
rose rouge
arête dure
doux parfum
sec désert
haut sommet
bas fond
lent mouvement
fer solide
vers libre
vent froid
sel fin
sou dur
rat gris
vin rouge
oui clair
non net
jus sucré
riz blanc
sel épais
jeu amusant
air frais
eau pure
sol dur
feu vif

File diff suppressed because it is too large Load Diff

110
PenduWIlfried/motsMoyen.txt Normal file
View File

@@ -0,0 +1,110 @@
ordinateur
téléphone
bibliothèque
calendrier
aventure
document
ordinateur
musicien
ordinateur
philosophie
restaurant
chocolat
photographie
laboratoire
impression
pédagogique
température
municipale
conversation
influence
architecture
horizon
incroyable
profession
développement
expérience
recherche
université
télévision
ordinateur
démocratie
connaissance
créativité
éducation
électronique
exercice
information
intelligence
organisation
participation
présentation
recommandation
réussite
situation
stratégie
technologie
travail
valeur
véhicule
vocabulaire
vulnérable
architecture
automobile
bibliothèque
biographie
catégorie
champion
climatique
collection
communication
compétence
conférence
connaissance
construction
consultation
contribution
coordination
déclaration
démonstration
développement
différence
difficulté
diplomatie
économie
éducation
électrique
électronique
élément
émotion
entreprise
équipe
espace
essentiel
exemple
expérience
exposition
fabrication
famille
fonction
formation
génération
gestion
habitation
histoire
identité
immédiat
importance
individu
industrie
information
initiative
instruction
intégration
intérêt
introduction
investissement
invitation
journaliste
justification
langue