From 956ce2578c50af28ada8025a3db5ec5cd83cb46c Mon Sep 17 00:00:00 2001 From: julien lengrand-lambert Date: Thu, 12 Sep 2013 19:35:17 +0200 Subject: [PATCH] Implements Royal Flush detector --- e_54.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/e_54.py b/e_54.py index f03eacc..9da5e44 100644 --- a/e_54.py +++ b/e_54.py @@ -27,8 +27,7 @@ is a clear winner. How many hands does Player 1 win? ''' - -from itertools import groupby +import collections def enum(*sequential, **named): enums = dict(zip(sequential, range(len(sequential))), **named) @@ -46,7 +45,7 @@ class PokerRanking: "Flush", "Full_House", "Four_of_a_Kind", - "Straight Flush", + "Straight_Flush", "Royal_Flush") def calculate_hand_rank(self, hand): @@ -54,6 +53,28 @@ class PokerRanking: Given a PokerHand, calculate its value The Poker hand is assumed to be sorted by value """ + rank = None + if(self.is_straight(hand)): + straight = True + rank = _ranks.Straight + + if(self.is_flush(hand)): + flush = True + rank = _ranks.Flush # this is better! + + if straight or flush: # We gonna finish early + + if straight and flush: # even better news + if is_royal_flush(hand): + rank = _ranks.Royal_Flush # Jackpot! + else: + rank = _ranks.Straight_Flush # Not bad :) + + return rank + + else: # We have something less funny + print "Not yet!" + def is_flush(self, hand): """ @@ -64,6 +85,25 @@ class PokerRanking: bools = [card.color == first_color for card in hand.cards] return (len([x for x in bools if x is True]) == len(bools)) + def is_royal_flush(self, hand, check=False): + """ + Testing if we have a royal flush + We may check whether we have a flush and a straight first. + But in our current context, we already know this, so this is easier + """ + if check: + straight = self.is_straight(hand) + flush = self.is_flush(hand) + res = (straight is True and flush is True) + else: + res = True + + if res: + if hand.cards[0].value == Card._values[-1]: # Highest value + return True + + return False # Not Royal Flush + def is_straight(self, hand): """ Detect whether the user has a straight hand or not @@ -176,6 +216,16 @@ def winning_hands(filename, player=1): my_game = games[0] my_hand = my_game.hand_1 + print Card._values[-1] + + aaa = [card.value for card in my_hand.cards] + print aaa + aaa[1] = "K" + print set(aaa) + bbb = collections.Counter(aaa).items() + print bbb + print len(bbb) + pok = PokerRanking() if __name__ == '__main__':