Results 1 to 5 of 5
  1. #1
    K-Scale is offline Member
    Join Date
    May 2010
    Location
    Muskoka, Ontario
    Posts
    3
    Rep Power
    0

    Default Hangman Game // HELP //

    Hi all
    I am having trouble with this "simple" hangman game. Could use some pointers.

    ASSIGNMENT:
    Create a simple guessing game, similar to Hangman, in which the user guess letters then attempts to guess a partially hidden phrase. Display a phrase in which some of the letters are replaced by asterisks; for example "e*e*****" (for "elephant"). Each time the user guesses a letter, either place the letter in the correct spot (or spots) in the phrase and display it again, or tell the user the guessed letter is not in the phrase. Display a congratulatory message when the entire correct phrase has been deduced. Save the game as SecretPhrase.java

    Well I have done most of it. Except it's a long series of if statements and the characters are only replaced exactly where I tell them to. It works fine and I wouldn't mind handing it in it's just that I don't know (and can't find) the code to tell the program to end when all the characters are entered and display "Congratulations!"

    Also if anyone knows some code that could just detect where the letters are supposed to go, rather than using setCharAt() that would really help. THANKS!

    Java Code:
    import java.util.Scanner;
    public class SecretPhrase
    {
    
        Scanner keyboard = new Scanner(System.in);
        String SecretPhrase, guess;
        int x;
        String hiddenPhrase = "********";
        String secretPhrase = "elelphant";
        StringBuilder word = new StringBuilder("********");
    
    
    
    
      public void StringBuilder()
      {
        String secretPhrase2 = "elephant";
        String hiddenPhrase2 = "********";
      }
    
    
    public static void main(String args[])
    	{
             SecretPhrase display = new SecretPhrase();
             display.Guess();
    
    	}
    
    
    	public void Guess()
    	{
    		System.out.println("\nGuess a letter in the word!");
    		guess = keyboard.next();
    		guess = guess.toLowerCase();
    
    
    
    
    
    
    
        for(x = 0; x < hiddenPhrase.length(); x++)
        {
    		if(guess.equals("e"))
    		{
    			//StringBuilder word = new StringBuilder("********");
    			x = hiddenPhrase.length();
    			word.setCharAt(0, 'e');
    			word.setCharAt(2, 'e');
                System.out.println("Correct!\n"+word);
                Guess();
    		}
    
    
    
            else
    		if(guess.equals("l"))
    		{
    		  word.setCharAt(1, 'l');
    		  System.out.println("Correct!\n"+word);
    		  Guess();
    	    }
            else
    		if(guess.equals("p"))
    		{
    		  word.setCharAt(3, 'p');
    		  System.out.println("Correct!\n"+word);
    		  Guess();
    	    }
    
            else
    		if(guess.equals("h"))
    		{
    		  word.setCharAt(4, 'h');
    		  System.out.println("Correct!\n"+word);
    		  Guess();
    	    }
            else
    		if(guess.equals("a"))
    		{
    		  word.setCharAt(5, 'a');
    		  System.out.println("Correct!\n"+word);
    		  Guess();
    	    }
            else
    	    if(guess.equals("n"))
    	    {
    	      word.setCharAt(6, 'n');
    	      System.out.println("Correct!\n"+word);
    		  Guess();
    	    }
            else
    		if(guess.equals("t"))
    		{
    		  word.setCharAt(7, 't');
    		  System.out.println("Correct!\n"+word);
    		  Guess();
    	    }
    
            else
            System.out.println("Sorry no "+guess+"'s");
            Guess();
            ++x;
    
    
    
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    Instead of calling Guess() again going down/recuring one level, try putting the code in a forever loop: while(true). Then when your done you can exit the loop with a break statement and give out a message.

    Instead of explicitly coding each letter in the hidden word (that is EXTREMELY POOR coding) use a search technique to see if the entered letter is in the hidden word. The String method indexOf() could be used.
    You'd need to put it in a loop in case a letter is repeated. Also look at using the starting point of the indexOf() method.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your if blocks have to go as this ties your code irretrievably to the data making your program very inflexible. The question you should ask yourself is this: How hard would it be to change the hiddenPhrase? If you have to fiddle with many if blocks to do this, something is wrong.

    One way is to have your, hiddenPhrase StringBuilder (I like your use of this) and secretPhrase String, and then when the user enters a letter, first check if it is in the hiddenPhrase (if it has been uncovered already) by first converting the StringBuilder to String via toString() and then using the String method contains(...), and if not check if it's in the secretPhrase using the same method. If present, then, you might as well iterate through the secretPhrase in a for loop looking at each char via the charAt(int index) method, and if it is the selected char, change the corresponding char in the hiddenPhrase StringBuilder object. After any change to StringBuilder, check if it contains "*", and if not, then the puzzle's been solved.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    No, you don't want to turn this in. :) Take another try at this. You've learned looping constructs, right? You have a secret word in a String, and your user is guessing one letter at a time. So for each letter:
    Java Code:
            add the letter that was guessed to a guessedLetters String
            start with an empty display String
            set a boolean correctGuess to false, and set a boolean wordGuessed to true
            loop from zero to the length of the secret word
                    get the letter at your loop counter's position in the secret word
                    see if that letter is in your guessedLetters String
                    if it is there
                            append it to your display String
                            set correctGuess to true
                    else
                            append a * to your display String
                            set wordGuessed to false
            show your display String
            use your two boolean variables to show appropriate messages
    That's one turn. Wrap all of that in another loop, and you're finished when wordGuessed is true.

    -Gary-

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    I didn't see Fubarable's post before I posted. His algorithm will work too. But you definitely don't want to hard-code each letter of your secret word.

    -Gary-

Similar Threads

  1. Hangman Game Help Please
    By 9tjh in forum New To Java
    Replies: 4
    Last Post: 12-04-2009, 04:19 AM
  2. Hangman Game..
    By iPetey in forum New To Java
    Replies: 4
    Last Post: 05-07-2009, 03:24 PM
  3. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 07:47 PM
  4. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 02:56 PM
  5. Create the game Hangman
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:16 AM

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
  •