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

Write a program in java to play a word-guessing game like Hangman. Also please p

ID: 3691353 • Letter: W

Question

Write a program in java to play a word-guessing game like Hangman. Also please provide a output for ths program it's the only way I can know for certain the program works.

Requirements:

It must randomly choose a word from a list of words. All words must be read from a data text file which contains at least 15 words. Each word includes at least 8 letters because it’s relatively easier to guess.

It must stop when all the letters are guessed. Program will ask player whether to keep going or quit.

It must give them limited tries and stop after they run out. (20 tries limitation)

It must display letters they have already guessed (either only the incorrect guesses or all guesses).

It must record player’s score and write the score into the score text file before user quit the game if the score belongs to the top 5 scores. That means the score file will store the 5 highest scores and the irrelative player names. At the beginning of the game, the top 5 scores list will be shown on the screen.

Score rules: 3 points for no missed letter,

2 points for 4 or less than 4 missed letters,

1 point for equal to or less than 7 tries.

Strategy:

(1) Define some array variables (or use ArrayList better) to contain word list, top five scores, and top five names.

(2) Define some String variables to contain guessed letters. Use string methods to print out every step guess.

(3) Define a variable to contain the score.

(4) At the beginning, program will read data file and score file and show score file content on the screen.

(5) Before program quits, it will compare the score player got to the top 5 scores. If the score is bigger than the fifth highest record, the top 5 records will be updated and the score file will be updated as well.

Hint:You may need to compare two string, but if they are not same type, a compare function can be created, in which program can compare each element and return a boolean value.

Hint: For input, Scanner can be used, but when program needs to read data from file, and when to read data from keyboard, the parameters are different. For example: Read data from file: File dataFile = new File(“data.txt”); Scanner input = new Scanner(dataFile); Read data from keyboard: Scanner input = new Scanner(System.in);

Hint: For output, Form atter can be used. For example: Formatter output = new Formatter(“score.txt”); output.format(“%d%d%s ", i+1, top5ScoreList.get(i),top5NameList.get(i)); System.out.printf(“%d%d%s ", i+1, top5ScoreList.get(i),top5NameList.get(i));

Output looks like the following:

This output is just an example. You may make it better.

Hint: To insert one value into an array, you may use ArrayList<> type and use add(index, name) method. For example:List<String> top5NameList = new ArrayList<String>(); top5NameList.add(index, name);

Word:-- eciicati Guess: n Word:__ Misses: Guess: Word:- eci_icatio Miasess Guess: P Word PeC cation Miosea: Gueas: u peciication Miasest 1u Guess: Word: PeCi ication Guesn:

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