using java ( easy way please) Write a reverse Hangman game in which the user thi
ID: 3638185 • Letter: U
Question
using java( easy way please)
Write a reverse Hangman game in which the user thinks of a word and the computer tries to
guess the letters in that word. The user tells the computer how many letters the word contains.
Limit your program to a maximum of 6 guesses. Use a random number generator to generate the
guesses. Use the following code to generate your letter guesses randomly:
// picks a letter at random that we haven't yet guessed
public static char getGuess(String guesses) {
Random r = new Random();
char guess;
do {
guess = (char)('A' + r.nextInt(26));
} while (guesses.indexOf(guess) != -1);
return guess;
}
Your program should be stored in a file called ReverseHangman.java.
Log of execution (user input underlined)
This program plays a game of reverse hangman.
You think up a word and I'll try to guess
the letters.
How many letters are in your word? 2
+--+
| |
|
|
|
|
+-----
I've got 0 of the 2 letters so far
I guess Q
Is that letter in the word? n
+--+
| |
| O
|
|
|
+-----
I've got 0 of the 2 letters so far
I guess O
Is that letter in the word? n+--+
| |
| O
| |
|
|
+-----
I've got 0 of the 2 letters so far
I guess B
Is that letter in the word? n
+--+
| |
| O
| |
|
|
+-----
I've got 0 of the 2 letters so far
I guess T
Is that letter in the word? n
+--+
| |
| O
| |
| /
|
+-----
I've got 0 of the 2 letters so far
I guess X
Is that letter in the word? n
+--+
| |
| O
| |
| /
|
+-----
I've got 0 of the 2 letters so far
I guess H
Is that letter in the word? n
+--+
| |
| O
| /|
| /
|
+-----
I've got 0 of the 2 letters so far
I guess G
Is that letter in the word? n
+--+
| || O
| /|
| /
|
+-----
You beat me this time.
Explanation / Answer
import java.util.Scanner; class RevHang { String letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char randChar(){ int rand=(int)(Math.random()*letters.length()); //0-25 char guess=letters.charAt(rand); letters=letters.substring(0,rand)+letters.substring(rand+1); return guess; } void main() { Scanner s=new Scanner(System.in); System.out.println("Enter number of letters"); int n=s.nextInt(), correct=0; char reply; for(int i=0;i<6;i++){ System.out.println("I've got "+correct+" out of "+n); System.out.println("I guess "+randChar()); System.out.println("Is that letter in the word?"); reply=s.next().charAt(0); if(reply=='y'){ i--; correct++; } if(correct==n){ System.out.println("I win"); break; } } if(correct!=n) System.out.println("I lose"); } } I'll leave the ASCII graphics to you.. Just put the print statements after the if(reply=='n'). Even better, have a seperate function that prints the hanging man for you with a parameter for incorrect guesses, or just another class variable. NOTE: if you plan to re-use objects of htis class to play multiple games, don't forget ro reinitialize the String letters to its original complete form.Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.