In order to complete this applet, we can split it into the following parts:
Your applet is supposed to pick a random word and then have the user guess it. In this part, we will worry about creating such a dictionary. Normally, you would like to have a large dictionary of words to play this game. However, a complete dictionary would also inlcude lots of easy to guess 1-letter and 2 or 3-letter words that would make the game really easy to play. For our purposes, we will pick a set of words that are atleast 6 to 9 letters long.
Ideally, when your Hangman applet is loaded by anyone any where on the WWW, it should be able to retreive the dictionary file and select words from it. Java Applets are normally prevented from reading files due to security considerations. Some applet viewers do let you relax such a security restriction. We will pretend that this restriction does exist and write an applet that literally carries with it a dictionary. Of course that leads to concerns about carrying a HUGE dictionary of words over the net. We have to mae a trade-off here, though, without sacrificing the interestingness of the resulting applet. Let us say, a dictionary of 2000 words would serve as a good balance. The total size of such a dictionary would be just under 20K bytes and so is not unreasonable for file size.
You can define such a dictionary as a separate class as follows:
public class Dictionary { public static final int nWords = 2000; // #of words contained in dictionary public Dictionary() { // creates a new dictionary of nWords .... } public String getWord() { // returns a word from the dictionary } }
In the above, nWords will have to be set to the number of words contained in the dictionnary. The class will also define an array of String type and then assign each word in the dictionary to the elements of the array. For example:
String words[] = new String[10]; ... words[0] = "acrobat"; words[1] = "broccoli"; words[2] = "creative"; ... words[9] = "zirconia";
Then, you can define getWord to pick a random number between 0 and nWords and return the word sitting in that position of the array words.
Create such a class in a separate file called Dictionary.java and add 10 test words to it. Add this file to your project, so you can use getWord to select a random word. Later, we will give you a 2000-word dictionary that you can add to your program. For now, this test Dictionary would serve the purpose.
In your Hangman applet, define a method called newWord that selects a new word and puts it in a global variable of the applet. For the moment, just have the applet print this word out in the console window.
You will need to create a new Dictionary object as follows:
Dictionary D = new Dictionary();
Then the command
theWord = D.getWord();
will assign a random word from the dictionary into theWord an applet String variable.
At this point, you have an applet that draws the GUI and is also able to select a word from the dictionary. Next, let us activate the buttons so that the user interaction is enabled.
All user interaction in this applet happens through the 27 buttons you have defined. Thus, you will have to implement ActionListener interface and add the actionPerformed method to your applet.
In action performed, you have to distinguish between the buttons. Here is an example of it:
public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("New Word") newWord(); else // some button between a-z is pressed processButton(cmd.charAt(0)); repaint(); }
In the above, processButton will get the character that was pressed by the user. This may not be the final version of actionPerformed, but it is a very good start.
Add user interaction to the applet as described. For now, newWord picks a new word and prints it to the console window. processButton also prints the letter corresponding to the button in the console window.
Once this is done, you will have an applet that has most of the pieces ready for putting together the rest of the Hangman game.
Complete this portion of the project by Wednesday, December 3. |
Step 4: Keeping Score and other cosmetics