We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,307 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with making Hangman work...?

So far, it's all working great, but all I need left is to get the method WORD1() to go to WORD2() when the person types in the correct letters. The first word is "Java". How would I make it so that when they type in "Java", it will go to WORD2()?

public static void main(String[] args) {
    
    STAND();
    WORD1();
    WORD2();
    
  }

  public static void WORD1() { //Java
    char[] JAVA = {'j','a','v'};
    char key = ' ';
    String done;
    
    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(JAVA[0] == 'j' && JAVA[1] == 'a' && JAVA[2] == 'v')
      {
        c.clear();
        counter = 0;
        STAND();
        WORD2();
      }
      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();
        }
      }
    }
  }

  public static void WORD2() {
    //code here
  }
5
Contributors
52
Replies
4 Days
Discussion Span
2 Years Ago
Last Updated
53
Views
Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Help?

Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

When the method WORD1 has the 4 letters required, have it do a return.
Execution will then return to the main method where WORD2 will be called.

See cross post at:
http://www.java-forums.org/new-java/43960-hangman.html#post208262

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The code you've posted looks like a collection of may different attempts to solve the problem and has gotten to be a mess.
Perhaps you should trash it and start over with a new design. Leave out the drawing and GUI stuff and just work on the logic of how to read in the letters and test when you have the correct letters in the correct sequence.

Start with creating new logic and then code it.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

I would if I had the time to. This took long enough to make since I am a beginner.

Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

How about starting with a simple version of the program where each WORDi method only needs to read in and accept/validate one or two letters instead of 4. When you get the logic worked out, then you could move to the 4 letters you are trying to process now.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

I only need 5 words for this program. I just seriously need help doing the returning. Could you maybe show me how you'd do it?

Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

My suggestion is to start with a simple program, figure out the logic for one letter for each method and when that works, move on to two letters for each method.
When you get that to work, you should have the idea on how to write the logic for more letters.

Good luck.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

...how many times should I repeat myself? I'd like to stick with what I have but I'd like to learn how to use the returning.

Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Go to this site and search for return. It has an explanation and examples.
http://download.oracle.com/javase/tutorial/reallybigindex.html

Then write a small, simple program and test the two ways to use the return statement.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

No one is going to do your homework for you. Also you should keep in mind many schools automatically search forums and compare them to submitted homeworks. If you plan on taking code from forums you will most likely get caught and in trouble.

I suggest learning how to program and trying to do it yourself first. When you a problem then ask for a few pointers. No one is going to do all of your work for you.

\007
Junior Poster in Training
67 posts since Apr 2011
Reputation Points: 21
Solved Threads: 6
Skill Endorsements: 0

I'm not asking for someone to do the homework for me. I'm just asking for an example of how I could use the returning in this situation so that I can implement it into it.

Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Here's a simple example:

private void aMethod(int x) {
  if (x < 10)  // Test if the value in range
     return;   // exit the method if too small
   // do other stuff here
} // end aMethod()
NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

So, in this case should I be using ints or Strings?

Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Use ints for numberic data and Strings for string data.

I'm not sure what the "case" is you are talking about.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

I'm still lost as to how I could make this work using returning. Sorry. :/

Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Please describe in more detail what you want your program to do.
You need to have a design for the program before you write it.
What you have posted now is a mixed up mess.

My suggestion is to start with a simple program, figure out the logic for one letter for each method and when that works, move on to two letters for each method.
When you get that to work, you should have the idea on how to write the logic for more letters.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Alright, sorry if I wasn't clear in the OP. The current problem is: once the person fills out the required letters for the first word ("Java"), it doesn't automatically go to the second word which is method WORD2().

In other words:

- I type "J", "A", and "V" into the keyboard, completing the word.
- When I have "JAVA" filled out, it doesn't automatically go to WORD2() as it should.

Feriscool
Light Poster
42 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

WORD1 should return to the calling method when it has received the 4 letters.
The calling method can then call the WORD2 method.
also the WORD1 method could return an error code if there was a problem to tell the calling method about it.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1289 seconds using 2.83MB