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
|
from poker.constants import Rank, Value
from poker.models import Card, Hand
def _is_flush(cards: list[Card]) -> bool:
"""Calculate whether hand is a flush."""
return len(set(card.suit for card in cards)) == 1
def _is_straight(cards: list[Card]) -> bool:
"""Calculate whether hand is a straight."""
card_values = sorted([card.value for card in cards])
# If card values are not unique it is not a straight
if len(set(card_values)) != len(card_values):
return False
# Check for a ace low straight
if card_values == [Value.TWO, Value.THREE, Value.FOUR, Value.FIVE, Value.ACE]:
return True
# If all card differences are 1 it is a straight
diffs = [second - first for first, second in zip(card_values[:-1], card_values[1:])]
if set(diffs) == {1}:
return True
return False
def is_royal_flush(hand: Hand) -> bool:
"""Calculate whether hand a royal flush."""
if not _is_flush(hand.cards):
return False
values = [card.value for card in hand.cards]
return all(
value in values
for value in [Value.ACE, Value.KING, Value.QUEEN, Value.JACK, Value.TEN]
)
def is_straight_flush(hand: Hand) -> bool:
"""Calculate whether hand a straight flush."""
return _is_straight(hand.cards) and _is_flush(hand.cards)
def is_four_of_a_kind(hand: Hand) -> bool:
"""Calculate whether hand has four of a kind."""
return hand.value_counts[0][1] == 4
def is_full_house(hand: Hand) -> bool:
"""Calculate whether hand is a full house."""
return hand.value_counts[0][1] == 3 and hand.value_counts[1][1] == 2
def is_flush(hand: Hand) -> bool:
"""Calculate whether hand is a flush."""
return _is_flush(hand.cards) and not _is_straight(hand.cards)
def is_straight(hand: Hand) -> bool:
"""Calculate whether hand is a flush."""
return _is_straight(hand.cards) and not _is_flush(hand.cards)
def is_three_of_a_kind(hand: Hand) -> bool:
"""Calculate whether hand is a full house."""
return (
hand.value_counts[0][1] == 3
and hand.value_counts[1][1] == 1
and hand.value_counts[2][1] == 1
)
def is_two_pair(hand: Hand) -> bool:
"""Calculate whether hand is a two pair."""
return hand.value_counts[0][1] == 2 and hand.value_counts[1][1] == 2
def is_pair(hand: Hand) -> bool:
"""Calculate whether hand is a pair."""
return hand.value_counts[0][1] == 2 and hand.value_counts[1][1] == 1
def get_rank(hand: Hand) -> Rank:
"""Get hand rank.
TODO: Use a factory pattern to avoid this huge flow control.
"""
if is_royal_flush(hand):
return Rank.ROYAL_FLUSH
elif is_straight_flush(hand):
return Rank.STRAIGHT_FLUSH
elif is_four_of_a_kind(hand):
return Rank.FOUR_OF_A_KIND
elif is_full_house(hand):
return Rank.FULL_HOUSE
elif is_flush(hand):
return Rank.FLUSH
elif is_straight(hand):
return Rank.STRAIGHT
elif is_three_of_a_kind(hand):
return Rank.THREE_OF_A_KIND
elif is_two_pair(hand):
return Rank.TWO_PAIR
elif is_pair(hand):
return Rank.PAIR
else:
return Rank.HIGH_CARD
|