[previous] [up] [next]     [index]
Next: The Varieties of Data Up: Compound DataPart 1: Previous: Extended Exercise: Moving Circles

Extended Exercise: Hangman

external


Figure: Three stages of the hangman picture

Hangman is a two-player, word-guessing game. One player thinks of a three-letter[footnote] word and draws the noose of a gallows (see figure [cross-reference]); the other player tries to guess the word, one letter at a time. For every wrong guess, the first player adds another part to the drawing (see figure [cross-reference]): first the head, then the body, the arms, and the legs. If, however, a guess agrees with one or two letters in the chosen word, the first player reveals the position(s) where this letter occurs. The game is over when the second player guesses the complete word or when the first player has completed the stick figure.

Let's design a program that plays the role of the first player. The program consists of two parts: one for drawing the figure, and another for determining whether a guess occurs in the chosen word and where.


Exercises

Exercise 6.7.1

Develop the function draw-next-part, which draws the pieces of a hangman figure. The function consumes one of the seven symbols: 'right-leg 'left-leg 'left-arm 'right-arm 'body 'head 'noose

It always returns true and draws the matching part of the figure. See figure [cross-reference] for three snapshots of intermediate stages.[footnote]

Hints: Add (start 200 200) to the top of the definition window. Start with the noose and develop one component at a time. If a component of the stick figure requires more than one drawing operation, combine the operations using an and-expression, which evaluates the two expressions and ensure that both results are trueSolution


The second task of the first player is to determine whether a guess is among the letters of the chosen word and, if so, where it occurs. Our recipe requires that, before we design a function for this task, we need to analyze our data and provide data definitions. The key objects of the game are words and letters. A word consists of three letters. A letter is represented with the symbols 'a through 'z. Using just those letters, however, is not enough because the program also needs to maintain a word that records how much the second player has guessed. The solution is to add one extra ``letter'' to our alphabet that is distinct from the others; the hangman teachpack uses '_ for this purpose.


Exercises

Exercise 6.7.2

Provide a structure definition and a data definition for representing three-letter words.  Solution

Exercise 6.7.3

Develop the function reveal. It consumes three arguments:

  1. the chosen word, which is the word that we have to guess;
  2. the status word, which shows which portion of the word has been revealed so far; and
  3. a letter, which is our current guess.
The function produces a new status word, that is, a word that contains ordinary letters and '_. The fields in the new status word are determined by comparing the guess with each pair of letters from the status word and the chosen word:
  1. If the guess is equal to the letter in the chosen word, the guess is the corresponding letter in the new status word.
  2. Otherwise, the new letter is the corresponding letter from the status word.

Test the function with the following examples:

(reveal (make-word 't 'e 'a) (make-word '_ 'e '_) 'u)
;; expected value 
(make-word '_ 'e '_)

(reveal (make-word 'a 'l 'e) (make-word 'a '_ '_) 'e) ;; expected value: (make-word 'a '_ 'e)

(reveal (make-word 'a 'l 'l) (make-word '_ '_ '_) 'l) ;; expected value (make-word '_ 'l 'l)

The first one shows what happens when the guess does not occur in the word; the second one shows what happens when it does occur; and the last one shows what happens when it occurs twice.

Hints: (1) Remember to develop auxiliary functions when a definition becomes too large or too complex to manage.

(2) The function reveal consumes two structures and one atomic value (a letter). This suggests that we use the design recipe for compound data (figure [cross-reference]). For the template, it is best to write down the selector expressions in a two-column format, one column per word. Solution


When the functions draw-next-part and reveal are properly tested, set teachpack to hangman.ss and play the game by evaluating

(hangman make-word reveal draw-next-part)
The hangman function chooses a three-letter word randomly and displays a window with a pop-up menu for letters. Choose letters and, when ready, click the Check button to see whether the guess is correct. Comment out the test cases for exercise [cross-reference] so that their drawing effects don't interfere with those of hangman.


[previous] [up] [next]     [index]
Next: The Varieties of Data Up: Compound DataPart 1: Previous: Extended Exercise: Moving Circles

PLT