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

Executable Class CincoGuesser Class CincoGuesser & Class Dictionary Make sure to

ID: 3758560 • Letter: E

Question

Executable Class CincoGuesser

Class CincoGuesser & Class Dictionary

Make sure to save time to test your program, some of these functions will be tricky to code! Program Description In this program, you will be creating a word guessing game called Cinco, except that in your program the computer will guess the words, not a human player! In this game, the human will pick a (legal) secret five letter word from the list of legal secret words. The computer guesses a word. A word is only a legal guess if it appears in the Dictionary.

A word is a legal secret word only if it appears in the Dictionary and does not have a repeated letter. A word list of about 2400 five letter words is provided1 (click this link to get the word list). Note, that the program can be run with different length word lists. I suggest making your own (very short) word lists to greatly simplfy your initial testing! The file will always consist of five-letter words, one per line, all in lowercase and in alphabetical order. Your program will always read its word list from a file named cinco-words.txt. Each round, the computer guesses a word from its list of words.

After the computer guesses, the human enters the number of letters in the guess that also exist in the secret word and how many of those letters are in the correct spot. If the score is five and five, then the computer has guessed your secret word and the game ends, printing out how many guesses it took the computer to win. If the socre is less than five and five, the computer will update its Dictionary (by removing one or more words from its word list) and then makes its next guess.

If the computer runs out of words to guess, it will inform the player that they must have made a mistake or cheated. Some sample runs are provided below. Any letter in your secret word should only be matched once. For example, Your secret word: fable Computer's guessed word: eagle results in Matching: 3 In-Place: 3 and not Matching: 4 In-Place: 3 This program should be constructed using two classes. One class, named Dictionary, will take care of inputting and storing the words from a file, selecting a random word from the list (for each guessed word by the computer,) and removing one or more words from the list after the computer's guess (this is the hardest part of the assignment.)

It should also include one or more helper functions since removing words from the list involves several steps. Secret words must have five different letters. For example, eagle is a valid guess, but not a valid secret word. The other class, named CincoGuesser, will keep track of the game state, handle the user input and output, and contain the main function. The main game loop (which will include all human i/o over STDIN and STDOUT) must be in a function named consoleUI().

A required main() function for the CincoGuesser class is provided. Review the starter code for more information about these classes and members. Your output must match the style and formatting of the example, as the testing may be automated. This is a program with many details. I strongly urge you to keep your program compiling and running at all stages of your project. Keep all your functions simple at first—the minimum to compile and run. Add code to one function at a time and test as you code.

Provided starter code ::

Sample Execution::

This should be everything, just two things to compile CincoGuesser.java & Dictionary.java

Should include main function, also this teacher is really strict so anything that is NOT coherent with the program basics ie: number input or less than or more than 5 letter guesses would need to be accounted for. So file exception errors etc... Thanks

Explanation / Answer

import java.util.Scanner;

   

/**

  * Plays a word guessing game with one player.

  */

public class WordGuess {

  

    public static void main(String[] args) {

        final String SECRET_WORD = "BRAIN";

        final String FLAG = "!";

        String wordSoFar = "", updatedWord = "";

        String letterGuess, wordGuess = "";

        int numGuesses = 0;

        Scanner input = new Scanner(System.in);

          

        /* begin game */

        System.out.println("WordGuess game. ");

        for (int i = 0; i < SECRET_WORD.length(); i++) {

            wordSoFar += "-";                               //word as dashes

        }

        System.out.println(wordSoFar + " ");               //display dashes

      

        /* allow player to make guesses*/

        do {

            System.out.print("Enter a letter (" + FLAG + " to guess entire word): ");

            letterGuess = input.nextLine();

            letterGuess = letterGuess.toUpperCase();            

          

            /* increment number of guesses */

            numGuesses += 1;

              

            /* player correctly guessed a letter--extract string in wordSoFar up to the letter

33

             * guessed and then append guessed letter to that string. Next, extract rest of

34

             * wordSoFar and append after the guessed letter

35

             */

36

            if (SECRET_WORD.indexOf(letterGuess) >= 0) {

37

                updatedWord = wordSoFar.substring(0, SECRET_WORD.indexOf(letterGuess));

38

                updatedWord += letterGuess;                                             

                updatedWord += wordSoFar.substring(SECRET_WORD.indexOf(letterGuess)+1, wordSoFar.length());

                wordSoFar = updatedWord;

            }

                  

            /* display guessed letter instead of dash */

            System.out.println(wordSoFar + " ");

        } while (!letterGuess.equals(FLAG) && !wordSoFar.equals(SECRET_WORD));

          

        /* finish game and display message and number of guesses */

        if (letterGuess.equals(FLAG)) {

            System.out.println("What is your guess? ");

            wordGuess = input.nextLine();

            wordGuess = wordGuess.toUpperCase();

        }

        if (wordGuess.equals(SECRET_WORD) || wordSoFar.equals(SECRET_WORD)) {

            System.out.println("You won!");     

        } else {

            System.out.println("Sorry. You lose.");

        }

        System.out.println("The secret word is " + SECRET_WORD);

        System.out.println("You made " + numGuesses + " guesses.");             

    }

} //end of program

import java.io.*;

import java.lang.*;

import java.util.*;

class GuessWord

{

      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 Guess a Word ");

            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 Guess a Word

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

import java.util.Scanner;