Enhance the Number Guessing Game program from the exercises, changing it to a Gu
ID: 3737359 • 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
import java.util.Scanner;
import java.util.Random;
public class Guess_game
{
public static void main(String args[])
{
String input = null;
//method for inputing string
Scanner inputReader = new Scanner(System.in);
System.out.print("Enter String:" );
// here is how you take the input from keyboard
input = inputReader.nextLine();
//removing spaces from string
input = input.replaceAll(" ", "");
//converting all string to upper case for ease of search
input = input.toUpperCase( );
//calculating length of string
int L = input.length();
//max times user allowed to search
int C = L+5;
//generating random number
Random rnd = new Random();
int P = 1 + rnd.nextInt(L);
char find = input.charAt(P);
Scanner scanner = new Scanner(System.in);
int flag=0;//for checking if user able to find or not
for (int index = 0; index < C; index++) {
//inputing the character
System.out.print("Enter a character: ");
char c = scanner.next().charAt(0);
c = Character.toUpperCase(c); //converting character to uppercase
if (c == find) //if user enetered the desired charcter
{
System.out.println("Congrates! You guessed in " + index+1 + " guesses." );
flag=1;
break;
}
else
{
System.out.println("Try again. You have only " + (C-(index+1)) +" guesses remaining.");
}
}
//if user not able to find the character in C attempts
if(flag == 0){
System.out.println("Too bad. You had " + C + " chances and did not find the letter. The letter was "+ find + ".");
}
}
}
OUTPUT:
dps@machine:~/Documents/Chegg$ javac Guess_game.java
dps@machine:~/Documents/Chegg$ java Guess_game
Enter String:Hello World here We are guessing.
Enter a character: .
Try again. You have only 32 guesses remaining.
Enter a character: W
Try again. You have only 31 guesses remaining.
Enter a character: o
Try again. You have only 30 guesses remaining.
Enter a character: a
Try again. You have only 29 guesses remaining.
Enter a character: r
Congrates! You guessed in 41 guesses.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.