-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoker_hand_evaluator.py
134 lines (113 loc) · 4.59 KB
/
poker_hand_evaluator.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
import json
class Card:
def __init__(self, card, hand):
self.suit = card['suit']
self.rank = card['rank']
self.hand = hand
def rank_score(self):
if isinstance(self.rank, int):
return self.rank
else:
return self.convert_alpha()
def convert_alpha(self):
royalty_conversion = {'jack': 11, 'queen': 12, 'king': 13,
'ace': self.ace_conversion()}
return royalty_conversion[self.rank]
def ace_conversion(self):
if {2,3,4,5}.issubset([self.hand[card]['rank'] for card in self.hand]):
return 1
else:
return 14
class Hand:
def __init__(self, hand):
self.cards = [Card(hand[card], hand) for card in hand]
self.suits = [card.suit for card in self.cards]
self.rank_scores = sorted([card.rank_score() for card in self.cards])
self.score = self.SCORE[self.poker_hand()]
SCORE = {'high card': 1,
'one pair': 2,
'two pair': 3,
'three of a kind': 4,
'straight': 5,
'flush': 6,
'full house': 7,
'four of a kind': 8,
'straight flush': 9}
def frequency_of_ranks(self):
frequencies = {count: sorted([score for score in set(self.rank_scores)
if self.rank_scores.count(score) == count], reverse=True)
for count in reversed(self.list_of_counts())}
return frequencies
def poker_hand(self):
if self.check_for_straight():
if self.check_for_flush():
return 'straight flush'
return 'straight'
elif self.check_for_flush() and not self.check_for_straight():
return 'flush'
elif self.list_of_counts() == [1,4]:
return 'four of a kind'
elif self.list_of_counts() == [2,3]:
return 'full house'
elif self.list_of_counts() == [1,1,3]:
return 'three of a kind'
elif self.list_of_counts() == [1,2,2]:
return 'two pair'
elif self.list_of_counts() == [1,1,1,2]:
return 'one pair'
else:
return 'high card'
def list_of_counts(self):
count_pattern = sorted([self.rank_scores.count(score)
for score in set(self.rank_scores)])
return count_pattern
def check_for_flush(self):
output = len(set(self.suits)) == 1
return output
def check_for_straight(self):
output = [self.rank_scores[i]-self.rank_scores[i+1] for i in
range(len(self.rank_scores)-1)].count(-1) == 4
return output
class Game:
def __init__(self, data):
self.hand_one = Hand(data['hand_one'])
self.hand_two = Hand(data['hand_two'])
def evaluate_hands(self):
if self.hand_one.score > self.hand_two.score:
return 'Hand one wins by score.'
elif self.hand_one.score < self.hand_two.score:
return 'Hand two wins by score.'
else:
return self.evaluate_tied_hands()
def evaluate_tied_hands(self):
print('Hands are tied. Looking down the ranks:\n')
for key in self.hand_one.frequency_of_ranks():
print('Looking for card(s) with count {}:\n'.format(key))
for i in range(len(self.hand_one.frequency_of_ranks()[key])):
rank_one = self.hand_one.frequency_of_ranks()[key][i]
rank_two = self.hand_two.frequency_of_ranks()[key][i]
print('Comparing ranks of hand one: {}, hand two: {}.'.format(
rank_one, rank_two))
if rank_one > rank_two:
return '\nHand one wins by a higher rank.'
elif rank_one < rank_two:
return '\nHand two wins by a higher rank.'
else:
print('Still tied. Continue looking.\n')
else:
return 'Identical hands. Draw.'
def print_information(self):
print('Hand one: {}'.format([(card.suit, card.rank) for card in
self.hand_one.cards]))
print('Hand two: {} \n'.format([(card.suit, card.rank) for card in
self.hand_two.cards]))
print('Hand one is a {} and has a score of {}.'.format(
self.hand_one.poker_hand(), self.hand_one.score))
print('Hand two is a {} and has a score of {}.\n'.format(
self.hand_two.poker_hand(), self.hand_two.score))
print(self.evaluate_hands())
if __name__ == '__main__':
with open('hands.json', 'r') as hands:
data = json.load(hands)
game = Game(data)
game.print_information()