# 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:

• Pick a random distance for the hole from 250 to 500 yards.
• For each turn, repeat the following steps until the distance to the hole is less than 25 yards.
• Display the distance remaining to the hole.
• Let the user select a club by typing a number between 1 and 9. Continue to request a club until the user enters a valid number.
• Assume that the average distance the ball travels is give by the formula:
avgDist = 300 - (clubNumber * 30)
Once you have computed the average distance, you should add a random integer between -20 and +20 to keep the game interesting.
• Subtract the distance computed in the previous step from the distance remaining to the hole. Note that it is possible to overshoot the hole. You should make sure that your program always uses a positive value for the remaining distance to the hole.
• When the remaining distance to the hole is less than 25 yards, then the user has reached the green, display the number of strokes taken and end the program.

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
You're 79 yards from the pin.
Which club? 9
You're 59 yards from the pin.
Which club? 8
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:

• A constructor that takes the bug's center point and radius, as well as a graphical window. The constructor should build the bug, store all of the bug's parts in an instance variable, and then draw it in the window.
• Methods to animate and rotate the bug.

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)
w.setBackground("black")

# 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):
b1.rotate(135/float(steps))
b2.rotate(-270/float(steps))
sleep(0.05)

# 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
w.getMouse()
w.close()
```
Submit

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