-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackjack.py
More file actions
303 lines (256 loc) · 6.75 KB
/
Copy pathBlackjack.py
File metadata and controls
303 lines (256 loc) · 6.75 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
import sys
import math
import random
wallet = 100
bets = []
values = {
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'10': 10,
'J': 10,
'Q': 10,
'K': 10,
'A': 1
}
cards = list(values.keys())
dealerHand = []
playerHands = [[], []]
didSplit = False
currentHand = 0
def init():
global dealerHand
global playerHands
global didSplit
global currentHand
dealerHand = []
playerHands = [[], []]
didSplit = False
currentHand = 0
playerHands[0].append(random.choice(cards))
dealerHand.append(random.choice(cards))
playerHands[0].append(random.choice(cards))
dealerHand.append(random.choice(cards))
if len(dealerHand) != 2 or len(playerHands[0]) != 2:
init()
def fullInit():
global wallet
global bets
bets = [0]
while bets[0] <= 0:
print("Your current balance is {}".format(wallet))
response = input("Place your bet: ")
if response.isdigit():
numIn = int(response)
if numIn > wallet:
print("Your bet can't exceed your balance\n")
elif numIn <= 0:
print("You can't bet nothing\n")
else:
bets[0] = numIn
else:
print("Please type a whole number\n")
init()
def calcHand(hand):
total = 0
aces = 0
for card in hand:
total += values[card]
if card == 'A':
aces += 1
for ace in range(aces):
if (total + 10) <= 21:
total += 10
return total
def calcCurrent():
return calcHand(playerHands[currentHand])
def checkBust():
if calcCurrent() > 21:
print("Your hand busted\n")
return True
else:
return False
def playDealer():
global bets
global dealerHand
global playerHands
global didSplit
while calcHand(dealerHand) < 17:
dealerHand.append(random.choice(cards))
dealerVal = calcHand(dealerHand)
playerVal = calcHand(playerHands[0])
net = 0
if dealerVal == 21:
if playerVal != 21:
net -= bets[0]
if didSplit and calcHand(playerHands[1]) != 21:
net -= bets[1]
elif dealerVal > 21:
if playerVal == 21:
net += math.ceil(1.5 * bets[0])
elif playerVal < 21:
net += bets[0]
else:
net -= bets[0]
if didSplit:
playerVal2 = calcHand(playerHands[1])
if playerVal2 == 21:
net += math.ceil(1.5 * bets[1])
elif playerVal2 < 21:
net += bets[1]
else:
net -= bets[1]
else:
if playerVal == 21:
net += math.ceil(1.5 * bets[0])
elif playerVal > 21 or playerVal < dealerVal:
net -= bets[0]
elif playerVal > dealerVal:
net += bets[0]
if didSplit:
playerVal2 = calcHand(playerHands[1])
if playerVal2 == 21:
net += math.ceil(1.5 * bets[1])
elif playerVal2 > 21 or playerVal2 < dealerVal:
net -= bets[1]
elif playerVal2 > dealerVal:
net += bets[1]
endRound(net)
def loseHand():
global bets
global dealerHand
global playerHands
global didSplit
net = 0
if didSplit:
if calcHand(playerHands[0]) > 21:
net -= bets[0]
net -= bets[1]
endRound(net)
else:
playDealer()
else:
net -= bets[0]
endRound(net)
def endRound(net):
global wallet
global bets
global dealerHand
global playerHands
global didSplit
print("Dealer: {}".format(", ".join(dealerHand)))
if didSplit:
print("Hand 1: {}".format(", ".join(playerHands[0])))
print("Bet: {}".format(bets[0]))
print("Hand 2: {}".format(", ".join(playerHands[1])))
print("Bet: {}".format(bets[1]))
else:
print("Player: {}".format(", ".join(playerHands[0])))
print("Bet: {}".format(bets[0]))
wallet += net
if net == 0:
print("No change")
elif net > 0:
print("You won {}!".format(net))
else:
print("You lost {}...".format(-net))
if input("Play again? (y): ").lower() == 'y':
fullInit()
else:
sys.exit()
def stand():
global didSplit
global currentHand
if didSplit and currentHand == 0:
currentHand = 1
else:
playDealer()
def hit():
global playerHands
global didSplit
global currentHand
playerHands[currentHand].append(random.choice(cards))
if checkBust():
if didSplit and currentHand == 0:
currentHand = 1
else:
loseHand()
def double():
global bets
global playerHands
global didSplit
global currentHand
bets[currentHand] *= 2
playerHands[currentHand].append(random.choice(cards))
if checkBust():
if didSplit and currentHand == 0:
currentHand = 1
else:
loseHand()
else:
playDealer()
def split():
global bets
global playerHands
global didSplit
if not didSplit and len(playerHands[0]) == 2 and playerHands[0][0] == playerHands[0][1]:
didSplit = True
bets.append(bets[0])
playerHands[1] += playerHands[0].pop()
playerHands[0].append(random.choice(cards))
playerHands[1].append(random.choice(cards))
else:
print("Split unavailable\n")
def surrender():
global bets
global playerHands
global didSplit
if not didSplit and len(playerHands[0]) == 2:
bets[0] //= 2
loseHand()
else:
print("Surrender unavailable\n")
def showValue():
print("Current value: {}\n".format(calcCurrent()))
def swapHand():
global currentHand
currentHand = 1 if currentHand == 0 else 0
def reset():
init()
actionDict = {
's': stand,
'stand': stand,
'h': hit,
'hit': hit,
'd': double,
'double': double,
'double down': double,
'p': split,
'split': split,
'u': surrender,
'surrender': surrender,
'c': showValue,
'calc': showValue,
'calculate': showValue
}
def main():
fullInit()
while True:
print("Bet: {}".format(bets[currentHand]))
print("Dealer: #, {}".format(dealerHand[1]))
if didSplit:
print("Hand {}: {}".format(currentHand + 1, ", ".join(playerHands[currentHand])))
else:
print("Player: {}".format(", ".join(playerHands[0])))
print("Actions: (s)tand, (h)it, (d)ouble down, s(p)lit, s(u)rrender")
action = input().lower()
if action in actionDict:
actionDict[action]()
else:
print("Invalid action\n")
main()