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

In this homework, we will implement the game Hangman . This is how your program

ID: 3691160 • Letter: I

Question

In this homework, we will implement the game Hangman. This is how your program will work.

It will begin with a welcome message.

Then it will ask the user to enter the name of a file containing several phrases. These are the phrases the player will be asked to guess. These phrases should be loaded into an array.

During each round, a phrase will be chosen randomly from this list for the user to guess. The player will try to guess the letters in the phrase. As she guesses, the letters she correctly guesses will be revealed. She will receive 10 points for each matching letter, and she will lose 5 points for each incorrect guess. After each guess, the current pattern of guessed and unguessed letters in the phrase will be revealed, as well as the Hangman picture. The Hangman picture will depict the gallows along with up to 7 segments: the head, the torso, the left arm, the right arm, the waist, the left leg, and the right leg. If the player makes 7 incorrect guesses, she loses that round, but she wins the round if she correctly guesses the phrase before making 7 incorrect guesses. The number of points the player earned during that round will be shown at the end of the round.

The player will be asked if she wants to play another round at the end of each round. Once the player decides that she no longer wishes to continue, the program will show the maximum score the player earned.

Your program will be graded as follows:

You use a function called welcome to print the welcome message. (1 point)

You load the phrases from a file identified by the user into an array of strings. The array should be flexible in size, meaning that you should distinguish between count and capacity. (3 points)

Your program uses a function called chooseRandomPhrase to select a random phrase from the list of phrases each round (2 points)

Your program generates and manages the pattern of guessed and unguessed letters. (3 points)

Your program correctly identifies and reveals letters as you guess them using a function called checkLetter. (3 points)

Your program keeps track of the score by rewarding each matching letter with 10 points and deducting 5 points for each incorrect guess. (2 points)

Your program uses a function called printHangman to print the current hangman picture based on the number of incorrect guesses (4 points)

Your program ends each round if either the player guesses the phrase or the player incorrectly guessed too many times. (2 points)

Your program asks the user if she wants to solve another puzzle at the end of each round and resets values appropriately (2 points)

Your program keeps track of and shows the maximum score at the end of the game. (2 points)

Your submit the program as Hangman_YourLastName.java. (1 point)

Your comment your code sufficiently (1 point)

So, this program is worth 25 points.

Note that 15 points will be deducted if the program doesn't compile, and 10 points will be deducted if it crashes in the middle of running it.

Here is some sample output:

************ WELCOME TO HANGMAN ************

We'll show you some phrases, and you'll

guess the letters. You'll get 10 points

for each correct guess, and you'll lose 5

points for each incorrect guess. Good luck!

********************************************

Enter the name of the file containing phrases: hangman.txt

__________

|     |  

|

|

|

|

|

==============

Current string: *** *******

Enter guess: I

Great. That letter was found.

__________

|     |  

|

|

|

|

|

==============

Current string: *** *I*****

Enter guess: M

Sorry. That letter was not found.

__________

|     |  

|     0  

|

|

|

|

==============

Current string: *** *I*****

Enter guess: O

Sorry. That letter was not found.

__________

|     |  

|     0  

|     |  

|

|

|

==============

Current string: *** *I*****

Enter guess: C

Sorry. That letter was not found.

__________

|     |  

|     0  

|    /|  

|

|

|

==============

Current string: *** *I*****

Enter guess: T

Great. That letter was found.

__________

|     |  

|     0  

|    /|  

|

|

|

==============

Current string: T** *I*****

Enter guess: H

Great. That letter was found.

__________

|     |  

|     0  

|    /|  

|

|

|

==============

Current string: TH* *I*****

Enter guess: E

Great. That letter was found.

__________

|     |  

|     0  

|    /|  

|

|

|

==============

Current string: THE *I***E*

Enter guess: G

Great. That letter was found.

__________

|     |  

|     0  

|    /|  

|

|

|

==============

Current string: THE *IGG*E*

Enter guess: W

Great. That letter was found.

__________

|     |  

|     0  

|    /|  

|

|

|

==============

Current string: THE WIGG*E*

Enter guess: L

Great. That letter was found.

__________

|     |  

|     0  

|    /|  

|

|

|

==============

Current string: THE WIGGLE*

Enter guess: S

Great. That letter was found.

Congratulations. You have solved the string THE WIGGLES.

Your score was 85.

Try again? Y

__________

|     |  

|

|

|

|

|

==============

Current string: ****** ******

Enter guess: I

Sorry. That letter was not found.

__________

|     |  

|     0  

|

|

|

|

==============

Current string: ****** ******

Enter guess: M

Great. That letter was found.

__________

|     |  

|     0  

|

|

|

|

==============

Current string: M***** ******

Enter guess: O

Great. That letter was found.

__________

|     |  

|     0  

|

|

|

|

==============

Current string: MO**** ******

Enter guess: J

Great. That letter was found.

__________

|     |  

|     0  

|

|

|

|

==============

Current string: MOJ*** ******

Enter guess: X

Sorry. That letter was not found.

__________

|     |  

|     0  

|     |  

|

|

|

==============

Current string: MOJ*** ******

Enter guess: Y

Sorry. That letter was not found.

__________

|     |  

|     0  

|    /|  

|

|

|

==============

Current string: MOJ*** ******

Enter guess: N

Sorry. That letter was not found.

__________

|     |  

|     0  

|    /|

|

|

|

==============

Current string: MOJ*** ******

Enter guess: J

Sorry. That letter was not found.

__________

|     |  

|     0  

|    /|

|     =

|

|

==============

Current string: MOJ*** ******

Enter guess: Q

Sorry. That letter was not found.

__________

|     |  

|     0  

|    /|

|     =

|    /

|

==============

Current string: MOJ*** ******

Enter guess: S

Great. That letter was found.

__________

|     |  

|     0  

|    /|

|     =

|    /

|

==============

Current string: MOJ*** **S***

Enter guess: Z

Sorry. That letter was not found.

Sorry. The phrase was MOJAVE DESERT.

Your score was 5.

Try again? N

Your max score was 85.

Thank you for playing.

Explanation / Answer

Answer :

import java.util.Scanner;
   import java.io.*;

    public class Hangmangame
   {
       public static void main(String[] args) throws IOException
      {
         Scanner sc = new Scanner(System.in);
         char repeat = 'n';
         String unknown;
         StringBuffer interupts;
         final int MAXCOUNT = 6;
         int parts_of_body;
         boolean visit;
         String guess;
         String count_guess;
         char alpha_char;
        
         Scanner inputfile = new Scanner(new FileReader("words.txt"));
    
         do {
       
            unknown = inputfile.next();
            count_guess = "";
            visit = false;
            parts_of_body = MAXCOUNT;
       
       
            interupts = makeinterupts(unknown);
         
            while (! visit)
            {
               System.out.println("Here is your word: " + interupts);
               System.out.println("count_guess so far: " + count_guess);
               System.out.print("enter a guess (alpha_char or word): ");
               guess = sc.next();
          
          
               if (guess.length() > 1)
               {
                  if (guess.equals(unknown))
                     System.out.println("you win the game!");
                  else
                     System.out.println("you lost the game");
                  visit=true;
               }
               else
               {
                  alpha_char = guess.charAt(0);
                  count_guess += alpha_char;
                  if (unknown.indexOf(alpha_char) < 0)
                  {   --parts_of_body;
                     System.out.print("bad guess - ");
                  }
                  else
                  {
              
                     matchalpha_char(unknown, interupts, alpha_char);                                 
                  }
                  System.out.println(parts_of_body + "bodyparts are left");
                  if (parts_of_body == 0)
                  {   System.out.println("you lose");
                     visit = true;
                  }
                  if (unknown.equals(interupts.toString()))
                  {   System.out.println("you win!");
                     visit = true;
                  }
               }
          
            }
       
            if (inputfile.hasNext())
            {
               System.out.print("play repeat (y/n)?: ");
               repeat = sc.next().charAt(0);
            }
            else
               System.out.println("thanks for playing (no more words)");
         } while (inputfile.hasNext() && (repeat == 'Y' || repeat == 'y'));
      }



       public static void matchalpha_char(String unknown, StringBuffer interupts, char alpha_char)
      {
         for (int index = 0; index < unknown.length(); index++)
            if (unknown.charAt(index) == alpha_char)
               interupts.setCharAt(index, alpha_char);
         System.out.print("nice guess - ");
      }

       public static StringBuffer makeinterupts(String s)
      {
         StringBuffer interupts = new StringBuffer(s.length());
         for (int count=0; count < s.length(); count++)
            interupts.append('-');
         return interupts;
      }
   

   }

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