Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Hangman

  1. #1
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Exclamation Hangman

    I'm trying to create a hangman game, but the problem is, how would I make it so that if the person presses "J", "A", and "V" on the keyboard, it moves onto word2();?

    Java Code:
    public static void WORD1() { //Java
        char key = ' ';
        
        c.setFont(new Font("arial", Font.PLAIN, 50));
        c.setColor(Color.BLACK);
        c.drawString("_", 251, 472);
        c.drawString("_", 287, 472);
        c.drawString("_", 322, 472);
        c.drawString("_", 357, 472);
        
        while(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v')
        {
          key = c.getChar();
    
          if (key == 'J' || key == 'j')
          {
            c.setFont(new Font("arial", Font.BOLD, 50));
            c.setColor(Color.BLACK);
            c.drawString("J", 250, 470);
          }
          else if (key == 'A' || key == 'a')
          {
            c.setFont(new Font("arial", Font.BOLD, 50));
            c.setColor(Color.BLACK);
            c.drawString("A", 285, 470);
            c.drawString("A", 355, 470);
          }
          else if (key == 'V' || key == 'v')
          {
            c.setFont(new Font("arial", Font.BOLD, 50));
            c.setColor(Color.BLACK);
            c.drawString("V", 320, 470);
          }
          else if(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v')
          {
            COUNTER();
            
            if(counter == 11)
            {
              c.clear();
              c.setFont(new Font("arial", Font.PLAIN, 25));
              c.setColor(Color.BLACK);
              c.drawString("You          him! Try the next word!", 125, 250);
              c.setColor(Color.RED);
              c.drawString("killed", 174, 250);
              
              c.setFont(new Font("arial", Font.PLAIN, 15));
              c.setColor(Color.BLACK);
              c.drawString("The next word will automatically start in 5 seconds.", 150, 275);
              
              WAIT();
              c.clear();
              counter = 0;
              STAND();
              WORD2();
            }
          }
        }
      }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    A quick observation on a line of code:
    (key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v')

    If key is 'A' it is NOT 'J'. In other words this if will always be true
    However being at the end of the if/else if chain it could be OK in this instance.

    A GOOD IDEA - Always add a final else at the end of a chain of if/else if statements that prints out when all of the above if/else if statements were false.

    Nesting the call to WORD2 inside of the WORD1 method could be a problem. Have WORD1 return a value to a higher level method that would call WORD2
    Last edited by Norm; 05-12-2011 at 08:11 PM.

  3. #3
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    Alright, I'm not sure how I'd go around that though.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    It will probably take some thought and a redesign.

  5. #5
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    Probably. :p

  6. #6
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    Would something like this work?

    Java Code:
    if(key == 'j' && key == 'a' && key == 'v')
    {
          WORD2();
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    How can the variable key have three different values at the same time? You need a different way to store the 3 letters so that you can test if they are J, A and V. For example: Either concatenate then in a string or store them in an array.

  8. #8
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    Okay, if I were to store them in an array, how would I call upon it so that if they pressed those three keys, move to WORD2()?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Using something like this:
    Java Code:
    if(key[0] == 'j' && key[1] == 'a' && key[2] == 'v')

  10. #10
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    I'm a beginner, so please bear with me. I've tried doing this. It works but it only moves onto the next word (WORD2()) if I click a different letter on the keyboard.

    Java Code:
    public static void WORD1() { //Java
        [b][COLOR="Red"]char[] JAVA = {'j','a','v'};[/COLOR][/b]
        char key = ' ';
        
        c.setFont(new Font("arial", Font.PLAIN, 50));
        c.setColor(Color.BLACK);
        c.drawString("_", 251, 472);
        c.drawString("_", 287, 472);
        c.drawString("_", 322, 472);
        c.drawString("_", 357, 472);
        
        while(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v')
        {
          key = c.getChar();
          
          if (key == 'J' || key == 'j')
          {
            c.setFont(new Font("arial", Font.BOLD, 50));
            c.setColor(Color.BLACK);
            c.drawString("J", 250, 470);
          }
          else if (key == 'A' || key == 'a')
          {
            c.setFont(new Font("arial", Font.BOLD, 50));
            c.setColor(Color.BLACK);
            c.drawString("A", 285, 470);
            c.drawString("A", 355, 470);
          }
          else if (key == 'V' || key == 'v')
          {
            c.setFont(new Font("arial", Font.BOLD, 50));
            c.setColor(Color.BLACK);
            c.drawString("V", 320, 470);
          }
          [b][COLOR="Red"]else if(JAVA[0] == 'j' && JAVA[1] == 'a' && JAVA[2] == 'v')
          {
            c.clear();
            counter = 0;
            STAND();
            WORD2();
          }[/COLOR][/b]
          else if(key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v')
          {
            COUNTER();
            
            if(counter == 11)
            {
              c.clear();
              c.setFont(new Font("arial", Font.PLAIN, 25));
              c.setColor(Color.BLACK);
              c.drawString("You          him! Try the next word!", 125, 250);
              c.setColor(Color.RED);
              c.drawString("killed", 174, 250);
              
              c.setFont(new Font("arial", Font.PLAIN, 15));
              c.setColor(Color.BLACK);
              c.drawString("The next word will automatically start in 5 seconds.", 150, 275);
              
              WAIT();
              c.clear();
              counter = 0;
              STAND();
              WORD2();
            }
          }
        }  
      }

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    (key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v')
    This condition will always be true.

  12. #12
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    (key != 'J' || key != 'A' || key != 'V' || key != 'j' || key != 'a' || key != 'v')
    This condition will always be true.
    I don't quite understand. Should I remove the while loop?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    If key is equal to 'J' then key != 'A' is true.
    Since the comparisons are connected by ORs if any one is true then true is returned for the full conditional expression. The condition as you coded it will always be true.

    What is the logic for the WORD1 method? Is it supposed to loop on some conditions? If so you need a loop of some kind. If not, remove the while loop. You need to have a design for the method before you code it.

  14. #14
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    There is a loop and all that's there is:

    Java Code:
    public static void main(String[] args) {
        
        STAND();
        WORD1();
        WORD2();
        WORD3();
        WORD4();
        WORD5();
        
      }

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I have no idea what the code you posted is supposed to show or do.
    The code shows 6 methods being called one after the other. No logic or loops.

  16. #16
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    I have it all working fine, the only puzzle piece missing is getting it from one word to the other when the word is complete.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    getting it from one word to the other when the word is complete
    Not sure what this means. Your terminology in confusing.
    Do you mean that when one method (eg WORD1) finishes
    you want the execution to go to the next method( WORD2)?

    With the main() method as you show it, if the WORD1 method returns, then the WORD2 method would be called. When it returns, the WORD3 method would be called, etc

  18. #18
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    Yes, that is what I mean, I'll try returning but I've never used them. :/

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Time to learn how.

    See Cross-post at:
    http://www.daniweb.com/software-deve...threads/364323
    Last edited by Norm; 05-15-2011 at 12:00 AM.

  20. #20
    Feriscool is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    Arg, I don't have much time to finish this project. :/ I've been trying but can't seem to get it working.

Page 1 of 2 12 LastLast

Similar Threads

  1. hangman
    By coltragon in forum New To Java
    Replies: 2
    Last Post: 01-16-2010, 09:56 AM
  2. hangman
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-14-2009, 09:06 AM
  3. Hangman GUI help
    By kurt in forum New To Java
    Replies: 5
    Last Post: 05-22-2009, 10:22 AM
  4. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 04:42 AM
  5. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 02:30 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
  •