import java.util.Scanner;
import java.util.Random;
public class HangmanMy {
public static void main (String[] args){
System.out.println("\t\t\t\t\t************Welcome to Hangman*************");
Scanner input = new Scanner(System.in);
boolean done = false;
String guess;
String guesses = "";
char letter;
int wrongGuess = 6;
StringBuffer dashes;
System.out.println("\nChoose a category -> places / games / animals / subjects ");
String words = WordsToChoose(input.nextLine());
dashes = makeDashes(words);
while (done == false){
System.out.println("Here is your word: " + dashes);
System.out.println("Guesses so far: " + guesses);
System.out.print("enter a guess (letter or word): ");
guess = input.next();
// process the guess
if (guess.length() > 1)
{
if (guess.equalsIgnoreCase(words))
System.out.println("You got it, Good Job!!");
else
System.out.println("Thats not the word, the word was: " + words);
done=true;
}
else // process single letter guess
{
letter = guess.charAt(0);
guesses += letter;
if (words.indexOf(letter) < 0) // not there
{
wrongGuess--;
if((wrongGuess <= 5)&&(wrongGuess >= 1)){
System.out.println("Guesses left: " + wrongGuess);}
else if(wrongGuess == 0){
System.out.println("Too many Guesses,The real word was: " + words);
done = true;}
}
else // letter is in the words
{
matchLetter(words, dashes, letter);// put it in dashes where it belongs
if(words.equalsIgnoreCase(dashes.toString()))
System.out.println("\nYOU WIN!!!!!!");
}
}
while(done == true)
{
System.out.println("\nDo you want to play again?");
String answer = input.next();
if(answer.equalsIgnoreCase("no")){
System.out.println("GOOD-BYE, THANKS FOR PLAYING"); break;}
else if(answer.equalsIgnoreCase("yes")){
done = false;}
}
}
}
public static void matchLetter(String words, StringBuffer dashes, char letter)
{
for (int index = 0; index < words.length(); index++)
if (words.charAt(index) == letter)
dashes.setCharAt(index, letter);
System.out.print("Good guess: ");
}
public static StringBuffer makeDashes(String s)
{
StringBuffer dashes = new StringBuffer(s.length());
for (int count=0; count < s.length(); count++)
dashes.append('-');
return dashes;
}
public static String WordsToChoose(String category)
{
Random generator = new Random();
String name = "";
if (category.equalsIgnoreCase("places"))
{
String places[] = {"Barcelona", "London", "Paris", "Toronto", "Vancouver", "Frankfurt", "Sydney", "Florida"};
name = places[generator.nextInt(8)];
}
else if (category.equalsIgnoreCase("games"))
{
String games[] = {"BoderLands", "Halo", "FIFA", "GTA", "HITMAN", "Skyrim", "Minecraft", "Zombieville"};
name = games[generator.nextInt(8)];
}
else if (category.equalsIgnoreCase("animals"))
{
String animals[] = {"eagle", "tiger", "lion", "Jaguar", "rhinoceros", "sharks", "dolphin", "whale"};
name = animals[generator.nextInt(8)];
}
else if (category.equalsIgnoreCase("subjects"))
{
String subjects[] = {"Physics", "Tech", "Chemistry", "Gym", "English", "Religion", "Biology", "Math"};
name = subjects[generator.nextInt(8)];
}
return name.toLowerCase();
}
}
My second while loop has a problem: when i say yes to play again, it doesn't reset the game, it jus goes back where it left of with
I been trying to solve it for 5 days