-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjackauto.py
More file actions
459 lines (399 loc) · 17.3 KB
/
blackjackauto.py
File metadata and controls
459 lines (399 loc) · 17.3 KB
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
import time
from random import randint
from decimal import *
results = []
start_time = time.ctime()
verbose_mode = False
for i in range(0,55):
winnings = []
how_many_times = 1000
#start_time = time.ctime()
number_of_decks = 1
bot_risk_level = -4 + (2*i)# an int representing a percentage, 60 is normal
for i in range(1,how_many_times):
money = 100
deck = []
random_card = '3H'
suit = ':D'
player_hand = []
opponent_hand = []
slow = False
def countq(x):
total = ''
for i in range(len(x)):
total = total + x[i][0]#randint(1,10) #print(x[i][0])#
return total
def count(x):
total = 0
for i in range(len(x)):
if x[i][0] == '1':
total = total + 10
elif x[i][0] == 'J':
total = total + 10
elif x[i][0] == 'Q':
total = total + 10
elif x[i][0] == 'K':
total = total + 10
elif x[i][0] == 'A':
total = total + 11
else:
total = total + int(x[i][0])#randint(1,10) #print(x[i][0])#
return total
def ace_count(x):
total = 0
for i in range(len(x)):
if x[i][0] == '1':
total = total + 10
elif x[i][0] == 'J':
total = total + 10
elif x[i][0] == 'Q':
total = total + 10
elif x[i][0] == 'K':
total = total + 10
elif x[i][0] == 'A':
total = total + 1
else:
total = total + int(x[i][0])#randint(1,10) #print(x[i][0])#
return total
#https://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float
def is_int(s):
try:
int(s)
return True
except ValueError:
return False
def enforce_limit(x):
if x > 100:
if verbose_mode:
print('max bet is $100')
x = 100
if x > money:
if verbose_mode:
print('you only have $' + str(money))
x = money
return x
def intro():
if verbose_mode:
print('\n\nyou have $' + str(money) + ', deck is at ' + str(len(deck)) + ' cards')
def odds(x, y):
if y < 2:
y = 2
if y > 11:
y = 11
possibles = [0,0,0,0,0,0,0,0,0,0]
for i in range(len(x)):
if x[i][0] == '1':
possibles[8] = possibles[8] + 1
elif x[i][0] == 'J':
possibles[8] = possibles[8] + 1
elif x[i][0] == 'Q':
possibles[8] = possibles[8] + 1
elif x[i][0] == 'K':
possibles[8] = possibles[8] + 1
elif x[i][0] == 'A':
possibles[9] = possibles[9] + 1
else:
possibles[(int(x[i][0]) - 2)] = possibles[(int(x[i][0]) - 2)] + 1 #randint(1,10) #print(x[i][0])#
#print(possibles)
numerator = 0
denominator = len(x)
for i in range(y-1):
numerator = numerator + possibles[i]
odds_are = int((numerator / denominator)*100)
if verbose_mode:
print('the chance of getting a ' + str(y) + ' or less is ' + str(odds_are) +'%')
def return_odds(x, y):
if y < 2:
y = 2
if y > 11:
y = 11
possibles = [0,0,0,0,0,0,0,0,0,0]
for i in range(len(x)):
if x[i][0] == '1':
possibles[8] = possibles[8] + 1
elif x[i][0] == 'J':
possibles[8] = possibles[8] + 1
elif x[i][0] == 'Q':
possibles[8] = possibles[8] + 1
elif x[i][0] == 'K':
possibles[8] = possibles[8] + 1
elif x[i][0] == 'A':
possibles[9] = possibles[9] + 1
else:
possibles[(int(x[i][0]) - 2)] = possibles[(int(x[i][0]) - 2)] + 1 #randint(1,10) #print(x[i][0])#
#print(possibles)
numerator = 0
denominator = len(x)
for i in range(y-1):
numerator = numerator + possibles[i]
odds_are = int((numerator / denominator)*100)
#print('the chance of getting a ' + str(y) + ' or less is ' + str(odds_are) +'%')
return odds_are
def check_for_ace(x):
is_ace = False
for i in range(len(x)):
if x[i][0] == 'A':
is_ace = True
return is_ace
def check_for_need():
if count(opponent_hand) >= 17:
if count(opponent_hand) > count(player_hand):
if verbose_mode:
print('\nyou need to hit or you will lose this hand.')
elif count(opponent_hand) < count(player_hand):
if verbose_mode:
print('\nstand here! you win!')
else:
if verbose_mode:
print('\nstand here to push.')
else:
if count(player_hand) > 11:
if verbose_mode:
print('\nstanding here may be wise. the dealer may bust.')
else:
if verbose_mode:
print('you can hit here')
def odds_adjuster(x):
adjusted_odds = x
if x < 1:
adjusted_odds = 1
return adjusted_odds
def show_odds():
player_points = count(player_hand)
if player_points > 21:
player_points = ace_count(player_hand)
if verbose_mode:
print('\nfor you ('+str(player_points)+'):')
odds(deck, odds_adjuster(21 - player_points))
if verbose_mode:
print('for the dealer ('+str(count(opponent_hand))+'):')
odds(deck, odds_adjuster(21 - count(opponent_hand)))
check_for_need()
#ik = randint(0,4)
for j in range(0, number_of_decks):
for i in range(0,4):
if i == 0:
suit = 'h'
elif i == 1:
suit = 'd'
elif i == 2:
suit = 'C'
else:
suit = 'S'
for i in range(2,15):
random_card = str(i) + suit
if i == 11:
random_card = 'J' + suit
elif i == 12:
random_card = 'Q' + suit
elif i == 13:
random_card = 'K' + suit
elif i == 14:
random_card = 'A' + suit
deck.append(random_card)
#print(deck)
#print('should be 52: ' + str(len(deck)))
while len(deck) > 10:
intro()
#bet = input('how much do you bet?\n')
if money > 0:
deck_size = len(deck)
bet_variance = 10 + (104 - (deck_size*2))
#print('bet bot thinks the bet variance should be: ' + str(bet_variance))
bet = 54 - len(deck)#bet = randint(1,bet_variance)#randint(1,money)#
else:
bet = 0
if slow:
time.sleep(4)
if is_int(bet):
bet = enforce_limit(int(bet))
if verbose_mode:
print('bet is $' + str(bet) + '\n')
if slow:
time.sleep(4)
else:
if verbose_mode:
print('\n\nhey, how about we just put you down for $100...')
bet = 100
if verbose_mode:
print('bet is $' + str(bet))
player_hand = []
opponent_hand = []
for i in range(2):
temp_card = deck[randint(0,len(deck)-1)]
player_hand.append(temp_card)
deck.remove(temp_card)
temp_card = deck[randint(0,len(deck)-1)]
opponent_hand.append(temp_card)
deck.remove(temp_card)
if verbose_mode:
print('***********************************************')
print('\nplayer hand: ')
print(player_hand)
#print(countq(player_hand))
print(str(count(player_hand)))
print('\n\ndealer hand:')
print(opponent_hand)
#print(countq(opponent_hand))
print(str(count(opponent_hand)))
print('\n***********************************************\n\n')
continuing = True
while continuing:
if ace_count(player_hand) < 21:
if count(player_hand) == 21:
continuing = False
if verbose_mode:
print('\n\nBLACKJACK!!!\n\n')
else:
show_odds()
chance_here = return_odds(deck, 21 - count(player_hand))
if verbose_mode:
print('there is a ' + str(chance_here) + '% chance here ')
if chance_here > bot_risk_level:
response = 1#input('\n\nHit or stand?')
else:
if count(opponent_hand) >= 17:
if count(opponent_hand) > count(player_hand):
response = 1
else:
response = 2
else:
response = 2
if slow:
time.sleep(4)
if response == 1:#'h':
if verbose_mode:
print('\n\nhit...\n\n')
temp_card = deck[randint(0,len(deck)-1)]
player_hand.append(temp_card)
deck.remove(temp_card)
player_points = count(player_hand)
if player_points > 21:
player_points = ace_count(player_hand)
if verbose_mode:
print('you draw the ' + temp_card + ', you have ' + str(player_points))
print(player_hand)
elif response == 2:#'s':
if verbose_mode:
print('\n\nstand.\n\n')
player_points = count(player_hand)
if player_points > 21:
player_points = ace_count(player_hand)
if verbose_mode:
print('standing with ' + str(player_points))
print(player_hand)
continuing = False
else:
if verbose_mode:
print('\n\nwhat? i didn\'t understand that.')
else:
if ace_count(player_hand) == 21:
if verbose_mode:
print('blackjack!')
else:
if verbose_mode:
print('\n\nBusted!\n\n')
continuing = False
#print(player_hand)
if ace_count(player_hand) < 22:
if count(opponent_hand) > 16:
if verbose_mode:
print('dealer stands at ' + str(count(opponent_hand)))
else:
while ace_count(opponent_hand) < 17:
range_holder = len(deck)-1
# this is an attempt to fix the 10k breaking issue
#if range_holder < 1:
#range_holder = 1
if len(deck) > 0:
temp_card = deck[randint(0,range_holder)]
opponent_hand.append(temp_card)#.insert(len(opponent_hand)-1,temp_card)
else:
deck.remove(temp_card)
#print('\n\n************************\ndeck length is < 1' + str(len(deck)) + '\n***********************************\n\n')
break
if verbose_mode:
print('\n\ndealer draws ' + temp_card + ', dealer has ' + str(ace_count(opponent_hand)))
print(opponent_hand)
player_points = count(player_hand)
if player_points > 21:
player_points = ace_count(player_hand)
dealer_points = count(opponent_hand)
if dealer_points > 21:
dealer_points = ace_count(opponent_hand)
if player_points > dealer_points:
if player_points > 21:
if verbose_mode:
print('\n\nyou loose! (' + str(player_points) + ')')
money = money - bet
else:
if verbose_mode:
print('\n\nyou win! ' + 'you (' + str(player_points) + '), dealer (' + str(dealer_points) + ')')
money = money + bet
else:
if dealer_points > 21:
if verbose_mode:
print('\n\ndealer busts! you win! (' + str(player_points) + ')')
money = money + bet
elif dealer_points == player_points:
if verbose_mode:
print('push.'+ ' you (' + str(player_points) + '), dealer (' + str(dealer_points) + ')')
else:
if verbose_mode:
print('\n\nyou lose! you are a big loser!'+ ' \nyou (' + str(player_points) + '), dealer (' + str(dealer_points) + ')')
money = money - bet
if verbose_mode:
print('\n\nI need to reshuffle, ' + str(len(deck)) + ' cards left...')
print('you have $' + str(money))
winnings.append(money)
#for i in range(52):
# temp_card = deck[randint(0,len(deck)-1)]
# print('removing ' + temp_card)
# deck.remove(temp_card)
# print('should be 51: ' + str(len(deck)))
#print(deck)
#print('should be 1: ' + str(len(deck)))
total_winnings = Decimal('0.00')
for item in winnings:
total_winnings = total_winnings + item
starting_bank = Decimal('100.00')
total_adjusted_winnings = total_winnings - (starting_bank * how_many_times)
average_total_winnings = (total_winnings/how_many_times) - starting_bank
end_time = time.ctime()
print('\nstart time ' + str(start_time) + '\n end time ' + str(end_time) + '\n')
print(str(how_many_times) + ' times, (bot = ' + str(bot_risk_level) + '), decks = ' + str(number_of_decks) + ', $' + str(average_total_winnings) )
if verbose_mode:
print('start time ' + str(start_time) + '\n end time ' + str(end_time))
print('\n\nfor ' + str(how_many_times) + ' times through ' + str(number_of_decks) + ' decks, your average total winnings were: $' + str(average_total_winnings) )
print('\nmax winnings = $' + str(max(winnings)) + ', adjusted winnings = $' + str(total_adjusted_winnings))#+ ', total winnings = $' + str(total_winnings))
#result_string = '(' + str(how_many_times) + '), bot: ' + str(bot_risk_level) +', $' + str(average_total_winnings)
bag = []
bag.append(float(average_total_winnings))
bag.append(bot_risk_level)
bag.append(how_many_times)
results.append(bag)#results.append(result_string)
#print(results)
for each in results:
print(each)
#for each in results:
#print(each[2])
max_bot_value = max(results)
print('max return = $' + str(max_bot_value[0]) + ', bot value = ' + str(max_bot_value[1]) + '%')
end_time = time.ctime()
print('start time ' + str(start_time) + '\n end time ' + str(end_time))
file_name = 'auto_winnings_bigbet' + str(how_many_times) + '.txt'
outfile = open(file_name, 'wt')
for item in results:
print(str(item), file=outfile)#print(", ".join(str(item)), file=outfile)#
#print('max return = $' + str(max_bot_value[0]) + ', bot value = ' + str(max_bot_value[1]) + '%')
outfile.close()
print('finished writing to ' + file_name + '.\n')
file_name = 'auto_winnings_time' + str(how_many_times) + '.txt'
outfile = open(file_name, 'wt')
print('for ' + str(how_many_times) + ' cycles:', file=outfile)
print('start time ' + str(start_time), file=outfile)
print('end time ' + str(end_time), file=outfile)
#print(", ".join(str(item)), file=outfile)#
#print('max return = $' + str(max_bot_value[0]) + ', bot value = ' + str(max_bot_value[1]) + '%')
outfile.close()
print('finished writing to ' + file_name + '.\n')