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

This is killing me, can someone help me?..Write a hangman game that randomly gen

ID: 3642681 • Letter: T

Question

This is killing me, can someone help me?..Write a hangman game that randomly generates a word (declare an array to store words) and prompts the user to guess one letter. Each letter is displayed as an asterisk. When the user guesses correctly, the actual letter is displayed. When the user finishes a word, display the # of misses, then ask
the user if they want to try another word. Allow the user enter input yes/no. The program
should show how many incorrect guesses guessed, as well the # of allowed incorrect guesses
remaining.

Thanks so much:)

Explanation / Answer

please rate

import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class Hangman {
   
    String[] wordList = {"world","hello","program","hangman","game","random"};
    String guessWord;
    int noOfMisses = 0;
    int allowedMisses = 5;
    boolean[] foundChar;
    Random r = new Random();
   
    public boolean checkCompleted(){
        for(int i=0;i<foundChar.length;i++)
            if(!foundChar[i])
                return false;
        return true;
    }
   
    public void printCompletedChars(){
        for(int i=0;i<guessWord.length();i++){
            if(foundChar[i])
                System.out.print(guessWord.charAt(i));
            else
                System.out.print("*");
        }
        System.out.println();
    }
   
    public void tryToGuess(){
        Scanner input = new Scanner(System.in);
        foundChar = new boolean[guessWord.length()];
        boolean completed = false;
        while( (noOfMisses < allowedMisses) && !completed){
            printCompletedChars();
            char guess = input.next().charAt(0);
            if(guessWord.contains(guess+"")){
                for(int i=0;i<guessWord.length();i++){
                    if(guessWord.charAt(i)==guess)
                        foundChar[i] = true;
                }               
            }else{
                noOfMisses++;
            }
            completed = checkCompleted();
        }
        if(completed){
            System.out.println("You guessed correctly. ");
        }
        System.out.println("Correct answer is "+guessWord);       
        System.out.println("Total number of incorrect guesses "+noOfMisses);
    }
   
    public Hangman(){
       
        System.out.println("==================");
        System.out.println("Welcome to HANGMAN");
        System.out.println("==================");
        System.out.println("Incorrect guesses allowed "+allowedMisses);
        System.out.println("Start guessing..");       
        guessWord = wordList[r.nextInt(wordList.length)];
        tryToGuess();
    }
   
    public static void main(String[] args){
        int choice = 0;
        boolean finished = false;
        while(!finished){
            new Hangman();
            System.out.println("Do you want to continue? (y/n) ");
            try {
                choice = System.in.read();
            } catch (IOException e) { }
            if(choice=='y'){
                continue;
            }
            finished = 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