10.6 Case Study: Card Shuffling and Dealing Simulation
Our next example presents two custom classes that you can use to shuffle and deal a deck of cards. 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. First, we’ll test-drive these classes in an IPython session to demonstrate card shuffling and dealing capabilities and displaying the cards as text. Then we’ll look at the class definitions. Finally, we’ll use another IPython session to display the 52 cards as images using Matplotlib. We’ll show you where to get nice-looking public-domain card images.
10.6.1 ...
Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.