12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

18.11. Exercises 1796. Print a table of the classifications and their probabilities. Run your program with larger andlarger numbers of hands until the output values converge to a reasonable degree of accuracy.Compare your resultstothevalues atwikipedia.org/wiki/Hand_rankings.Exercise 18.6 This exercise uses TurtleWorld from Chapter 4. You will write code that makesTurtles play tag. If you are not familiar with the rules of tag, see wikipedia.org/wiki/Tag_(game).1. Download thinkpython.com/code/Wobbler.py and run it. You should see a TurtleWorldwiththree Turtles. Ifyou press the Run button, theTurtles wander at random.2. Read the code and make sure you understand how it works. The Wobbler class inherits fromTurtle,which means that theTurtlemethodslt,rt,fdandbkwork onWobblers.The step method gets invoked by TurtleWorld. It invokes steer, which turns the Turtlein the desired direction, wobble, which makes a random turn in proportion to the Turtle’sclumsiness, andmove,which moves forwardafew pixels, depending on the Turtle’sspeed.3. Create a file named Tagger.py. Import everything from Wobbler, then define a class namedTagger that inherits from Wobbler. Call make_world passing the Tagger class object as anargument.4. Add a steer method to Tagger to override the one in Wobbler. As a starting place, writea version that always points the Turtle toward the origin. Hint: use the math function atan2and theTurtleattributesx,yandheading.5. Modify steer so that the Turtles stay in bounds. For debugging, you might want to use theStep button, which invokessteponce on each Turtle.6. Modify steer so that each Turtle points toward its nearest neighbor. Hint: Turtles have anattribute,world,thatisareferencetotheTurtleWorldtheylivein,andtheTurtleWorldhasanattribute,animals,that isalistof all Turtles intheworld.7. Modify steer so the Turtles play tag. You can add methods to Tagger and you can overridesteer and __init__, but you may not modify or override step, wobble or move. Also,steerisallowed tochange the heading of theTurtle but not the position.Adjust the rules and your steer method for good quality play; for example, it should bepossible forthe slow Turtletotagthe fasterTurtles eventually.You can get mysolution fromthinkpython.com/code/Tagger.py.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!