Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from collections import Counter
- class Player(object):
- def __init__(self, place, nbr_players, hand, ingame=True):
- self.place = place
- self.nbr_players = nbr_players
- self.hand = hand
- self.ingame = ingame
- #Find the best combination of cards with what's on the table
- #Return a 3-tuple of the best possible hand, the highest-ranking
- # card(s) of the hand and a score used in case of a tie
- def best_hand(self, table):
- #Find if there's a straight
- #P.S: This is a horrendous code that I hope to never see again
- counter = 0
- counter_sf = 0
- tie_card = 0
- tie_card_sf = 0
- color_sf = 0
- #Creat a new list because Aces have two values
- cards = self.hand + table
- for i in range(len(cards)):
- if cards[i][0] == 1:
- cards.append((14, cards[i][1]))
- #And sort that list
- cards = sorted(cards, key=lambda x: x[0], reverse=True)
- #Now for the main course
- for i in range(1, len(cards)):
- step = cards[i-1][0] - cards[i][0]
- if step == 1:
- if counter == 0:
- tie_card = cards[i-1][0]
- counter += 1
- if cards[i][1] == cards[i-1][1]:
- if counter_sf == 0:
- tie_card_sf = cards[i-1][0]
- color_sf = cards[i-1][1]
- counter_sf += 1
- elif counter_sf < 4 and step > 1:
- tie_card_sf = 0
- counter_sf = 0
- elif step == 0 and counter_sf != 0 and cards[i][1] == color_sf:
- counter_sf += 1
- elif counter < 4:
- tie_card = 0
- tie_card_sf = 0
- counter = 0
- counter_sf = 0
- if counter_sf >= 4:
- #Straight flush
- return 8, tie_card_sf, tie_card_sf
- elif counter >= 4:
- #Straight
- return 4, tie_card, tie_card
- #We can now get rid of Aces double values
- cards = [val for val in cards if val[0]!=1]
- #Get a list of values occurences
- # sorted by occurences then values
- value_occur = Counter(elem[0] for elem in cards).most_common()
- value_occur = sorted(value_occur, key=lambda x: (x[1], x[0]), reverse=True)
- #Get a list of suits ocurences
- # sorted by occurences
- suit_occur = Counter(elem[1] for elem in cards).most_common()
- if value_occur[0][1] == 4:
- #Four of a kind
- 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]]))
- if value_occur[0][1] == 3:
- if value_occur[1][1] >= 2:
- #Full house
- return 6, value_occur[0][0], value_occur[1][0]
- elif suit_occur[0][1] < 5:
- #If we have Three of a kind, ties will be decided by the 2 other cards
- # so we'll use a weighted sum of it
- card_left = [elem[0] for elem in cards if elem[0]!=value_occur[0][0]][:2]
- card_score = 0
- for i in range(1,3):
- card_score += i * card_left[-i]
- #Three of a kind
- return 3, value_occur[0][0], card_score
- if suit_occur[0][1] >= 5:
- flush_highest = max([elem[0] for elem in cards if elem[1]==suit_occur[0][0]])
- #Flush
- return 5, flush_highest, flush_highest
- if value_occur[0][1] == 2:
- if value_occur[1][1] == 2:
- #Two pair
- 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]]))
- else:
- #If we have Pair, ties will be decided by the 3 other cards
- # so we'll use a weighted sum of it
- card_left = [elem[0] for elem in cards if elem[0]!=value_occur[0][0]][:3]
- card_score = 0
- for i in range(1,4):
- card_score += i * card_left[-i]
- #Pair
- return 1, value_occur[0][0], card_score
- #If we have a high card, ties will be decided by the 4 other cards
- # so we'll use a weighted sum of it
- card_left = [elem[0] for elem in cards if elem[0]!=value_occur[0][0]][:4]
- card_score = 0
- for i in range(1,5):
- card_score += i * card_left[-i]
- #High card
- return 0,value_occur[0][0], card_score
- #Choose if the player folds
- #Return bool of decision and flip self.ingame to False
- def fold(self, table):
- cards = self.hand + table
- hand_now = self.best_hand(table)
- #Never fold with Three of a kind
- if hand_now[0] > 2:
- return False
- #Never fold a Pair or Two pair with a figure or Ace
- if hand_now[0] > 0 and hand_now[1] > 10:
- return False
- #Fold otherwise
- self.ingame = False
- return True
- if __name__ == '__main__':
- table = [(9,4), (13,2) ,(9,2) ,(11,2) ,(7,4)]
- test = Player(0, 2, [(12,2), (11,4)])
- print(test.best_hand(table))
Advertisement
Add Comment
Please, Sign In to add comment