Help making a hangman program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yottabyte
    New Member
    • Nov 2008
    • 13

    Help making a hangman program

    Hey, I just started programming this September and I have an assignment in which I am supposed to create a hangman game. This is my first post on this forum so forgive me for any "noob" mistakes Now I'm still learning Java so this will be a basic program running in the console window (no GUI) using strings loops, arrays, etc but hopefully nothing too complicated. I've developed somewhat of an algorithm for the program, but it's really simple so far.

    Title screen
    main menu (play, instructions, quit)
    if they choose to select play there is a choice of difficulty (easy, med, hard)
    depending on what difficulty they choose there will be different words

    Now I've got all that simple stuff out of the way and I've gotten up to the part where it displays the blanks such as _ _ _ _ _ and now what I'm trying to do is figure out how to get input from the user and use it to fill in the blanks, and give them 5 "lives". What kind of process do I go through to make this happen? I'm not asking for any specific code but I might need some explanations on how I can piece everything together.

    Thanks in advance!!!
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Greetings, yottabyte!

    Welcome to bytes... Can you show us the code you referred to? Also you'll probably need to do a lot of the work as we help you figure it all out.

    It's exciting though, hope you survive it, it's quite the language, good choice.

    In a bit!

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      For console applications, use the Scanner class to get the input from the user or the Console class if you are using Java 6.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Also, I'd create a couter lives which counts (who would guess?) how many lives are left. Then you can put it all in a while loop... you should be able to figure out the rest.

        Greetings,
        Nepomuk

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Also also, read about regular expression; in particular the String.replaceA ll() method is a nice start. This game is particularly fitted to be solved by regular expressions. They take away all sorts of tedious loops and checks.

          kind regards,

          Jos

          Comment

          • yottabyte
            New Member
            • Nov 2008
            • 13

            #6
            Yeah i haven't exactly gotten too far yet but I want to know if I'm starting this off right

            I made an array that is like this:

            String[] easydifficulty = new String[51];
            easydifficulty[0] = "word";
            easydifficulty[1] = "another word";
            easydifficulty[2] = "another word";
            easydifficulty[3] = "another word";

            But I just did that to have all my strings in one place...how exactly do I make one random string appear as blanks _ _ _ _ _ and get the user input? I also don't fully understand what you meant by a counter to count lives

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              OK, now you can do it with an array and I guess as you're a beginner, that should do. To choose a random word from the array, have a look at java.util.Rando m. Read Reply #3 about how to get user input. And the lives counter? Well, say you may get it wrong 5 times, then you can have
              Code:
              int lives = 5
              somewhere and every time a wrong letter is entered, that variable is decremented (= reduced by one). Then it checks... etc.

              Oh, and writing the blanks? Well, you can find the length of a string with
              Code:
              String myString = "word";
              System.out.println(myString + " is " + myString.length() + " characters long.");
              That should get you going.

              Greetings,
              Nepomuk

              Comment

              • tiktik
                New Member
                • Nov 2008
                • 14

                #8
                hangman

                hi there...

                Last year, I did the whole hangman game in Pascal; however it was a little bit different in structure than yours is going to be; in such a way that mine first involved the first player to enter the "Secret_wor d", and then the second player would try to guess it. With regards to the blanks, an int variable "no_of_blan ks" would read the length of the "Secret_wor d" entered by player 1, and then would dislpay the corresponding amount of blanks for player 2 to guess...

                With regards to your question about how to get input from the user and use it to fill in the blanks, my approach to it was to read the character entered by the user, and then use an IF statement to check whether the character entered is correct (i.e. found in the "Secret_wor d"). If it turns out to be correct, then it should be dislpayed instead of the corresponding blank (in my case these were underscores _ ).

                Hope this helps.

                Kind regards,
                tiktik

                Comment

                • yottabyte
                  New Member
                  • Nov 2008
                  • 13

                  #9
                  Can you explain the secret word part a little bit more? I don't fully understand it but I think it will be important to continuing on my project.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by yottabyte
                    Can you explain the secret word part a little bit more? I don't fully understand it but I think it will be important to continuing on my project.
                    Don't go that way: you'll end up with a highly procedural and low level solution: Pascal doesn't have regular expression handling so you'll end up with lots of nasty little loops and byte fiddling. Have a look at this:

                    Suppose we have three Strings: word, used and correct; the word String contains the secret word, the used String contains the characters that are used by the user (whether correct or not) and the correct String contains the correctly guessed characters.

                    Suppose 'x' is the current guess character. So we can do:

                    Code:
                    if (used.indexOf(x) >= 0) // character already used
                    if the above test fails 'x' represents a new guess, so:

                    Code:
                    used+= x; // now set x as used
                    if (word.indexOf(x) < 0) // x is not in word
                    If the above test fails then x is a character in the word, so:

                    Code:
                    correct+= x;
                    String leftOver= word.replaceAll("[^"+correct+"]", "-");
                    The last line of this code snippet replaces all characters in the word that are not present in the String correct. They are replaced by a '-'.

                    If there are still '-'s left in that last String the word wasn't guessed yet:

                    Code:
                    boolean finished= leftOver.indexOf('-') < 0;
                    The few lines of code above represent the core of the game logic. Note that there are no silly loops, no counting, just the handling of the game logic.

                    kind regards,

                    Jos

                    Comment

                    • yottabyte
                      New Member
                      • Nov 2008
                      • 13

                      #11
                      Okay well I got the hard (?) part of the project done I hope. I can get it to display a bunch of # symbols as the blanks, then when you put in a letter it makes it appear in the puzzle. Now can I get some help on how to make it a loop though? I want to use a do-while loop but i don't know what to put in the brackets after the while part.

                      Code:
                      String []easywords=new String [51];
                         		easywords[0]="insertwordhere";
                      
                                      and so on until the last word...then
                      
                      int which = (int)(Math.random()*51);
                         		char[] guess= new char[easywords[which].length()];
                         		for(int index=0; index<easywords[which].length(); index++) guess[index]='#';
                         		
                      			[U]****do{ goes here?****[/U]
                      	   		for(int index=0; index<easywords[which].length(); index++) System.out.print(guess[index]);
                      	   		System.out.println();
                      	   		System.out.print("Guess a letter: ");
                      	   		char letter = In.getChar();
                      	   		for(int index=0; index<easywords[which].length(); index++) if(easywords[which].charAt(index)==letter) guess[index]=letter;
                      	   		for(int index=0; index<easywords[which].length(); index++) System.out.print(guess[index]);
                      	   		System.out.println();
                      	   		In.getChar();
                      [U]****}while (???) goes here?****[/U]
                      Now I need to put a do and a while in the spots I marked with a star right? But what would I put in the while bracket?? I'm stumped

                      Comment

                      • Nepomuk
                        Recognized Expert Specialist
                        • Aug 2007
                        • 3111

                        #12
                        The place you put that loop if fine, it will do the job. Now, there are several possible ways to check if you're finished, the most simple one would be to have an int variable that holds the amount of characters you haven't guessed yet. When that's 0, you've finished.

                        Greetings,
                        Nepomuk

                        Comment

                        • yottabyte
                          New Member
                          • Nov 2008
                          • 13

                          #13
                          Wait so is that supposed to be a word bank or lives?

                          Comment

                          • Nepomuk
                            Recognized Expert Specialist
                            • Aug 2007
                            • 3111

                            #14
                            Well, you could have two counters: one for the remaining lives, one for the letters you've guessed correctly. But that's just one way to do it - there are other possibilities.

                            Greetings,
                            Nepomuk

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              *cough* reply #10 *cough*

                              kind regards,

                              Jos

                              Comment

                              Working...