26-assignment-4

26-assignment-4 - CS 106A Handout #26 July 15, 2009...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Handout #26 CS 106A July 15, 2009 Assignment 4 — Hangman Due: Wednesday, July 22 at 10:30 A . M . For Assignment #4, your mission is to write a program that plays the game of Hangman. As an assignment, Hangman serves two purposes. First, the program is designed to give you some practice writing programs that manipulate strings and files using the facilities described in Chapter 8 on “Strings and Characters” and the Chapter 12, Section 12.4 “Using Data Files” (pages 486-491 only ). Second, by extending the program using the graphical tools from Chapter 9, you will have a chance to work with multiple classes in a single application. When it plays Hangman, the computer first selects a secret word at random from a list built into the program. The program then prints out a row of dashes—one for each letter in the secret word and asks the user to guess a letter. If the user guesses a letter that is in the word, the word is redisplayed with all instances of that letter shown in the correct positions, along with any letters correctly guessed on previous turns. If the letter does not appear in the word, the user is charged with an incorrect guess. The user keeps guessing 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 of the game are shown in Figure 1 on the next page. When it is played by children, the real fascination (a somewhat morbid fascination, I suppose) from Hangman comes from the fact that incorrect guesses are recorded by drawing an evolving picture of the user being hanged at a scaffold. For each incorrect guess, a new part of a stick-figure body—first the head, then the body, then each arm, each leg, and finally each foot—is added to the scaffold until the hanging is complete. For example, the three diagrams below 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:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
– 2 – In order to write the program that plays Hangman, you should design and test your program in three parts. The first part consists of getting the interactive part of the game working without any graphics at all and with a fixed set of secret words. The second part consists of building a separate class that maintains the scaffold diagram. The final part requires you to replace the supplied version of the secret word list with one that reads words 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)
Background image of page 2
– 3 – Part I—Playing a console-based game In the first part of this assignment, your job is to write a program that handles the user interaction component 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 word
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

26-assignment-4 - CS 106A Handout #26 July 15, 2009...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online