Question: Random Letter Program (Java) Write a program called LetterGuess.java, which plays a letter guessing game between the user and the computer. The program selects a
Random Letter Program (Java)
Write a program called LetterGuess.java, which plays a letter guessing game between the user and the computer. The program selects a random mystery letter between a-z inclusive. The user is then prompted to try and guess the mystery letter. If the user's guess is incorrect, the program gives the user a clue indicating whether the mystery letter is higher or lower than their guess. The user has five chances to determine the mystery letter. Should the user guess the mystery letter correctly, the program reports how many guesses it took the user to correctly guess the letter. If the user guesses incorrectly 5 times and they do not get it correctly on the 6th try, the program should display "Sorry, the letter I was thinking of was" along with the mystery letter. The program continues playing rounds of the guessing game with new mystery letters until the user decides to they no longer want to continue.
Example
Welcome! I'll pick a letter between a-z. You try to guess it. Let's play!
Example when user guesses letter in less than 5 tries.
Ok. I am now thinking of a letter.
Can you guess it? B(userInputs b)
Your guess must be lowercase in the range a-z. Try again.
Your guess? #(user inputs #)
Your guess must be lowercase in the range a-z. Try again.
Your guess? g (user inputs g)
Sorry, that guess is incorrect.
The letter I am thinking of is higher.
Can you guess it? k
Sorry, that guess is incorrect.
u
The letter I am thinking of is lower.
Can you guess it? i
Yes, the letter I was thinking of was i
You found it in 3 guesses.
Play again (Y/N)? Z(user enters z)
Please enter a valid response (Y/N): y
Example where user does not guess number in five tries
Ok. I am now thinking of a letter.
Can you guess it? m
Sorry, that guess is incorrect.
The letter I am thinking of is lower.
Can you guess it? b
Sorry, that guess is incorrect.
The letter I am thinking of is higher.
Can you guess it? d
Sorry, that guess is incorrect.
The letter I am thinking of is higher.
Can you guess it? g
Sorry, that guess is incorrect.
The letter I am thinking of is higher.
Can you guess it? j
Sorry, that guess is incorrect.
The letter I am thinking of is higher.
Can you guess it? k
Sorry, you lose. The letter I was thinking of was l
Play again (Y/N)? n
Decomposition:
void displayInstructions() - This method is called from the main method and displays the welcome message along with the game instructions.
int getRandom(int random) - generates random number
int playOneRound(Scanner keyboard, int mysteryLetter) - This method is called from the main method and plays a single round of the guessing game. The method returns an integer value representing how many guesses it took the user to guess the correct letter. If the user doesn't guess the correct letter and runs out of guesses, then a -1 is returned
char getUserGuess(Scanner console) - This method is called from playOneRound and prompts the user for their guess of the mystery letter. The guess entered by the user is returned to playOneRound(). Data Validation: Ensure the user enters a lowercase letter of the alphabet a-z.
char getPlayAgain(Scanner console) - This method accepts a scanner object and returns a Y or N indicating whether or not the user wants to play another round of the guessing game. Data Validation: Ensure the user enters either a Y or N. This can be either lower or uppercase.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
