# PokerHand Class # Module: pokerhand.py from card import Card from constants import * class PokerHand: def __init__(self, cards): self.cards = cards def __str__(self): output = '' for card in self.cards: output += str(card) + ' ' return output def __repr__(self): return str(self) def hand_type(self): self.cards.sort( ) # Check for straight flush if self.cards[0].rank + 1 == self.cards[1].rank and \ self.cards[1].rank + 1 == self.cards[2].rank and \ self.cards[2].rank + 1 == self.cards[3].rank and \ self.cards[3].rank + 1 == self.cards[4].rank and \ self.cards[0].suit == self.cards[1].suit and \ self.cards[1].suit == self.cards[2].suit and \ self.cards[2].suit == self.cards[3].suit and \ self.cards[3].suit == self.cards[4].suit: return STRAIGHT_FLUSH # Check for 4-of-a-kind elif self.cards[0] == self.cards[1] and \ self.cards[1] == self.cards[2] and \ self.cards[2] == self.cards[3]: return FOUR_OF_A_KIND elif self.cards[1] == self.cards[2] and \ self.cards[2] == self.cards[3] and \ self.cards[3] == self.cards[4]: return FOUR_OF_A_KIND # Check for full-house elif self.cards[0] == self.cards[1] and \ self.cards[1] == self.cards[2] and \ self.cards[3] == self.cards[4]: return FULL_HOUSE elif self.cards[0] == self.cards[1] and \ self.cards[2] == self.cards[3] and \ self.cards[3] == self.cards[4]: return FULL_HOUSE # Check for flush elif self.cards[0].suit == self.cards[1].suit and \ self.cards[1].suit == self.cards[2].suit and \ self.cards[2].suit == self.cards[3].suit and \ self.cards[3].suit == self.cards[4].suit: return FLUSH # Check for straight elif self.cards[0].rank + 1 == self.cards[1].rank and \ self.cards[1].rank + 1 == self.cards[2].rank and \ self.cards[2].rank + 1 == self.cards[3].rank and \ self.cards[3].rank + 1 == self.cards[4].rank: return STRAIGHT # Check for three-of-a-kind elif self.cards[0] == self.cards[1] and \ self.cards[1] == self.cards[2]: return THREE_OF_A_KIND elif self.cards[1] == self.cards[2] and \ self.cards[2] == self.cards[3]: return THREE_OF_A_KIND elif self.cards[2] == self.cards[3] and \ self.cards[3] == self.cards[4]: return THREE_OF_A_KIND # Check for two-pair elif self.cards[0] == self.cards[1] and \ self.cards[2] == self.cards[3]: return TWO_PAIR elif self.cards[0] == self.cards[1] and \ self.cards[3] == self.cards[4]: return TWO_PAIR elif self.cards[1] == self.cards[2] and \ self.cards[3] == self.cards[4]: return TWO_PAIR elif self.cards[0] == self.cards[1] or \ self.cards[1] == self.cards[2] or \ self.cards[2] == self.cards[3] or \ self.cards[3] == self.cards[4]: return ONE_PAIR else: return NO_PAIR