We will begin by defining the classes which will be used in order to separate out different aspects of the game of blackjack. We will model three of the components of the game:
Card
: A basic playing card. The card belongs to a suit and is worth a certain value.Deck
: A collection of cards. The deck shrinks as cards are drawn and contains 52 unique cards.Hand
: Each player's assigned cards. A hand is what defines each player's score and thus who wins.
Let's begin with the simplest concept—the Card
.
The Card
class will be the first class we define, as both of our other classes will need to use it. Open up a new file and type the following code:
import random class Card: def __init__(self, suit, value): self.suit = suit self.value = value def __repr__(self): return " of ".join((self.value, self.suit))
The only import we will need for our game is the random
module. This will allow us to shuffle our virtual deck of cards at the beginning...