Question: Using the Deck and Hand classes from this chapter, write snippets of code to do each of the following: a) Print out the names of
Using the Deck and Hand classes from this chapter, write snippets of code to do each of the following:
a) Print out the names of all 52 cards.
b) Print out the names of 13 random cards.
c) Choose 13 cards at random from a 52-card deck and show the cards in value order (Bridge hand order).
d) Deal and display four 13-card hands dealt from a shuffled deck.
class Deck(obj ect) :
def __ init __ (self) :
cards = []
for suit in Card . SUITS :
for rank in Card . RANKS :
cards . append(Card(rank , suit ) )
self . cards = cards
def size (self ) : return len(self . cards)
def deal (self ) : return self . cards . pop()
def shuffle (self ):
n = self . size O
cards = self . cards
for i , card in enumerate (cards) :
pos = randrange (i,n)
cards [i] = cards [pos]
cards [pos] = card
class Hand(obj ect ) :
def __ init __ ( self , label=" " ) :
self . label label
self . cards = []
def add(self , card) :
self . cards . append(card)
def dump(self ) :
print self . label + " ' s Cards :"
for c in self . cards :
print " ", c
In python. Will RATE
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
