from random import shuffle
# define the cards
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
values = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
# define each card
def __repr__(self):
return f"{self.value} of {self.suit}"
class Deck:
# all combinations of cards
def __init__(self):
self.cards = [Card(suit, value) for suit in suits for value in values]
# count number of cards in a deck
def count(self):
return len(self.cards)
# showing the count
def __repr__(self):
return f"Deck of {self.count()} cards"
# the shuffle instance
def shuffle(self):
cards = self.cards
if self.count() < len(self.cards):
raise ValueError("Missing cards! Can't be shuffled")
else:
shuffle(cards)
return cards
# deal x number cards each time
def _deal(self, number):
count = self.count()
if number > count:
raise ValueError("No more cards to deal!")
else:
cards = self.cards[:number]
self.cards = self.cards[number:]
return cards
# deal a single card each time
def deal_card(self):
cards = self._deal(1)
return cards
# deal a list of cards each time
def deal_hand(self, number):
cards = self._deal(number)
return cards
# define each card
card1 = Card("Hearts", "4")
card1
# number of cards in a deck
deck = Deck()
deck
# shuffle the deck
deck.shuffle()
# deal a single card
deck.deal_card()
# deal several cards
deck.deal_hand(3)
# deal more cards
deck._deal(10)
# only 38 cards left
deck