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. Also please provide a outp

ID: 3692840 • Letter: W

Question

Write a program in java to play a word-guessing game. Also please provide a output for this program it's the only way I can know for certain the program works. Also besides the code put the data text file and score test file and idicate where they are placed.

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 top5NameList = new ArrayList(); 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

Please follow the code and comments for description :

CODE :  
  
  
  
import java.io.*;

import java.lang.*;

import java.util.*;

class WordGuess

{

      public static int ReadWordsFromFile(String[] words)

      {

            try

            {

                  FileReader fr = new FileReader("words_input.txt");

                  BufferedReader br = new BufferedReader(fr);

                  int count = 0;

                  for (int i = 0; i < 100; i++)

                  {

                        String s = br.readLine();

                        if (s == null)

                              break;

                        words[count++] = s;

                  }

                  fr.close();

                  return count;

            }

            catch (FileNotFoundException e)

            {

                  System.out.println(e.getMessage());

                  return -1;

            }

            catch (IOException err)

            {

                  System.out.println(err.getStackTrace());

                  return -1;

            }

      }

      static public String ReadString()

      {

            try

            {

                  String inpString = "";

                  InputStreamReader input = new InputStreamReader(System.in);

                  BufferedReader reader = new BufferedReader(input);

                  return reader.readLine();

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

            return "";

      }

      public static void main(String[] args)

      {

            System.out.println("Welcome to Word Guessing ");

            String[] words = new String[100];

         

            int count = ReadWordsFromFile(words);

            if (count < 0)

            {

                  System.out.println("No words found in the file");

                  return;

            }

            if (words == null)

                  return; // Exception message was already shown

            int x = (int)(Math.random() * 100);

            int guessX = (x % count);

            String secretWord = words[guessX];

            int numChars = secretWord.length();

            System.out.print("Your secret word is: ");

            for(int i = 0; i < numChars; i++)

                  System.out.print("*");

            System.out.println();

            boolean bGuessedCorrectly = false;

            System.out.println("Guess now (To stop the program, enter #) : ");

            while (true)

            {

                  String choice = ReadString();

                  if (choice.startsWith("#"))

                        break;

                  if (choice.compareTo(secretWord) == 0)

                  {

                        bGuessedCorrectly = true;

                        break;

                  }

                  for (int i = 0; i < numChars; i++)

                  {

                        if (i < secretWord.length() &&

                              i < choice.length())

                        {

                              if (secretWord.charAt(i) == choice.charAt(i))

                                    System.out.print(choice.charAt(i));

                              else

                                    System.out.print("*");

                        }

                        else

                              System.out.print("*");

                  }

                  System.out.println();

            }

            if (bGuessedCorrectly == false)

                  System.out.println("Unfortunately you did not guess it correctly. The secret word is: " + secretWord);

            else

                  System.out.println("Congrats! You have guessed it correctly");       

      }

}


OUTPUT :

// contents of words_input.txt

computer

science

school

schooling

information

technology

Welcome to Word Guessing

Your secret word is: **********

Guess now (To stop the program, enter #) :

Kathir

***h******

School

**********

Tech

*ech******

techno

techno****

technology

Congrats! You have guessed it correctly  
  
  
  
The maximum part of the question has been done. The only thing I could not acheive is saving the score in a file.
  
Hope this is helpful.

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