Hangman
package Hangman;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static Hangman.Wordlist.WORD_LIST;
public class Hangman implements ActionListener {
static final Random RANDOM = new Random();
static String chosenWord;
int wordLength;
static int lives = 6;
static List<Character> display = new ArrayList<>();
List<Character> wrongGuesses = new ArrayList<>();
static JFrame frame;
static JPanel panel;
static JLabel wordDisplay;
static JLabel hangmanArt;
static JLabel hearths;
static JLabel input_message;
static JTextField input;
public Hangman() {
chosenWord = WORD_LIST.get(RANDOM.nextInt(WORD_LIST.size()));
wordLength = chosenWord.length();
lives = 6;
frame = new JFrame();
frame.setTitle("Hangman");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(330, 350);
frame.setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.WHITE);
wordDisplay = new JLabel();
wordDisplay.setBounds(200, 70, 300, 50);
StringBuilder startDisplay = new StringBuilder();
for (int i = 0; i < chosenWord.length(); i++) {
startDisplay.append("_ ");
}
wordDisplay.setText(startDisplay.toString());
panel.add(wordDisplay);
hangmanArt = new JLabel();
hangmanArt.setBounds(20, 20, 150, 200);
hangmanArt.setIcon(Art.hangman0);
panel.add(hangmanArt);
hearths = new JLabel();
hearths.setText("Lives: " + String.valueOf(lives));
hearths.setBounds(200, 20, 50, 20);
panel.add(hearths);
input = new JTextField();
input.setBounds(104, 257, 20, 20);
input.addActionListener(this);
panel.add(input);
input_message = new JLabel("Enter a letter : ");
input_message.setBounds(20, 250, 100, 30);
panel.add(input_message);
JOptionPane.showMessageDialog(null, "To win, guess the word before the person is hung.");
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
new Hangman();
}
public static void updateLabel(char guess) {
if (chosenWord.contains(String.valueOf(guess))) {
// Update wordDisplay
StringBuilder displayString = new StringBuilder();
for (int i = 0; i < chosenWord.length(); i++) {
char c = chosenWord.charAt(i);
if (display.contains(c)) {
displayString.append(c).append(' ');
} else if (c == guess) {
displayString.append(c).append(' '); // Print the guessed letter
display.add(c); // Add the guessed letter to the display list
} else {
displayString.append("_ ");
}
}
wordDisplay.setText(displayString.toString());
if (displayString.indexOf("_") == -1) {
JOptionPane.showMessageDialog(null, "Congratulations. you guessed the word: " + chosenWord);
}
} else {
lives--;
hearths.setText("Lives: " + lives);
if (lives == 6) {
hangmanArt.setIcon(Art.hangman0);
} else if (lives == 5) {
hangmanArt.setIcon(Art.hangman1);
} else if (lives == 4) {
hangmanArt.setIcon(Art.hangman2);
} else if (lives == 3) {
hangmanArt.setIcon(Art.hangman3);
} else if (lives == 2) {
hangmanArt.setIcon(Art.hangman4);
} else if (lives == 1) {
hangmanArt.setIcon(Art.hangman5);
} else {
hangmanArt.setIcon(Art.hangman6);
JOptionPane.showMessageDialog(null, "You lost. The correct word was: " + chosenWord);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
String guess = input.getText();
if (guess.length() == 1 && Character.isLetter(guess.charAt(0))) {
char guessedLetter = guess.charAt(0);
updateLabel(guessedLetter);
input.setText(""); // Clear the input field after each guess
} else {
JOptionPane.showMessageDialog(null, "Please enter a single letter.");
input.setText(""); // Clear the input field if invalid input
}
}
}
Last updated