Advertisement
MikeWP

Untitled

Mar 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. from random import shuffle
  2.  
  3. class Card:
  4.    
  5.     def __init__(self, suit, value):
  6.         self.alllowed = ["Hearts", "Diamonds", "Clubs", "Spades"]
  7.         if suit not in self.alllowed:
  8.             raise ValueError("Wrong suit!")
  9.         else:
  10.             self.suit = suit
  11.             self.value = value
  12.  
  13.     def __repr__(self):
  14.         return "{} of {}".format(self.value, self.suit)
  15.  
  16.  
  17. class Deck:
  18.  
  19.     def __init__(self):
  20.         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"]]
  21.  
  22.     def __repr__(self):
  23.         return "Deck of {} cards".format(self.count())
  24.  
  25.     def count(self):
  26.         return len(self.cards)
  27.  
  28.     def _deal(self, num):
  29.         d_lst = []
  30.         if not self.cards:
  31.             raise ValueError("All cards have been dealt")
  32.         elif num == 1:
  33.             return self.cards.pop()
  34.         else:
  35.             if num > self.count():
  36.                 num -= num - self.count()
  37.                 for i in range(0, num):
  38.                     d_lst.append(self.cards.pop())
  39.             else:
  40.                 for i in range(0, num):
  41.                     d_lst.append(self.cards.pop())
  42.             return d_lst
  43.  
  44.     def shuffle(self):
  45.         if self.count() == 52:
  46.             shuffle(self.cards)
  47.         else:
  48.             raise ValueError("Only full decks can be shuffled")
  49.  
  50.     def deal_card(self):
  51.         return self._deal(1)
  52.  
  53.     def deal_hand(self, num_of_c):
  54.         return self._deal(num_of_c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement