Enhance the Number Guessing Game program from the exercises, changing it to a Gu
ID: 3736871 • Letter: E
Question
Enhance the Number Guessing Game program from the exercises, changing it to a Guess the Letter" game. 1. Ask the user for a phrase, at least 5 characters in length; example: "Dogs always wag their tails" and remove the spaces in the string (use the String method .replaceAll(" "."")) With the length L of the string, calculate a random character position P, and the number of chances C to guess the number (C-L+5); example: string length L-23, P is random valuefrom 1.23, C-23-5-28 2. 3. Loop while the user has not guessed the correct character and number of chances C has not run out a. For each time the user guesses, i. If the guess is correct, the program displays, "Congrats! You guessed correctly in N guesses" where N is the number of guesses (loops) that have occurred; and the loop stops - ii. If the guess is incorrect, the program displays, Try again. You have only R guesses remaining" where R the number of remaining guesses (R C-number of guess (loops) so far) 4. If the user is unable to guess within the C number of chances, the program displays, "Too bad. You had C chances and did not find the letter. The letter was X" where Xis the character at position P. Hints: a) if the string has repeated letters (ex: "balloon"), that's okay and it gives the player a better chance of guessing! b) the String methods are always useful : charAt0. .toUpperCase0, lengthO, .replaceAllO, etc.Explanation / Answer
Solution:
code:
import java.util.*;
import java.io.*;
public class guessphrase
{
public static void main(String[] args) throws IOException
{int words,n,i,blanks;
Random gen = new Random();
int maxTries = 7;
int wordLength,index,foundletters;
boolean solved,found;
Scanner inScan = new Scanner(System.in);
//String letter;
char letter;
String secretWord="";
StringBuffer guessedLetters = new StringBuffer();
//the fileScan gets the first word for the game
Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
words=fileScan.nextInt();
n=gen.nextInt(words);
blanks=0;
for(i=0;i<=n;i++)
secretWord = fileScan.nextLine().toLowerCase();
//Creates a StringBuffer for the viewing of the letters guessed
StringBuffer word = new StringBuffer();
for(i = 0; i < secretWord.length(); i++)
if(secretWord.charAt(i)==' ')
{word.append(" ");
blanks++;
}
else
word.append("_");
System.out.println(word);
foundletters=secretWord.length()-blanks;
System.out.println("Welcome to the game of Guess a Phrase!!!!");
System.out.println("You will have 7 wrong guesses to guess what the phrase is.");
System.out.println("Your word is " + secretWord.length() + " (including blanks) letters long.");
System.out.println("The word: "+word);
solved=false;
while(maxTries > 0&&!solved)
{found=false;
System.out.print("Please enter a letter to guess: ");
//Scanner inScan = new Scanner("words.txt");
letter = Character.toLowerCase(inScan.next().charAt(0));
guessedLetters.append(letter + " ");
index=0;
while(secretWord.indexOf(letter,index) != (-1))
{found=true;
foundletters--;
word.setCharAt(secretWord.indexOf(letter,index),letter);
index=secretWord.indexOf(letter,index)+1;
System.out.println("correct guess ");
if(foundletters==0)
{solved=true;
System.out.println("congratulations on guessing the phrase");
}
}
System.out.println("The word: "+word);
if(!solved)
System.out.println(" The letters that you have guessed are: " + guessedLetters);
if(!found)
maxTries--;
if(!solved)
System.out.println("You have " + maxTries + " wrong guesses left. ");
}
if(!solved)
System.out.println("The word is "+secretWord);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.