def check_for_trips(cards): '''check for trips. assuming that full houses have already been caught. 4 element code''' rank_count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] code = [0] for c in cards: rank_count[c.return_rank() - 2] += 1 for t in range(len(rank_count)): if rank_count[t] == 3: #we have trips code.clear() code.append(4) #code for trips code.append(t + 2) #rank of trips k1 = 12 while k1 > -1: if rank_count[k1] == 1: #we have kicker number 1 code.append(k1 + 2) #kicker1 value k2 = k1 - 1 while k2 > -1: #this loop should actually never reach terminating condition since there should always be a 2nd kicker if rank_count[k2] == 1: #we have kicker number 2 code.append(k2 + 2) #kicker2 value return code k2 -= 1 k1 -= 1 return [0] #no trips
def check_for_straight(cards): '''check for straight. 2-element code. straight plus high card''' code = [0] #NOTE!!! assuming cards are already sorted (descending based on rank) #checking for non-wheel straight if cards[0].return_rank() - cards[1].return_rank() == 1: if cards[1].return_rank() - cards[2].return_rank() == 1: if cards[2].return_rank() - cards[3].return_rank() == 1: if cards[3].return_rank() - cards[4].return_rank() == 1: #we have a straight code.clear() code.append(5) code.append(cards[0].return_rank()) return code #checking for wheel if cards[0].return_rank() == 14: if cards[1].return_rank() == 5: if cards[2].return_rank() == 4: if cards[3].return_rank() == 3: if cards[4].return_rank() == 2: code.clear() code.append(5) #code for straight code.append(5) #signifying that the straight is 5-high return code return [0] #not a straight
def check_for_straight_flush(cards): '''check for straight flush. 2 element code, straight flush code plus the highest straight flush card''' code = check_for_straight(cards) if code[0] == 5: #we have a straight code = check_for_flush(cards) if code[0] == 6: # we have a flush code.clear() code.append(9) #code for straight flush if cards[0].return_rank( ) != 14: #not a straight flush containing an ace code.append( cards[0].return_rank() ) #highest card of the straigt flush (for comparison purposes) return code elif cards[0].return_rank( ) == 14: #can be a 5 high SF or ace high SF, need to discern if cards[1].return_rank() == 13: #ace high SF code.append(13) return code elif cards[1].return_rank() == 5: #5 high SF code.append(5) return code return [0] #not a straight flush
def check_for_two_pair(cards): '''check for two pair. assume bigger hands have already been caught. 4 element code''' rank_count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] code = [0] for c in cards: rank_count[c.return_rank() - 2] += 1 r = 12 while r >= 1: if rank_count[r] == 2: # we have pair number 1 c = r - 1 while c >= 0: if rank_count[c] == 2: # we have pair number 2 code.clear() code.append(3) # code for 2 pair code.append(r + 2) #rank of larger pair code.append(c + 2) #rank of smaller pair k = 12 while k >= 0: #loop should never reach terminating condition as there should always be a kicker if rank_count[k] == 1: #we have found our kicker code.append(k + 2) #value of kicker return code k -= 1 c -= 1 r -= 1 return [0]
def check_for_full_house(cards): '''check for full house, 3 element code, FH, trips rank, pair rank''' code = [0] rank_count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for c in cards: rank_count[c.return_rank() - 2] += 1 for r in range(len(rank_count)): if rank_count[r] == 3: # we have trips for p in range(len(rank_count)): if rank_count[p] == 2: # we have a pair, thus, FH code.clear() code.append(7) #code for full house code.append(r + 2) #rank of trips code.append(p + 2) #rank of pair return code return [0]
def check_for_quads(cards): '''checking for 4 of a kind, 3 element code. quads, quads rank, kicker rank''' code = [0] rank_count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for c in cards: rank_count[c.return_rank() - 2] += 1 for r in range(len(rank_count)): if rank_count[r] == 4: # we have quads code.clear() code.append(8) #code for quads code.append(r + 2) #rank of quads for r in range(len(rank_count)): if rank_count[r] == 1: #we have found the kicker code.append(r + 2) return code return [0] #no quqds
def check_for_flush(cards): '''check for flush, 6-element code, flush plus 5 ranked cards''' code = [0] suit_count = [0, 0, 0, 0] for c in cards: suit_count[c.return_suit() - 1] += 1 for s in suit_count: if s == 5: # we have a flush code.clear() code.append(6) #code for flush # this adds the ranks of the flush cards in descending order (for comparison purposes) for c in cards: code.append(c.return_rank()) return code return [0] #not a flush
def check_for_pair(cards): '''check for pair. assume bigger hands have already been caught. 5 element code''' rank_count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] code = [0] for c in cards: rank_count[c.return_rank() - 2] += 1 r = 12 while r >= 0: if rank_count[r] == 2: #we have found our pair code.clear() code.append(2) #code for a single pair code.append(r + 2) #rank of pair k = 12 while k >= 0: #should never reach terminating condition if rank_count[k] == 1: #we have found kicker1 code.append(k + 2) #value of kicker1 l = k - 1 while l >= 0: #should never reach terminating condition if rank_count[l] == 1: #we have found kicker2 code.append(l + 2) #value of kicker2 m = l - 1 while m >= 0: #should never reach terminating condition if rank_count[m] == 1: #we have found kicker3 code.append(m + 2) #value of kicker3 return code m -= 1 l -= 1 k -= 1 r -= 1 return [0] #not a 1pair hand
def read_hand(cards, lower_limit=0, possibilities=[True, True, True]): '''read a poker hand. will search for hands >= to lower limit. will not search for hands lower than lower limit parameter. if searching is ended prematurely due to the lower limit parameter, a list containing -1 will be returned. the "cards" parameter expects a list of exactly 5 card instances''' cards = sort_cards( cards) #sort cards first, descending order based on rank if possibilities[1]: code = check_for_straight_flush(cards) if code[0] == 9: return code #if we dont need to check for any hands lower than straight flush if lower_limit == 9: return [-1] if possibilities[0]: code = check_for_quads(cards) if code[0] == 8: return code #if we dont need to check for any hands lower than quads if lower_limit == 8: return [-1] if possibilities[0]: code = check_for_full_house(cards) if code[0] == 7: return code #if we dont need to check for any hands lower than full house if lower_limit == 7: return [-1] if possibilities[1]: code = check_for_flush(cards) if code[0] == 6: return code #if we dont need to sheck for any hands lower than flush if lower_limit == 6: return [-1] code = check_for_straight(cards) if code[0] == 5: return code #if we don't need to check for any hands lower than straight if lower_limit == 5: return [-1] code = check_for_trips(cards) if code[0] == 4: return code #if we don't need to check for any hands lower than trips if lower_limit == 4: return [-1] code = check_for_two_pair(cards) if code[0] == 3: return code #if we don't need to check for any hands lower than 2 pair if lower_limit == 3: return [-1] code = check_for_pair(cards) if code[0] == 2: return code #if we don't need to check for any hands lower than 1 pair if lower_limit == 2: return [-1] #if we get here, the hand is 'high card' code.clear() code.append(1) #code for high card for c in cards: code.append(c.return_rank()) #value of high card return code