@@ -125,6 +125,21 @@ def shuffle(self):
125125 rng = random .Random ()
126126 rng .shuffle (self .cards )
127127
128+ def deal (self , hands , num_cards = 999 ):
129+ """
130+ The two parameters are a list (or tuple) of hands and the total number
131+ of cards to deal.
132+ The second parameter is optional.
133+ If there aren't enogh cards, the method stops when all are dealt.
134+ """
135+ num_hands = len (hands )
136+ for i in range (num_cards ):
137+ if self .is_empty ():
138+ break # if there are no more cards
139+ card = self .pop () # take the next card
140+ hand = hands [i % num_hands ] # decide whose turn it is
141+ hand .add (card ) # add a card to that hand
142+ 128143 def remove (self , card ):
129144 """
130145 Takes a card as a parameter and removes it.
@@ -150,3 +165,42 @@ def is_empty(self):
150165 Returns True if the deck contains no cards.
151166 """
152167 return self .cards == []
168+ 169+ 170+ class Hand (Deck ):
171+ """
172+ Hand is a subclass of Deck, so it will inherit those methods as well.
173+ -->
174+ >> from cards import Card, Deck, Hand
175+ >> deck = Deck()
176+ >> deck.shuffle()
177+ >> hand = Hand("ben")
178+ >> deck.deal([hand], 5)
179+ >> print(hand)
180+ 4 of Diamonds
181+ 6 of Diamonds
182+ 4 of Spades
183+ 9 of Clubs
184+ 7 of Clubs
185+ -->
186+ """
187+ def __init__ (self , name = "" ):
188+ self .cards = []
189+ self .name = name
190+ 191+ def add (self , card ):
192+ """
193+ The remove() method is inherited from Deck.
194+ """
195+ self .cards .append (card )
196+ 197+ def __str__ (self ):
198+ """
199+ Overrides the one in the Deck class so we can include more information.
200+ """
201+ s = "Hand " + self .name
202+ if self .is_empty ():
203+ s += "is empty\n "
204+ else :
205+ s += " contains\n "
206+ return s + Deck .__str__ (self )
0 commit comments