Write a hangman game that randomly generates a word and prompts the user to gues
ID: 3541160 • Letter: W
Question
Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time. 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. Declare an array to store words as follows: String[] words = { "write", "that", ....};
I am comming up with an error can someone please HELP: THIS IS WHAT I CAME UP WITH:
import java.util.Random;
import java.util.Scanner;
class Hangman
{
public static void main(String[] args) {
Start();
}
public static void Start()
{
String[] words = { "cat", "dog", "book", "breakfast", "telephone", "pencil","smoke", "drive", "town", "polish", "hat"};
String[] word;
Random rand = new Random();
int w1 = rand.nextInt(words.length);
word = words[w1];
String guess = "";
int wl = word.length()+1;
for (int i = 1; i < wl; i++)
{
guess += "*";
}
String lu = "";
int tt = 0;
boolean canPlay = true;
Main(guess , tt, canPlay, lu, word);
}
public static void Main (String guess, int tt, boolean canPlay, String lu, String word)
{
if(tt==6)
{
canPlay = false;
Lose();
}
else
{
char g1[]= guess.toCharArray();
char w2[]= word.toCharArray();
System.out.print (" (Guess) Enter a letter in word");
for(int x = 0; x < g1.length; x++)
{
System.out.print(g1[x]);
}
System.out.print(" > ");
Guess(guess, tt, canPlay, lu, word, g1, w2);
}
}
public static void Guess(String guess, int tt, boolean canPlay, String lu, String word, char g1[], char w2[])
{
String tg1 = new String(g1);
String tg2 = new String(w2);
if(tg1.equals(tg2))
{
System.out.println("The word is " + word + ", " + " You missed " + tt + "time");
Win();
}
else
{
if(tt == 6)
{
System.out.println("You lost. The word was: " +word);
Lose();
}
else
{
Scanner input = new Scanner(System.in);
String letter = input.next();
if(word.contains(letter))
{
if(lu.contains(letter))
{
tt +=1;
System.out.println( " " + letter + " is already in the word ");
}
else
{
int wl = word.length();
for(int i = 0; i < wl; i++)
{
char aChar = letter.charAt(0);
char bChar = w2[i];
if(bChar == aChar)
{
g1[i]= aChar;
}
}
}
}
else
{
tt +=1;
System.out.println(" " + letter + " is not in the word ");
lu +=letter;
}
System.out.println();
boolean display = false;
for(int x=0; x < g1.length; x++)
{
if(g1[x] == '*')
display = true;
}
if(display)
{
System.out.println("(Guess) Enter a letter in word");
for(int x = 0; x {
System.out.print(g1[x]);
}
System.out.print(" > ");
}
lu += letter;
Guess(guess, tt, canPlay, lu, word, g1, w2);
}
}
}
public static void Lose()
{
Scanner input1 = new Scanner(System.in);
System.out.print(" Do you want to guess for another word? Enter y/n >");
String pa1 = input1.next();
if(pa1.contains ("y") || pa1.contains ("Y"))
{
Start();
}
}
public static void Win()
{
Scanner input2 = new Scanner(System.in);
System.out.print(" Do you want to quess for another word? Enter y/n > ");
String pa = input2.next();
if(pa.contains("y") || pa.contains("Y"))
{
Start();
}
}
}
;>
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class Hangman
{
public static void main(String[] args) {
Start();
}
public static void Start()
{
String[] words = { "cat", "dog", "book", "breakfast",
"telephone", "pencil","smoke", "drive", "town", "polish",
"hat"};
int i ;
String word;
Random rand = new Random();
int w1 = rand.nextInt(words.length);
word = words[w1];
String guess = "";
int wl = word.length()+1;
for( i = 1; i < wl; i++)
{
guess += "*";
}
String lu = "";
int tt = 0;
boolean canPlay = true;
Main(guess , tt, canPlay, lu, word);
}
public static void Main (String guess, int tt, boolean
canPlay, String lu, String word)
{
int x;
if(tt==6)
{
canPlay = false;
Lose();
}
else
{
char g1[]= guess.toCharArray();
char w2[]= word.toCharArray();
System.out.print (" (Guess) Enter a letter in word");
for( x = 0; x < g1.length; x++)
{
System.out.print(g1[x]);
}
System.out.print(" > ");
Guess(guess, tt, canPlay, lu, word, g1, w2);
}
}
public static void Guess(String guess, int tt, boolean
canPlay, String lu, String word, char g1[], char w2[])
{
int i;
String tg1 = new String(g1);
String tg2 = new String(w2);
if(tg1.equals(tg2))
{
System.out.println("The word is " + word + ", " + " You
missed " + tt + "time");
Win();
}
else
{
if(tt == 6)
{
System.out.println("You lost. The word was: " +word);
Lose();
}
else
{
Scanner input = new Scanner(System.in);
String letter = input.next();
if(word.contains(letter))
{
if(lu.contains(letter))
{
tt +=1;
System.out.println( " " + letter + " is already in the word
");
}
else
{
int wl = word.length();
for( i = 0; i < wl; i++)
{
char aChar = letter.charAt(0);
char bChar = w2[i];
if(bChar == aChar)
{
g1[i]= aChar;
}
}
}
}
else
{
tt +=1;
System.out.println(" " + letter + " is not in the word ");
lu +=letter;
}
System.out.println();
boolean display = false;
for( int x=0; x < g1.length; x++)
{
if(g1[x] == '*')
display = true;
}
if(display)
{
System.out.println("(Guess) Enter a letter in word");
for(int x = 0; x <g1.length;x++)
{
System.out.print(g1[x]);
}
System.out.print(" > ");
}
lu += letter;
Guess(guess, tt, canPlay, lu, word, g1, w2);
}
}
}
public static void Lose()
{
Scanner input1 = new Scanner(System.in);
System.out.print(" Do you want to guess for another word?
Enter y/n >");
String pa1 = input1.next();
if(pa1.contains ("y") || pa1.contains ("Y"))
{
Start();
}
}
public static void Win()
{
Scanner input2 = new Scanner(System.in);
System.out.print(" Do you want to quess for another word?
Enter y/n > ");
String pa = input2.next();
if(pa.contains("y") || pa.contains("Y"))
{
Start();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.