mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-09 12:29:22 +01:00
Scoring hands works now.
This commit is contained in:
parent
a5557cb636
commit
e183848d1b
156
others/poker.py
156
others/poker.py
@ -27,6 +27,8 @@
|
|||||||
# POSSIBILITY OF SUCH DAMAGE.
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
###
|
###
|
||||||
|
|
||||||
|
import supybot.fix
|
||||||
|
|
||||||
# Constants.
|
# Constants.
|
||||||
S = 's'
|
S = 's'
|
||||||
C = 'c'
|
C = 'c'
|
||||||
@ -63,67 +65,119 @@ class Card(object):
|
|||||||
__repr__ = __str__
|
__repr__ = __str__
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __cmp__(self, other):
|
||||||
cmp(self.rank, other.rank)
|
return cmp(self.rank, other.rank)
|
||||||
|
|
||||||
class Hand(object):
|
def combinations(L, n):
|
||||||
def __init__(self, cards):
|
if len(L) >= n:
|
||||||
self.cards = cards
|
if n == 0:
|
||||||
|
yield []
|
||||||
|
else:
|
||||||
|
(first, rest) = (L[:1], L[1:])
|
||||||
|
for miniL in combinations(rest, n-1):
|
||||||
|
yield first + miniL
|
||||||
|
for miniL in combinations(rest, n):
|
||||||
|
yield miniL
|
||||||
|
|
||||||
def cmpVal(self):
|
def sort(hand):
|
||||||
self.cards.sort()
|
hand.sort()
|
||||||
self.cards.reverse() # High before low.
|
hand.reverse()
|
||||||
first = 0
|
return hand
|
||||||
if self.isFlush() and self.isStraight():
|
|
||||||
first = 8
|
|
||||||
elif self.isFourOfAKind():
|
|
||||||
first = 7
|
|
||||||
elif self.isFullHouse():
|
|
||||||
first = 6
|
|
||||||
elif self.isFlush():
|
|
||||||
first = 5
|
|
||||||
elif self.isStraight():
|
|
||||||
first = 4
|
|
||||||
elif self.isThreeOfAKind():
|
|
||||||
first = 3
|
|
||||||
elif self.isTwoPair():
|
|
||||||
first = 2
|
|
||||||
elif self.isPair():
|
|
||||||
first = 1
|
|
||||||
return (first, self.cards)
|
|
||||||
|
|
||||||
# These assume self.cards is sorted.
|
def getFlush(hand):
|
||||||
def isFlush(self):
|
if all(lambda c: c.suit == hand[0].suit, hand):
|
||||||
return all(lambda s: s == self.cards[0].suit, self.cards)
|
return sort(hand)
|
||||||
|
return []
|
||||||
|
|
||||||
def isStraight(self):
|
def getStraight(hand):
|
||||||
diffs = [x-y for (x, y) in window(self.cards, 2)]
|
sort(hand)
|
||||||
return all(lambda x: x == 1, diffs)
|
if all(lambda i: i==1, [x.rank-y.rank for (x,y) in window(hand,2)]):
|
||||||
|
return hand
|
||||||
|
elif hand[0].rank == A:
|
||||||
|
(hand[0], hand[-1]) = (hand[-1], hand[0])
|
||||||
|
if all(lambda i: i == 1, [x.rank-y.rank for (x,y) in window(hand,2)]):
|
||||||
|
return hand
|
||||||
|
sort(hand)
|
||||||
|
return []
|
||||||
|
|
||||||
def _isSomeOfAKind(self, i):
|
def getPair(hand):
|
||||||
def eq(x, y):
|
sort(hand)
|
||||||
return x == y
|
for (x, y) in window(hand, 2):
|
||||||
for cards in window(self.cards, i):
|
if x.rank == y.rank:
|
||||||
if all(None, map(eq, window(cards, 2))):
|
return ([x,y], [c for c in hand if c.rank != x.rank])
|
||||||
return True
|
return ([], hand)
|
||||||
return False
|
|
||||||
|
|
||||||
def isFourOfAKind(self):
|
def getTrips(hand):
|
||||||
return _isSomeOfAKind(4)
|
sort(hand)
|
||||||
|
for (x, y, z) in window(hand, 3):
|
||||||
|
if x.rank == y.rank == z.rank:
|
||||||
|
return ([x,y,z], [c for c in hand if c.rank != x.rank])
|
||||||
|
return ([], hand)
|
||||||
|
|
||||||
def isThreeOfAKind(self):
|
def getFours(hand):
|
||||||
return _isSomeOfAKind(3)
|
sort(hand)
|
||||||
|
for (x, y, z, w) in window(hand, 4):
|
||||||
|
if x.rank == y.rank == z.rank == w.rank:
|
||||||
|
return ([x,y,z,w], [c for c in hand if c.rank != x.rank])
|
||||||
|
return ([], hand)
|
||||||
|
|
||||||
def isPair(self):
|
def score(cards):
|
||||||
return _isSomeOfAKind(2)
|
"""Returns a comparable value for a list of cards."""
|
||||||
|
def getRank(hand):
|
||||||
def isFullHouse(self):
|
assert len(hand) == 5
|
||||||
pass
|
(pair, pairRest) = getPair(hand)
|
||||||
|
if pair:
|
||||||
def __cmp__(self, other):
|
# Can't be flushes or straights.
|
||||||
cmp(self.cmpVal(), other.cmpVal())
|
(trips, tripRest) = getTrips(hand)
|
||||||
|
if trips:
|
||||||
|
(fours, fourRest) = getFours(hand)
|
||||||
|
if fours:
|
||||||
|
return (7, fours + fourRest)
|
||||||
|
(pair, _) = getPair(tripRest)
|
||||||
|
if pair:
|
||||||
|
# Full house.
|
||||||
|
return (6, trips + pair)
|
||||||
|
sort(tripRest)
|
||||||
|
return (3, trips + tripRest)
|
||||||
|
(otherPair, twoPairRest) = getPair(pairRest)
|
||||||
|
if otherPair:
|
||||||
|
if otherPair[0] > pair[0]:
|
||||||
|
return (2, otherPair + pair + twoPairRest)
|
||||||
|
else:
|
||||||
|
return (2, pair + otherPair + twoPairRest)
|
||||||
|
sort(pairRest)
|
||||||
|
return (1, pair + pairRest)
|
||||||
|
else:
|
||||||
|
flush = getFlush(hand)
|
||||||
|
if flush:
|
||||||
|
straight = getStraight(hand)
|
||||||
|
if straight:
|
||||||
|
return (8, straight)
|
||||||
|
return (5, flush)
|
||||||
|
straight = getStraight(hand)
|
||||||
|
if straight:
|
||||||
|
return (4, straight)
|
||||||
|
hand.sort()
|
||||||
|
return (0, hand)
|
||||||
|
first = 0
|
||||||
|
second = None
|
||||||
|
for hand in combinations(cards, 5):
|
||||||
|
(maybeFirst, maybeSecond) = getRank(hand)
|
||||||
|
if maybeFirst > first:
|
||||||
|
first = maybeFirst
|
||||||
|
second = maybeSecond
|
||||||
|
elif maybeFirst == first:
|
||||||
|
second = max(second, maybeSecond)
|
||||||
|
assert len(second) == 5, 'invalid second len'
|
||||||
|
return (first, second)
|
||||||
|
|
||||||
deck = []
|
deck = []
|
||||||
for suit in [S, H, C, D]:
|
for suit in [S, H, C, D]:
|
||||||
for rank in [A, K, Q, J] + range(2, 11):
|
for rank in [A, K, Q, J] + range(10, 1, -1):
|
||||||
deck.append(Card(rank, suit))
|
deck.append(Card(rank, suit))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import random
|
||||||
|
random.shuffle(deck)
|
||||||
|
for hand in window(deck, 7):
|
||||||
|
print '%s: %s' % (hand, score(hand))
|
||||||
|
Loading…
Reference in New Issue
Block a user