Advertisement
Guest User

Cards and Hand.py

a guest
Nov 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. #Define Card by storing Number (Ace = 1, Jack = 11, Queen = 12, King = 13) value
  2. # and Suit value (0=Hearts, 1=Clubs, 2=Spades, 3=Diamonds)
  3. class Card:
  4.     SUIT = ["Hearts", "Clubs", "Spades", "Diamonds"]
  5.     VALUES = ["ACE", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "JACK", "QUEEN", "KING"]
  6.     CARD_SUIT=0  
  7.     CARD_VALUE=0
  8.     def __init__(self, number, suit):
  9.         self.CARD_SUIT = suit
  10.         self.CARD_VALUE= number
  11.    
  12.     def print_card(my_card):
  13.         print(str(Card.VALUES[my_card.CARD_VALUE]) + " of " + Card.SUIT[my_card.CARD_SUIT])
  14.  
  15. class Hand:
  16. #Track each players 5 card hand
  17. #Detect strength of hand (Pair, 2 pair, 3 of a kind etc)
  18. #Compare 2 hands to see which one is stronger
  19. #Sort method to assist hand detection
  20.  
  21.     cards_in_hand = []
  22.     def __init__(self, cards):
  23.        
  24.         for c in cards:
  25.             #adding each card to the cards in hand
  26.             self.cards_in_hand.append(c)
  27.           #  self.cards_in_hand.append(Card(c[0], c[1]))
  28. #self.cards_in_hand = sort_hard(cards_in_hand)
  29.  
  30.  
  31.     def print_hand(my_hand):
  32.         print("It should be entering this function but it isnt")
  33.         for c in my_hand.cards_in_hand:
  34.             Card.print_card(c)
  35.    
  36.     def sort_hard(hand_1):
  37.         #Returns a sorted hand based upon values of cards in hand
  38.         return hand_1;
  39.    
  40.  
  41.     def hand_strength(hand_1):
  42.         #Detect what the hand is
  43.         # 0 = High Card, 1 = Pair, 2 = Two Pair, 3 = Three of a Kind*
  44.         # 4 = Straight, 5 = Flush, 6 = Full House, 7 = Four of a Kind
  45.         # 8 = Straight Flush 9 = Royal Flush
  46.         return 1;
  47.  
  48.     def stronger_hand(hand_1, hand_2):
  49.         strength_1 = hand_strength(hand_1)
  50.         strength_2 = hand_strength(hand_2)
  51.  
  52.         if strength_1 > strength_2:  #returns -1 if hand_1 is stronger
  53.             return -1
  54.         elif strength_2 > strength_1:
  55.             return 1 #returns 1 if hand_2 is stronger
  56.         else:
  57.             return 0 #returns 0 if hand_1 and hand_2 are of equal strength
  58.        
  59.     #below are methods for detecting each type of hand
  60.     def pair(my_hand):
  61.         return False
  62.     def two_pair(my_hand):
  63.         return False
  64.     def three_of_a_kind(my_hand):
  65.         return False
  66.     def straight(my_hand):
  67.         return False
  68.     def flush(my_hand):
  69.         suit = my_hand.cards_in_hand[0].CARD_SUIT
  70.         for card in my_hand.cards_in_hand:
  71.             if card.CARD_SUIT != suit:
  72.                 return False
  73.         return True
  74.     def full_house(my_hand):
  75.         return False
  76.     def four_of_a_kind(my_hand):
  77.         return False
  78.     def straight_flush(my_hand):
  79.         if straight(my_hand) & flush(my_hand):
  80.             return True
  81.         return False
  82.     def royal_flush(my_hand):
  83.         return False
  84.  
  85.  
  86. handy = Hand([(1, 2), (1, 3), (2, 3), (1, 1), (3, 8)])
  87. Hand.print_hand(handy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement