Write an interactive program that plays a game of hangman St

Write an interactive program that plays a game of hangman. Store the word to be guessed in successive elements of an array of individual characters called word. The player must guess the letters belonging to word. The program should terminate when either all letters have been guessed correctly (the player wins) or a specified number of incorrect guesses have been made (the computer wins).

Solution

Answer:

import java.util.*;
   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;
      }
   

   }

 Write an interactive program that plays a game of hangman. Store the word to be guessed in successive elements of an array of individual characters called word
 Write an interactive program that plays a game of hangman. Store the word to be guessed in successive elements of an array of individual characters called word

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site