-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreflop.py
222 lines (194 loc) · 10.4 KB
/
preflop.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
#-------------------------------------------------------------------------------
# Name: preflop.py
# Purpose: Controls the game of the preflop stage. (only two hole cards are known)
#-------------------------------------------------------------------------------
import numpy as np
import pickle
import roundcontrol
import preflop_table
from preflop_table import card_prob
import math
import winningprob
# Calculate the bet value that maximizes expections
def calcBetValue(pi, potSize):
assert(pi < 0.5)
return int(float(potSize)/(1/pi-2))
# Player makes the bet action.
def preflopMakeAction(game, playerIndex, tableIndex = 1):
'''
game: an object of all imformation of the game
playerIndex: indicate which player is making action
tableIndex: indicate which table to use in preflop_table.py
return: modified tableIndex.
'''
pi = card_prob(game.player(playerIndex).holdcards()[0], game.player(playerIndex).holdcards()[1], tableIndex)
# if this player is computer, use our AI.
# if the player is computer, use our AI.
if game.player(playerIndex).isComputer:
# when two players has different money in pot, the opponent raised
if abs(game.player(0).potMoney - game.player(1).potMoney) != 0:
callValue = abs(game.player(0).potMoney - game.player(1).potMoney)
# different strategies based on pi
if pi < 0.5:
betValue = calcBetValue(pi, game.currentmoneyinpot())
if betValue < callValue:
print "Computer", playerIndex, "Fold......"
game.player(playerIndex).bet(0, "F", pi, game.currentmoneyinpot())
print
return False, 1-playerIndex, tableIndex
if betValue <= 2*callValue:
print "Computer", playerIndex, "Call Value", int(min(callValue, game.player(playerIndex).moneyInHand))
game.player(playerIndex).bet(min(callValue, game.player(playerIndex).moneyInHand), "C", pi, game.currentmoneyinpot())
print
else:
allInValue = game.player(1-playerIndex).moneyInHand + game.player(1-playerIndex).potMoney - game.player(playerIndex).potMoney
betValue = min(betValue, game.player(playerIndex).moneyInHand, allInValue)
betValue = max(betValue, 2*game.player(1-playerIndex).lastBet)
betValue = min(betValue, game.player(playerIndex).moneyInHand, allInValue)
betValue = max(betValue, 2)
print "Computer", playerIndex, "Raise Value", int(betValue)
game.player(playerIndex).bet(int(betValue), "R", pi, game.currentmoneyinpot())
print
else:
if 0.2*pi + np.random.uniform() < 0.6:
print "Computer", playerIndex, "Call Value", int(min(callValue, game.player(playerIndex).moneyInHand))
game.player(playerIndex).bet(min(callValue, game.player(playerIndex).moneyInHand), "C", pi, game.currentmoneyinpot())
print
else:
betValue = (pi-0.2) * game.currentmoneyinpot()
allInValue = game.player(1-playerIndex).moneyInHand + game.player(1-playerIndex).potMoney - game.player(playerIndex).potMoney
betValue = min(betValue, game.player(playerIndex).moneyInHand, allInValue)
betValue = max(betValue, 2*game.player(1-playerIndex).lastBet)
betValue = min(betValue, game.player(playerIndex).moneyInHand, allInValue)
betValue = max(betValue, 2)
print "Computer", playerIndex, "Raise Value", int(betValue)
game.player(playerIndex).bet(int(betValue), "R", pi, game.currentmoneyinpot())
print
# two players has the same money in pot, the opponent called
# if this player is human, ask for input
else:
while True:
print "Computer current money in hand is ", game.player(1-playerIndex).moneyInHand
print "Your current money in hand is ", game.player(playerIndex).moneyInHand
action = raw_input("Enter your decision: input 'C' for Call or 'R' for Raise or 'F' for Fold:\n")
if action == "F":
print "You Fold......"
return False, 1-playerIndex, tableIndex
elif action == "C":
callValue = abs(game.player(0).potMoney - game.player(1).potMoney)
game.player(playerIndex).bet(callValue, "C", pi, game.currentmoneyinpot())
print
break
elif action == "R":
betValue = math.ceil(game.player(1-playerIndex).lastBet * 2)
if betValue >= min(game.player(playerIndex).moneyInHand, game.player(1-playerIndex).lastBet + game.player(1-playerIndex).moneyInHand):
betAmount = min(game.player(playerIndex).moneyInHand, game.player(1-playerIndex).lastBet + game.player(1-playerIndex).moneyInHand)
else:
#print "Debug..........", game.player(1-playerIndex).lastBet, betValue
betAmount = raw_input("Enter the amount you want to raise:\n")
while int(betAmount) < betValue or int(betAmount) > min(game.player(playerIndex).moneyInHand, game.player(1-playerIndex).lastBet + game.player(1-playerIndex).moneyInHand):
print "Your min and max raise values are", int(betValue), min(game.player(playerIndex).moneyInHand, game.player(1-playerIndex).lastBet + game.player(1-playerIndex).moneyInHand)
betAmount = raw_input("Enter the amount you want to raise:\n")
game.player(playerIndex).bet(int(betAmount), "R", pi, game.currentmoneyinpot())
print
if betAmount > 0.2 * min(game.player(0).moneyInHand, game.player(1).moneyInHand):
tableIndex = 3
else:
tableIndex = max(2, tableIndex)
break
else:
print "Wrong input entered"
return True, playerIndex, tableIndex
# Handle the game flow before flop stage
def preflop(game, alterDealer):
'''
game: an object of all imformation of the game
alterDealer: the dealer of this stage
'''
# computer == player1
# user == player0
# print out human player's hole cards
for i in xrange(game.numPlayer):
if not game.player(i).isComputer:
print "Your Cards: ", game.player(i).holdcards()
print
#print "Computer Cards: ", game.player(1).holdcards()
# Big blind and small blind are bet automatically
game.player(alterDealer).bet(2, "")
game.player(1 - alterDealer).bet(1, "")
game.increStageIndex()
for i in xrange(game.numPlayer):
if game.player(i).isComputer:
print "Computer's money:", game.player(i).moneyInHand
else:
print "Your Money: ", game.player(i).moneyInHand
print "Your Cards: ", game.player(i).holdcards()
print "Total money on pot:", game.currentmoneyinpot()
tableIndex = 1
forward, winIndex, tableIndex = preflopMakeAction(game, 1 - alterDealer, tableIndex)
# Test if game ends after the previous action
if not forward:
return False, winIndex
print "Total money on pot:", game.currentmoneyinpot()
print "==================="
forward, winIndex, tableIndex = preflopMakeAction(game, alterDealer, tableIndex)
if not forward:
return False, winIndex
print "Total money on pot:", game.currentmoneyinpot()
print "==================="
# All players make actions alternatively until one of the players stopped raise
while game.player(0).potMoney != game.player(1).potMoney and game.player(0).moneyInHand != 0 and game.player(1).moneyInHand != 0:
forward, winIndex, tableIndex = preflopMakeAction(game, 1 - alterDealer, tableIndex)
if not forward:
return False, 1 - alterDealer
print "Total money on pot:", game.currentmoneyinpot()
print "==================="
if game.player(0).potMoney == game.player(1).potMoney or game.player(0).moneyInHand == 0 or game.player(1).moneyInHand == 0:
break
forward, winIndex, tableIndex = preflopMakeAction(game, alterDealer, tableIndex)
if not forward:
return False, alterDealer
print "Total money on pot:", game.currentmoneyinpot()
print "==================="
# If one of the players has no money in hand, only the other player can take one more action.
if game.player(0).moneyInHand == 0 and game.player(1).moneyInHand != 0:
forward, winIndex, tableIndex = preflopMakeAction(game, 1, tableIndex + 1)
if not forward:
return False, 1
print "Total money on pot:", game.currentmoneyinpot()
print "==================="
elif game.player(1).moneyInHand == 0 and game.player(0).moneyInHand != 0:
forward, winIndex, tableIndex = preflopMakeAction(game, 0, tableIndex + 1)
if not forward:
return False, 0
print "Total money on pot:", game.currentmoneyinpot()
print "==================="
for i in xrange(game.numPlayer):
if game.player(i).isComputer:
print "Computer's money:", game.player(i).moneyInHand
print
else:
print "Your money:", game.player(i).moneyInHand
print "Your Cards: ", game.player(i).holdcards()
print
print "Total money on pot:", game.currentmoneyinpot()
winIndex = -1
# Advance to the end of game when one of the players has no money in hand
if game.player(0).moneyInHand == 0 or game.player(1).moneyInHand == 0:
hand0 = game.player(0).holdcards() + game.publicCards
hand1 = game.player(1).holdcards() + game.publicCards
best0, strength0 = winningprob.bestfive(hand0)
best1, strength1 = winningprob.bestfive(hand1)
print "Your Best Five:", best0, "Original Cards in hand:", game.player(0).holdcards()
print "Computer's Best Five:", best1, "Original Cards in hand:", game.player(1).holdcards()
if hand0 == hand1:
winIndex = -1
print "Tie!!\n"
elif hand0 > hand1:
winIndex = 0
print "You win!\n"
else:
winIndex = 1
print "Computer wins!\n"
return False, winIndex
return True, -1