-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitting.py
151 lines (126 loc) · 5.12 KB
/
splitting.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
__author__ = 'miller'
__guy_who_looked_over_Jims_shoulder_and_also_wrote_some_cool_stuff__ = 'que'
############################# THIS MODULE CONSIDERS THE POSSIBILITY OF SPLITTING A HAND ################################
############################### PLAYS ON AN INFINITE DECK AND WRITES RESULTS TO .TXT FILE ##############################
import numpy as np
import infinte_deck as id
import random
import make_tables
#read in data from .txt files, to be used as reference in calculation
soft_table = make_tables.read_table_file('Soft Hit Strategy.txt')
reg_table = make_tables.read_table_file('hit strategy.txt')
#plays a hand for a player, considering opportunities to split
#ARGS: player_score as int; dealer show card as int, soft as boolean
#RETURN: player score as int OR calls self
def play_a_hand_player(player_score,dealer_show_card_index,soft=False):
player_current_score = player_score
row_index = player_current_score - 4
column_index = dealer_show_card_index
if player_current_score >= 11:
if soft == False:
if reg_table[row_index][column_index] == False:
return player_current_score
else:
new_card = id.deck(random.randint(1,13))
if new_card == 'ace':
player_new_score = player_current_score + 1
else:
player_new_score = player_current_score + new_card
if player_new_score > 21:
return 0
else:
return play_a_hand_player(player_new_score,dealer_show_card_index,soft=False)
else:
if soft_table[row_index][column_index] == False:
return player_current_score
else:
new_card = id.deck(random.randint(1,13))
if new_card == 'ace':
player_new_score = player_current_score + 1
else:
player_new_score = player_current_score + new_card
if player_new_score <= 21:
return play_a_hand_player(player_new_score,dealer_show_card_index,soft=True)
else:
return play_a_hand_player(player_new_score-10,dealer_show_card_index,soft=False)
else:
new_card = id.deck(random.randint(1,13))
if new_card == 'ace':
player_new_score = player_current_score + 11
return play_a_hand_player(player_new_score,dealer_show_card_index,soft=True)
else:
player_new_score = player_current_score + new_card
return play_a_hand_player(player_new_score,dealer_show_card_index,soft=False)
#takes two cards and calculates the score
#ARGS: card1, card2 as int OR str (if card == 'ace')
#RETURN: (score as int, soft as boolean) as tuple
def make_a_score(card1,card2):
if (card1=='ace') and (card2=='ace'):
score = 12
soft = True
elif (card1=='ace'):
card1 = 11
score = card1+card2
soft = True
elif (card2=='ace'):
card2 = 11
score = card1+card2
soft = True
else:
score = card1+card2
soft = False
return (score,soft)
return_away_no_split = np.zeros((10,13))
l = 0
number_of_trials = 10000
#play number_of_trials hands ber [i][j] index and populate array
for card_total in (4,6,8,10,12,14,16,18,20,'ace'):
for j in range(13):
for x in range(number_of_trials):
total_return = 0.0
dealer_hand = [id.deck(j+1),id.deck(random.randint(1,13))]
dealer_score = id.play_a_hand_dealer(dealer_hand)
if dealer_score == 'bust':
dealer_score = 0
if card_total == 'ace':
score = 12
issoft = True
else:
score = card_total
issoft = False
if (10 in dealer_hand) and ('ace' in dealer_hand):
dealer_blackjack = True
else:
dealer_blackjack = False
player_score = play_a_hand_player(score,j,soft=issoft)
if dealer_blackjack == True:
money_return = -1.0
else:
if player_score == 0:
money_return = -1.0
elif player_score > dealer_score:
money_return = 1.0
elif player_score == dealer_score:
money_return = 0.0
else:
money_return = -1.0
total_return = money_return
return_away_no_split[l][j] += total_return
l += 1
#normalize array populate previously
return_no_split_norm = np.zeros((10,13))
r = 0
for row in return_away_no_split:
c = 0
for column in row:
return_no_split_norm[r][c] = float(float(column)/float(number_of_trials))
c += 1
r += 1
#write result to .txt file
g2 = open('no_splitting_return4.txt','w')
g2.write("Expected return after not split (rows represent player's split card (2,3,4,...), columns represent card dealer shows (ace,2,...,king)"+'\n')
for line in return_no_split_norm:
for element in line:
string = str(element)+'\t'
g2.write(string)
g2.write('\n')