您在這裡

Exercises

8 九月, 2015 - 10:43

Exercise 18.6. The following are the possible hands in poker, in increasing order of value (and decreasing order of probability):

pair: two cards with the same rank

two pair: two pairs of cards with the same rank

three of a kind: three cards with the same rank

straight: five cards with ranks in sequence (aces can be high or low, so Ace-2-3-4-5is a straight and so is 10-Jack-Queen-King-Ace, but Queen-King-Ace-2-3is not.)

flush: five cards with the same suit

full house: three cards with one rank, two cards with another

four of a kind: four cards with the same rank

straight flush: five cards in sequence (as defined above) and with the same suit

The goal of these exercises is to estimate the probability of drawing these various hands.

1.

Download the following files fromhttp: // thinkpython. com/ code:

Card.py:A complete version of the Card, Deckand Handclasses in this chapter.

PokerHand.py:An incomplete implementation of a class that represents a poker hand, and some code that tests it.

2.

If you runPokerHand.py, it deals seven 7-card poker hands and checks to see if any of them contains a flush. Read this code carefully before you go on.

3.

Add methods to PokerHand.py named has_pair, has_twopair, etc. that return True or False according to whether or not the hand meets the relevant criteria. Your code should work correctly for “hands” that contain any number of cards (although 5 and 7 are the most common sizes).

4.

Write a method named classify that figures out the highest-value classification for a hand and sets the label attribute accordingly. For example, a 7-card hand might contain a flush and a pair; it should be labeled “flush”.

5.

When you are convinced that your classification methods are working, the next step is to estimate the probabilities of the various hands. Write a function in PokerHand.py that shuffles a deck of cards, divides it into hands, classifies the hands, and counts the number of times various classifications appear.

6.

Print a table of the classifications and their probabilities. Run your program with larger and larger numbers of hands until the output values converge to a reasonable degree of accuracy. Compare your results to the values at http: // en. wikipedia. org/ wiki/ Hand_ rankings .

 

Solution:http: // thinkpython. com/ code/ PokerHandSoln. py .

Exercise 18.7. This exercise uses TurtleWorld from Case study: interface design. You will write code that makes Turtles play tag. If you are not familiar with the rules of tag, see http: // en. wikipedia. org/wiki/ Tag_ ( game).

1.

Download http: // thinkpython. com/ code/ Wobbler. pyand run it. You should see a TurtleWorld with three Turtles. If you press the Runbutton, the Turtles wander at random.

2.

Read the code and make sure you understand how it works. The Wobblerclass inherits from Turtle, which means that the Turtlemethods lt, rt, fdand bkwork on Wobblers.

The stepmethod gets invoked by TurtleWorld. It invokes steer, which turns the Turtle in the desired direction, wobble, which makes a random turn in proportion to the Turtle’s clumsiness, and move, which moves forward a few pixels, depending on the Turtle’s speed.

3.

Create a file named Tagger.py. Import everything from Wobbler, then define a class named Tagger that inherits from Wobbler. Call make_worldpassing the Taggerclass object as an argument.

4.

Add a steermethod to Taggerto override the one in Wobbler. As a starting place, write a version that always points the Turtle toward the origin. Hint: use the math function atan2and the Turtle attributes x, yand heading.

5.

Modify steerso that the Turtles stay in bounds. For debugging, you might want to use the Stepbutton, which invokes steponce on each Turtle.

6.

Modify steerso that each Turtle points toward its nearest neighbor. Hint: Turtles have an attribute, world, that is a reference to the TurtleWorld they live in, and the TurtleWorld has an attribute, animals, that is a list of all Turtles in the world.

7.

Modify steerso the Turtles play tag. You can add methods to Taggerand you can override steerand __init__, but you may not modify or override step, wobbleor move. Also, steeris allowed to change the heading of the Turtle but not the position.

Adjust the rules and your steermethod for good quality play; for example, it should be possible for the slow Turtle to tag the faster Turtles eventually.

 

Solution: http: // thinkpython. com/ code/ Tagger. py.