Software Design
Spring 2007

Homework 6

Due: Wednesday 7 March

The reading for this assignment is Chapters 13-14 of How to think....

Turtle Tag

  1. Download from the class web page:

    wget http://wb/sd/code/
    It contains code very similar to from Homework 2. You might find that it makes more sense now :)

    The object of this homework is to make the turtles play tag. When the program starts, it will choose one of the turtles to be `it'. This turtle will chase the others, and when it catches one, it tags the other turtle. The other turtle becomes `it,' and the game proceeds.

    Some additional rules are:

    1. Turtles have to stay in bounds. You can use an algorithm like what you used on Homework 2.

    2. After a turtle is tagged, it has to sulk for a while before it can start chasing the other turtles.

    3. It should be possible, eventually, for the slow turtle to tag the faster turtles.

    When you run you will see that there are buttons in the control panel labelled Run, Stop, Step and Clear. Press Run to start the game and Stop to call a time-out. When you press Step, the World object invokes the run method once on each turtle. This feature might be helpful for debugging.

    Here are some suggestions for methods you might want to have in the Tagger class:

    • Compute the distance to another turtle.

    • Turn toward another turtle. HINT: see the documentation of math.atan2.

    • Find the closest other turtle. Note: the world object has an attribute named animals that is a list of all the Turtles currently in the world.

    • Tag another turtle.

    • Change color (maybe the turtle who is `it' should be red and a sulking turtle should be blue).

    WARNING: Turtles already have an attribute named tag, so you should not create a method named tag.