Guest User

Player.py

a guest
May 29th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.30 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from collections import Counter
  4.  
  5. class Player(object):
  6.  
  7.     def __init__(self, place, nbr_players, hand, ingame=True):
  8.         self.place = place
  9.         self.nbr_players = nbr_players
  10.         self.hand = hand
  11.         self.ingame = ingame
  12.  
  13.  
  14.     #Find the best combination of cards with what's on the table
  15.     #Return a 3-tuple of the best possible hand, the highest-ranking
  16.     #   card(s) of the hand and a score used in case of a tie
  17.     def best_hand(self, table):
  18.         #Find if there's a straight
  19.         #P.S: This is a horrendous code that I hope to never see again
  20.         counter = 0
  21.         counter_sf = 0
  22.         tie_card = 0
  23.         tie_card_sf = 0
  24.         color_sf = 0
  25.         #Creat a new list because Aces have two values
  26.         cards = self.hand + table
  27.         for i in range(len(cards)):
  28.             if cards[i][0] == 1:
  29.                 cards.append((14, cards[i][1]))
  30.     #And sort that list
  31.         cards = sorted(cards, key=lambda x: x[0], reverse=True)
  32.     #Now for the main course
  33.         for i in range(1, len(cards)):
  34.             step = cards[i-1][0] - cards[i][0]
  35.             if step == 1:
  36.                 if counter == 0:
  37.                     tie_card = cards[i-1][0]
  38.                 counter += 1
  39.                 if cards[i][1] == cards[i-1][1]:
  40.                     if counter_sf == 0:
  41.                         tie_card_sf = cards[i-1][0]
  42.                         color_sf = cards[i-1][1]
  43.                     counter_sf += 1
  44.                 elif counter_sf < 4 and step > 1:
  45.                     tie_card_sf = 0
  46.                     counter_sf = 0
  47.             elif step == 0 and counter_sf != 0 and cards[i][1] == color_sf:
  48.                 counter_sf += 1
  49.             elif counter < 4:
  50.                 tie_card = 0
  51.                 tie_card_sf = 0
  52.                 counter = 0
  53.                 counter_sf = 0
  54.  
  55.  
  56.         if counter_sf >= 4:
  57.             #Straight flush
  58.             return 8, tie_card_sf, tie_card_sf
  59.         elif counter >= 4:
  60.             #Straight
  61.             return 4, tie_card, tie_card
  62.  
  63.         #We can now get rid of Aces double values
  64.         cards = [val for val in cards if val[0]!=1]
  65.  
  66.  
  67.         #Get a list of values occurences
  68.         #   sorted by occurences then values
  69.         value_occur = Counter(elem[0] for elem in cards).most_common()
  70.         value_occur = sorted(value_occur, key=lambda x: (x[1], x[0]), reverse=True)
  71.         #Get a list of suits ocurences
  72.         #   sorted by occurences
  73.         suit_occur = Counter(elem[1] for elem in cards).most_common()
  74.  
  75.  
  76.         if value_occur[0][1] == 4:
  77.             #Four of a kind
  78.             return 7, value_occur[0][0], (100*value_occur[0][0] + max([elem[0] for elem in cards if elem[0]!=value_occur[0][0]]))
  79.  
  80.         if value_occur[0][1] == 3:
  81.             if value_occur[1][1] >= 2:
  82.                 #Full house
  83.                 return 6, value_occur[0][0], value_occur[1][0]
  84.             elif suit_occur[0][1] < 5:
  85.                 #If we have Three of a kind, ties will be decided by the 2 other cards
  86.                 #   so we'll use a weighted sum of it
  87.                 card_left = [elem[0] for elem in cards if elem[0]!=value_occur[0][0]][:2]
  88.                 card_score = 0
  89.                 for i in range(1,3):
  90.                     card_score += i * card_left[-i]
  91.                 #Three of a kind
  92.                 return 3, value_occur[0][0], card_score
  93.  
  94.         if suit_occur[0][1] >= 5:
  95.             flush_highest = max([elem[0] for elem in cards if elem[1]==suit_occur[0][0]])
  96.             #Flush
  97.             return 5, flush_highest, flush_highest
  98.  
  99.         if value_occur[0][1] == 2:
  100.             if value_occur[1][1] == 2:
  101.                 #Two pair
  102.                 return 2, value_occur[0][0], (100*value_occur[1][0] + max([elem[0] for elem in cards if elem[0]!=value_occur[0][0] and elem[0]!=value_occur[1][0]]))
  103.             else:
  104.                 #If we have Pair, ties will be decided by the 3 other cards
  105.                 #   so we'll use a weighted sum of it
  106.                 card_left = [elem[0] for elem in cards if elem[0]!=value_occur[0][0]][:3]
  107.                 card_score = 0
  108.                 for i in range(1,4):
  109.                     card_score += i * card_left[-i]
  110.                 #Pair
  111.                 return 1, value_occur[0][0], card_score
  112.  
  113.         #If we have a high card, ties will be decided by the 4 other cards
  114.         #   so we'll use a weighted sum of it
  115.         card_left = [elem[0] for elem in cards if elem[0]!=value_occur[0][0]][:4]
  116.         card_score = 0
  117.         for i in range(1,5):
  118.             card_score += i * card_left[-i]
  119.         #High card
  120.         return 0,value_occur[0][0], card_score
  121.  
  122.     #Choose if the player folds
  123.     #Return bool of decision and flip self.ingame to False
  124.     def fold(self, table):
  125.         cards = self.hand + table
  126.         hand_now = self.best_hand(table)
  127.  
  128.         #Never fold with Three of a kind
  129.         if hand_now[0] > 2:
  130.             return False
  131.  
  132.         #Never fold a Pair or Two pair with a figure or Ace
  133.         if hand_now[0] > 0  and hand_now[1] > 10:
  134.             return False
  135.  
  136.         #Fold otherwise
  137.         self.ingame = False
  138.         return True
  139.  
  140.  
  141. if __name__ == '__main__':
  142.     table = [(9,4), (13,2) ,(9,2) ,(11,2) ,(7,4)]
  143.     test = Player(0, 2, [(12,2), (11,4)])
  144.     print(test.best_hand(table))
Advertisement
Add Comment
Please, Sign In to add comment