CS 2500 - Lab 3

Pair programming

The lab assignment is to be completed in pairs. Each pair works at one computer. The pilot types and the co-pilot watches and helps. Remember to trade roles!

Don't delete your work

In previous labs some students deleted their solutions for exercises after completing them. Don't do this! It is common for exercises to make use of functions or templates defined in earlier exercises.

Follow the Design Recipe

As always, use the design recipe when designing programs!

Part I: Structure Definitions

For each of the following word problems, extract structure definitions for the data involved. You don't need to write tests for them, but be sure to test them informally by trying them out.

Remember structure definitions from class?
(define-struct name (field ...))

  1. The Boston Zoo keeps track of information for every animal that is kept there. For each animal, they store its name, species, age, breakfast hour, and dinner hour (if they don't get fed twice a day, they try to eat the visitors...).
  2. Each zoo attendant has a name, and is assigned to watch over exactly three different animals.
  3. Switch Roles
  4. In addition to watching over animals, the attendants must clean a zone of the zoo. The zoo is divided into three zones: Birds, Reptiles, and Mammals.

    For each Zone, they track the cleaner's name, and the number of animals kept there.

    Hint: You probably need more than one structure here.

Part II: Data Definitions

Take your first two structure definitions from above and turn them into data definitions. Recall that a data definition for structured data specifies what kind of data each structure field contains. Refer to Section 6.4 of HtDP if you need to review!

Part III: Templates

Switch Roles
Construct a template for each of the data definitions from Part II. Recall that the template describes what we know about the input to a function (what accessors we can use on them). For each expression in your template, record the kind of data to which it evaluates. If you need to review templates for compound data, look in Section 6.5 of HtDP.

Part IV: From Templates to Functions

Consider the templates you built from the data definitions and design the following functions based on them. You must formulate examples and tests!
  1. Using your solution to Exercise 1 above (data definition, structure definition and template), develop a function that updates an animal's age by adding 1 to it (presumably the function will be used on its birthday).
  2. Using your solution to Exercise 1 above (data definition, structure definition and template), develop a program that takes an animal and the current hour, and returns whether it's mealtime.

  3. Switch Roles
  4. In preperation for next April Fool's Day, the system manager of the zoo wants you to design a function that takes an animal description, and returns the same structure with years converted to dog years.

    Note there are 7 dog years in 1 human year.
  5. Using your data definition, structure definition and template from above, develop a program that determines the total age of the animals a given attendant has been assigned to.

Part V: Challenge Problem

Some of the TAs (oddly, they've asked to remain anonymous) are working on a game that they call Chip, the Cheap Sheep. So far, they've put together a few frames of animation for it:

0 1 2 3
Frame 0 Frame 1 Frame 2 Frame 3

Your goal is to create a simple proof-of-concept game engine: Chip will run from offscreen to the point the user clicks on.

  1. Create a function named which-chip that takes a number, and returns the corresponding image from the sequence above.

    This can be done several ways... which one is best?

    You should be able to drag the images from the webpage into DrScheme, once they are there you should know what to do. If not save them to the desktop, and use "Insert">"Insert Image..." from the DrScheme menu bar.

  2. Write a data and structure definition for your world. You'll want to be able to know Chip's coordinates, the coordinates he is running to, and which frame of the animation he is currently on.

    While you're at it, write a template for functions that take a world, and define a variable named world0 that is your initial world (start by assuming the user clicked in the center, and Chip is just offscreen).
  3. Write a function named move-chip that takes a world and returns a world; moving Chip to the left by some amount. (It looks like he's going pretty fast!)

    This assumes he starts at the y-coordinate of his destination, and only has to go left.

    If gets to his destination, have him stop moving.

  4. Now, make Chip respond to mouse clicks. Write a clack function that, in response to a mouse event, creates a new world, with the mouse coordinates as Chip's new destination, and with Chip teleported offscreen (presumably at the same y-coordinate as his destination), so that he can run to the point the user clicked on. Be sure to ignore all mouse events except "button-down".
  5. Write a function named draw-chip that places the correct image (frame) of Chip into an empty-scene of size 400x400 (remember the function you made before?)
  6. Write a function named next-chip that increments the frame field of your world, and wraps the number so it cannot be greater than 3.

    The remainder function can be used for this:
       (remainder 1 4) ;; ==>  1
       (remainder 2 4) ;; ==>  2
       (remainder 5 4) ;; ==>  1
       (remainder 6 4) ;; ==>  2
  7. Write a function named tock that takes your world and calls move-chip and next-chip to return a completely updated world.

    Hint: You want to nest the calls... how should it be done?
  8. Put all this together with a big-bang and see Chip the Cheap sheep run!

       (big-bang world0
                 (on-draw draw-chip)
                 (on-tick tock)
                 (on-mouse clack))
  9. If you still have extra time, try variations: maybe make a bouncing ball for Chip to chase.

That's All Folks

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you're struggling with any of the material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.