Docsity

Hangman - Section Handout - Programming Methodology- 27, Exercises of Programming Methodologies

Toggle sidebar
bg1
Mehran Sahami Handout #27
CS 106A October 24, 2007
Assignment #4 — Hangman
Due: 3:15pm on Monday, November 5th
Based on a handout by Eric Roberts
For this assignment, 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. 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:
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Hangman - Section Handout - Programming Methodology- 27 and more Exercises Programming Methodologies in PDF only on Docsity!

Mehran Sahami Handout

CS 106A October 24, 2007

Assignment #4 — Hangman

Due: 3:15pm on Monday, November 5th

Based on a handout by Eric Roberts

For this assignment, 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. 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:

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)

A game that used this implementation of the HangmanLexicon class would quickly

become uninteresting because there are only ten words available. Even so, it will allow

you to develop the rest of the program and then come back and improve this part later.

Part I is a string manipulation problem using the methods developed in Chapter 8. The

sample runs in Figure 1 should be sufficient to illustrate the basic operation of the game,

but the following points may help to clarify a few issues:

  • At the beginning of your run method, you need to create a new HangmanLexicon and

store it in an instance variable. If you extend the program to allow the user to play

multiple games, the creation of the HangmanLexicon should be performed outside the

loop that plays the game repeatedly so that this operation is performed once rather than

for every game.

  • You should accept the user’s guesses in either lower or upper case , even though all

letters in the secret words are written in upper case.

  • If the user guesses something other than a single letter, your program should tell the

user that the guess is illegal and accept a new guess.

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"); } }; }
  • If the user guesses a correct letter more than once, your program should simply do

nothing. Guessing an incorrect letter a second time should be counted as another

wrong guess. (In each case, these interpretations are the easiest way to handle the

situation, and your program will probably do the right thing even if you don’t think

about these cases in detail.)

Remember to finish Part I before moving on to Part II. Part II is arguably more fun, but it

is essential to develop large programs in manageable stages.

Part II—Adding graphics

For Part II, your task is simply to extend the program you have already written so that it

now keeps track of the Hangman graphical display. Although you might want to spice

things up in your extensions, the simple version of the final picture for the unfortunate

user who has run out of guesses looks like this:

The scaffold and the tiny bit of rope above the head are drawn before the game begins,

and then the parts are added in the following order: head, body, left arm, right arm, left

leg, right leg, left foot, right foot. Because this picture is simpler than most of the figures

you have drawn for section problems, the challenge of this part of the assignment does

not lie so much in using the acm.graphics package but rather in implementing the

separation of functions between the class that performs the console-based interaction and

the class that manages the display. That class is called HangmanCanvas and is included in

the starter project in the form of the stub implementation shown in Figure 3 on the next

page.

call those methods from the console-based Hangman class even before you complete their

implementation.

The first thing you should do when you begin Part II is to create a new HangmanCanvas—

in precisely the do-nothing form in which it has been given to you—and install it in the

program window next to the console. The Hangman class itself is an instance of a

ConsoleProgram, which means that the startup code in the ACM libraries has installed

an IOConsole in the window that fills the entire space. Your next task is to add a

HangmanCanvas to the program window as well. You will learn much more about how

to do this kind of operation toward the end of the CS 106A, but the code you need for this

part is extremely simple. First, in the instance variables section of the Hangman program,

you need to declare an instance variable for the canvas by writing

private HangmanCanvas canvas;

and then add the following init method to your program:

public void init() { canvas = new HangmanCanvas(); add(canvas); }

Note that your Hangman program will have both an init and a run method as a result,

and that is perfectly fine. This init method initializes the canvas and adds it to the

window prior to the run method being executed; the run method is where the execution

of your game will start after the window is initialized. By default, the contents of the

program window are given equal amounts of space side by side. Since this is a console

program, the console is already installed and will therefore show up in the left column.

When you add the HangmanCanvas it will occupy the second column, which means that

the console and graphics components of the window will each get half the screen area, as

shown in Figure 4 below. Input and output from the Hangman program will continue to

appear on the console, and any objects you add to the HangmanCanvas stored in the

variable canvas will appear in the area on the right.

Figure 4. Screen shot showing side-by-side console (left) and canvas (right)

You can now go through and add the calls to the methods in HangmanCanvas. Every time

you start a game, for example, you will need to call

canvas.reset();

to delete all the body parts from the canvas and redraw the scaffold. Similarly, you will

have to call displayWord and noteIncorrectGuess at the appropriate points in your

code. As of yet, nothing will actually be displayed on the canvas when you make these

calls, but your program should run just the same as it did before, freeing you to

concentrate on implementing the methods in HangmanCanvas. Note that you should not

add any more public methods to HangmanCanvas (adding private helper methods is

fine though).

The implementation of HangmanCanvas should be reasonably straightforward. Although

the sizes of the scaffold and the various body parts are given to you, their positions are

not specified, so you will have to do some arithmetic to calculate the coordinates. The

center line of the body should be centered horizontally on the screen, and the scaffold

should be displayed a bit higher than the center so that there is room underneath for two

labels: a label in a large font showing the secret word as it currently stands and a label in

a smaller font showing the incorrect guesses. Figure 5 shows how the screen appears at

the end of the tragic session in which the user was unable to guess FUZZY.

Figure 5. The tragic ending of a Hangman game