-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoker.py
executable file
·140 lines (112 loc) · 3.8 KB
/
poker.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
import random
import pandas as pd
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return self.rank + ' of ' + self.suit
class Deck:
def ranks(self):
#return ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
# return ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
return ['2','3','4','5','6','7','8','9','10','11','12','13','14']
def suits(self):
#return ['clubs', 'diamonds', 'hearts', 'spades']
return ['c', 'd', 'h', 's']
def __init__(self):
self.full = []
self.full = [Card(rank,suit) for rank in self.ranks() for suit in self.suits()]
def fullDeck(self):
self.fulldeck = [str(self.full[nr]) for nr in range(len(self.full))]
return self.fulldeck
def drawHand(self, n):
return random.sample(self.full, n)
def remover(self, cards):
self.left = set(self.full) - set(cards)
return self.left
def drawRiver(self, n):
return random.sample(self.left, n)
def checkFlush(suits):
for letter in deck.suits():
if suits.count(letter) >= 5:
return True
def checkStraight(ranks):
straight = []
for n in range(9):
straight1 = '23456'
straight2 = [int(straight1[0]) + n,int(straight1[1]) + n,int(straight1[2]) + n,int(straight1[3]) + n,int(straight1[4]) + n]
straight.append(''.join(map(str, straight2)))
tmp =list(set(ranks))
tmp.sort(key=float)
temp = ''.join(tmp)
if any(sublist in temp for sublist in straight):
return True
elif '2345' in temp and temp[-1] == '14':
return True
def check4(ranks):
for rank in deck.ranks():
if ranks.count(rank) == 4:
return True
def check3(ranks):
for rank in deck.ranks():
if ranks.count(rank) == 3:
return True
def checkFH(ranks):
if check3(ranks):
for rank in deck.ranks():
if ranks.count(rank) == 2:
return True
def checkStraightFlush(ranks, suits):
if checkFlush(suits) and checkStraight(ranks):
return True
def checkRoyalFlush(ranks, suits):
if checkFlush(suits) and checkStraight(ranks):
if '1011121314' in ''.join(sorted(set(ranks))):
return True
def checkPair(ranks):
for rank in deck.ranks():
if ranks.count(rank) == 2:
return True
def check2Pair(ranks):
if checkPair(ranks) == True and checkFH(ranks) != True:
if len(set(ranks)) < 6:
return True
def checkPoker(poker):
ranks, suits = [obj.rank for obj in poker], [obj.suit for obj in poker]
if checkRoyalFlush(ranks, suits):
return 'Royal flush'
elif checkStraightFlush(ranks, suits):
return 'Straight flush'
elif check4(ranks):
return 'Four of a kind'
elif checkFH(ranks):
return 'Full house'
elif checkFlush(suits):
return 'Flush'
elif checkStraight(ranks):
return 'Straight'
elif check3(ranks):
return 'Three of a kind'
elif check2Pair(ranks):
return 'Two pair'
elif checkPair(ranks):
return 'One pair'
else:
return 'High card'
def generateCSV(filename, nrOutcomes):
# deck = Deck()
outcomes = []
for i in range(nrOutcomes):
hand = set(deck.drawHand(2))
deck.remover(hand)
river = set(deck.drawRiver(5))
poker = river.union(hand)
outcomes.append(checkPoker(poker))
df = pd.DataFrame(outcomes, columns=['outcome'])
df.index.name = 'Tries'
df.index += 1
df.to_csv(filename)
if __name__ == '__main__':
deck = Deck()
generateCSV('handData.csv', 10000)