-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.py
504 lines (448 loc) · 17.9 KB
/
blackjack.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
from types import NoneType
import requests
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sqlite3 as sql
def generate_deck(count):
response = requests.post(f"https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count={count}")
data = response.json()
deckid = data['deck_id']
return deckid
def draw_card(deckid, cards):
#response.raise_for_status() # raises exception when not a 2xx response
#print(response.status_code)
try:
response = requests.post(f"https://deckofcardsapi.com/api/deck/{deckid}/draw/?count={cards}")
data = response.json()
# if data['cards'] == NoneType or data['cards'][0] == NoneType:
# print("BUG:", data)
# exit()
return data
except:
draw_card(deckid, cards)
# if response.status_code == 200:
# data = response.json()
# return data
# else:
# draw_card(deckid, cards)
def return_card(deckid, card):
requests.post(f"https://deckofcardsapi.com/api/deck/{deckid}/return/?cards={card}")
return None
def reshuffle(deckid):
requests.post(f"https://deckofcardsapi.com/api/deck/{deckid}/shuffle")
return 0
def convert(data):
if data[0] == 'A':
return 11
if data[0] == 'K':
return 10
if data[0] == 'Q':
return 10
if data[0] == 'J':
return 10
if data[0] == '0':
return 10
else:
return int(data[0])
def count(x):
if x < 7:
return(1)
elif x > 9:
return(-1)
else:
return(0)
#d31xyjxoan40
def manual_play():
money_to_bet = input("Enter starting money:")
while not money_to_bet.isdigit():
print("\nPlease enter a number")
money_to_bet = input("Enter starting money:")
money_to_bet = int(money_to_bet)
deck_id = generate_deck(1)
game = 1
deck_count = 0
while True:
# check deck size at start of round
card = draw_card(deck_id, 1)
if card['remaining'] <= 10:
deck_count = reshuffle(deck_id)
print("Deck reshuffled")
return_card(deck_id, card['cards'][0]['code'])
# running game count
print(f"Game: {game}")
game += 1
# running card count
print("Deck Strength: ", deck_count)
bet = input("Enter Bet:")
while not bet.isdigit():
print("\nError, please enter a number")
bet = input("Enter Bet:")
while int(bet) < 10:
print("\nThe house minimum bet is 10, please try again")
bet = input("Enter Bet:")
# play a hand
print("\nEnter 1 to draw")
print("Enter any key to exit\n")
x = input()
if x == "1":
# player draw
player_hand_count = 0
# first card
card = draw_card(deck_id, 1)
player_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# second card
card = draw_card(deck_id, 1)
player_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# hit loop
while True:
print(f"Hand Strength: {player_hand_count}")
print("Enter 1 to hit")
print("Enter any key to stay\n")
if input() == "1":
card = draw_card(deck_id, 1)
player_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
else:
break
# condition when player bust, dealer hand still played
if player_hand_count > 21:
print("Dealer Wins !\n\n")
money_to_bet -= int(bet)
print("Current Wallet:", money_to_bet)
dealer_hand_count = 0
#first card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
#second card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# player didn't bust, dealer hand played
else:
dealer_hand_count = 0
#first card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
#second card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
print("Dealer Strength:", dealer_hand_count)
# hit logic
while dealer_hand_count < 16:
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
print("Dealer Strength:", dealer_hand_count)
if ((dealer_hand_count > 21) or (player_hand_count > dealer_hand_count)):
print("Player Wins !\n\n")
money_to_bet += int(bet)
print("Current Wallet:", money_to_bet)
elif(player_hand_count == dealer_hand_count):
print("Draw !\n\n")
else:
print("Dealer Wins !\n\n")
money_to_bet -= int(bet)
print("Current Wallet:", money_to_bet)
else:
exit()
def automated_play():
money_to_bet = input("Enter starting money:")
while not money_to_bet.isdigit():
print("\nPlease enter a number")
money_to_bet = input("Enter starting money:")
money_to_bet = int(money_to_bet)
deck_id = generate_deck(1)
deck_count = 0
unit = 10
rounds = 1
total_win = 0
total_lost = 0
total_draw= 0
won = 0
lost = 0
draw = 0
while True:
games_to_play = input("Enter number of games to simulate or enter x to exit:")
if games_to_play == 'x':
break
while not games_to_play.isdigit():
games_to_play = input("Enter number of games to simulate or enter x to exit:")
games_to_play = int(games_to_play)
if(won+lost+draw != 0):
print("Games Won %: ", (won)/(won+lost+draw))
print("Games Lost %: ", (lost)/(won+lost+draw))
print("Games Drawn %: ", (draw)/(won+lost+draw))
data[f'Test {rounds}'] = win_results
plt.plot(np.arange(data.shape[0]), data[f'Test {rounds}'], color='black', alpha=0.2)
rounds += 1
print(data)
print("Current Money: ", money_to_bet)
total_win += won
total_lost += lost
total_draw += draw
# reset stats before new simulation
won = 0
lost = 0
draw = 0
game = 1
win_results = []
while game <= games_to_play:
print(game)
# set game bet
bet = (deck_count-1)*unit
if bet < unit:
bet = unit
# check deck size at start of round
card = draw_card(deck_id, 1)
if card['remaining'] <= 10:
deck_count = reshuffle(deck_id)
print("Deck reshuffled")
return_card(deck_id, card['cards'][0]['code'])
# player draw
player_hand_count = 0
# first card
card = draw_card(deck_id, 1)
player_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# second card
card = draw_card(deck_id, 1)
player_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# player hit loop
while player_hand_count < 16:
card = draw_card(deck_id, 1)
player_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# condition when player bust, dealer hand still played
if player_hand_count > 21:
money_to_bet -= bet
dealer_hand_count = 0
#first card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
#second card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# player didn't bust, dealer hand played
else:
dealer_hand_count = 0
#first card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
#second card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# hit logic
while dealer_hand_count < 16:
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
if ((dealer_hand_count > 21) or (player_hand_count > dealer_hand_count)):
won += 1
money_to_bet += bet
elif(player_hand_count == dealer_hand_count):
draw += 1
else:
lost += 1
money_to_bet -= bet
if(won+lost+draw != 0):
win_results.append((won)/(won+lost+draw)-0.5)
# running game count
game += 1
# this was originally going to generate seperate tables based on the number of games played in the simulation
# I have realized it is likely less logic intensive if I simply use one large table and filter by number of rows when I plot the data later
def database_gen(games):
games = 'Games_'+str(games)
con = sql.connect('game_data_storage.db')
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS " + f"{games} " + "(Simulation_1 text)")
con.commit()
cur.execute("SELECT * FROM " + f"{games}")
items = cur.fetchall()
print(items)
con.close()
print("Table Created")
def insert_entry(games, win_results):
games = 'Games_'+str(games)
con = sql.connect('game_data_storage.db')
cur = con.cursor()
items = cur.fetchall()
print ('TEST', items)
cursor = con.execute("SELECT * FROM " + f"{games}")
names = list(map(lambda x: x[0], cursor.description))
names = str(int(names[-1][-1])+1)
if not items:
names = '1'
else:
cur.execute("ALTER TABLE " + f"{games}" + " ADD COLUMN " + f'Simulation_{names}' + " text")
print(names)
for item in win_results:
item = str(item)
print("INSERT INTO " + f"{games} " + f'(Simulation_{names}) ' + "VALUES " + f"{item}")
cur.execute("INSERT INTO " + f"{games} " + f'(Simulation_{names}) ' + "VALUES " + f"{item}")
items = cur.fetchall()
print(items)
con.commit()
con.close()
def graph_automated_play():
money_to_bet = input("Enter starting money:")
while not money_to_bet.isdigit():
print("\nPlease enter a number")
money_to_bet = input("Enter starting money:")
money_to_bet = int(money_to_bet)
deck_id = generate_deck(1)
deck_count = 0
unit = 10
rounds = 1
total_win = 0
total_lost = 0
total_draw= 0
won = 0
lost = 0
draw = 0
while True:
simulation = 1
games_to_play = input("Enter number of games to simulate or enter x to exit:")
if games_to_play == 'x':
break
while not games_to_play.isdigit():
games_to_play = input("Enter number of games to simulate or enter x to exit:")
games_to_play = int(games_to_play)
#database_gen(games_to_play)
simulations = input("Enter number of times to repeat or enter x to exit:")
if simulations == 'x':
break
while not simulations.isdigit():
simulations = input("Enter number of times to repeat or enter x to exit:")
simulations = int(simulations)
while simulation <= simulations:
#generate new deck for each sim
#deck_id = generate_deck(1)
print("Simulation", simulation)
total_win += won
total_lost += lost
total_draw += draw
# reset stats before new simulation
won = 0
lost = 0
draw = 0
game = 1
win_results = []
while game <= games_to_play:
print(game)
# set game bet
bet = (deck_count-1)*unit
if bet < unit:
bet = unit
# check deck size at start of round
card = draw_card(deck_id, 1)
deck_count += count(convert(card['cards'][0]['code']))
#print(card['remaining'])
if card['remaining'] <= 10:
deck_count = reshuffle(deck_id)
print("Deck reshuffled")
#return_card(deck_id, card['cards'][0]['code'])
# player draw
player_hand_count = 0
# first card
card = draw_card(deck_id, 1)
player_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# second card
card = draw_card(deck_id, 1)
player_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# player hit loop
while player_hand_count < 16:
card = draw_card(deck_id, 1)
player_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# condition when player bust, dealer hand still played
if player_hand_count > 21:
lost += 1
money_to_bet -= bet
dealer_hand_count = 0
#first card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
#second card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# player didn't bust, dealer hand played
else:
dealer_hand_count = 0
#first card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
#second card
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
# hit logic
while dealer_hand_count < 16:
card = draw_card(deck_id, 1)
dealer_hand_count += convert(card['cards'][0]['code'])
deck_count += count(convert(card['cards'][0]['code']))
if ((dealer_hand_count > 21) or (player_hand_count > dealer_hand_count)):
won += 1
money_to_bet += bet
elif(player_hand_count == dealer_hand_count):
draw += 1
else:
lost += 1
money_to_bet -= bet
if(won+lost+draw != 0):
win_results.append((won)/(won+lost+draw)-0.5)
#print(win_results)
# running game count
game += 1
simulation += 1
#if(won+lost+draw != 0):
#insert_entry(games_to_play, win_results)
data[f'Test {rounds}'] = win_results
plt.plot(np.arange(data.shape[0]), data[f'Test {rounds}'], color='black', alpha=0.2)
rounds += 1
# initialize dataframe
data = pd.DataFrame()
def win_graph():
plt.plot(np.arange(data.shape[0]), data.mean(axis=1), label='Average', color='red')
plt.plot(np.arange(data.shape[0]), np.zeros(data.shape[0]), label='50 % Win Rate', color='blue')
plt.title("Win Percentage Analysis")
plt.xlabel("Games Played")
plt.ylabel("Win Percentage")
plt.legend()
plt.savefig('2000 Game Win Analysis')
plt.show()
#event loop
def main():
while True:
x = input("""
Enter 1 for manual play
Enter 2 for automated play
Enter 3 for graph generation
Enter any key to exit\n
""")
if x == "1":
manual_play()
if x == "2":
automated_play()
if x == "3":
graph_automated_play()
win_graph()
else:
break
if __name__ == "__main__":
main()