-
Notifications
You must be signed in to change notification settings - Fork 0
/
PokerHandFunction.py
89 lines (78 loc) · 3.26 KB
/
PokerHandFunction.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
def findPokerHand(hand):
ranks = []
suits = []
possibleRanks = []
for card in hand:
if len(card) == 2:
rank = card[0]
suit = card[1]
else:
rank = card[0:2]
suit = card[2]
if rank == "A":
rank = 14
elif rank == "K":
rank = 13
elif rank == "Q":
rank = 12
elif rank == "J":
rank = 11
ranks.append(int(rank))
suits.append(suit)
sortedRanks = sorted(ranks)
# Royal Flush and Straight Flush and Flush
if suits.count(suits[0]) == 5: # Check for Flush
if 14 in sortedRanks and 13 in sortedRanks and 12 in sortedRanks and 11 in sortedRanks \
and 10 in sortedRanks:
possibleRanks.append(10)
elif all(sortedRanks[i] == sortedRanks[i - 1] + 1 for i in range(1, len(sortedRanks))):
possibleRanks.append(9)
else:
possibleRanks.append(6) # -- Flush
# Straight
# 10 11 12 13 14
# 11 == 10 + 1
if all(sortedRanks[i] == sortedRanks[i - 1] + 1 for i in range(1, len(sortedRanks))):
possibleRanks.append(5)
handUniqueVals = list(set(sortedRanks))
# Four of a kind and Full House
# 3 3 3 3 5 -- set --- 3 5 --- unique values = 2 --- Four of a kind
# 3 3 3 5 5 -- set -- 3 5 ---- unique values = 2 --- Full house
if len(handUniqueVals) == 2:
for val in handUniqueVals:
if sortedRanks.count(val) == 4: # --- Four of a kind
possibleRanks.append(8)
if sortedRanks.count(val) == 3: # --- Full house
possibleRanks.append(7)
# Three of a Kind and Pair
# 5 5 5 6 7 -- set -- 5 6 7 --- unique values = 3 -- three of a kind
# 8 8 7 7 2 -- set -- 8 7 2 --- unique values = 3 -- two pair
if len(handUniqueVals) == 3:
for val in handUniqueVals:
if sortedRanks.count(val) == 3: # -- three of a kind
possibleRanks.append(4)
if sortedRanks.count(val) == 2: # -- two pair
possibleRanks.append(3)
# Pair
# 5 5 3 6 7 -- set -- 5 3 6 7 - unique values = 4 -- Pair
if len(handUniqueVals) == 4:
possibleRanks.append(2)
if not possibleRanks:
possibleRanks.append(1)
# print(possibleRanks)
pokerHandRanks = {10: "Royal Flush", 9: "Straight Flush", 8: "Four of a Kind", 7: "Full House", 6: "Flush",
5: "Straight", 4: "Three of a Kind", 3: "Two Pair", 2: "Pair", 1: "High Card"}
output = pokerHandRanks[max(possibleRanks)]
print(hand, output)
return output
if __name__ == "__main__":
findPokerHand(["KH", "AH", "QH", "JH", "10H"]) # Royal Flush
findPokerHand(["QC", "JC", "10C", "9C", "8C"]) # Straight Flush
findPokerHand(["5C", "5S", "5H", "5D", "QH"]) # Four of a Kind
findPokerHand(["2H", "2D", "2S", "10H", "10C"]) # Full House
findPokerHand(["2D", "KD", "7D", "6D", "5D"]) # Flush
findPokerHand(["JC", "10H", "9C", "8C", "7D"]) # Straight
findPokerHand(["10H", "10C", "10D", "2D", "5S"]) # Three of a Kind
findPokerHand(["KD", "KH", "5C", "5S", "6D"]) # Two Pair
findPokerHand(["2D", "2S", "9C", "KD", "10C"]) # Pair
findPokerHand(["KD", "5H", "2D", "10C", "JH"]) # High Card