Java - Applet - Guess the Word

"Guess the Word" game in Java applet using an array of words. The user has to input a letter and the applet will check if the letter is in the word. The user has a limited number of guesses before losing the game.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class GuessTheWord extends Applet implements ActionListener {
    private final String[] words = {"computer", "java", "program", "language", "developer"};
    private String word;
    private int guesses;
    private String displayWord;
    private TextField inputField;
    private Label resultLabel;
    private Button guessButton;

    public void init() {
        word = words[(int) (Math.random() * words.length)];
        guesses = 6;
        displayWord = new String(new char[word.length()]).replace('\0', '_');
        Label promptLabel = new Label("Guess a letter: ");
        inputField = new TextField(1);
        inputField.addActionListener(this);
        guessButton = new Button("Guess");
        guessButton.addActionListener(this);
        resultLabel = new Label("");
        add(promptLabel);
        add(inputField);
        add(guessButton);
        add(resultLabel);
    }

    public void actionPerformed(ActionEvent e) {
        String letter = inputField.getText();
        if (letter.length() != 1) {
            resultLabel.setText("Please enter a single letter.");
        } else {
            boolean correct = false;
            String newDisplayWord = "";
            for (int i = 0; i < word.length(); i++) {
                if (word.charAt(i) == letter.charAt(0)) {
                    newDisplayWord += letter.charAt(0);
                    correct = true;
                } else {
                    newDisplayWord += displayWord.charAt(i);
                }
            }
            if (!correct) {
                guesses--;
                resultLabel.setText("Incorrect. " + guesses + " guesses left.");
            } else {
                displayWord = newDisplayWord;
                if (displayWord.equals(word)) {
                    resultLabel.setText("You win!");
                    guessButton.setEnabled(false);
                } else {
                    resultLabel.setText("Correct. " + guesses + " guesses left.");
                }
            }
            inputField.setText("");
            repaint();
        }
    }

    public void paint(Graphics g) {
        g.drawString(displayWord, 10, 100);
    }
}

In this applet, the words are stored in an array called words. The applet initializes by choosing a random word from the array and creating a displayWord that shows the letters of the word as underscores.

The user inputs a letter in the inputField and clicks the guessButton or presses enter. The applet checks if the letter is in the word and updates the displayWord accordingly. If the letter is not in the word, the guesses counter is decremented. If the user has no more guesses left, the game is over.

The result of the guess is displayed in the resultLabel at the bottom of the applet. If the user wins, the guessButton is disabled so they cannot continue playing.