Java help Write a hangman game that randomly generates a word and prompts the us
ID: 3862720 • Letter: J
Question
Java help
Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Hint: StringBuilder will be very helpful in solving the problem.
Declare an array to store words, as follows:
// Add any words you wish in this array
String[] words = {"write", "that", ...};
(Guess) Enter a letter in word ******* > p
(Guess) Enter a letter in word p****** > r
(Guess) Enter a letter in word pr**r** > p
p is already in the word
(Guess) Enter a letter in word pr**r** > o
(Guess) Enter a letter in word pro*r** > g
(Guess) Enter a letter in word progr** > n
n is not in the word
(Guess) Enter a letter in word progr** > m
(Guess) Enter a letter in word progr*m > a
The word is program. You missed 1 time
Do you want to guess another word? Enter y or n>
Explanation / Answer
package hangman;
import java.util.Scanner;
public class Hangman
{
static String[] words = { "hangman","program" , "words" , "test" , "game" };
static boolean isCorrect;
public static void main(String[] args)
{
Scanner ip = new Scanner(System.in);
String play="y";
while (play.equals("y"))
{
String word=getword();
String hword=gethword(word);
int mcount=0;
while (!word.equals(hword))
{
System.out.print("(Guess) Enter a letter in word " +hword+">");
char ch=ip.next().charAt(0);
if (!isalreadyinword(hword, ch))
{
hword=getguess(word,hword,ch);
if(!isCorrect)
{
System.out.println(ch + " is not in the word.");
mcount++;
}
}
else
{
System.out.println(ch + " is already in the word.");
}
}
System.out.println("The word is "+ word + " You missed " + mcount+" times");
System.out.println("Do you want to guess another word ? Enter y or n >");
play=ip.next();
}
}
public static String getword()
{
return words[(int) (Math.random() * words.length) ];
}
public static String gethword(String word)
{
String h = "";
for(int i=0;i<word.length();i++)
{
h +="*";
}
return h;
}
static public String getguess(String word, String hword, char ch)
{
isCorrect = false;
StringBuilder s = new StringBuilder(hword);
for(int i=0;i<word.length();i++)
{
if (ch ==word.charAt(i) && s.charAt(i) == '*')
{
isCorrect=true;
s=s.deleteCharAt(i);
s=s.insert(i,ch);
}
}
return s.toString();
}
public static boolean isalreadyinword(String hword, char ch)
{
for(int i=0;i<hword.length();i++)
{
if(ch==hword.charAt(i))
{
return true;
}
}
return false;
}
}
output:
run:
(Guess) Enter a letter in word *****>n
n is not in the word.
(Guess) Enter a letter in word *****>a
a is not in the word.
(Guess) Enter a letter in word *****>g
g is not in the word.
(Guess) Enter a letter in word *****>m
m is not in the word.
(Guess) Enter a letter in word *****>a
a is not in the word.
(Guess) Enter a letter in word *****>n
n is not in the word.
(Guess) Enter a letter in word *****>p
p is not in the word.
(Guess) Enter a letter in word *****>r
(Guess) Enter a letter in word **r**>g
g is not in the word.
(Guess) Enter a letter in word **r**>o
(Guess) Enter a letter in word *or**>a
a is not in the word.
(Guess) Enter a letter in word *or**>m
m is not in the word.
(Guess) Enter a letter in word *or**>d
(Guess) Enter a letter in word *ord*>s
(Guess) Enter a letter in word *ords>w
The word is words You missed 10 times
Do you want to guess another word ? Enter y or n >
y
(Guess) Enter a letter in word *******>g
(Guess) Enter a letter in word ***g***>m
(Guess) Enter a letter in word ***gm**>a
(Guess) Enter a letter in word *a*gma*>n
(Guess) Enter a letter in word *angman>h
The word is hangman You missed 0 times
Do you want to guess another word ? Enter y or n >
n
BUILD SUCCESSFUL (total time: 3 minutes 22 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.