[import java.util.Scanner;
import java.util.Random;
public class HangmanMy {
public static void main (String[] args){
System.out.println("Welcome to the game Hangman :)/>");
Scanner input = new Scanner(System.in);
boolean done = false;
String guess;
String guesses = "";
char letter;
int wrongGuess = 0;
StringBuffer dashes;
System.out.println("Choose a category -> places / games / animals / subjects ");
String words = WordsToChoose(input.nextLine());
dashes = makeDashes(words);
while (! done)
{
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().replaceAll("\\W", "");
// process the guess
if (guess.length() > 1)
{
if (guess.equalsIgnoreCase(words))
System.out.println("you win!");
else
System.out.println("Thats the wrong guess");
done=true;
}
else // process single letter guess
{
letter = guess.charAt(0);
guesses += letter;
if (words.indexOf(letter) < 0) // not there
{
wrongGuess++;
if(wrongGuess < 6)
System.out.print("Bad guess: ");
else if(wrongGuess >= 6)System.out.println("Too many Guesses,The real word was: " + words);
}
else // letter is in the words
{
// put it in dashes where it belongs
matchLetter(words, dashes, letter);
}
if(words.equalsIgnoreCase(dashes.toString())){
System.out.print("\nyou win!");
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);
words = words.replaceAll("\\W","");
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[] = {"Barc elona", "Lond on", "Par is", "Los Angeles", "Miami Beach", "San Diego", "Sydney", "Puerto Rico"};
name = places[generator.nextInt(8)];
}
else if (category.equalsIgnoreCase("games"))
{
String games[] = {"Call of duty", "Halo", "FIFA", "Gta", "AssassinsCreed", "Need for speed", "AngryBirds", "FruitNinja"};
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", "Computer Science", "chemistry", "gym", "english", "religion", "computer tech", "accounting"};
name = subjects[generator.nextInt(8)];
}
return name.toLowerCase();
}
}
//MY PROGRAM DOSEN'T END, IT DOSENT ASK THE USER IF THEY WANT TO PLAY IT AGAIN, ALSO THE DASHES FOR THE WORDS IS MESSED SINCE IT CREATES A DASH FOR SPACE, I NEED UR HELP FAST. AND ONE MOREE THING IF THERE IS MORE THAN 6 WRONG GUESSES THE OUTPUT OF THE REAL WORD IS ON THE TOP INSTEAD OF THE BOTTOMM
//HELP ME!! PLEASE
RaJsHaH.23 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.