Guest User

Untitled

a guest
Mar 25th, 2010
3,822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 KB | None | 0 0
  1. import itertools
  2. import random
  3. import pickle
  4. import time
  5.  
  6. # data
  7. SUITS = ('c', 'd', 'h', 's')
  8. RANKS = ('2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A')
  9.  
  10. DECK = tuple(''.join(card) for card in itertools.product(RANKS, SUITS))
  11.  
  12. ORDER_LOOKUP = dict(zip(DECK, range(52)))
  13. RANK_LOOKUP = dict(zip(RANKS, range(13)))
  14. SUIT_LOOKUP = dict(zip(SUITS, range(4)))
  15.  
  16. # utility functions
  17. def cmp_cards(a, b):
  18.     return cmp(ORDER_LOOKUP[a], ORDER_LOOKUP[b])
  19.    
  20. def cmp_tuples(a, b):
  21.     n1 = len(a)
  22.     n2 = len(b)
  23.     if n1 != n2:
  24.         return cmp(n1, n2)
  25.     return cmp(a, b)
  26.    
  27. def suit(card):
  28.     return card[1]
  29.    
  30. def suit_int(card):
  31.     return SUIT_LOOKUP[card[1]]
  32.    
  33. def rank(card):
  34.     return card[0]
  35.    
  36. def rank_int(card):
  37.     return RANK_LOOKUP[card[0]]
  38.    
  39. def card_int(card):
  40.     s = 1 << suit_int(card)
  41.     r = rank_int(card)
  42.     c = (s << 4) | r
  43.     return c
  44.    
  45. # test functions
  46. def is_straight(cards):
  47.     previous = rank_int(cards[0]) - 1
  48.     for card in cards:
  49.         r = rank_int(card)
  50.         if r != previous + 1:
  51.             if not (r == 12 and previous == 3):
  52.                 return False
  53.         previous = r
  54.     return True
  55.    
  56. def is_flush(cards):
  57.     s = suit(cards[0])
  58.     return all(suit(card) == s for card in cards)
  59.    
  60. def same_rank(cards):
  61.     r = rank(cards[0])
  62.     return all(rank(card) == r for card in cards)
  63.    
  64. def split_ranks(cards, indexes):
  65.     for index in indexes:
  66.         a, b = cards[:index], cards[index:]
  67.         if same_rank(a) and same_rank(b):
  68.             return True
  69.     return False
  70.    
  71. def is_full_house(cards):
  72.     return split_ranks(cards, (2, 3))
  73.    
  74. def is_four(cards):
  75.     return split_ranks(cards, (1, 4))
  76.    
  77. def is_pat(cards):
  78.     return is_straight(cards) or is_flush(cards) or is_full_house(cards) or is_four(cards)
  79.    
  80. def is_straight_flush(cards):
  81.     return is_straight(cards) and is_flush(cards)
  82.    
  83. def rank_count(cards):
  84.     result = {}
  85.     for card in cards:
  86.         r = rank_int(card)
  87.         result[r] = result.get(r, 0) + 1
  88.     return result
  89.    
  90. def is_three(cards, counts=None):
  91.     counts = counts or rank_count(cards)
  92.     for rank, count in counts.iteritems():
  93.         if count == 3:
  94.             return True
  95.     return False
  96.    
  97. def is_two_pair(cards, counts=None):
  98.     pairs = 0
  99.     counts = counts or rank_count(cards)
  100.     for rank, count in counts.iteritems():
  101.         if count == 2:
  102.             pairs += 1
  103.     return pairs == 2
  104.    
  105. def is_pair(cards, counts=None):
  106.     counts = counts or rank_count(cards)
  107.     for rank, count in counts.iteritems():
  108.         if count == 2:
  109.             return True
  110.     return False
  111.    
  112. def get_ranks(counts):
  113.     values = [(count, rank) for rank, count in counts.iteritems()]
  114.     values.sort(reverse=True)
  115.     values = [n[1] for n in values]
  116.     return values
  117.    
  118. def get_straight_rank(cards):
  119.     top = rank_int(cards[-1])
  120.     bottom = rank_int(cards[0])
  121.     if top == 12 and bottom == 0:
  122.         return 3
  123.     return top
  124.    
  125. def evaluate_hand(cards):
  126.     flush = is_flush(cards)
  127.     straight = is_straight(cards)
  128.     counts = rank_count(cards)
  129.     ranks = get_ranks(counts)
  130.     if straight:
  131.         ranks = [get_straight_rank(cards)]
  132.     if straight and flush:
  133.         value = 9
  134.     elif is_four(cards):
  135.         value = 8
  136.     elif is_full_house(cards):
  137.         value = 7
  138.     elif flush:
  139.         value = 6
  140.     elif straight:
  141.         value = 5
  142.     elif is_three(cards, counts):
  143.         value = 4
  144.     elif is_two_pair(cards, counts):
  145.         value = 3
  146.     elif is_pair(cards, counts):
  147.         value = 2
  148.     else:
  149.         value = 1
  150.     ranks.insert(0, value)
  151.     return tuple(ranks)
Advertisement
Add Comment
Please, Sign In to add comment