[previous] [up] [next]     [index]
Next: Mutable Structures Up: Encapsulation Previous: Abstracting with State Variables

Practice with Encapsulation

Exercise 39.2.1

Develop the program make-city. It manages a collection of traffic lights. The program should provide four services:

  1. adding a traffic light with a label (string);
  2. removing a traffic light by label;
  3. switching the state of a traffic light with some given label; and
  4. resetting a traffic light to red with some given label.
Hint: The first two services are provided directly; the last two are implemented by the simulated traffic lights.

After the development of the program is completed, develop a graphical user interface. Solution

Exercise 39.2.2

Develop make-master. The program consumes nothing, creates an instance of the color-guessing game of section [cross-reference], and produces the master-check function as the only result. After the player has guessed the answer, the function should simply respond with ``game over.'' A typical dialog would proceed as follows:

> (define master1 (make-master))
> (master-check 'red 'red) 
'NothingCorrect 
> (master-check 'black 'pink) 
'OneColorOccurs 
... 
Compare this with the first dialogue in section [cross-reference].

Add a service to make-master that reveals the hidden colors. That way a player who is tired of playing the game can find out the answer. Solution

Exercise 39.2.3

Develop make-hangman. The program consumes a list of words, creates a hangman game using the list, and produces the hangman-guess function as a result. A player would use the dialogue as follows:

> (define hangman-easy (make-hangman (list 'a 'an 'and 'able 'adler)))
> (define hangman-difficult (make-hangman (list 'ardvark ...))) 
> (hangman-easy 'a) 
``You won'' 
> (hangman-difficult 'a) 
(list 'head (list '_ '_ '_ '_ '_ '_)) 
> ... 
Compare this with the first dialogue in section [cross-reference].

Add a service to make-master that reveals the chosen word.

An optional extension is to equip the program with a graphical user interface and a graphical view of the stick figure. Reuse existing solutions as much as possible. Solution

Exercise 39.2.4

Develop make-player. The program abstracts over the functions of section [cross-reference]. Using the program, we can create several players that wander through the campus:

(define player1 (make-player 'BioEngineering))
(define player2 (make-player 'MuddBuilding)) 
... 
The argument to make-player specifies the initial position of the player.

Each instance should be able to produce

  1. a picture of the current surroundings;
  2. a list of the available building connections; and
  3. a move from one place to another through an available connection.

Extension: Two players may be in the same building at the same time, but they cannot interact. Extend the game so that two players in the same building can interact in some form. Solution

Exercise 39.2.5

Develop the program moving-pictures. It consumes a position and a picture, that is, a list of shapes as defined in sections [cross-reference], and [cross-reference], and [cross-reference]. (Also see [cross-reference] for functions on moving pictures.) It supports two services. First, it can place the shape at a specific position. Second, it can reset the picture to the initially given position. Solution



[previous] [up] [next]     [index]
Next: Mutable Structures Up: Encapsulation Previous: Abstracting with State Variables

PLT