Figure: Three stages of the hangman picture
Hangman is a two-player, word-guessing game. One player thinks of a
three-letter
word and draws the noose of a gallows
(see figure
); 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
): 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.
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
for three snapshots of intermediate
stages.
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 true. Solution
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.
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:
Test the function with the following examples:
(reveal (make-word 't 'e 'a) (make-word '_ 'e '_) 'u) ;; expected value (make-word '_ 'e '_)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.(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)
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
). 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