Back to the Hangman Ideal...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    Back to the Hangman Ideal...

    I need some practice and so I decide to take on this project.
    Trying to figure out if I could use several Class for this project.
    1. one for the images of the hangman when they guess wrong it draw a image
    2. one for the input info. ie user tries to guess the letters for the word This one is all most done. This is done with JFrame,
    3. one for the words that will be stored in a arraylist or an array and it will choose the word ramdomly.
    any Ideal would be great.

    nomad
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    #2
    Originally posted by nomad
    I need some practice and so I decide to take on this project.
    Trying to figure out if I could use several Class for this project.
    1. one for the images of the hangman when they guess wrong it draw a image
    2. one for the input info. ie user tries to guess the letters for the word This one is all most done. This is done with JFrame,
    3. one for the words that will be stored in a arraylist or an array and it will choose the word ramdomly. [size=2][/size]
    [size=2]
    [/size]any Ideal would be great.

    nomad
    3. one for the words that will be stored in a arraylist or an array and it will choose the word ramdomly. I have this done, used array and with ramdon and Math.abs

    Comment

    • DeMan
      Top Contributor
      • Nov 2006
      • 1799

      #3
      You might also consider having a seperate "Driver" class....or is that the role of (2) ?

      Comment

      • nomad
        Recognized Expert Contributor
        • Mar 2007
        • 664

        #4
        Originally posted by DeMan
        You might also consider having a seperate "Driver" class....or is that the role of (2) ?
        OK what is a Driver class. Is that the main?
        If so I think role 2 would be the best place to run the application.

        nomad

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          And don't forget those nifty regular expressions that can be used for checking
          which letters are (in) correct.

          kind regards,

          Jos

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by JosAH
            And don't forget those nifty regular expressions that can be used for checking
            which letters are (in) correct.

            kind regards,

            Jos
            That the next step, I need to figure out.

            nomad

            Comment

            • nomad
              Recognized Expert Contributor
              • Mar 2007
              • 664

              #7
              Originally posted by nomad
              I need some practice and so I decide to take on this project.
              Trying to figure out if I could use several Class for this project.
              1. one for the images of the hangman when they guess wrong it draw a image
              2. one for the input info. ie user tries to guess the letters for the word This one is all most done. This is done with JFrame,
              3. one for the words that will be stored in a arraylist or an array and it will choose the word ramdomly.
              any Ideal would be great.

              nomad
              I'm currently working on the hangman itself.
              I don't know if I could draw it or have images which are already created.
              I think drawing the hangman would be easier to do, but I would like to do the images instead.
              If I use the images and this is where I'm stuck. Should I use ImageIcon for them?
              Please help...

              Comment

              • DeMan
                Top Contributor
                • Nov 2006
                • 1799

                #8
                YOu could draw them as needed, or create seperate components which appear as needed, although perhaps the easiest approach (I think) is to make a series of gallows in different stages of construction -> Then you can change the picture (kind of like a flip book) to give the impression of the gallows being constructed

                Comment

                • nomad
                  Recognized Expert Contributor
                  • Mar 2007
                  • 664

                  #9
                  Originally posted by nomad
                  I need some practice and so I decide to take on this project.
                  Trying to figure out if I could use several Class for this project.
                  1. one for the images of the hangman when they guess wrong it draw a image
                  2. one for the input info. ie user tries to guess the letters for the word This one is all most done. This is done with JFrame,
                  3. one for the words that will be stored in a arraylist or an array and it will choose the word ramdomly.
                  any Ideal would be great.

                  nomad
                  I have the ramdom words Class done. Now I'm trying to figure out how to compare the input to the ramdom word.
                  [size=2]This is what I have so far...[/size]
                  [size=2][/size]
                  Code:
                   class PersonClass { 
                  
                  static Scanner kbd;
                  
                  
                  
                  public static void main(String[] args) {
                  
                  {
                  
                  kbd = new Scanner(System.in);
                  
                  String text[];
                  
                  text = new String[12];
                  
                  int number = 0;
                  
                  
                  
                  text[number++] = "MONDAY";
                  
                  text[number++] = "PROGRAMING";
                  
                  text[number++] = "GOOD DAY";
                  
                  text[number++] = "ASSIGNMENT";
                  
                  text[number++] = "FOR SCORE";
                  
                  text[number++] = "PERSONAL";
                  
                  text[number++] = "WORK IS NOT FUN";
                  
                  text[number++] = "GOOD BYE";
                  
                  text[number++] = "DICIONARY";
                  
                  text[number++] = "I LOVE ITALY";
                  
                  text[number++] = "BIKE RIDING";
                  
                  text[number++] = "GOOD GRIEF";
                  
                  
                  
                   
                  
                  /* Create random number generator. */
                  
                  Random random = new Random();
                  
                  
                  
                   
                  
                  /* Choose an integer between 0 and the size of the array; effectively
                  
                  chooses the index of one element of the array. */
                  
                  int randomIndex = Math.abs(random.nextInt()%text.length);
                  
                  
                  
                  /*Prints out the word*/
                  
                  System.out.println(text[randomIndex]);
                  
                  System.out.println("There are this many character for this word " + text[randomIndex].length());
                  
                  /* guess the word */
                  
                  String word = text[randomIndex];//place a string to hold word.
                  
                  System.out.print(word);//testing ramdom word
                  
                  System.out.print("enter a letter");
                  
                  String inputWord = kbd.next();
                  
                  
                  
                  if (inputWord == (text[randomIndex])){
                  
                  System.out.println("you got one word");
                  
                  }else{
                  
                  /* Display the randomly-chosen word. */
                  
                  JOptionPane.showMessageDialog(null, "The random word was " +
                  
                  text[randomIndex]);
                  
                  }
                  
                  //Do you want to play another game
                  
                  //System.out.println("Do you want to play another game? Y or N ");
                  
                  }
                  
                  }
                  Code:
                  }
                  
                  [left][/left]

                  [size=2]Not to sure if I need to place the ramdom word in a string or not. I base this ideal that Jos gave back on the org. hangman ideal.[/size]
                  Code:
                  String secret= "apple"; // the secret word
                  String correctChars= "p"; // previously correctly guessed chars
                  String guess= "e"; // the new guess
                  String newSecret= secret.replaceAll("[^"+correctChars+guess+"]", "-");
                  [size=2]
                  The value of newSecret will be equal to "a--l-"; if you want to make sure that
                  the remaining new 'secret' doesn't contain any hidden letters do this:

                  [/size]
                  Code:
                   
                  
                  boolean win(String secret, String correctChars) {
                     String temp= secrect.replaceAll("[^"+correctChars+"]", "-");
                     return temp.indexOf('-') < 0;
                  }
                  Any ideals and help would be great.

                  nomad

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    May I suggest that you don't use those ugly arrays? You wrote an entire article
                    yourself on Lists (ArrayLists in particular). Why not use those? And why not
                    initialize them using a, say, .properties file? You can even add clues to the
                    words to be guessed. Suppose you have a .properties file like this:

                    Code:
                    dog             = an easy word: woof!
                    defenestration  = nobody knows this word
                    ...
                    A Properties object is a HashTable which implements the Map interface. You
                    can get the keySet() (the words to the left of the equal sign) and you can build
                    a List out of that. You can pick a random element out of that List and optionally
                    get the clue message given that key from the Properties object. Much more fun
                    than just thost stupid arrays.

                    kind regards,

                    Jos

                    Comment

                    • nomad
                      Recognized Expert Contributor
                      • Mar 2007
                      • 664

                      #11
                      Originally posted by JosAH
                      May I suggest that you don't use those ugly arrays? You wrote an entire article
                      yourself on Lists (ArrayLists in particular). Why not use those? And why not
                      initialize them using a, say, .properties file? You can even add clues to the
                      words to be guessed. Suppose you have a .properties file like this:

                      Code:
                      dog = an easy word: woof!
                      defenestration = nobody knows this word
                      ...
                      A Properties object is a HashTable which implements the Map interface. You
                      can get the keySet() (the words to the left of the equal sign) and you can build
                      a List out of that. You can pick a random element out of that List and optionally
                      get the clue message given that key from the Properties object. Much more fun
                      than just thost stupid arrays.

                      kind regards,

                      Jos
                      array was easier to figure out..
                      I guess I can rework it.

                      nomad

                      Comment

                      • DeMan
                        Top Contributor
                        • Nov 2006
                        • 1799

                        #12
                        Might be an idea to do that eventually. If you take up JosAH's suggestion of using a clue, you may like to define an object that holds all this information together......

                        The rework should be reasonable simple. At this stage you need the Add(), Get() and possible size() methods (which add an element, retrieve an element and return the size of an element respectively).

                        Comment

                        Working...