INTRODUCTION TO PROGRAMMING IN C++
Kalamazoo College

MINI-LAB: STAYING IN BOUNDS AND ADDING COLOR

Using Conditional Statements


This set of Mini-Lab Exercises includes the following:



Watch Out!

In our current program, there is nothing to keep a fish from moving forward right out of the aquarium. This does not make sense. We can use the Whether or Not selection pattern to change direction when a fish gets to the wall.

Exercise

Research the AquaFish partial interface to discover how to know whether a fish is up against a wall and how to make a fish change direction. Modify your main function to have each fish change direction before moving forward if it is about to hit a wall.



One Fish, Two Fish, Red Fish, Blue Fish

The fish in our aquarium are somewhat homogenous, differing only in their location and direction. If we construct fish in two different colors, we should also modify our fish display function (the Display Show function) to display fish in those colors. We can use the Alternative Action selection pattern to do this.

Exercise

Research the AquaFish partial interface to discover how to specify the color of a fish as you create it. Also look to see how you would find out a fish's color. Edit your main function to make each of your fish either red or blue as you construct them.

Then edit the Display Show function that displays a single fish. Set the color of the pen to RED or BLUE depending on the color of the fish. Note that you can't just pass the fish's color to the SetBrush function, because fish colors are stored as strings, whereas the argument to the SetBrush function is an integer constant. Instead, you should use the Alternative Action Selection pattern to set the pen color depending on the color of the fish.



Rainbow Fish

Why should there only be two colors of fish in our aquarium? We can use the Sequential Choice selection pattern to add diversity of color when constructing and displaying fish.

Exercise

Choose several colors (more than two) to use for fish in the aquarium. You may wish to look at the colors.h file in the CMU Graphics Library to see which colors you can display. Edit your main function to make each of your fish a different color.

Now edit the Display Show function to set the pen color to the color of the fish, using the Sequential Choice Selection pattern. Remember that fish colors are stored as strings, whereas the argument to the SetBrush function is an integer constant.



Copyright Alyce Faulstich Brady, 1999.
The Selection Pattern was written by Joe Bergin, of Pace University.