-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblackjack3.py
More file actions
272 lines (219 loc) · 7.32 KB
/
Copy pathblackjack3.py
File metadata and controls
272 lines (219 loc) · 7.32 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
import random
import sys
HEARTS = chr(9829)
DIAMONDS = chr(9830)
SPADES = chr(9824)
CLUBS = chr(9827)
def create_deck():
""" generate a 6 deck shoe of shuffled cards """
card_deck = []
for x in range(6):
for suit in (HEARTS, DIAMONDS, SPADES, CLUBS):
for rank in range(2, 11):
card_deck.append((str(rank) + str(suit)))
for face_cards in ('A', 'J', 'Q', 'K'):
card_deck.append((str(face_cards) + str(suit)))
random.shuffle(card_deck)
return card_deck
def cards_value(cards):
""" assign values to cards, aces default to 11
:param cards: cards (rank, suit)
:return: total value of all cards
"""
card_value = 0
aces = 0
for card in cards:
if card[0] in ['J', 'Q', 'K']:
card_value += 10
elif card[0:2] == '10':
card_value += 10
elif card[0] == 'A':
aces += 1
else:
card_value += int(card[0])
for ace in range(aces):
if card_value + 11 <= 21:
card_value += 11
else:
card_value += 1
return card_value
def show_hand(player, dealer, hide_dealer):
"""function to display cards, if dealer false hide one card
:param player: player cards
:param dealer: dealer cards
:param hide_dealer: hide a card or not
:return:
"""
if hide_dealer:
print("Dealer's Hand")
print("XX " + dealer[0], end=" ")
print()
else:
print("Dealer's Hand")
for card in dealer:
print(card, end=" ")
print()
print("Player's Hand")
for card in player:
print(card, end=" ")
print()
def draw(cards):
""" draws a single card and adds it to hand
:param cards:
:return: cards + one card
"""
cards += (deck.pop(),)
return cards
# @pysnooper.snoop()
def player_action(cards, wager, cash):
"""allows player to hit or stick
:param cash:
:param wager: bet
:param cards:player cards
:return: cards after draw
"""
if cards_value(cards) == 21:
return cards, wager
# checks for 9, 10 , 11 for double down, also checks for aces with default value of 11
if cards_value(cards) in (9, 10, 11) or (
cards_value(cards) in (19, 20, 21) and (cards[0][0] == 'A' or cards[1][0] == 'A')):
double = input("(D)ouble down?").upper()
if double == 'D':
if wager * 2 <= cash:
wager = int(wager) * 2
cards = draw(cards)
return cards, wager
else:
return cards, wager
cards, wager = hit_stick(cards, wager)
return cards, wager
def hit_stick(cards, wager):
while True:
action = input("(H)it, (S)tick").upper()
if action == "H":
cards = draw(cards)
show_hand(cards, dealer_cards, True)
if cards_value(cards) > 21:
return cards, wager
if action == "S":
break
return cards, wager
def dealer_action(cards):
"""dealer keeps drawing cards until total value is over 16
:param cards:
:return: dealer hand
"""
while cards_value(cards) < 16:
cards = draw(cards)
return cards
def check_win(wager, cash, total_pot, player_crds, dealer_crds):
""" checks various win conditions
:param wager: amount of bet
:param cash: total player money
:param total_pot: pot from pushing
:param player_crds:
:param dealer_crds:
:return:
"""
# player has 2 cards totaling 21 player wins 1.5 * bet
if len(player_crds) == 2 and cards_value(player_crds) == 21:
cash += int((int(wager) * 1.5)) + total_pot
total_pot = 0
print("Player wins with a natural 21\n")
return cash, total_pot
# player loses bet
elif cards_value(player_crds) > 21:
show_hand(player_crds, dealer_crds, False)
cash -= int(wager)
total_pot = 0
print("Player busted\n\n")
return cash, total_pot
# player wins bet
elif cards_value(dealer_crds) > 21:
show_hand(player_crds, dealer_crds, False)
cash += int(wager) + total_pot
total_pot = 0
print("Dealer busted\n\n")
return cash, total_pot
# player loses bet
elif cards_value(player_crds) < cards_value(dealer_crds):
show_hand(player_crds, dealer_crds, False)
cash -= int(wager)
total_pot = 0
print("Dealer wins\n\n")
return cash, total_pot
# bet should be added to pot
elif cards_value(player_crds) == cards_value(dealer_crds):
show_hand(player_crds, dealer_crds, False)
print("Player and dealer tied, pushing bet\n\n")
cash -= int(wager)
total_pot = int(wager) * 2
return cash, total_pot
# player wins bet
else:
show_hand(player_crds, dealer_crds, False)
cash += int(wager) + total_pot
total_pot = 0
print("Player wins\n\n")
return cash, total_pot
def get_bet(cash):
while True:
wager = input(f"How much do you want to wager, up to {cash}? Or (Q)uit").upper()
if wager == "Q":
print("Thanks for playing")
sys.exit()
if not wager.isdigit():
continue
wager = int(wager)
if wager <= cash:
return wager
def split_cards(cards, wager, cash, push_pot, dealer_crds):
wager = wager // 2
# Draw second card for each half of split
hand1 = (cards[0], deck.pop(), )
hand2 = (cards[1], deck.pop(), )
# process hand 1
print("Hand1")
show_hand(hand1, dealer_crds, True)
hand1, wager = hit_stick(hand1, wager)
dealer_crds = dealer_action(dealer_crds)
show_hand(hand1, dealer_crds, False)
cash, push_pot = check_win(wager, cash, push_pot, hand1, dealer_crds)
print("Hand2")
show_hand(hand2, dealer_crds, False)
hand2, wager = hit_stick(hand2, wager)
show_hand(hand2, dealer_crds, False)
cash, push_pot = check_win(wager, cash, push_pot, hand2, dealer_crds)
return cash, push_pot
if __name__ == '__main__':
money = 100
pot = 0
deck = create_deck()
while True:
if money <= 0:
print("You're broke, thanks for playing")
sys.exit()
bet = get_bet(money)
# gets first two cards for player and dealer
player_cards = deck.pop(), deck.pop()
# player_cards = ('9♠', '9♦') # for testing splits
dealer_cards = deck.pop(), deck.pop()
while True:
# display hands
show_hand(player_cards, dealer_cards, True)
print()
# if cards match offer split
if player_cards[0][0] == player_cards[1][0]:
split = input("(S)plit?").upper()
if split == 'S':
money, pot = split_cards(player_cards, bet, money, pot, dealer_cards)
break
else:
# allows player to hit or stick
player_cards, bet = player_action(player_cards, bet, money)
# dealer draws card if total card value is less than 16
dealer_cards = dealer_action(dealer_cards)
# payout
money, pot = check_win(bet, money, pot, player_cards, dealer_cards)
break
print(f"You have {money}, and there's a pot of {pot}")