Results 1 to 5 of 5

Thread: Hangman Game..

  1. #1
    iPetey is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default Hangman Game..

    Ok so i'm super struggling in writing a hangman game in blueJ. So far i have a class for canvas, a class to control the canvas, a class to generate random words, and the main class. Now the generating works fine, but i'm just so burned out where im at right now.. Please bare with me as i am super NEWB. Right now i am at the point where the person types a letter and then i search thru the word. I'm not sure how to handle when the word has the same letter in it twice, and im also not sure how to handle it when it is incorrect because when i compile and i type a letter if it is incorrect incorrect shows up the screen like 8 times, dont get why. i need some help someone!!! lol

    Java Code:
    import java.util.*;
    /**
     * hangman
     * 
     * @author petey 
     * @version (a version number or a date)
     */
    public class Hangman
    {
        // instance variables - replace the example below with your own
         private Scanner InputS;
         private int MissedTimes;
         private Generator generator;
    
        /**
         * Constructor for objects of class Hangman
         */
        public Hangman()
        {
            // initialise instance variables
            InputS = new Scanner(System.in);
            generator = new Generator();
            MissedTimes = 0;
        }
    
        public String word()
        {
            String
            word = generator.generate();
            return word;
        }
        public void startGame()
        {
           boolean yesno=false;
           
            System.out.print("Play HangMan?(Yes/No)");
            String toDo = InputS.nextLine();
            toDo = toDo.toLowerCase();
            if (toDo.equalsIgnoreCase("yes"))
            {
                
                yesno=true;
            }
            else
            {
                yesno=false;
            }
            
           while(yesno)
           {
             String secretWord = generator.generate();
             while (MissedTimes < 8 ) {
                 
                
                System.out.print("Please type a letter");
                System.out.print(" ");
                String Letter = InputS.nextLine();
                
               char letterChar = Letter.charAt(0);
               for (int i = 0; i < secretWord.length (); i++)
            {
                if(secretWord.charAt(i) == letterChar) {
                    // found
                    System.out.print(" ");
                    System.out.print("Correct! "+ letterChar + "");
                    
                    System.out.print(" ");
                    // draw the letter on the canvas
                    
                }
                else 
                {
                   System.out.print("Incorrect"); 
                }
    
            }
            }
                
                
                
               
              
                 System.out.print("Play HangMan?(Yes/No)");
                 InputS = new Scanner(System.in);
                 toDo = InputS.nextLine();
                 toDo = toDo.toLowerCase();
            if (toDo.equalsIgnoreCase("yes"))
            {
                yesno=true;
            }
            else
            {
                yesno=false;
                System.out.print("Goodbye.");
            }
            
        }
        
    }
    
    }

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Incorrect (and Correct) will be repeated for every letter, as it is inside the for loop. Put the checks inside the loop, but the messages outside it.

    Also, what is the point of this?
    Java Code:
    System.out.print(" ");
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    iPetey is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by OrangeDog View Post
    Incorrect (and Correct) will be repeated for every letter, as it is inside the for loop. Put the checks inside the loop, but the messages outside it.

    Also, what is the point of this?
    Java Code:
    System.out.print(" ");
    that was to create a space, maybe incorrect?

    can you please give me an example of what you mean, i dont know how to do it

  4. #4
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    if you want to print space you can use \t option

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Quote Originally Posted by ron87 View Post
    if you want to print space you can use \t option
    That prints a tab, not a space. My point was, why not add the space to the other strings?

    Java Code:
    System.out.print(" Correct! "+ letterChar + " ");
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 07:47 PM
  2. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 05:42 AM
  3. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 03:30 AM
  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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •