Create in Java a GUI for the Hangman game. The game is played between the comput
ID: 3833207 • Letter: C
Question
Create in Java a GUI for the Hangman game. 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
PLEASE USE COMMENTS
Examples:
Explanation / Answer
import java.util.Arrays; import java.util.Scanner; public class Hangman{ public static void main(String[] args) { String[] words = {"writer", "that", "program"}; // Pick random index of words array int randomWordNumber = (int) (Math.random() * words.length); // Create an array to store already entered letters char[] enteredLetters = new char[words[randomWordNumber].length()]; int triesCount = 0; boolean wordIsGuessed = false; do { // infinitely iterate through cycle as long as enterLetter returns true // if enterLetter returns false that means user guessed all the letters // in the word e. g. no asterisks were printed by printWord switch (enterLetter(words[randomWordNumber], enteredLetters)) { case 0: triesCount++; break; case 1: triesCount++; break; case 2: break; case 3: wordIsGuessed = true; break; } } while (! wordIsGuessed); System.out.println(" The word is " + words[randomWordNumber] + " You missed " + (triesCount -findEmptyPosition(enteredLetters)) + " time(s)"); } /* Hint user to enter a guess letter, returns 0 if letter entered is not in the word (counts as try), returns 1 if letter were entered 1st time (counts as try), returns 2 if already guessed letter was REentered, returns 3 if all letters were guessed */ public static int enterLetter(String word, char[] enteredLetters) { System.out.print("(Guess) Enter a letter in word "); // If-clause is true if no asterisks were printed so // word is successfully guessed if (! printWord(word, enteredLetters)) return 3; System.out.print(" > "); Scanner input = new Scanner(System.in); int emptyPosition = findEmptyPosition(enteredLetters); char userInput = input.nextLine().charAt(0); if (inEnteredLetters(userInput, enteredLetters)) { System.out.println(userInput + " is already in the word"); return 2; } else if (word.contains(String.valueOf(userInput))) { enteredLetters[emptyPosition] = userInput; return 1; } else { System.out.println(userInput + " is not in the word"); return 0; } } /* Print word with asterisks for hidden letters, returns true if asterisks were printed, otherwise return false */ public static boolean printWord(String word, char[] enteredLetters) { // Iterate through all letters in word boolean asteriskPrinted = false; for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.