daily pastebin goal
60%
SHARE
TWEET

Untitled

a guest Aug 15th, 2018 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. # constants for card values ACE, KING, etc.
  4.  
  5. valueSet =  set(['2','3','4','5','6','7','8','9','0','J','Q','K','A','?'])
  6. suitSet = set(['s','h','d','c','?'])
  7.  
  8.  
  9. def value_from_letter(let):
  10.     if let in valueSet:
  11.         if let == 'A':
  12.             return 14
  13.         elif let == 'K':
  14.             return 13
  15.         elif let == 'Q':
  16.             return 12
  17.         elif let == 'J':
  18.             return 11
  19.         elif let == '0':
  20.             return 10
  21.         elif let == '?':
  22.             return 0
  23.         else:
  24.             return int(let)
  25.     else:
  26.         raise ValueError('invalid Card value: {}'.format(let))
  27.  
  28. def check_suit(let):
  29.     if let in suitSet:
  30.         return let
  31.     else:
  32.         raise ValueError('invalid Card suit: {}'.format(let))
  33.  
  34. def num_to_suit(num):
  35.     numDict = {0:'c', 1:'d', 2:'h', 3:'s'}
  36.     try:
  37.         return numDict[num]
  38.     except:
  39.         return '?'
  40.  
  41. def suit_to_num(suit):
  42.     suitDict = {'c':0, 'd':1, 'h':2, 's':3}
  43.     return suitDict[suit]
  44.  
  45. def letter_from_value(val):
  46.     if val == 14:
  47.         return 'A'
  48.     elif val == 13:
  49.         return 'K'
  50.     elif val == 12:
  51.         return 'Q'
  52.     elif val == 11:
  53.         return 'J'
  54.     elif val == 10:
  55.         return '0'
  56.     elif val == 0:
  57.         return '?'
  58.     else:
  59.         return str(val)
  60.  
  61. def is_valid_value(value):
  62.     # convert num to letter before judging
  63.     if letter_from_value(value) not in valueSet:
  64.         return False
  65.     else:
  66.         return True
  67.  
  68. def is_valid_suit(suit):
  69.     if suit not in suitSet:
  70.         return False
  71.     else:
  72.         return True
  73.  
  74. class Card:
  75.     """A class to represent a card with a value and suit"""
  76.     def __init__(self, value_let = '?', suit_let = '?'):
  77.         self.value = value_from_letter(value_let)
  78.  
  79.         self.suit = check_suit(suit_let)
  80.  
  81.     def __str__(self):
  82.         if is_valid_value(self.value) == True and is_valid_suit(self.suit) == True:
  83.             return '{}{}'.format(letter_from_value(self.value), self.suit)
  84.         else:
  85.             raise ValueError('invalid Card')
  86.  
  87.     def __repr__(self):
  88.         if is_valid_value(self.value) == True and is_valid_suit(self.suit) == True:
  89.             return 'Card({}{})'.format(letter_from_value(self.value), self.suit)
  90.         else:
  91.             raise ValueError('invalid Card')
  92.  
  93.     def __eq__(self, other):
  94.         if self.value == other.value and self.suit == other.suit:
  95.             return True
  96.         else:
  97.             return False
  98.  
  99.     def __lt__(self, other):
  100.         if self.value != other.value:
  101.             return self.value < other.value
  102.         else:
  103.             return suit_to_num(self.suit) < suit_to_num(other.suit)
  104.  
  105.     def is_valid(self):
  106.         # valid if only in valueset AND  known
  107.         if is_valid_value(self.value) and is_valid_suit(self.suit) and self.value != 0 and self.suit != '?':
  108.             return True
  109.         else:
  110.             return False
  111.  
  112. def card_from_num(num):
  113.     pick_value = num % 13 + 2
  114.     pick_suit = num_to_suit(num // 13)
  115.     c = Card()
  116.     c.value = pick_value
  117.     c.suit = pick_suit
  118.     # how to use is_valid()?
  119.     if c.is_valid():
  120.         return c
  121.     else:
  122.         raise ValueError('invalid input number')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top