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

Write a reverse Hangman game in which the user thinks of a word and the computer

ID: 441293 • Letter: W

Question

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

I'm going to try again--just rate 1 of them

import java.util.*;

public class regespage372Num2

{public static void main(String[] args)

{Scanner in=new Scanner(System.in);

int found,count,right=0,wrong=0,max=6,trys=0;

String guessed = "";

char guess;

Random r = new Random();

System.out.print("How many letters in your word? ");

count=in.nextInt();

gallow(wrong);

while(trys<max)

{System.out.print(right+" letters of "+count+" found: ");

System.out.println(trys+" trys of "+max+" used");

trys++;

guess=getGuess(guessed,r);

guessed+=guess;

if(inWord(in,"Is "+guess+" in the word?")=='y')

{System.out.print("How many "+guess+" in your word? ");

found=in.nextInt();

right+=found;

}

else

wrong++;

System.out.println();

gallow(wrong);

}

if(right==count)

System.out.println("I win");

else

System.out.println("You win");

}

public static void gallow(int trys)

{System.out.println(" +--+");

System.out.println(" | |");

if(trys==0)

System.out.println(" |");

else

System.out.println(" | O");

if(trys<=1)

System.out.println(" |");

else if(trys<=4)

System.out.println(" | |");

else if(trys==5)

System.out.println(" | |\");

else

System.out.println(" | /|\");

if(trys<=2)

System.out.println(" |");

else if(trys==3)

System.out.println(" | \");

else

System.out.println(" | / \");

System.out.println(" |");

System.out.println(" +-----");

}

public static char getGuess(String guesses,Random r)

{char letter;

int num;

do

{num=r.nextInt(26);

letter=(char)('A'+num);

}while(guesses.indexOf(letter)!=-1);

return letter;

}

public static char inWord(Scanner in,String mess)

{String answer;

char letter;

System.out.print(mess+"(y/n)");

answer=in.next();

letter=Character.toLowerCase(answer.charAt(0));

while(letter!='y'&&letter!='n')

{System.out.println("invalid reponse "+mess+"(y/n)");

answer=in.next();

letter=Character.toLowerCase(answer.charAt(0));

}

return letter;

}

}


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