-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pokerhands.py
230 lines (190 loc) · 9.08 KB
/
Pokerhands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from operator import sub
class PokerCard(object):
def __init__(self, card):
self.card = card
d = {'T': '10', 'J': '11', 'Q': '12', 'K': '13', 'A': '14'}
for k, v in d.items():
card = card.replace(k, v)
self.value = int(card[:-1])
self.suit = card[-1]
def __repr__(self):
return str(self.card)
def __eq__(self, other):
return (self.value == other.value) # & (self.suit == other.suit)
def __lt__(self, other): # for sorting
return self.value < other.value
def __gt__(self, other): # for set
return self.value > other.value
def __hash__(self): # for set behaviour on values
return self.value
class PokerHand(object):
# TODO REFACTOR POKERHANDS with Statepattern for different comparision systems
# (i.e. delegating the hand "identification" and the comparision of same rank
# into a specific subclass. For instance class Straight. Further consider
# decorator pattern for upgrading from straight behaviour to straight_flush.
# All this eases read- & maintainability AND! allows for minor changes such
# as rule change of lower Ace as required in this challenge in particular!
def __init__(self, hand):
cards = hand.split(' ')
self.cards = [PokerCard(card) for card in cards]
self.cards.sort()
self.myrank()
# print(self)
def __repr__(self):
hand = ['highcard', 'pair', 'two_pair', 'three', 'straight', 'flush',
'full_house', 'four', 'straight_flush', 'royal_flush'][self.rank]
return 'rank {}, {}, {}'.format(self.rank, hand, self.cards)
def myrank(self):
# looking for pair orders
d_values = {k: [] for k in set(card.value for card in self.cards)}
for card in self.cards:
d_values[card.value].append(card)
self.multiples = [v for v in d_values.values() if len(v) > 1]
self.multiples.sort(key=len, reverse=True) # for full house relevant
self.remain = {v[0] for v in d_values.values() if len(v) == 1}
def royal_flush():
return straight_flush() & (max(self.cards).value == 14)
def straight_flush():
return flush() & straight()
def fullhouse():
return (len(self.multiples) == 2) & (set(len(l) for l in self.multiples) == {2, 3})
def flush():
return len(set(x.suit for x in self.cards)) == 1
def straight():
valuelist = list(x.value for x in self.cards)
valuelist.sort()
return set(map(sub, valuelist[1:], valuelist[:-1])) == {1}
def twopair():
return (len(self.multiples) == 2) & (set(len(l) for l in self.multiples) == {2})
bo = [royal_flush(), straight_flush(), fullhouse(), flush(), straight(), twopair(), True]
self.rank = max([v for v, b in zip([9, 8, 6, 5, 4, 2, 0], bo) if b])
if len(self.multiples) == 1: # pair, three, four
self.rank = (1, 3, 7)[len(self.multiples[0]) - 2]
def compare_with(self, other):
assert (isinstance(other, PokerHand))
if self.rank > other.rank:
return 'Win'
if self.rank < other.rank:
return 'Loss'
elif self.rank == other.rank:
if self.rank in (1, 2, 3, 6, 7): # full house
for a, b in zip(self.multiples, other.multiples):
if a[0] > b[0]:
return 'Win'
elif a[0] < b[0]:
return 'Loss'
else:
continue
if self.rank in (0, 1, 2, 3, 4, 5, 7, 8):
# check unused cards
mine = self.remain - other.remain
oppo = other.remain - self.remain
if any((mine == set(), oppo == set())):
return 'Tie' # longer set wins
elif max(mine) > max(oppo):
return 'Win'
elif max(mine) < max(oppo):
return 'Loss'
return 'Tie'
if __name__ == '__main__':
# (PokerCard behaviour) ----------------------------------------------------
# card = PokerCard('TH')
# card1 = PokerCard('JH')
# card2 = PokerCard('JC')
# card3 = PokerCard('3S')
# card4 = PokerCard('4S')
#
# card < card1 # higher value*, same suit
# card1 > card2 # same value, different suit*
# card == card # identity
#
# a = set([card, card1, card2])
# b = set([card, card1])
#
# c = a - b
# c == set([card2])
#
# d = b - a
# d == set()
#
# c > d # due to len
#
# max(c)
# # max(d) # will not work, as empty sequence
# max([card, card1, card2])
#
# e = set([*b, card3, card4])
# f = set([*b, card2])
#
# g = f - e
# h = e - f
#
# max(g) > max(h)
# (PokerHand identification) -----------------------------------------------
# high = PokerHand("3S 6H QH 5S 4C")
# pair = PokerHand("3S 6H 4H 5S 4C")
# pair2 = PokerHand("2S 2H 4H 5S 4C")
# three = PokerHand("AH AC 5H 6H AS")
# straight = PokerHand("2S 3H 4H 5S 6C")
# flush = PokerHand("2S AS TS QS JS")
# full_house = PokerHand("2S AH 2H AS AC")
# four = PokerHand("JS JD JC JH AD")
# straight_flush = PokerHand("2H 3H 4H 5H 6H")
# royal_flush = PokerHand("AS KS QS JS TS")
# (PokerHand behaviour) ----------------------------------------------------
def runTest(msg, expected, hand, other):
player, opponent = PokerHand(hand), PokerHand(other)
print(expected)
print("{}: '{}' {} against '{}'".format(msg, hand, expected, other))
assert (player.compare_with(opponent) == expected)
# runTest("Highest straight flush wins", "Loss", "2H 3H 4H 5H 6H", "KS AS TS QS JS")
# runTest("Straight flush wins of 4 of a kind", "Win", "2H 3H 4H 5H 6H", "AS AD AC AH JD")
# runTest("Highest 4 of a kind wins", "Win", "AS AH 2H AD AC", "JS JD JC JH 3D")
# runTest("4 Of a kind wins of full house", "Loss", "2S AH 2H AS AC", "JS JD JC JH AD")
# runTest("Full house wins of flush", "Win", "2S AH 2H AS AC", "2H 3H 5H 6H 7H")
# runTest("Highest flush wins", "Win", "AS 3S 4S 8S 2S", "2H 3H 5H 6H 7H")
# runTest("Flush wins of straight", "Win", "2H 3H 5H 6H 7H", "2S 3H 4H 5S 6C")
# runTest("Equal straight is tie", "Tie", "2S 3H 4H 5S 6C", "3D 4C 5H 6H 2S")
# runTest("Straight wins of three of a kind", "Win", "2S 3H 4H 5S 6C", "AH AC 5H 6H AS")
# runTest("3 Of a kind wins of two pair", "Loss", "2S 2H 4H 5S 4C", "AH AC 5H 6H AS")
# runTest("2 Pair wins of pair", "Win", "2S 2H 4H 5S 4C", "AH AC 5H 6H 7S")
# runTest("Highest pair wins", "Loss", "6S AD 7H 4S AS", "AH AC 5H 6H 7S")
# runTest("Pair wins of nothing", "Loss", "2S AH 4H 5S KC", "AH AC 5H 6H 7S")
# runTest("Highest card loses", "Loss", "2S 3H 6H 7S 9C", "7H 3C TH 6H 9S")
# runTest("Highest card wins", "Win", "4S 5H 6H TS AC", "3S 5H 6H TS AC")
# runTest("Equal cards is tie", "Tie", "2S AH 4H 5S 6C", "AD 4C 5H 6H 2C")
# runTest('higher flush wins (highcard)', 'Win', '4C 5C 9C 8C KC', '8C 9C 5C 3C TC')
# runTest('straight loses to flush', 'Loss', 'QC KH TS JS AH', 'JH AH TH KH QH')
# runTest('', 'Loss', '3S 8S 9S 5S KS', 'JH 8H AH KH QH')
# runTest('', 'Win', 'JH AH TH KH QH', 'QC KH TS JS AH')
# runTest('high card KS, but going down', 'Win', 'TS KS 5S 9S AC', 'JH 8S TH AH QH')
# runTest('flush flush 8C kicker', 'Win', '4C 5C 9C 8C KC', '3S 8S 9S 5S KS')
# runTest('fullhouse fullhouse', 'Loss', '3D 2H 3H 2C 2D', '2H 2C 3S 3H 3D')
# runTest('pair pair', 'Loss', 'KS 8D 4D 9S 4S', 'KD 4S KC 3H 8S')
# runTest('', 'Loss', '2D 6D 3D 4D 5D', 'JH 9H TH KH QH')
# runTest('', 'Win', 'QC KH TS JS AH' ,'JS QS 9H TS KH')
runTest('two straigt, low ace', 'Loss', '2D AC 3H 4H 5S', '2S 3H 4H 5S 6C')
# (Sort behaviour) ---------------------------------------------------------
from random import shuffle
from itertools import chain
SORTED_POKER_HANDS = list(map(PokerHand, ["KS AS TS QS JS",
"2H 3H 4H 5H 6H",
"AS AD AC AH JD",
"JS JD JC JH 3D",
"2S AH 2H AS AC",
"AS 3S 4S 8S 2S",
"2H 3H 5H 6H 7H",
"2S 3H 4H 5S 6C",
"2D AC 3H 4H 5S",
"AH AC 5H 6H AS",
"2S 2H 4H 5S 4C",
"AH AC 5H 6H 7S",
"AH AC 4H 6H 7S",
"2S AH 4H 5S KC",
"2S 3H 6H 7S 9C"]))
lstCopy = SORTED_POKER_HANDS.copy()
shuffle(lstCopy)
userSortedHands = chain(sorted(lstCopy))
for hand in SORTED_POKER_HANDS:
assert next(userSortedHands), hand
print('')