06.09.2021 Views

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

16.7 OldMaidGame class 175<br />

Hand frank: 10 of Diamonds matches 10 of Hearts<br />

>>> print hand<br />

Hand frank contains<br />

Ace of Spades<br />

2 of Diamonds<br />

6 of Hearts<br />

Queen of Clubs<br />

7 of Diamonds<br />

5 of Clubs<br />

Jack of Diamonds<br />

Notice that there is no init method for the OldMaidHand class. We inherit it<br />

from Hand.<br />

16.7 OldMaidGame class<br />

Now we can turn our attention <strong>to</strong> the game itself. OldMaidGame is a subclass<br />

of CardGame <strong>with</strong> a new method called play that takes a list of players as an<br />

argument.<br />

Since init is inherited from CardGame, anewOldMaidGame object contains a<br />

new shuffled deck:<br />

class OldMaidGame(CardGame):<br />

def play(self, names):<br />

# remove Queen of Clubs<br />

self.deck.removeCard(Card(0,12))<br />

# make a hand for each player<br />

self.hands = []<br />

for name in names :<br />

self.hands.append(OldMaidHand(name))<br />

# deal the cards<br />

self.deck.deal(self.hands)<br />

print "---------- Cards have been dealt"<br />

self.printHands()<br />

# remove initial matches<br />

matches = self.removeAllMatches()<br />

print "---------- Matches discarded, play begins"<br />

self.printHands()

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

Saved successfully!

Ooh no, something went wrong!