10.6 Case Study: Card Shuffling and Dealing Simulation

  • Class Card represents a playing card that has a face ('Ace', '2', '3', …, 'Jack', 'Queen', 'King') and a suit ('Hearts', 'Diamonds', 'Clubs', 'Spades')
  • Class DeckOfCards represents a deck of 52 playing cards as a list of Card objects

10.6.1 Test-Driving Classes Card and DeckOfCards

Before we look at classes Card and DeckOfCards, let’s use an IPython session to demonstrate their capabilities.

Creating, Shuffling and Dealing the Cards

In [1]:
from deck import DeckOfCards
In [2]:
deck_of_cards = DeckOfCards()
  • DeckOfCards method __init__ creates the 52 Card objects in order by suit and by face within each suit
  • Printing a deck_of_cards object calls its __str__ method
In [3]:
print(deck_of_cards)
Ace of Hearts      2 of Hearts        3 of Hearts        4 of Hearts        
5 of Hearts        6 of Hearts        7 of Hearts        8 of Hearts        
9 of Hearts        10 of Hearts       Jack of Hearts     Queen of Hearts    
King of Hearts     Ace of Diamonds    2 of Diamonds      3 of Diamonds      
4 of Diamonds      5 of Diamonds      6 of Diamonds      7 of Diamonds      
8 of Diamonds      9 of Diamonds      10 of Diamonds     Jack of Diamonds   
Queen of Diamonds  King of Diamonds   Ace of Clubs       2 of Clubs         
3 of Clubs         4 of Clubs         5 of Clubs         6 of Clubs         
7 of Clubs         8 of Clubs         9 of Clubs         10 of Clubs        
Jack of Clubs      Queen of Clubs     King of Clubs      Ace of Spades      
2 of Spades        3 of Spades        4 of Spades        5 of Spades        
6 of Spades        7 of Spades        8 of Spades        9 of Spades        
10 of Spades       Jack of Spades     Queen of Spades    King of Spades     

  • Shuffle the deck and print the deck_of_cards object again
In [4]:
deck_of_cards.shuffle()
In [5]:
print(deck_of_cards)
8 of Diamonds      8 of Clubs         7 of Clubs         Queen of Diamonds  
10 of Spades       2 of Clubs         10 of Hearts       6 of Diamonds      
5 of Hearts        Queen of Spades    7 of Hearts        5 of Clubs         
9 of Hearts        King of Hearts     9 of Clubs         6 of Hearts        
2 of Spades        10 of Diamonds     6 of Spades        5 of Spades        
10 of Clubs        3 of Spades        Ace of Spades      2 of Diamonds      
Ace of Diamonds    4 of Hearts        3 of Diamonds      8 of Hearts        
Queen of Hearts    2 of Hearts        8 of Spades        Queen of Clubs     
Jack of Hearts     3 of Clubs         Ace of Hearts      9 of Spades        
7 of Diamonds      4 of Clubs         Jack of Spades     5 of Diamonds      
4 of Diamonds      6 of Clubs         Ace of Clubs       King of Diamonds   
3 of Hearts        7 of Spades        Jack of Diamonds   King of Spades     
Jack of Clubs      King of Clubs      9 of Diamonds      4 of Spades        

Dealing Cards

  • Can deal one Card at a time by calling method deal_card
  • IPython calls the returned Card object’s __repr__ method to produce the string output
In [6]:
deck_of_cards.deal_card()
Out[6]:
Card(face='8', suit='Diamonds')

Class Card’s Other Features

  • Deal another card and pass it to the built-in str function
In [7]:
card = deck_of_cards.deal_card()
In [8]:
str(card)
Out[8]:
'8 of Clubs'
  • Each Card has a corresponding image file name, which you can get via the image_name read-only property
    • Used when we display the Cards as images
In [9]:
card.image_name
Out[9]:
'8_of_Clubs.png'

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.