forked from vivienyuwenchen/SoftDes-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
340 lines (291 loc) · 10.4 KB
/
model.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from poker import *
from montecarlo import *
import view
import pygame
import sys
import inspect
import random
from pygame.locals import *
def get_user_input(buttons):
"""gets input from user via buttons"""
raise_button = buttons[0]
check_button = buttons[1]
call_button = buttons[2]
fold_button = buttons[3]
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
# Consider adding an action property to button.
# Then you needn't retrieve and test each one individually:
# for button in buttons:
# if button.rect.collidepoint(event.pos):
# return button.action
if raise_button.rect.collidepoint(event.pos):
act = "raise"
elif call_button.rect.collidepoint(event.pos):
act = "call"
if check_button.rect.collidepoint(event.pos):
act = "check"
if fold_button.rect.collidepoint(event.pos):
act = "fold"
try:
return act
except:
pass
def get_bot_input(episode, game, player):
"""gets move from bot and updates trainer"""
act = mc_control_epsilon_greedy(episode, game, player)
return act
def get_game_status(game):
# This should probably be a tuple, since each of its items has a completely
# different type and meaning based on its position.
# I'm hedging (“probably”) because I haven't looked to see whether there's
# code that mutates the list once it's constructed.
# Or it could be namedtuple, or class (e.g. GameStatus).
return [game.player1.pocket, game.player2.pocket, game.community_cards.cards, game.player1.funds, game.player2.funds, game.table_pot]
def process_user_input(game, player, other, buttons):
"""
Get user or bot input.
"""
money = 100
move = get_user_input(buttons)
if move:
print(move)
print(player.wager)
print(other.wager)
if move == "fold":
player.fold()
elif move == "raise": # player bets an amount
player.call(other.wager)
player.bet(money)
elif move == "check" or move == "call" or move == "match":
if player.funds - money < 0:
print("You don't have enough money. Sorry")
process_user_input(game, player, other, buttons)
if other.wager < player.wager:
print("You can't match when you're ahead on betting!")
process_user_input(game, player, other, buttons)
player.call(other.wager)
player.check()
return player.wager
else:
pass
# why is this different from process_user_input?
# If it needs to be different, can it share code without duplication,
# by calling a common function?
def process_bot_input(game, player, other, episode):
"""
Get user or bot input.
"""
money = 100
move = get_bot_input(episode, game, player)
if move:
if move == "fold":
player.fold()
elif move == "raise": # player bets an amount
if player.funds - money < 0:
print("You don't have enough money. Sorry")
process_bot_input(game, player, other, episode)
player.call(other.wager)
player.bet(money)
elif move == "check":
if player.wager != other.wager:
print("You can't check. You haven't bet enough.")
process_input(game, player, other)
player.check()
elif move == "call":
if other.wager < player.wager:
print("You can't call when you're ahead on betting!")
process_input(game, player, other)
if player.funds - money < 0:
print("You don't have enough money. Sorry.")
process_input(game, player, other)
player.call(other.wager)
player.check()
return player.wager
else:
pass
def betting(game, episode, buttons):
"""Players bet against each other"""
potential_wager = process_user_input(
game, game.player1, game.player2, buttons)
if potential_wager:
game.player1.wager = potential_wager
game.update_tablepot()
# try to DRY the player1 and player2 code
if game.player1.folded:
print("player1 folded")
return False
game.player2.wager = process_bot_input(
game, game.player2, game.player1, episode)
game.update_tablepot()
if game.player2.folded:
print("player2 folded")
return False
if game.player1.wager == game.player2.wager:
print("moving on")
return True
else:
print("you're stuck in betting")
return betting
else:
return 'no input'
def newround(game):
game.community_cards.cards = []
# deal
game.player1.pocket.cards = []
game.player2.pocket.cards = []
game.new_deck()
deal(game.deck, game.player1.pocket.cards, 2)
deal(game.deck, game.player2.pocket.cards, 2)
game.player1.folded = False
game.player2.folded = False
game.player1.wager = 50
game.player1.funds -= 50
game.player2.wager = 100
game.player2.funds -= 100
game.update_tablepot()
def preflop(game, episode, buttons):
check_status = betting(game, episode, buttons)
return check_status
def flop(game, episode, buttons, run_status):
# deal
if run_status == 'go':
deal(game.deck, game.community_cards.cards, 3)
print("Player 1:", game.player1.pocket.cards)
print("Player 2:", game.player2.pocket.cards)
print("Community Cards:", game.community_cards.cards)
check_status = betting(game, episode, buttons)
return check_status
def turn(game, episode, buttons, run_status):
if run_status == 'go':
deal(game.deck, game.community_cards.cards, 1)
print("Player 1:", game.player1.pocket.cards)
print("Player 2:", game.player2.pocket.cards)
print("Community Cards:", game.community_cards.cards)
check_status = betting(game, episode, buttons)
return check_status
# flop, turn, and river look identical
def river(game, episode, buttons, run_status):
if run_status == 'go':
deal(game.deck, game.community_cards.cards, 1)
print("Player 1:", game.player1.pocket.cards)
print("Player 2:", game.player2.pocket.cards)
print("Community Cards:", game.community_cards.cards)
check_status = betting(game, episode, buttons)
return check_status
def showdown(game, episode):
"""Finds Winner Gives Money"""
# return winner
print("Player 1:", game.player1.pocket.cards)
print("Player 2:", game.player2.pocket.cards)
print("Community Cards:", game.community_cards.cards)
if game.player1.folded:
game.winner = "Player2"
game.player2.funds += game.table_pot
elif game.player2.folded:
game.winner = "Player1"
game.player1.funds += game.table_pot
else:
winner = compare_hands(
game.player1.pocket.cards, game.player2.pocket.cards, game.community_cards.cards)
if winner == "Player1":
game.winner = "Player1"
game.player1.funds += game.table_pot
elif winner == "Player2":
game.winner = "Player2"
game.player2.funds += game.table_pot
else:
game.winner = "Tie"
game.player1.funds += game.table_pot / 2
game.player2.funds += game.table_pot / 2
mc_control_epsilon_greedy(episode, game, game.player1)
mc_control_epsilon_greedy(episode, game, game.player2)
print("Winner:", game.winner)
print("Player 1:", game.player1.funds)
print("Player2:", game.player2.funds)
print("Game Over")
print("New Round")
def update_game(game, episode, buttons, run_status):
"""
Do the appropriate action to the game, increase table pot,
check if they folded, etc
As always, folded skips straight to the showdown round
Else, generate a bot input, and change the game accordingly
Folded skips to showdown
"""
game_round = game.round
if game_round == 'newround':
newround(game)
print(game_round)
game.round = 'preflop'
print(game.round)
return 'go'
# again, try to DRY these cases. The logic looks the same except for
# the name of the function that is called.
# I believe they could be combined into something like:
# round_actions = {'preflop': preflop, 'flop': flop, 'turn': turn, 'river': river}
# …
# elif game_round in round_actions:
# check = round_actions[game_round](game, episode, buttons)
# if …
elif game_round == 'preflop':
check = preflop(game, episode, buttons)
if check == True:
game.round = 'flop'
return 'go'
elif check == False:
game.round = 'showdown'
print(game.round)
return 'go'
elif check == 'no input':
game.round = 'preflop'
return 'stop'
return 'stop'
elif game_round == 'flop':
check = flop(game, episode, buttons, run_status)
if check == True:
game.round = 'turn'
return 'go'
elif check == False:
game.round = 'showdown'
return 'go'
elif check == 'no input':
game.round = 'flop'
return 'stop'
return 'stop'
pass
elif game_round == 'turn':
check = turn(game, episode, buttons, run_status)
if check == True:
game.round = 'river'
return 'go'
elif check == False:
game.round = 'showdown'
return 'go'
elif check == 'no input':
game.round = 'turn'
return 'stop'
return 'stop'
elif game_round == 'river':
check = turn(game, episode, buttons, run_status)
if check == True:
game.round = 'showdown'
return 'go'
elif check == False:
game.round = 'showdown'
return 'go'
elif check == 'no input':
game.round = 'river'
return 'stop'
return 'stop'
elif game_round == 'showdown':
showdown(game, episode)
#game.player1.wager = 100
#game.player2.wager = 50
# game.update_tablepot()
game.round = 'newround'
return 'go'
pass