Hangman Applet (Version 3)

Design

In order to complete this applet, we can split it into the following parts:

  1. The applet GUI
  2. Defining a dictionary of words
  3. Enabling user-interaction (playing the game)
  4. Keeping score and other cosmetics
  5. Finishing up

 

Step 4: Keeping Score and other cosmetics

Next, we will need two methods, drawLeft and drawRight. drawleft will be responsible for drawing the Hangman figure and drawRight will draw the word guessed so far and the scores etc on the right hand side.

Update: If you choose to use canvases for drawing the left and right sides as two different canvases (as discussed in class) you should use the repaint/paint methods in the canvases to draw the appropriate content. As shown in class, you can use set methods to transfer relevant information needed by the paint methods. You can still use the logic described below.

Lets us work on drawRight first:

void drawRight(int x, int y, int w, int h) {
 // draws the information in box whose upper left
 // corner is at (x, y) and is of width, w and height h

  // 1. draw the dashed word, 
  // for example: for "estimate", draw _ _ _ _ _ _ _ _

  // 2. draw the word guesses so far. For example, if user has correctly
  // guessed the letters: e t m
  // the screen will show:  e   t   m   t e
  //                        - - - - - - - - 

  // draw the score(s)
}

drawRight will be called by paint method where it will supply the x, y, w, h parameters.

You will need to maintain an array of characters (same length as the word selected to be guessed). Initialize this array to contain dashes ('-'). The best place to do this initialization is in newWord.

Each time the user presses a letter-button, you should see if the letter pressed is contained in the word to be guessed. In your array above, replace the dashes in the position where the letter occurs in the word to be guessed. I.e. if the word to be guessed is

estimate

your array will initially contain {'-', '-', '-', '-', '-', '-', '-', '-'}

and, after guessing e, t, and m, it will contain: {'e', '-', 't', '-', 'm', '-', 't', 'e'}

You will need at least two variables to keep score for the current word. One that indicates how many incorrect guesses there have been so far, and the other to keep a count of the number of total guesses so far. The first variable will be used by drawLeft to draw the appropriate state of the Hangman. We'll worry about drawLeft later. Both these variable should be set to zero every time a new word is selected (in newWord).

Add all of these to your applet. Now, your applet will be able to draw the word on the right side and also draw the score. Look up the definition of enable and disable methods for enabling or disabling buttons (See the text). See if you can figure this part out and add it to the applet. If you think about it now, drawLeft is also not that hard. Give it the number of wrong guesses and it will draw the required state of Hangman.

 Complete this portion of the project by Monday, December 8.

Step 5: Finishing up