Unformatted text preview:

Math 121: Introduction to Computing Handout #16Assignment 4 — HangmanAssignment #4 is to write a program that plays the game of Hangman. This assignmentserves two purposes. First, it is designed to give you some practice writing programs thatmanipulate strings and files. Second, you will have a chance to work with multipleclasses in a single application.When it plays Hangman, the computer first selects a secret word at random from a listbuilt into the program. The program then prints out a row of dashes—one for each letterin the secret word—and asks the user to guess a letter. If the user guesses a letter that isin the word, the word is redisplayed with all instances of that letter shown in the correctpositions, along with any letters correctly guessed on previous turns. If the letter doesnot appear in the word, the user is charged with an incorrect guess. The user keepsguessing letters until either (1) the user has correctly guessed all the letters in the word or(2) the user has made eight incorrect guesses. Two sample runs that illustrate the play ofthe game are shown in Figure 1 on the next page.When it is played by children, the real fascination (a somewhat morbid one) of Hangmancomes from the fact that incorrect guesses are recorded by drawing an evolving picture ofthe user being hanged at a scaffold. For each incorrect guess, a new part of a stick-figurebody—first the head, then the body, then each arm, each leg, and finally each foot—isadded to the scaffold until the hanging is complete. For example, the three diagramsbelow show the drawing after the first incorrect guess (just the head), the third (the head,body, and left arm), and the diagram at the tragic end of a losing game:– 2 –In order to write the program that plays Hangman, you should design and test yourprogram in three parts. The first part consists of getting the interactive part of the gameworking without any graphics at all and with a fixed set of secret words. The second partconsists of building a separate class that maintains the scaffold diagram. The final partrequires you to replace the supplied version of the secret word list with one that readswords from a file. The rest of this handout describes these three parts in more detail.Figure 1. Two sample runs of the Hangman program (console only)– 3 –Part I—Playing a console-based gameAs the first part of this assignment, write a program that handles the user interactioncomponent of the game—everything except the graphical display. To solve the problem,your program must be able to:• Choose a random word to use as the secret word. That word is chosen from a wordlist, as described in the following paragraph.• Keep track of the user’s partially guessed word, which begins as a series of dashes andis then updated as correct letters are guessed.• Implement the basic control structure and manage the details (ask the user to guess aletter, keep track of the number of guesses remaining, print out the various messages,detect the end of the game, and so forth).The only operation that is beyond your current knowledge is that of representing the listof words from which you can choose a word at random. For the first two parts of theassignment, you will simply make use of a class that is provided to you, calledHangmanLexicon, that provides a small list of words that will allow you to test yourprogram. (A lexicon is like a dictionary but does not necessarily include definitions,making it a more appropriate name for a class that provides a list of words with noassociated meanings.) The implementation of the class you’ve been given is only atemporary expedient to make it possible to code the rest of the assignment. In Part III,you will replace the definition we’ve provided with one that reads a list of words from adata file.The strategy of creating a temporary implementation that provides enough functionalityto implement the rest of the program is a common technique in programming. Suchtemporary implementations are usually called stubs. In this assignment, the starterproject comes with a stub implementation of the HangmanLexicon class, shown in Figure2. The class contains two public methods: getWordCount(), which returns the numberof words in the lexicon, and getWord(i), which returns the word at index i. Like allindices in Java, the value i runs from 0 to one less than the number of words.– 4 –Figure 2. Stub implementation of HangmanLexicon/* * File: HangmanLexicon.java * ------------------------- * This file contains a stub implementation of the HangmanLexicon * class that you will reimplement for Part III of the assignment. */import acm.util.*;public class HangmanLexicon {/** Returns the number of words in the lexicon. */public int getWordCount() {return 10;}/** Returns the word at the specified index. */public String getWord(int index) {switch (index) {case 0: return "BUOY";case 1: return "COMPUTER";case 2: return "CONNOISSEUR";case 3: return "DEHYDRATE";case 4: return "FUZZY";case 5: return "HUBBUB";case 6: return "KEYHOLE";case 7: return "QUAGMIRE";case 8: return "SLITHER";case 9: return "ZIRCON";default: throw new ErrorException("getWord: Illegal index");}};}A game that used this implementation of the HangmanLexicon class would quicklybecome uninteresting because there are only ten words available. Even so, it will allowyou to develop the rest of the program and then come back and improve this part later.Part I is a string manipulation problem. The sample runs in Figure 1 should be sufficientto illustrate the basic operation of the game, but the following points may help to clarify afew issues:• At the beginning of your run method, you need to create a new HangmanLexicon andstore it in an instance variable. If you extend the program to allow the user to play– 5 –multiple games, the creation of the HangmanLexicon should be performed outside theloop that plays the game repeatedly so that this operation is performed once rather thanfor every game.• You should accept the user’s guesses in either lower or upper case, even though allletters in the secret words are written in upper case.• If the user guesses something other than a single letter, your program should tell theuser that the guess is illegal and accept a new guess.• If the user guesses a correct letter


View Full Document

REED MATH 121 - Study Notes

Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?