Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    NightRider785 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Classic Hangman Help

    I am writing a program for an intro to java class. This is very basic, for/while loops, StdDraw, input etc.
    I am having a few issues, which are toward the bottom. Starts at Line 283
    First issue is trying to restart the program after a person loses. I am trying to start it over by calling main(); from EndGame(), however I get the following error:

    Hangman.java:294: error: method main in class Hangman cannot be applied to given types;
    main();
    ^
    required: String[]
    found: no arguments
    reason: actual and formal argument lists differ in length

    I have also noticed that when I try to change my hangman picture ie) the arms go from up to down, I am redrawing the arms but in white rather than black. This works for a couple of times, but eventually it just draws them gray... How do I clear just one part of a drawing? Also, how can I just end the program if they type no for play again?

    Here is the actual program:


    Java Code:
     public class Hangman
       {
          
          public static void drawGallows()
          {
             StdDraw.setPenColor(StdDraw.GRAY);
             //Base
             StdDraw.filledRectangle(0.3, 0.3, 0.3, 0.04);
             
             StdDraw.setPenColor(StdDraw.BLACK);
             //Post
             StdDraw.line(0.35, 0.34, 0.35, 0.85);
             //Arm out
             StdDraw.line(0.35, 0.85, 0.6, 0.85);
             //Rope
             StdDraw.line(0.6, 0.85, 0.6, 0.75);
          }
          public static void wordSelect()
          {
            // int Word = (int)(Math.random() * 5) + 1;
             int Word = 2;
             if (Word == 1)
             {
                wordComputer();
             }
             if (Word == 2)
             {
                wordCollege();
             }
             if (Word == 3)
             {
                wordFinalExam();
             }
             if (Word == 4)
             {
                wordParkingTicket();
             }
             if (Word == 5)
             {
                wordHomework();
             }
          }
          public static void wordComputer()
          {
             //StdDraw.text(0.04, 0.12, "C");
             double x = 0 + 0.12;
             double x1 = 0.08 + 0.12;
             for (int i=0;i<=7;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
          }
          public static void wordCollege()
          {
             double x = 0 + 0.12;
             double x1 = 0.08 + 0.12;
             for (int i=0;i<=6;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
             int tries = 0;
             boolean correct = false;
             
             while (tries < 8 && !correct)
             {
                System.out.print("Guess a letter: ");
                String guess = StdIn.readString();
    
               /* if (guess.length() != 1)
                {
                  System.out.print("Only enter 1 letter!");
               
                }*/
                if (guess.equals("c"))
                {
                   StdDraw.picture(0.155, 0.14, "C1.jpg");
                }
                else if (guess.equals("o"))
                {
                   StdDraw.picture(0.275, 0.14, "O1.jpg");
                }
                else if (guess.equals("l"))
                {
                   StdDraw.picture(0.395, 0.14, "L1.jpg");
                   StdDraw.picture(0.515, 0.14, "L1.jpg");
    
                }
                else if (guess.equals("e"))
                {
                   StdDraw.picture(0.635, 0.14, "E1.jpg");
                   StdDraw.picture(0.875, 0.14, "E1.jpg");
    
                }
                else if (guess.equals("g"))
                {
                   StdDraw.picture(0.755, 0.14, "G1.jpg");
                }
                else
                {
                   System.out.println("Incorrect guess...");
                   if (tries == 0)
                   {
                      drawHead();
                   }
                   if (tries == 1)
                   {
                      drawEyes();
                   }
                   if (tries == 2)
                   {
                      drawMouth();
                   }
                   if (tries == 3)
                   {
                      drawBody();
                   }
                   if (tries == 4)
                   {
                      drawLegs();
                   }
                   if (tries == 5)
                   {
                      drawArms();
                   }
                   if (tries == 6)
                   {
                      drawFinal();
                   }
                   if (tries == 7)
                   {
                      dead();
                      
                   }
                   tries++;
                }
             }
          }   
          public static void wordFinalExam()
          {
             
             double x = 0 + 0.12;
             double x1 = 0.08 + 0.12;
             double x2 = 0 + 0.12;
             double x3 = 0.08 + 0.12;
             //Final
             for (int i=0;i<=4;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
             //Exam
             for (int i=0;i<=3;i++)
             {
                StdDraw.line(x2, 0, x3, 0);
                x2 = x2 + 0.12;
                x3 = x3 + 0.12;            
             }
    
          }
          public static void wordParkingTicket()
          {
             
             double x = 0 + 0.12;
             double x1 = 0.08 + 0.12;
             double x2 = 0 + 0.12;
             double x3 = 0.08 + 0.12;
             //Parking
             for (int i=0;i<=6;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
             //Ticket
             for (int i=0;i<=5;i++)
             {
                StdDraw.line(x2, 0, x3, 0);
                x2 = x2 + 0.12;
                x3 = x3 + 0.12;            
             }
    
          }
          public static void wordHomework()
          {
             double x = -0.12 + 0.12;
             double x1 = -0.04 + 0.12;
             for (int i=0;i<=7;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
          }
          public static void drawHead()
          {
             StdDraw.circle(0.6, 0.69, 0.06);
             return;
          }
          public static void drawEyes()
          {
            StdDraw.line(0.56, 0.7, 0.59, 0.7); 
            StdDraw.line(0.61, 0.7, 0.64, 0.7);
            return;
          }
          public static void drawMouth()
          {
             StdDraw.line(0.57, 0.67, 0.63, 0.67);
             return;
          }
          public static void drawBody()
          {
             StdDraw.line(0.6, 0.63, 0.6, 0.45);
             return;
          }
          public static void drawLegs()
          {
             StdDraw.line(0.6, 0.45, 0.55, 0.37);
             StdDraw.line(0.6, 0.45, 0.65, 0.37);
             return;
          }
          public static void drawArms()
          {
             StdDraw.line(0.6, 0.55, 0.52, 0.55);
             StdDraw.line(0.6, 0.55, 0.68, 0.55);
             return;
          }
          public static void drawFinal()
          {
             StdDraw.setPenColor(StdDraw.WHITE);
             StdDraw.line(0.57, 0.67, 0.63, 0.67);
             StdDraw.line(0.56, 0.7, 0.59, 0.7); 
             StdDraw.line(0.61, 0.7, 0.64, 0.7);
             StdDraw.line(0.6, 0.55, 0.52, 0.55);
             StdDraw.line(0.6, 0.55, 0.68, 0.55);
    
             StdDraw.setPenColor(StdDraw.BLACK);
             StdDraw.filledCircle(0.6, 0.67, .012);
             StdDraw.filledCircle(0.575, 0.7, .009);
             StdDraw.filledCircle(0.625, 0.7, .009);
             StdDraw.line(0.6, 0.55, 0.52, 0.63);
             StdDraw.line(0.6, 0.55, 0.68, 0.63);
             return;
          }
          public static void dead()
          {
             StdDraw.setPenColor(StdDraw.WHITE);
             StdDraw.filledCircle(0.6, 0.67, .012);
             StdDraw.circle(0.6, 0.67, .012);
             StdDraw.filledCircle(0.575, 0.7, .009);
             StdDraw.circle(0.575, 0.7, .009);
             StdDraw.filledCircle(0.625, 0.7, .009);
             StdDraw.circle(0.625, 0.7, .009);
             StdDraw.line(0.6, 0.55, 0.52, 0.63);
             StdDraw.line(0.6, 0.55, 0.68, 0.63);
             
             StdDraw.setPenColor(StdDraw.WHITE);
             StdDraw.line(0.6, 0.55, 0.52, 0.63);
             StdDraw.line(0.6, 0.55, 0.68, 0.63);
             
             
             StdDraw.setPenColor(StdDraw.BLACK);
             //Eyes
             StdDraw.line(0.56, 0.69, 0.59, 0.71); 
             StdDraw.line(0.59, 0.69, 0.56, 0.71);
             StdDraw.line(0.61, 0.69, 0.64, 0.71);
             StdDraw.line(0.64, 0.69, 0.61, 0.71);
             //Mouth
             StdDraw.line(0.57, 0.67, 0.63, 0.67);
             //Arms
             StdDraw.line(0.6, 0.55, 0.53, 0.45);
             StdDraw.line(0.6, 0.55, 0.67, 0.45);
    
             
             StdAudio.play("Lose.wav");
             System.out.println("You Lose......");
             EndGame();
           }
            public static void EndGame()
           {
             System.out.println("Play Again?? ");
             String answer = (StdIn.readString());
             
             if (answer.equals("yes"))
             {
                main();
             }
             if (answer.equals("no"))
              {
                StdAudio.play("GameOver1.wav");
                
              }
             else
             {
                System.out.println("Please enter either yes or no only.");
                EndGame();
             }
          } 
       
          public static void main(String[] args)
          {
             
             StdDraw.clear();
             StdAudio.play("Bonanza.wav");
             drawGallows();
             wordSelect();
             
             
          }
       }
    Last edited by NightRider785; 04-26-2014 at 05:51 AM.

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

    Default Re: Classic Hangman Help

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    NightRider785 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Classic Hangman Help

    All done, thanks!

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

    Default Re: Classic Hangman Help

    Hangman.java:294: error: method main in class Hangman cannot be applied to given types;
    main();
    ^
    required: String[]
    found: no arguments
    reason: actual and formal argument lists differ in length
    When the code calls a method the args it passes must match what are in the method's definition.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    NightRider785 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Classic Hangman Help

    Still a little too much of a beginner to even know what args is. "public static void main (String[] args)" is just what we've been told to use. So perhaps you could give me an example of what you are saying?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Classic Hangman Help

    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    NightRider785 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Classic Hangman Help

    I have to press further Norm, first a quick overview of who I am to help explain why I am struggling like this, this is my first real semester of java so I am what may be known as a newb or noob..with that said, I am having difficulties understanding the tutorials. Basically I feel as if the code I wrote should be working with the menu I have that is at the end of the game. I have what I believe is all the correct. Would you mind highlighting the specific line of my code or lines you are suggesting I edit with the method and arrays? I feel slightly as if this is hopeless but I don't want to give up at this point, I have put in countless hours to get as far as I have lol. All of your help is very much appreciated.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Classic Hangman Help

    I was talking about how to fix this error:
    I am trying to start it over by calling main(); from EndGame(), however I get the following error:

    Hangman.java:294: error: method main in class Hangman cannot be applied to given types;
    main();
    ^
    required: String[]
    found: no arguments
    reason: actual and formal argument lists differ in length
    The main method is defined as:
    Java Code:
      public static void main (String[] args)
    That shows that the arguments to the main method is a String array.

    To call the method you need to define an String array for its argument. The tutorial talks about how to define an array. What question(s) do you have about how to define a String array?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    NightRider785 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Classic Hangman Help

    I'm posting my updated code, I have solved most of my problems as stated above.
    My current issue is with isKeyPressed, beginning at line 70. Everything works ok until line 98. If I delete everything on line 98 and leave it as 'else', it just cycles through, giving instant game over. I do not want to put in 19 IF statements for all other letters. Is there a better way I can approach this? Obviously 'or' does nothing, and '||' does nothing. Is there a different operand for this?


    Java Code:
    import java.awt.event.KeyEvent;
      
       public class Hangman
       {
          
          public static void drawGallows()
          {
             StdDraw.setPenColor(StdDraw.GRAY);
             //Base
             StdDraw.filledRectangle(0.3, 0.3, 0.3, 0.04);
             
             StdDraw.setPenColor(StdDraw.BLACK);
             //Post
             StdDraw.line(0.35, 0.34, 0.35, 0.85);
             //Arm out
             StdDraw.line(0.35, 0.85, 0.6, 0.85);
             //Rope
             StdDraw.line(0.6, 0.85, 0.6, 0.75);
          }
          public static void wordSelect()
          {
            // int Word = (int)(Math.random() * 5) + 1;
             int Word = 2;
             if (Word == 1)
             {
                wordComputer();
             }
             if (Word == 2)
             {
                wordCollege();
             }
             if (Word == 3)
             {
                wordFinalExam();
             }
             if (Word == 4)
             {
                wordParkingTicket();
             }
             if (Word == 5)
             {
                wordHomework();
             }
          }
          public static void wordComputer()
          {
             //StdDraw.text(0.04, 0.12, "C");
             double x = 0 + 0.12;
             double x1 = 0.08 + 0.12;
             for (int i=0;i<=7;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
          }
          public static void wordCollege()
          {
             double x = 0 + 0.12;
             double x1 = 0.08 + 0.12;
             for (int i=0;i<=6;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
             int tries = 0;
             boolean correct = false;
             
             while (tries < 8 && !correct)
             {
                StdDraw.text(0.5, 1, "Guess a Letter");
                
                if (StdDraw.isKeyPressed(KeyEvent.VK_C))
                {
                   StdDraw.picture(0.155, 0.14, "C1.jpg");
                }
                else if (StdDraw.isKeyPressed(KeyEvent.VK_O))
                {
                   StdDraw.picture(0.275, 0.14, "O1.jpg");
                }
                else if (StdDraw.isKeyPressed(KeyEvent.VK_L))
                {
                   StdDraw.picture(0.395, 0.14, "L1.jpg");
                   StdDraw.picture(0.515, 0.14, "L1.jpg");
    
                }
                else if (StdDraw.isKeyPressed(KeyEvent.VK_E))
                {
                   StdDraw.picture(0.635, 0.14, "E1.jpg");
                   StdDraw.picture(0.875, 0.14, "E1.jpg");
    
                }
                else if (StdDraw.isKeyPressed(KeyEvent.VK_G))
                {
                   StdDraw.picture(0.755, 0.14, "G1.jpg");
                }
                else if (StdDraw.isKeyPressed(KeyEvent.VK_A))
                {
                   System.out.println("Incorrect guess...");
                   StdAudio.play("Buzzer.wav");
                   if (tries == 0)
                   {
                      drawHead();
                   }
                   if (tries == 1)
                   {
                      drawEyes();
                   }
                   if (tries == 2)
                   {
                      drawMouth();
                   }
                   if (tries == 3)
                   {
                      drawBody();
                   }
                   if (tries == 4)
                   {
                      drawLegs();
                   }
                   if (tries == 5)
                   {
                      drawArms();
                   }
                   if (tries == 6)
                   {
                      drawFinal();
                   }
                   if (tries == 7)
                   {
                      dead();
                      
                   }
                   tries++;
                }
             }
          }   
          public static void wordFinalExam()
          {
             
             double x = 0 + 0.12;
             double x1 = 0.08 + 0.12;
             double x2 = 0 + 0.12;
             double x3 = 0.08 + 0.12;
             //Final
             for (int i=0;i<=4;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
             //Exam
             for (int i=0;i<=3;i++)
             {
                StdDraw.line(x2, 0, x3, 0);
                x2 = x2 + 0.12;
                x3 = x3 + 0.12;            
             }
    
          }
          public static void wordParkingTicket()
          {
             
             double x = 0 + 0.12;
             double x1 = 0.08 + 0.12;
             double x2 = 0 + 0.12;
             double x3 = 0.08 + 0.12;
             //Parking
             for (int i=0;i<=6;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
             //Ticket
             for (int i=0;i<=5;i++)
             {
                StdDraw.line(x2, 0, x3, 0);
                x2 = x2 + 0.12;
                x3 = x3 + 0.12;            
             }
    
          }
          public static void wordHomework()
          {
             double x = -0.12 + 0.12;
             double x1 = -0.04 + 0.12;
             for (int i=0;i<=7;i++)
             {
                StdDraw.line(x, 0.1, x1, 0.1);
                x = x + 0.12;
                x1 = x1 + 0.12;            
             }
          }
          public static void drawHead()
          {
             StdDraw.circle(0.6, 0.69, 0.06);
             return;
          }
          public static void drawEyes()
          {
            StdDraw.line(0.56, 0.7, 0.59, 0.7); 
            StdDraw.line(0.61, 0.7, 0.64, 0.7);
            return;
          }
          public static void drawMouth()
          {
             StdDraw.line(0.57, 0.67, 0.63, 0.67);
             return;
          }
          public static void drawBody()
          {
             StdDraw.line(0.6, 0.63, 0.6, 0.45);
             return;
          }
          public static void drawLegs()
          {
             StdDraw.line(0.6, 0.45, 0.55, 0.37);
             StdDraw.line(0.6, 0.45, 0.65, 0.37);
             return;
          }
          public static void drawArms()
          {
             StdDraw.line(0.6, 0.55, 0.52, 0.55);
             StdDraw.line(0.6, 0.55, 0.68, 0.55);
             return;
          }
          public static void drawFinal()
          {
             StdDraw.setPenRadius(.005);
             StdDraw.setPenColor(StdDraw.WHITE);
             StdDraw.line(0.57, 0.67, 0.63, 0.67);
             StdDraw.line(0.56, 0.7, 0.59, 0.7); 
             StdDraw.line(0.61, 0.7, 0.64, 0.7);
             StdDraw.line(0.6, 0.55, 0.52, 0.55);
             StdDraw.line(0.6, 0.55, 0.68, 0.55);
    
             StdDraw.setPenRadius();
             StdDraw.setPenColor(StdDraw.BLACK);
             StdDraw.filledCircle(0.6, 0.67, .012);
             StdDraw.filledCircle(0.575, 0.7, .009);
             StdDraw.filledCircle(0.625, 0.7, .009);
             StdDraw.line(0.6, 0.55, 0.52, 0.63);
             StdDraw.line(0.6, 0.55, 0.68, 0.63);
             return;
          }
          public static void dead()
          {
             StdDraw.setPenRadius(.005);
             StdDraw.setPenColor(StdDraw.WHITE);
             // Erase previous Eyes & Mouth
             StdDraw.filledCircle(0.6, 0.67, .012);
             StdDraw.circle(0.6, 0.67, .012);
             StdDraw.filledCircle(0.575, 0.7, .009);
             StdDraw.circle(0.575, 0.7, .009);
             StdDraw.filledCircle(0.625, 0.7, .009);
             StdDraw.circle(0.625, 0.7, .009);
             StdDraw.line(0.6, 0.55, 0.52, 0.63);
             StdDraw.line(0.6, 0.55, 0.68, 0.63);
             
             //Draw final Eyes, Mouth, and Arms
             StdDraw.setPenRadius();       
             StdDraw.setPenColor(StdDraw.BLACK);
             //Eyes
             StdDraw.line(0.56, 0.69, 0.59, 0.71); 
             StdDraw.line(0.59, 0.69, 0.56, 0.71);
             StdDraw.line(0.61, 0.69, 0.64, 0.71);
             StdDraw.line(0.64, 0.69, 0.61, 0.71);
             //Mouth
             StdDraw.line(0.57, 0.67, 0.63, 0.67);
             //Arms
             StdDraw.line(0.6, 0.55, 0.53, 0.45);
             StdDraw.line(0.6, 0.55, 0.67, 0.45);
             loseGame();
           }
           public static void loseGame()
           {
                StdDraw.setPenColor(StdDraw.RED);
    		      StdDraw.text(0.5, 1, "You lost...");
                StdAudio.play("Lose.wav");
                StdDraw.text(0.5, 0.95, "Press Y to play again, N to quit");         
           }
           public static void winGame()
           {
                StdDraw.setPenColor(StdDraw.BLUE);
    		      StdDraw.text(1, 1, "You WIN!! Congratulations, you live to see another day!");
                StdDraw.text(0.5, 0.4, "Press Y to play again, N to quit");
           }
           public static boolean playAgain()
    	   {
    		   while (true)
    		   {
    			   if (StdDraw.isKeyPressed(KeyEvent.VK_Y))
    			   {
    				   return true;
    			   }
    			   else if (StdDraw.isKeyPressed(KeyEvent.VK_N))
    			   {
    				   StdAudio.play("GameOver1.wav");
                   StdDraw.setPenColor(StdDraw.RED);
    		         StdDraw.text(0.5, 0, "GAME OVER");
                   return false;
    			   }
    		   }
    	   }
    
          
          public static void main(String[] args)
          {
             do
             {
                
                StdDraw.clear();
                StdAudio.play("Bonanza.wav");
                drawGallows();
                wordSelect();
             }
             while (playAgain());
          }
       }

  10. #10
    NightRider785 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Classic Hangman Help

    And if I use isKeyPressed for wrong answers, the longer I hold in ie) 'A', it goes through them all. I need it to only do one line for each time a wrong letter is used, not all 8 at once if someone forgets to let go of the key...

  11. #11
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Classic Hangman Help

    Quote Originally Posted by NightRider785 View Post
    And if I use isKeyPressed for wrong answers, the longer I hold in ie) 'A', it goes through them all. I need it to only do one line for each time a wrong letter is used, not all 8 at once if someone forgets to let go of the key...
    Well, to solve this issue, you need to simply keep track of what has already been chosen as incorrect. Possibly add it to an arraylist, and then add a check something like
    Java Code:
    if(isKeyPressed && !alreadyPressed.contains("R") {
    }
    or whatever the correct syntax is for how you're doing it.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Classic Hangman Help

    One major problem with the posted code is that it doesn't have any comments describing what the different sections of the code are supposed to do.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Classic Hangman Help

    And the code is really hard to read in my eyes.

    - all static .... That is something that I am not used to.
    - only local variables - that needs a fix! For example: you choose a word but after the method is done, you no longer know the chosen word.
    - double code. The wordXYZ methods more or less have the same code. That should be refactored (if I understood it correctly, you are just drawing lines for characters. So you could write a method that takes the word as argument and then draws the required lines)
    - magic numbers - maybe you should introduce some constants with meaningful names. A "double x = -0.12 + 0.12;" is an example ...what are these values?
    - meaningful naming. A method is doing something so it should not have a noun as name like wordHomework. If it draws the lines for the word Homework, then it should be drawLinesForWordHomework.

    That are my current comments in the hope that they help.

    Konrad

  14. #14
    NightRider785 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Classic Hangman Help

    Quote Originally Posted by AlexGraal View Post
    Well, to solve this issue, you need to simply keep track of what has already been chosen as incorrect. Possibly add it to an arraylist, and then add a check something like
    Java Code:
    if(isKeyPressed && !alreadyPressed.contains("R") {
    }
    or whatever the correct syntax is for how you're doing it.
    I made an array list, called it 'letters' and it contains all letters of the alphabet, so it's [26].
    Not sure what you would have for 'alreadyPressed', or if that would be the array list I mentioned above. If I use if(isKeyPressed && !alreadyPressed.contains("R"), obviously the 2nd part wont work, but the isKeyPressed also brings an error of 'Symbol not found'. I do not think I can use '.contains', but with this:
    Java Code:
     
    //class used if word "COLLEGE" is randomly picked by computer
    int tries = 0;
             boolean correct = false;
             
             while (tries < 8 && !correct)
             {
                StdDraw.text(0.5, 1, "Guess a Letter");
                String[]letters = new String[26];
             letters[0] = "A";
             letters[1] = "B";
             letters[2] = "C";
             letters[3] = "D";
             letters[4] = "E";
             letters[5] = "F";
             letters[6] = "G";
             letters[7] = "H";
             letters[8] = "I";
             letters[9] = "J";
             letters[10] = "K";
             letters[11] = "L";
             letters[12] = "M";
             letters[13] = "N";
             letters[14] = "O";
             letters[15] = "P";
             letters[16] = "Q";
             letters[17] = "R";
             letters[18] = "S";
             letters[19] = "T";
             letters[20] = "U";
             letters[21] = "V";
             letters[22] = "W";
             letters[23] = "X";
             letters[24] = "Y";
             letters[25] = "Z";
                if (StdDraw.isKeyPressed(KeyEvent.VK_C))
                {
                   StdDraw.picture(0.155, 0.14, "C1.jpg");
                }
                else if (StdDraw.isKeyPressed(KeyEvent.VK_O))
                {
                   StdDraw.picture(0.275, 0.14, "O1.jpg");
                }
                else if (StdDraw.isKeyPressed(KeyEvent.VK_L))
                {
                   StdDraw.picture(0.395, 0.14, "L1.jpg");
                   StdDraw.picture(0.515, 0.14, "L1.jpg");
    
                }
                else if (StdDraw.isKeyPressed(KeyEvent.VK_E))
                {
                   StdDraw.picture(0.635, 0.14, "E1.jpg");
                   StdDraw.picture(0.875, 0.14, "E1.jpg");
    
                }
                else if (StdDraw.isKeyPressed(KeyEvent.VK_G))
                {
                   StdDraw.picture(0.755, 0.14, "G1.jpg");
                }
                else if (StdDraw.isKeyPressed())
                   {
                   System.out.println("Incorrect guess...");
                   StdAudio.play("Buzzer.wav");
                   if (tries == 0)
                   {
                      drawHead();
                   }
                   else if (tries == 1)
                   {
                      drawEyes();
                   }
                   else if (tries == 2)
                   {
                      drawMouth();
                   }
                   else if (tries == 3)
                   {
                      drawBody();
                   }
                   else if (tries == 4)
                   {
                      drawLegs();
                   }
                   else if (tries == 5)
                   {
                      drawArms();
                   }
                   else if (tries == 6)
                   {
                      drawFinal();
                   }
                   else if (tries == 7)
                   {
                      dead();
                      
                   }
                   tries++;
                }
             }
    My line 59 is not correct, as I'm not sure what to do yet, but if I start with that... How would I subtract from the list? ie) User types 'c', it puts a 'C' in the right play in the drawing (Line 35-38), now what could I put in there to remove 'C' or letters[2] from the choice in the array?
    Last edited by NightRider785; 04-29-2014 at 11:53 PM.

  15. #15
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Classic Hangman Help

    Sorry, let me restate what I said. I glanced over the code, but didn't study it in-depth.

    I'm just throwing you an idea here.

    Everytime a person chooses a letter, simply add it to an array called pressedKeys or chosenLetters or something like that.

    Then, use the already existing method, arraylist.contains("letter"), to check if the person has pressed that before. Basically, before taking any action, make sure that that letter isn't in the arraylist. If you DO take action and check if the letter is right/wrong, also add the letter to the arraylist. That way, no letter can be chosen twice.

    This is much simpler than having a separate arraylist with all of the letters. If you DO want to have an array list with all of the letters, you can get the index of a given letter by arraylist.IndexOf("letter"). If it returns a value > 0, then the letter hasn't been used before. Proceed with game and remove letter from arraylist. If it returns -1, then the letter isn't in the arraylist. Letter has already been used - don't proceed with action.


    Furthermore, to me, having a separate method to draw the body and stuff is cleaner. Maybe something like this:

    Java Code:
    public static drawStickman(int wrong) {
         switch(wrong) {
              case 1: 
                  //draw the head
              case 2:
                 //draw the body
              case 3:
                 //...
       
              case 9: 
                 //draw the eyebrows
              default:
                  //draw YOU LOSE or something
    }
    Then simply do

    Java Code:
    if(incorrect choice) {
       numberWrong++;
       drawStickman(numberWrong);
    }
    Though I guess it doesn't really matter much.
    Last edited by AlexGraal; 04-30-2014 at 12:19 AM.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Classic Hangman Help

    I don't think the StdDraw class's methods supports what is needed for this program. The code should receive the key press and process it. Once a key is pressed, how long it stays pressed shouldn't be an issue for normal durations.
    The isKeyPressed() method must be called for ALL the keys on the keyboard to catch correct/wrong key presses. On faster computers it is possible the program could cycle around the loop and detect that the key was pressed multiple times for a slow user.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    NightRider785 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Classic Hangman Help

    Alex:
    I was toying with putting the draw stickman in a separate method, but moved off of that to deal with letter choice, but that is also very helpful so I do not have the draw parts for every word I have.
    If I go the second method (more like what we have done in my class):
    "If you DO want to have an array list with all of the letters, you can get the index of a given letter by arraylist.IndexOf("letter"). If it returns a value > 0, then the letter hasn't been used before. Proceed with game and remove letter from arraylist. If it returns -1, then the letter isn't in the arraylist. Letter has already been used - don't proceed with action."

    How would you remove a letter from the arraylist? Arrays have been a challenge for me to grasp for some reason. I am working on getting better at them, but with this program due tomorrow, I am pulling my hair out trying to figure them out. If you could show me an example like you did above with the arraylist, that would be amazing!

    Norm:
    I'm beginning to realize I may have made a wrong choice on choosing hangman for this, as StdDraw is what the game has to be based in... I think my while loop is throwing off the isKeyPressed command. It's looping through too quickly for the proper response time to let off the key
    Last edited by NightRider785; 04-30-2014 at 12:32 AM.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Classic Hangman Help

    Another problem could be missed key presses. If the isKeyPressed() method is not called while the key is pressed, the program will never know that the key was pressed.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Classic Hangman Help

    Quote Originally Posted by NightRider785 View Post
    Alex:
    I was toying with putting the draw stickman in a separate method, but moved off of that to deal with letter choice, but that is also very helpful so I do not have the draw parts for every word I have.
    If I go the second method (more like what we have done in my class):
    "If you DO want to have an array list with all of the letters, you can get the index of a given letter by arraylist.IndexOf("letter"). If it returns a value > 0, then the letter hasn't been used before. Proceed with game and remove letter from arraylist. If it returns -1, then the letter isn't in the arraylist. Letter has already been used - don't proceed with action."

    How would you remove a letter from the arraylist? Arrays have been a challenge for me to grasp for some reason. I am working on getting better at them, but with this program due tomorrow, I am pulling my hair out trying to figure them out. If you could show me an example like you did above with the arraylist, that would be amazing!

    Norm:
    I'm beginning to realize I may have made a wrong choice on choosing hangman for this, as StdDraw is what the game has to be based in... I think my while loop is throwing off the isKeyPressed command. It's looping through too quickly for the proper response time to let off the key

    Well, you have two options for the removal.

    [CODE]
    import java.util.*;

    ArrayList<String> letters = new ArrayList<String>();
    //add all the letters here however you want.

    //to remove the letter e:
    letters.remove("e");

    //OR

    letters.remove(letters.IndexOf("e"));
    [CODE]

    Obviously the first one is much cleaner:
    http://www.tutorialspoint.com/java/util/arraylist_remove_object.htm

  20. #20
    NightRider785 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Classic Hangman Help

    Quote Originally Posted by AlexGraal View Post
    Well, you have two options for the removal.

    Java Code:
    import java.util.*;
    
    ArrayList<String> letters = new ArrayList<String>();
    //add all the letters here however you want.
    
    //to remove the letter e:
    letters.remove("e");
    
    //OR
    
    letters.remove(letters.IndexOf("e"));
    Obviously the first one is much cleaner:
    http://www.tutorialspoint.com/java/u...ove_object.htm
    Ok, so would I have an IF statement for each letter?
    Java Code:
     if (StdDraw.isKeyPressed(KeyEvent.VK_C))
                {
                   StdDraw.picture(0.155, 0.14, "C1.jpg");
                   letters.remove("c");
                }
    //etc until the false letters
    else if (StdDraw.isKeyPressed(KeyEvent.VK_A))
    {
           letters.remove("a");
    }
    //etc till they are all gone or game is lost?
    Or could I have two separate arraylists, 1 with correct letters, and one with wrong letters?
    Last edited by NightRider785; 04-30-2014 at 05:11 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. FM-Classic 1.9 beta 2
    By java software in forum Java Software
    Replies: 0
    Last Post: 05-04-2013, 03:17 AM
  2. Classic Palindrome problem
    By nanderson05 in forum New To Java
    Replies: 14
    Last Post: 05-14-2012, 10:45 AM
  3. Java Classic RPG 2007-06-25
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-26-2007, 09:43 PM

Posting Permissions

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