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

In several Game Zone assignments earlier in this book, you created games similar

ID: 3668997 • Letter: I

Question

In several Game Zone assignments earlier in this book, you created games similar to Hangman in which the user guesses a secret phrase by selecting a series of letters. These versions had limited appeal because each contained only a few possible phrases to guess; after playing the games a few times, the user would have memorized all the phrases. Now create a version in which any number of secret phrases can be saved to a file before the game is played. Use a text editor such as Notepad to type in any number of phrases into a file, one per line. Save the file as Phrases.txt. Then create a game that randomly selects a phrase from the file and allows a user to guess the phrase letter by letter. Save the game as SecretePhraseUsingFile.java

Explanation / Answer

I have written the java code for the game as desired by you:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;

public class SecretePhraseUsingFile {

   private static final int guess_count = 6;
   public static void main(String[] args){
       String text = "";
       try{
           FileReader fr = new FileReader("Phrases.txt"); // reading file phrases.txt
           BufferedReader bReader = new BufferedReader(fr);
           String line = bReader.readLine();
           while(line != null){
               if(line.indexOf("'") == -1){                  
               text += line+",";
               }
               line = bReader.readLine();
           }
           bReader.close();
       }
       catch(Exception e){
       //There was a problem!
           System.out.println("ERROR: There was a problem reading the file. "+
       e.getMessage());
           System.exit(1);
       }

       String[] englishWords = text.split(",");
       Random rand = new Random();

       int randNum = rand.nextInt(englishWords.length-1);
       String wordToGuess = englishWords[randNum];
       String[] lettersGuessed = new String[26];
       int lettersIndex = 0;      
       int mistakes = 0;
      
       Scanner in = new Scanner(System.in);
      
       boolean youWon = false;

       CharSequence guess = "";

       while(mistakes < guess_count && !youWon){
           display_status(mistakes);
           //Display the word to guess
           displayPhrase(wordToGuess, lettersGuessed);
           System.out.println("You have guessed:"+Arrays.toString(lettersGuessed));
           guess = in.next();
           lettersGuessed[lettersIndex] = guess.toString();
           lettersIndex = lettersIndex + 1;
           if(!wordToGuess.contains(guess)){
               mistakes = mistakes + 1;
               System.out.println("Wrong. The letter "+guess.toString()+" is not in the word.");
           }
           //Check to see if the user won
           youWon = winner(wordToGuess, lettersGuessed);
       }

       if(youWon){
           System.out.println("You win! The word is "+wordToGuess);
       }else{
           display_status(mistakes);
           System.out.println("You lose! The word is "+wordToGuess);
       }
   }
  
   public static void display_status(int mistakes){
       String line1 = " O | ";
       if(mistakes == 0){
           line1 = " | ";
       }
      
       String line2 = " | ";
       if(mistakes >= 4){
           line2 = " \|/ | ";
       }else if(mistakes == 3){
           line2 = " \| | ";
       }else if(mistakes == 2){
           line2 = " | | ";
       }
      
       String line3 = " | ";
       if(mistakes >= 5){
           line3 = " | | ";
       }

       String line4 = " | ";
       if(mistakes >= 6){
           line4 = " ^ | ";
       }

       System.out.println(
               " ----- "+
               " | | "+
       line1+//" O | "+
       line2+//" \|/ | "+
       line3+//" | | "+
       line4+//" ^ | "+
               " | "+
               " -------- ");
   }


   public static void displayPhrase(String wordToGuess, String[] lettersGuessed){
       System.out.print("Word to Guess: ");
       //Loop over each letter in wordToGuess
       for(int i = 0; i < wordToGuess.length(); i++){
           char temp = wordToGuess.charAt(i);
           if(stringArrayContains(lettersGuessed, temp)){
               System.out.print(wordToGuess.charAt(i)+" ");
           }else{
               System.out.print("_ ");
           }
       }
       System.out.println(" ");
   }
  
   public static boolean winner(String wordToGuess, String[] lettersGuessed){
       //Loop over each letter in wordToGuess
       for(int i = 0; i < wordToGuess.length(); i++){
           char temp = wordToGuess.charAt(i);
           if(!stringArrayContains(lettersGuessed, temp)){
               return false;
           }
       }
       return true;
   }
  
  
   public static boolean stringArrayContains(String[] strArray, char letter){
       for(int i = 0; i < strArray.length; i++){
           if(strArray[i] != null && strArray[i].equalsIgnoreCase(String.valueOf(letter))){
               return true;
           }
       }
       return false;
   }
  
   public static void testgame(){
       int i = 0;
       display_status(i++);
       display_status(i++);
       display_status(i++);
       display_status(i++);
       display_status(i++);
       display_status(i++);
       display_status(i++);
       display_status(i++);
   }
}

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