-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_rules.py
313 lines (247 loc) · 10.4 KB
/
game_rules.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# check if input is valid
def valid_input(card_set):
"""This function checks if a given card set is in an acceptable format
Args:
card_set (list or tuple): input sets should be list of tuples or tuple of tuples
"""
if isinstance(card_set, list) or isinstance(card_set, tuple):
for elm in card_set:
assert isinstance(
elm, tuple), "valid inputs are list of tuples or tuple of tuples!"
# check if the cards comes from a single deck
def single_deck(card_set1, card_set2):
"""This function checks if given card sets come from the same deck
Args:
card_set1 (list or tuple): input sets should be list or tuple
card_set2 (list or tuple): input sets should be list or tuple
Returns:
boolean: returns if card sets are from a single deck
"""
deck_dict = {}
for elm in card_set1:
if elm in deck_dict:
deck_dict[elm] += 1
else:
deck_dict[elm] = 1
for elm in card_set2:
if elm in deck_dict:
deck_dict[elm] += 1
else:
deck_dict[elm] = 1
return max(deck_dict.values()) == 1
# function definitions for poker hierarchies
def flush(card_set):
"""This function checks if given card set results in flush
Args:
card_set (list or tuple): card set that a user is holding
Returns:
boolean: returns if card set contains flush
"""
suits = [suit for value, suit in card_set]
return len(set(suits)) == 1
def royal_flush(card_set):
"""This function checks if given card sets results in royal flush
Args:
card_set (list or tuple): card set that a user is holding
Returns:
boolean: returns if card set contains royal flush
"""
required_royalflush = ['ace', 'king', 'queen', 'jack', '10']
if len(card_set) == 4:
required_royalflush = set(required_royalflush[:4])
if len(card_set) == 3:
required_royalflush = set(required_royalflush[:3])
values = set([value for value, suit in card_set])
condition = len(values.intersection(required_royalflush)) == len(values)
return flush(card_set) and condition
def straight_flush(card_set):
"""This function checks if given card set results in straight flush
Args:
card_set (list or tuple): card set that a user is holding
Returns:
boolean: returns if card set contains straight flush
"""
rank = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'jack': 11, 'queen': 12, 'king': 13, 'ace': 14}
sorted_values = sorted([rank[value] for value, suit in card_set])
required_values = list(range(min(sorted_values), max(sorted_values)+1))
condition = (sorted_values == required_values)
return flush(card_set) and condition
def four_of_kind(card_set):
"""This function checks if given card set results in four of a kind
Args:
card_set (list or tuple): card set that a user is holding
Returns:
boolean: returns if card set contains four of a kind
"""
values = [value for value, suit in card_set]
value_counts = [values.count(value) for value in set(values)]
return 4 in value_counts
def four_of_kind_comp(card_set1, card_set2):
"""This function checks two four of kind card sets and returns the winner
Args:
card_set1 (list or tuple): card set that a user is holding
card_set2 (list or tuple): card set that a user is holding
Returns:
string : returns the winner as player1 or player2
"""
rank = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'jack': 11, 'queen': 12, 'king': 13, 'ace': 14}
values1 = [value for value, suit in card_set1]
value_counts1 = {value: values1.count(value) for value in set(values1)}
values2 = [value for value, suit in card_set2]
value_counts2 = {value: values2.count(value) for value in set(values2)}
key_list1 = list(value_counts1.keys())
val_list1 = list(value_counts1.values())
val1 = key_list1[val_list1.index(4)]
key_list2 = list(value_counts2.keys())
val_list2 = list(value_counts2.values())
val2 = key_list2[val_list2.index(4)]
if rank[val1] > rank[val2]:
return 'player1'
else:
return 'player2'
def full_house(card_set):
"""This function checks if given card set results in a full house
Args:
card_set (list or tuple): card set that a user is holding
Returns:
boolean: returns if card set contains full house
"""
values = [value for value, suit in card_set]
value_counts = [values.count(value) for value in set(values)]
return 3 in value_counts and 2 in value_counts
def full_house_comp(card_set1, card_set2):
"""[summary]
Args:
card_set1 (list or tuple): card set that a user is holding
card_set2 (list or tuple): card set that a user is holding
Returns:
string : returns the winner as player1 or player2
"""
rank = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'jack': 11, 'queen': 12, 'king': 13, 'ace': 14}
values1 = [value for value, suit in card_set1]
value_counts1 = {value: values1.count(value) for value in set(values1)}
values2 = [value for value, suit in card_set2]
value_counts2 = {value: values2.count(value) for value in set(values2)}
key_list1 = list(value_counts1.keys())
val_list1 = list(value_counts1.values())
val1 = key_list1[val_list1.index(3)]
key_list2 = list(value_counts2.keys())
val_list2 = list(value_counts2.values())
val2 = key_list2[val_list2.index(3)]
if rank[val1] > rank[val2]:
return 'player1'
else:
return 'player2'
def straight(card_set):
"""This function checks if given card set results in a straight
Args:
card_set (list or tuple): card set that a user is holding
Returns:
boolean: returns if card set contains straight
"""
rank = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'jack': 11, 'queen': 12, 'king': 13, 'ace': 14}
sorted_values = sorted([rank[value] for value, suit in card_set])
required_values = list(range(min(sorted_values), max(sorted_values)+1))
return sorted_values == required_values
def straight_comp(card_set1, card_set2):
"""[summary]
Args:
card_set1 (list or tuple): card set that a user is holding
card_set2 (list or tuple): card set that a user is holding
Returns:
string : returns the winner as player1 or player2 or tie (in case of a tie game)
"""
rank = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'jack': 11, 'queen': 12, 'king': 13, 'ace': 14}
sorted_values1 = sorted([rank[value] for value, suit in card_set1])
sorted_values2 = sorted([rank[value] for value, suit in card_set2])
if max(sorted_values1) == max(sorted_values2):
return 'tie'
elif max(sorted_values1) > max(sorted_values2):
return 'player1'
else:
return 'player2'
def three_of_kind(card_set):
"""This function checks if given card set results in a three of a kind
Args:
card_set (list or tuple): card set that a user is holding
Returns:
boolean: returns if card set contains three of a kind
"""
values = [value for value, suit in card_set]
value_counts = [values.count(value) for value in set(values)]
return 3 in value_counts
def two_pair(card_set):
"""This function checks if given card set results in two pair
Args:
card_set (list or tuple): card set that a user is holding
Returns:
boolean: returns if card set contains two pair
"""
values = [value for value, suit in card_set]
value_counts = [values.count(value) for value in set(values)]
return value_counts.count(2) == 2
def two_pair_comp(card_set1, card_set2):
"""[summary]
Args:
card_set1 (list or tuple): card set that a user is holding
card_set2 (list or tuple): card set that a user is holding
Returns:
string : returns the winner as player1 or player2
"""
rank = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'jack': 11, 'queen': 12, 'king': 13, 'ace': 14}
values1 = [value for value, suit in card_set1]
value_counts1 = {value: values1.count(value) for value in set(values1)}
values2 = [value for value, suit in card_set2]
value_counts2 = {value: values2.count(value) for value in set(values2)}
needed1 = {k: v for (k, v) in value_counts1.items() if v == 2}
needed2 = {k: v for (k, v) in value_counts2.items() if v == 2}
this1 = {k: rank[k] for k, v in needed1.items()}
this2 = {k: rank[k] for k, v in needed2.items()}
key_list1 = list(this1.keys())
val_list1 = list(this1.values())
val1 = key_list1[val_list1.index(max(val_list1))]
key_list2 = list(this2.keys())
val_list2 = list(this2.values())
val2 = key_list2[val_list2.index(max(val_list2))]
if rank[val1] > rank[val2]:
return 'player1'
else:
return 'player2'
def one_pair(card_set):
"""This function checks if given card set results in a pair
Args:
card_set (list or tuple): card set that a user is holding
Returns:
boolean: returns if card set contains a pair
"""
values = [value for value, suit in card_set]
value_counts = [values.count(value) for value in set(values)]
return value_counts.count(2) == 1
def high_card(card_set1, card_set2):
"""This function checks which of the given card sets is the winner
Args:
card_set1 (list or tuple): card set that a user is holding
card_set2 (list or tuple): card set that a user is holding
Returns:
string : returns the winner as player1 or player2 or tie (in case of a tie game)
"""
rank = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'jack': 11, 'queen': 12, 'king': 13, 'ace': 14}
sorted_values1 = sorted([rank[value]
for value, suit in card_set1], reverse=True)
sorted_values2 = sorted([rank[value]
for value, suit in card_set2], reverse=True)
for val1, val2 in zip(sorted_values1, sorted_values2):
if val1 > val2:
return 'player1'
elif val2 > val1:
return 'player2'
else:
continue
return 'tie'