Spring 2003
(Sections 02 & 03 only) |
Program # 5
Due by 9 pm onWednesday of Week # 9. |
|
| ||
| ||
|
Hangman is a two player game word game where one player chooses a word and the other tries to figure out what the word is by guessing letters in it. In this game the role of the first player (who chooses the word) will be played by the computer.
The play goes as follows: The computer will guess a word and show it to the player as a series of dashes, one for each letter in the word. The player is then asked to guess a letter. If the letter is in the word, any blanks corresponding to that letter are filled in. If the letter is not in the word, the player moves one step closer to losing. This is indicated by adding another body part to a stick figure hanging from a gallows. If the full figure is completed before the word is completely guessed, the player is dead and, thus, loses. (Children's games are often such sweet, gentle activities, aren't they?)
To help you along, the following classes are provided:
public static String getRandomWord()
public void drawPicture()
public void drawWord(String word, boolean[] guessedLetters)
true
represents a letter (correct or not) that has already been guessed;public void increaseBadness()
public boolean isDead()
true
if the player is now dead.You should just compile and use these two classes as they are. Do not edit them! In theory, you should be able to use them from just the information provided above but, if you are interested in how either of these work, do feel free to look at their implementation.
Write a program, Hangman.java
, that will do the following:
Hangman
with the user until the player does not answer "y"
(for "yes") to the "Play again?"
question. drawPicture()
for the current player to display the appropriate picture.)drawWord()
with appropriate parameters to display the state of the word for the current player.)Some hints:
public static String Keyboard.readString() // This will read a string from the keyboard and return it to you.
public static String Keyboard.readInt() // This will read an integer from the keyboard and return it to you.
Hangman
on paper with a friend or classmate, and make note of how the game proceeds. What will your program need to do? What methods and variables will you need to accomplish it? How will you go about it?boolean
array to keep track of which letters have been guessed. The drawWord()
method requires that you pass it such an array; you may find other uses for it (e.g., to check for duplicates). Remember (from the Cipher Lab, and other class activities) how you can use the distance from a given character (c) to the letter 'a' in a number of interesting ways.String
. (Again, drawWord()
requires this.) A review of the String methods may prove useful (see your textbook appendices or other Java references). You can select particular characters from a String by using the charAt()
method from the String class. You can check if a character is contained in a String by using the indexOf()
method.Character
class methods to help manage letters vs. non-letters and upper- vs. lower-case letters. (Again, look in the Appendices or other Java references.)% java Hangman I'm thinking of a word. It has 4 letters. ______ | | | | | | |________ ____ Guess? e I'm sorry, there is no 'e'. ______ | | | O | | | |________ ____ Guess? s I'm sorry, there is no 's'. ______ | | | O | | | | |________ ____ Guess? g I'm sorry, there is no 'g'. ______ | | | O | | | | | |________ ____ Guess? a I'm sorry, there is no 'a'. ______ | | | O | /| | | | |________ ____ Guess? l I'm sorry, there is no 'l'. ______ | | | O | /|\ | | | |________ ____ Guess? m I'm sorry, there is no 'm'. ______ | | | O | /|\ | | | / |________ ____ Guess? w I'm sorry, there is no 'w'. ______ | | | O | /|\ | | | / \ |________ I'm sorry, you seem to have died. The word was "tonk" Again? yes I'm thinking of a word. It has 6 letters. ______ | | | | | | |________ ______ Guess? m I'm sorry, there is no 'm'. ______ | | | O | | | |________ ______ Guess? M You have already guessed 'm'. Guess again: m You have already guessed 'm'. Guess again: 8 I'm sorry, '8' is not a letter. Guess again: g I'm sorry, there is no 'g'. ______ | | | O | | | | |________ ______ [... In the interest of space, I'll skip a bit ...] Guess? a There is a letter 'a'. ______ | | | O | /|\ | | | / |________ fia_co Guess? s There is a letter 's'. ______ | | | O | /|\ | | | / |________ Congratulations, you got "fiasco!" Again? n (Thinking, "That was just too close...") %
Use the "handin" facility available from your Central Unix account on Polylog1 to submit your game file:
It is expected that your source code will contain full internal documentation. Use the standard comment block at the beginning of the program for your design specifications and other general information. Use comments throughout your programs to clarify to anyone reading your program what you are doing at each step along the way. If you have additional comments to make, you may submit a supplemental README document, in plain text format, but its existence must be documented in the program file you submit.
You are not to edit either of the two files you were given and, thus, you should not submit copies of those. Your code must run with your instructor's copy of those files.
Instructions about handin are given on preceeding pages on this site. You will submit to the "Pgm5" folder by 9 pm, Wednesday of the 9th week.
Back: | This Instructor's CSC-101 HomePage |
Up: | This Instructor's HomePage |