-
January 6th, 2013, 08:01 PM
#1
[RESOLVED] Need help with hangman program java
Code:
import java.util.Scanner;//imports a Scanner Class
import java.util.Random;//imports a Randomizer
public class HangmanMy {
public static void main (String[] args){
System.out.println("\t\t\t\t\t************Welcome to Hangman*************");//Intro to the game
Scanner input = new Scanner(System.in);//Instalizes a new Scanner called input
boolean done = false;//new boolean variable done that is set to false
boolean reset = true;//new boolean variable reset that is set to true
String guess;//new String Variable guess
String guesses = "";
char letter;//new char variable
int wrongGuess = 6;//new interger variable wrongGuess that stores the value 6 in it, which is the max wrongGuess the user can have
StringBuffer dashes;//new StringBuffer variable dashes
while(reset == true){
System.out.println("Choose a category -> places / games / animals / subjects ");//Asks the user what types of word they want
String words = wordsToChoose(input.nextLine());//creates a new String and itand chooses the word from the method wordsToChoose from the answer given
dashes = makeDashes(words);//makes dashes for each of the letter in the word
done = false;
while(done == false){//when done is false, it does this
System.out.println("Here is your word: " + dashes);//prints out the word in dashes
System.out.println("Guesses so far: " + guesses);//the guessess the user made so far
System.out.print("enter a guess (letter or word): ");//this asks the user to enter a guess
guess = input.next();//takes the answer
// process the guess
if (guess.length() > 1)//if the length of the answer was more than 1 letter it will do this:
{
if (guess.equalsIgnoreCase(words))//if the guess was the word
System.out.println("You got it, Good Job!!");//it prints out this on the screen and the games ends
else //if it dosen't match the word
System.out.println("Thats not the word, the word was: " + words);//it prints out this on the screen and the games ends
done=true;//now done is true so the loop will stop
}
else // if the user only puts a letter it will process a single letter guess
{
letter = guess.charAt(0);
guesses += letter;
if (words.indexOf(letter) < 0) //if the letter given is not in the word it will do this:
{
wrongGuess--;//this takes out 1 wrongGuess from six everytime the user gets the letter wrong
if((wrongGuess <= 5)&&(wrongGuess >= 1)){
System.out.println("Guesses left: " + wrongGuess);}//prints out this when the wrongGuess are between 5-1
else if(wrongGuess == 0){//when the wrongGuess becomes 0
System.out.println("Not Bad! Unfortunately you're all out of guesses. My word was " + words);// prints out the real word cause there were too many guesses
done = true;}//done becomes true and the loop stops
}
else //if the letter is in the words
{
matchLetter(words, dashes, letter);// put it in dashes where it belongs
if(words.equalsIgnoreCase(dashes.toString()))//if the user fills in the dashes using only letters it will do this
System.out.println("\nYOU WIN!!!!!!");//print out you 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;}
if(answer.equalsIgnoreCase("yes")){
reset = true; break;}
}
}
}
}
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 won`t work, what i mean is that when i say yes to play again it dosen`t reset,
could u please help
-
January 12th, 2013, 07:58 AM
#2
Re: Need help with hangman program java
Hello fellow Java user This is what i came up with while studying your code.
by using the break(); command you only disable the current loop , be it a while loop, a for loop, or a do while loop. now the problem with your code is as it follows:
Code:
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;}
if(answer.equalsIgnoreCase("yes")){
reset = true; break;}
}
by using the command break(); in the following line:
Code:
if(answer.equalsIgnoreCase("no")){
System.out.println("GOOD-BYE, THANKS FOR PLAYING"); break;
you basically just break out from the while(done==true) loop, but being given the fact that reset is still true, the main loop has no reason to stop. What i would do is the following. Replace the faulty line with this:
if(answer.equalsIgnoreCase("no")){
System.out.println("GOOD-BYE, THANKS FOR PLAYING"); reset = false; break;
I haven't seen you do this in your code, so basically you don't break the main loop's condition (while(reset == true)), so it has no reason to stop.
some ground notes.
While using loops and methods with booleans (true/false) you dont need to actually write the code like this
while(boolean == true) or while(boolean == false)
It is considered good programming to do the following
while(boolean)/*this means the boolean is true*/ or while(!boolean) /*this mean that the boolean is false*/ (Thanks keang for sharing this a long while ago)
well, i hope it helps
-
January 12th, 2013, 12:04 PM
#3
Re: Need help with hangman program java
dick ***** son a crap head
-
January 12th, 2013, 07:58 PM
#4
Re: [RESOLVED] Need help with hangman program java
-
January 12th, 2013, 12:04 PM
#5
Re: [RESOLVED] Need help with hangman program java
Mutha ****er son of a *****
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|