-
Notifications
You must be signed in to change notification settings - Fork 0
/
attendant.py
288 lines (257 loc) · 9.95 KB
/
attendant.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
# necessary imports
import secrets
import ccount_agent
import benchmark
import bb_player
import numpy as np
"""
The attendant draws cards and verifies hands
Definitions:
Status codes
status -> int
* 0: hand sum < 17
* 1: hand sum >= 17
* 2: hand sum == 21
* 3: hand sum > 21
outcome_status -> int
* 4: loss
* 5: win
* 6: draw
action_status -> int
* 7: hit
* 8: stand
* 9: double
"""
# list of strategies to test in main driver function
names = [
"bb",
"random",
"hi_lo",
"ko",
"zen",
"ten",
"halves",
"uston"
]
# the "attendant" draws blackjack cards (hand) from a defined
def draw_card(shoe: list[int], hand: list[int]) -> list[list[int], list[int]]:
# draw a card from top of shoe
pulled_card = shoe[0]
# add pulled card to hand
hand.append(pulled_card)
# shoe is updated
shoe = shoe[1:]
# return the game state
return [shoe, hand]
# check the hand's sum and output status code (to inform game state)
def verify(hand: list[int]) -> int:
# check hand's sum
hand_sum = benchmark.blackjack_sum(hand)
# check if hand is valid
if hand_sum > 21:
# invalid hand
status = 3
# blackjack?
elif hand_sum == 21:
# hand has a blackjack
status = 2
# valid for player?
elif hand_sum < 21:
# invalid for dealer?
if hand_sum >= 17:
# stop game, check hands
status = 1
else:
# game is valid for player
status = 0
# card status settled
return status
# status code 4 is loser
def loser(winning_hand: list[int], wager: float) -> list[list[int], float, int]:
# losing hand, bet is removed from balance
return [winning_hand, -1.0 * wager, 4]
# status code 5 is winner
def winner(player_hand: list[int], wager: float) -> list[list[int], float, int]:
# winning hand, gain 1.5x wager
return [player_hand, 1.5 * wager, 5]
# status code 6 is a draw
def draw(player_hand: list[int], wager: float) -> list[list[int], float, int]:
# no loss, gain nothing
return [player_hand, 0.0 * wager, 6]
# Verification Section
# a basic, proof-of-concept blackjack game that runs for a single round
# our player plays against the house and randomly selects hit/stand/double
# returns winning hand (if draw, player's hand) and status for player
def basic_game(shoe: list[int], wager: float, name: str) -> list[list[int], float, int]:
# initialization
house_hand = []
player_hand = []
# actions are hit/stand/double for player
action_status = 7
# first deal, 2 cards each
for _ in range(2):
[shoe, player_hand] = draw_card(shoe, player_hand)
[shoe, house_hand] = draw_card(shoe, house_hand)
# keep track of game state
state = 0
count = 0.0
base_distribution = [4 for _ in range(13)]
# for bb-agent
transitions = benchmark.transitions
emissions = benchmark.emissions
# game state machine
while True:
# new state
state += 1
# post-initial
if state == 1:
# check if name is a ccount strategy
if name not in names[0:2]:
# do preliminary card counting
# player counts
for card in player_hand:
count += ccount_agent.ccount(name, card)
# assumed "house" hand
for card in house_hand[1:]:
count += ccount_agent.ccount(name, card)
# account for other card
# ccount_agent.ccount(name, np.abs(6 - house_hand[1]))
# figure out action
action_status = ccount_agent.ccount_action(state, count)
# action conditionals
# double the wager
if action_status == 9:
wager = wager * 2
# draw new card and count it
[shoe, player_hand] = draw_card(shoe, player_hand)
count += ccount_agent.ccount(name, player_hand[-1])
# hit
elif action_status == 7:
# draw new card and count it
[shoe, player_hand] = draw_card(shoe, player_hand)
count += ccount_agent.ccount(name, player_hand[-1])
# random agent preliminaries
elif name == names[1]:
# choose a random valid action
action_status = secrets.choice(range(7,10))
# double wager
if action_status == 9:
wager = wager * 2
# draw new card
[shoe, player_hand] = draw_card(shoe, player_hand)
# play a hit
if action_status == 7:
# draw new card
[shoe, player_hand] = draw_card(shoe, player_hand)
# bb-player preliminaries
elif name == names[0]:
# update distribution with player hand
for card in player_hand:
base_distribution = bb_player.update_distribution(base_distribution, card)
# update distribution with assumed house hand
for card in house_hand[1:]:
base_distribution = bb_player.update_distribution(base_distribution, card)
# get step from current tree level
[action_status, transitions, emissions] = bb_player.action_tree_step(transitions, emissions, player_hand, state, base_distribution)
# double wager
if action_status == 9:
wager = wager * 2
# draw new card and update distribution with new card
[shoe, player_hand] = draw_card(shoe, player_hand)
base_distribution = bb_player.update_distribution(base_distribution, player_hand[-1])
# hit
elif action_status == 7:
# draw new card and update distribution with new card
[shoe, player_hand] = draw_card(shoe, player_hand)
base_distribution = bb_player.update_distribution(base_distribution, player_hand[-1])
# decide if player will draw or not
elif state != 1 and action_status == 7:
# check if name is a ccount strategy
if name not in names[0:2]:
# update card count with the new house card
count += ccount_agent.ccount(name, card)
# figure out action
action_status = ccount_agent.ccount_action(state, count)
# player chooses to hit
if action_status == 7:
# draw card and update count from known count
[shoe, player_hand] = draw_card(shoe, player_hand)
count += ccount_agent.ccount(name, player_hand[-1])
# random action agent
elif name == names[1]:
# pick hit or stand
action_status = secrets.choice(range(7,9))
# agent chooses to hit
if action_status == 7:
# get a new card
[shoe, player_hand] = draw_card(shoe, player_hand)
# bb-player agent
elif name == names[0]:
# get current action state
[action_status, transitions, emissions] = bb_player.action_tree_step(transitions, emissions, player_hand, state, base_distribution)
# agent chooses to hit
if action_status == 7:
# draw card and update distribution
[shoe, player_hand] = draw_card(shoe, player_hand)
base_distribution = bb_player.update_distribution(base_distribution, player_hand[-1])
# house draw
[shoe, house_hand] = draw_card(shoe, house_hand)
# get game state status
house_status = verify(house_hand)
player_status = verify(player_hand)
# get hand sums
house_sum = benchmark.blackjack_sum(house_hand)
player_sum = benchmark.blackjack_sum(player_hand)
# house hand is playable
if house_status == 0:
# check if player hand is playable
if player_status < 2:
# do nothing, go to action
continue
# check if player has blackjack
elif player_status == 2:
# winner!
return winner(player_hand, wager)
# player lost
else:
# loser :(
return loser(house_hand, wager)
# house hand is >= 17 but not >= 21; end gameplay
elif house_status == 1:
# check if player hand is playable
if player_status == 1:
# check if house won
if house_sum > player_sum:
# loser :(
return loser(house_hand, wager)
# check if player won
elif house_sum < player_sum:
# winner!
return winner(player_hand, wager)
# draw
else:
return draw(player_hand, wager)
# check if player has blackjack
elif player_status == 2:
# winner!
return winner(player_hand, wager)
# player lost
else:
# loser :(
return loser(house_hand, wager)
# house hand is blackjack
elif house_status == 2:
# draw, player also has blackjack
if player_status == 2:
return winner(player_hand, wager)
# loser :(
else:
return loser(house_hand, wager)
# house went bust
else:
# draw, player also went bust
if player_status == 3:
return draw(player_hand, wager)
# winner! any value that isn't a bust
else:
return winner(player_hand, wager)