Fixed straight handling for low ace.

This commit is contained in:
Jeremy Fincher 2004-10-15 13:25:44 +00:00
parent e183848d1b
commit d761590f3c

View File

@ -90,13 +90,13 @@ def getFlush(hand):
def getStraight(hand): def getStraight(hand):
sort(hand) sort(hand)
if all(lambda i: i==1, [x.rank-y.rank for (x,y) in window(hand,2)]): diffs = [x.rank-y.rank for (x,y) in window(hand,2)]
if diffs == [1,1,1,1]:
return hand return hand
elif hand[0].rank == A: elif hand[0].rank == A and diffs == [9,1,1,1]:
(hand[0], hand[-1]) = (hand[-1], hand[0]) ace = hand.pop(0)
if all(lambda i: i == 1, [x.rank-y.rank for (x,y) in window(hand,2)]): hand.append(ace)
return hand return hand
sort(hand)
return [] return []
def getPair(hand): def getPair(hand):
@ -120,6 +120,16 @@ def getFours(hand):
return ([x,y,z,w], [c for c in hand if c.rank != x.rank]) return ([x,y,z,w], [c for c in hand if c.rank != x.rank])
return ([], hand) return ([], hand)
STRAIGHT_FLUSH = '8:straight flush'
FOUR_OF_A_KIND = '7:four of a kind'
FULL_HOUSE = '6:full house'
FLUSH = '5:flush'
STRAIGHT = '4:straight'
THREE_OF_A_KIND = '3:three of a kind'
TWO_PAIR = '2:two pair'
PAIR = '1:pair'
RUNT = '0:runt'
def score(cards): def score(cards):
"""Returns a comparable value for a list of cards.""" """Returns a comparable value for a list of cards."""
def getRank(hand): def getRank(hand):
@ -131,33 +141,33 @@ def score(cards):
if trips: if trips:
(fours, fourRest) = getFours(hand) (fours, fourRest) = getFours(hand)
if fours: if fours:
return (7, fours + fourRest) return (FOUR_OF_A_KIND, fours + fourRest)
(pair, _) = getPair(tripRest) (pair, _) = getPair(tripRest)
if pair: if pair:
# Full house. # Full house.
return (6, trips + pair) return (FULL_HOUSE, trips + pair)
sort(tripRest) sort(tripRest)
return (3, trips + tripRest) return (THREE_OF_A_KIND, trips + tripRest)
(otherPair, twoPairRest) = getPair(pairRest) (otherPair, twoPairRest) = getPair(pairRest)
if otherPair: if otherPair:
if otherPair[0] > pair[0]: if otherPair[0] > pair[0]:
return (2, otherPair + pair + twoPairRest) return (TWO_PAIR, otherPair + pair + twoPairRest)
else: else:
return (2, pair + otherPair + twoPairRest) return (TWO_PAIR, pair + otherPair + twoPairRest)
sort(pairRest) sort(pairRest)
return (1, pair + pairRest) return (PAIR, pair + pairRest)
else: else:
flush = getFlush(hand) flush = getFlush(hand)
if flush: if flush:
straight = getStraight(hand) straight = getStraight(hand)
if straight: if straight:
return (8, straight) return (STRAIGHT_FLUSH, straight)
return (5, flush) return (FLUSH, flush)
straight = getStraight(hand) straight = getStraight(hand)
if straight: if straight:
return (4, straight) return (STRAIGHT, straight)
hand.sort() hand.sort()
return (0, hand) return (RUNT, hand)
first = 0 first = 0
second = None second = None
for hand in combinations(cards, 5): for hand in combinations(cards, 5):