Hand Class methods override the methods from the Deck class that have the same

# Hand class methods override the methods from the deck

This preview shows page 47 - 56 out of 69 pages.

HandClass methods override the methods from the Deckclass that have the same name. The Deckclass also has methods called __str__ and sortWhat does “overriding” mean? Constructor For sorting the cards in a hand . For pretty printing
Create a Deck. Shuffle It. Extract 10 Cards. Make a Hand. Print it. D = Deck() D.shuffle() H = Hand(‘CVL’) for k in range(10): c = D.pop_card() H.add_card(c) print H CVL: Ace of Hearts Three of Clubs Four of Spades Four of Diamonds Five of Hearts Six of Hearts Seven of Spades Eight of Spades Queen of Clubs Queen of Spades
Create a Deck. Shuffle It. Extract 10 Cards. Make a Hand. Print it. D = Deck() D.shuffle() H = Hand(‘CVL’) for k in range(10): c = D.pop_card() H. add_card (c) print H CVL: Queen of Clubs Three of Clubs Eight of Spades Six of Hearts Queen of Spades Ace of Hearts Five of Hearts Four of Spades Seven of Spades Four of Diamonds The add_card method is inherited from the Deck class
Create a Deck. Shuffle It. Extract 10 Cards. Make a Hand. Print it. D = Deck() D.shuffle() H = Hand(‘CVL’) for k in range(10): C = D.pop_card() H.add_card(C) print H CVL: Queen of Clubs Three of Clubs Eight of Spades Six of Hearts Queen of Spades Ace of Hearts Five of Hearts Four of Spades Seven of Spades Four of Diamonds The print function from the Hand class overrides the print function from the Deck Class
Inheritance Chit Chat The existing class Deck is the parent The new class Hand is the child Hand is a subclass of Deck Inheritance is a very important mechanism when it comes to maintaining and updating software.
Decks and Hands Things to do with a deck of cards: 1. Shuffle 2. Sort* 3. Add a card 4. Remove a card Things to do with a hand of cards: 1. Compare 2. Sort* 3. Add a card 4. Remove a card *Maybe sort in different ways
Another Example of Overriding As written, when a Deck is sorted, it is sorted by suit first and then by rank. To be different, when a Hand is sorted, let’s sort by rank first and then by suit. Seven of Clubs Ten of Diamonds Six of Hearts Eight of Hearts Ace of Spades Ace of Spades Six of Hearts Seven of Clubs Eight of Hearts Ten of Diamonds vs
The sort Method in the Hand Class def sort(self): self.cards.sort(MyCompare) def MyCompare(H1,H2): if H1.rank > H2.rank: return 1 if H1.rank < H2.rank: return -1 if H1.suit > H2.suit: return 1 if H1.suit < H2.suit: return -1 return 0 Sorts by rank first, then suit. This sort Method overrides the sort method in Deck, which sorts by suit first, then rank.
Three of Hearts Four of Spades Seven of Diamonds Five of Spades Queen of Diamonds Four of Hearts Ten of Diamonds Queen of Hearts Two of Spades Ace of Clubs Ace of Clubs Seven of Diamonds Ten of Diamonds Queen of Diamonds Three of Hearts Four of Hearts Queen of Hearts Two of Spades Four of Spades Five of Spades A random 10-card deck D D.sort() Sorts by suit first, then rank .

#### You've reached the end of your free preview.

Want to read all 69 pages?

• Fall '19
• Playing card, Hand class, Deck class