Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import shuffle
- class Card:
- def __init__(self, suit, value):
- self.alllowed = ["Hearts", "Diamonds", "Clubs", "Spades"]
- if suit not in self.alllowed:
- raise ValueError("Wrong suit!")
- else:
- self.suit = suit
- self.value = value
- def __repr__(self):
- return "{} of {}".format(self.value, self.suit)
- class Deck:
- def __init__(self):
- self.cards = ["{} of {}".format(x, y) for x in ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] for y in ["Hearts", "Diamonds", "Clubs", "Spades"]]
- def __repr__(self):
- return "Deck of {} cards".format(self.count())
- def count(self):
- return len(self.cards)
- def _deal(self, num):
- d_lst = []
- if not self.cards:
- raise ValueError("All cards have been dealt")
- elif num == 1:
- return self.cards.pop()
- else:
- if num > self.count():
- num -= num - self.count()
- for i in range(0, num):
- d_lst.append(self.cards.pop())
- else:
- for i in range(0, num):
- d_lst.append(self.cards.pop())
- return d_lst
- def shuffle(self):
- if self.count() == 52:
- shuffle(self.cards)
- else:
- raise ValueError("Only full decks can be shuffled")
- def deal_card(self):
- return self._deal(1)
- def deal_hand(self, num_of_c):
- return self._deal(num_of_c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement