Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create in Java a GUI for the Hangman game. This project is about a word guessing

ID: 3832885 • Letter: C

Question

Create in Java a GUI for the Hangman game. This project is about a word guessing game called “Hangman”. The game is played between the computer and one player. The player needs to guess the correct word by guessing a single character each time.

The GUI should have the following:

Level 1:

Your program should be able to work with at least 20 words.

The words may have different sizes.Therefore, your program should handle words of various sizes When the game begins, one of these twenty words are chosen randomly from an array.

Display a row of dashes the length of the random word

All 26 alphabets should be displayed initially

After each guess, the guessed alphabet is removed from the display, so that the player knows that the alphabets remaining on the screen have not been used yet.

One button to restart the game

Another button to begin a new game

One field to display score

One field that shows the word in terms of blanks (dashes). These blanks should be populated with the right alphabet after each correct guess.

One field showing the number of wrong guesses

One field showing the number of wrong guesses allowed

Alphabet Selection – By entering the alphabet or clicking on an alphabet

Alphabet is entered into a textfield by the player and press a button called “Guess”. This should notify the program that a new guess has been made

OR

All the alphabets are displayed as buttons and the player selects an alphabet by clicking on the appropriate button. The button click should inform the program that a new guess has been made.

If the player made 10 incorrect guesses, the program should notify the player that the game has ended and they have lost because the player used up all their guesses.

If the player correctly guesses the word, before using up all their guesses the game should notify the player that they have won

Examples:

Level 2: The game with a few updates

A GUI with Stick Figure instead of the “number of incorrect guesses” field that builds up based on each incorrect guess.

A timer function that ends the game after a certain period of time.

Example:

Level 3: Words read from a file in place of being hardwired into an array

Read a file in that has all the words stored as one word per line

Store each word locally

Everything else should be like levels 1 and 2.

Extra credit (2%): Add an interesting background to your GUI instead of leaving it plain.

PLEASE INCLUDE COMMENTS

incorrect guesses 0 out of 10 NEW RESTART A B C DIE FIGH IIJ K L M NO P Q R S T Score: Incorrect guesses 0 out of 10 NEW RESTART GUESS A B C DE F G H I J KIL MIN O P Q R S T U V W TY Z Score:

Explanation / Answer

I have written the program which clearly describes you about each and every function of the hangman game which is helpful to understand the code by reading out the comments.

Program:-

// The import statements are comprises of many classes and methods.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

/* I have created a class called playHangManGame by extending the JPanel import file */

public class playHangManGame extends JPanel {

private Display screenDisplay;

private ArrayList < JButton > alphabet = new ArrayList < JButton > ();
private JButton nextButton;
private JButton quitOption;
private String message;
private WordList WordPattern;
private String currentWord;
private String randomGuesses;
private boolean finishGameOver;
private int timepassGuesses;

/* This class buttonHandler helps the user to play the game or quit. */

private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JButton whichButton = (JButton) evt.getSource();
String cmd = evt.getActionCommand();
if (cmd.equals("Quit")) {
System.exit(0);
} else {
message = "You can use the command " + cmd;
}
screenDisplay.repaint();
}
}

// It will display the current Context for the user which is required

private class Display extends JPanel {
Display() {
setPreferredSize(new Dimension(720, 350));
setBackground(new Color(150, 270, 110));
setFont(new Font("Times New Roman", Font.NORMAL, 70));
}
protected void paintComponentValue(Graphics grph) {
super.paintComponentValue(grph);
((Graphics2D) grph).setStroke(new BasicStroke(3));
if (message != null) {
grph.setColor(Color.BLUE);
grph.drawString(message, 50, 25);
}
}
}


// This function helps the user to start playing the application

public playHangManGame() {

ButtonHandler btnHandlr = new ButtonHandler();

screenDisplay = new Display();
JPanel bottom = new JPanel();

setLayout(new BorderLayout(5, 5));
add(screenDisplay, BorderLayout.CENTER);
add(bottom, BorderLayout.NORTH);

nextButton = new JButton("You're Next word is");
nextButton.addActionListener(btnHandlr);
bottom.add(nextButton);

quitOption = new JButton("You wanna GIVE UP.?");
quitOption.addActionListener(btnHandlr);
bottom.add(quitOption);

JButton quitHand = new JButton("Want to QUIT.?");
quitHand.addActionListener(btnHandlr);
bottom.add(quitHand);


setBackground(new Color(100, 50, 210));
setBorder(BorderFactory.createLineBorder(new Color(100, 50, 210), 1));

WordPattern = new WordList("WordPattern.txt");

startHangmanGame();

}

// It will display the random words to be guessed and check the correct answer

private void startHangmanGame() {
finishGameOver = false;
randomGuesses = "";
timepassGuesses = 0;
nextButton.setEnabled(false);
for (int i = 0; i < alphabet.size(); i++) {
alphabet.get(i).setEnabled(true);
}
quitOption.setEnabled(true);
int index = (int)(Math.random() * WordPattern.getWordCount());
currentWord = WordPattern.removeWord(index);
currentWord = currentWord.toUpperCase();
message = "The current word has the " + currentWord.length() + " letters. So, Its time to play the HANGMAN Game";
}

// To check whether word matches and completed with given missing words.

private boolean checkCompletedWord() {
for (int i = 0; i < currentWord.length(); i++) {
char ch = currentWord.charAt(i);
if (randomGuesses.indexOf(ch) == -1) {
return false;
}
}
return true;
}

// It is the main application of the java program

public static void main(String[] args) {
JFrame wind = new JFrame("Hangman Game");
playHangManGame panel = new playHangManGame();
wind.setContentPane(panel);
wind.pack();
wind.setResizable(true);
wind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
wind.setLocation((screen.width - wind.getWidth()) / 2,
(screen.height - wind.getHeight()) / 2);
wind.setVisible(true);
}

}
/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote