Project: The virtual pet

As a somewhat larger example of object-oriented programming, we consider the development of the controlling software for a virtual pet, a small appliance that has recently become popular with eleven-year-olds, at least in my house. My daughter's virtual pet is an egg-shaped device about five centimeters high, three wide, and one thick, with a black-and-white liquid-crystal display showing the two-dimensional world in which the pet lives. There are buttons, or combinations of buttons, that one can push in order to feed the pet, play games with it, clean up its disgusting messes, tell it to go to sleep, and give it medications when it is ailing.

A software interface to a simpler but somewhat similar virtual pet can be found at /u2/stone/courses/scheme/html/virtual-pet-project.ss. This file contains the definition for a virtual-pet constructor procedure as well as the definition of an additional object with which the virtual-pet interacts -- a clock that ticks off the cycles of the pet's virtual lifetime.

  1. Copy this file into your account, load it into XEmacs, and read through it to find out what the internal fields of the virtual-pet object are and what methods it can respond to.

  2. Load the program into Chez Scheme. Invoke the create-registered-virtual-pet procedure like this to obtain and name a virtual-pet object:

    (define Fido (create-registered-virtual-pet))
    
  3. Send your new pet some messages. I recommend starting with status, let-sleep, and feed, in that order.

  4. The definition of the water method (which gives the virtual pet a drink of water when it is thirsty) is missing. Fill it in, using the feed method as an example. Virtual pets created before this change has been completed will not exhibit any change in behavior even after you save the revised definition and give it to Scheme, so to test your code you'll need to define a new virtual pet.

  5. Add a bath method to the virtual-pet object.

  6. Define a separate water-dish object that has one field, an indication of the number of cubic centimeters of water are currently in the disk, and can handle three messages: fill! (which puts the maximum amount of water in the dish), empty! (which changes the amount of water in the dish to zero), and grow-older! (which reduces the amount of water in the dish by one percent, through evaporation, unless it is empty). Then remove the water method from the virtual-pet object and arrange instead for the pet to visit the water dish whenever it gets thirsty, emptying it if there is water in it and howling otherwise.

  7. Add fields for height and weight to the definition of a virtual pet and revise the various methods to adjust the values of those fields in a plausible way.


This document is available on the World Wide Web as

http://www.math.grin.edu/courses/Scheme/fall-1997/virtual-pet-project.html

created December 9, 1997
last revised December 10, 1997

John David Stone (stone@math.grin.edu)