-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoker_4_cards_straight_map.py
99 lines (90 loc) · 3.29 KB
/
poker_4_cards_straight_map.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
# This file is meant to be used on JSMAPREDUCE - a free map reduce infrastructure.
# Nevertheless, the curious can read through this file to understand the logic
# These map reduce files together count the number of 4 cards straight hands in the game of poker. This is not an actual category of hands in Poker.
# The Poker hands will be generated by a kernel function available on the JSMAPREDUCE website.
# Python Mapper() : Given unique 5-card hand (csv string), return the made hand.
# e.g. 'flush', 'straight', etc
def Mapper(jsmr_context, dataline):
cards = dataline.split(',') # 5 cards like 'QH' (for Q of hearts)
# Get counts of all faces and suits.
counts = ({
'2':0, '3':0, '4':0, '5':0, '6':0, '7':0, '8':0, '9':0, 'T':0,
'J':0, 'Q':0, 'K':0, 'A':0,
'S':0, 'C':0, 'D':0, 'H':0
})
for card in cards:
face = card[0]
suit = card[1]
counts[face] += 1
counts[suit] += 1
is_flush = (
(counts['S'] == 5) or
(counts['C'] == 5) or
(counts['D'] == 5) or
(counts['H'] == 5))
is_straight = False
straightrunfaces = 'A23456789TJQKA'; # note: ace ('A') appears twice
for i in range(0, 10):
if (counts[straightrunfaces[i]] and
counts[straightrunfaces[i+1]] and
counts[straightrunfaces[i+2]] and
counts[straightrunfaces[i+3]] and
counts[straightrunfaces[i+4]]):
is_straight = True
break
is_4cardsstraight = False
if (counts[straightrunfaces[0]] and
counts[straightrunfaces[1]] and
counts[straightrunfaces[2]] and
counts[straightrunfaces[3]] and
not counts[straightrunfaces[4]]):
is_4cardsstraight = True
elif (counts[straightrunfaces[10]] and
counts[straightrunfaces[11]] and
counts[straightrunfaces[12]] and
counts[straightrunfaces[13]] and
not counts[straightrunfaces[9]]):
is_4cardsstraight = True
else:
for i in range(1, 10):
if (counts[straightrunfaces[i]] and
counts[straightrunfaces[i+1]] and
counts[straightrunfaces[i+2]] and
counts[straightrunfaces[i+3]] and
not counts[straightrunfaces[i+4]] and
not counts[straightrunfaces[i-1]]):
is_4cardsstraight = True
break
is_quad, is_trip, is_pair, is_two_pair = False, False, False, False
faces = 'A23456789TJQK'
for i in range(0, len(faces)):
face_count = counts[faces[i]]
if face_count == 4:
is_quad = True
elif face_count == 3:
is_trip = True
elif face_count == 2:
if is_pair: # saw another pair before?
is_two_pair = True
is_pair = True
# Emit output: a (stringized) count of '1' for the detected hand.
if is_straight and is_flush:
jsmr_context.Emit('straightflush', '1')
elif is_quad:
jsmr_context.Emit('4ofakind', '1')
elif is_trip and is_pair:
jsmr_context.Emit('fullhouse', '1')
elif is_flush:
jsmr_context.Emit('flush', '1')
elif is_straight:
jsmr_context.Emit('straight', '1')
elif is_4cardsstraight:
jsmr_context.Emit('4cardsstraight', '1')
elif is_trip:
jsmr_context.Emit('3ofakind', '1')
elif is_two_pair:
jsmr_context.Emit('2pair', '1')
elif is_pair:
jsmr_context.Emit('pair', '1')
else:
jsmr_context.Emit('highcard', '1')