CS21 HW11: Practice

Due 11:59pm Tuesday, December 11, 2007

This programming assignment is optional. You may use this assignment to replace you lowest homework or quiz score. You should work alone.

You should save your programs for this assignment in cs21/homework/11. Skeleton versions of the programs will appear in this directory when you run update21 in a terminal window. The program handin21 will only submit files in this directory.

Simulating a game of golf

The goal of this problem is to allow you to practice top-down design. Your solution to this problem should be saved in a file called golf.py.

Your task is to write a simulation of the game of golf. To simplify the problem, assume that the game is played in a single dimension so that the only thing that matters is distance to the hole. Your simulation should include the following steps:

Be sure to break the problem up into reasonable functions. Here is a sample run of the program:

Let's play golf!
You're on the tee.
You're 317 yards from the pin.
Which club? 0
Please enter a number between 1 and 9
Which club? 2
Your shot went 238 yards.
You're 79 yards from the pin.
Which club? 9
Your shot went 20 yards.
You're 59 yards from the pin.
Which club? 8
Your shot went 51 yards.
After 3 strokes you're on the green and only 8 yards away!

Creating a Bug class

The goal of this problem is to allow you to practice creating and using classes. Your solution to this problem should be saved in a file called bug.py.

Recall that on homework 4 we created graphical bugs and figured out how to animate and rotate them. At the time we only knew about functions, so we implemented them using functions. Now that we know about object-oriented programming, we will re-implement bugs using a class.

Your Bug class should include the following:

Below is an example of a main program that tests the Bug class. Your main program need not do this, but be sure that it tests all of the functionality available within the Bug class.

def main():
    # create the graphics window
    w = GraphWin("Bugs everywhere!", 500, 500)

    # create two instances of the Bug class with different sizes
    b1 = Bug(Point(50,50), 15, w)
    b2 = Bug(Point(100, 400), 50, w)

    # test rotating
    steps = 50
    for i in range(steps):

    # test animating
    for i in range(60):
        b1.animate(1, 1, 5, 0.0005)
        b2.animate(1, 0, 5, 0.0005)

    # wait for a mouse click to end the program

Once you are satisfied with your programs, hand them in by typing handin21 in a terminal window.