-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack.py
More file actions
244 lines (209 loc) · 6.53 KB
/
blackjack.py
File metadata and controls
244 lines (209 loc) · 6.53 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
import random
import time
all_cards = [
"2S","3S","4S","5S","6S","7S","8S","9S",
"10S","JS","QS","KS","AS","2H","3H","4H",
"5H","6H","7H","8H","9H","10H","JH","QH",
"KH","AH","2C","3C","4C","5C","6C","7C",
"8C","9C","10C","JC","QC","KC","AC","2D",
"3D","4D","5D","6D","7D","8D","9D","10D",
"JD","QD","KD","AD",
]
face_cards = ["J", "Q", "K", "A"]
############ game functions
def deal_card():
# returns a random card from the deck, then removes that card from the deck
# shuffles if deck runs out of cards
global deck
if len(deck) < 1: # when the deck runs out, reshuffle
deck = all_cards
print("Shuffling deck")
card_index = random.randrange(0,len(deck))
this_card = deck[card_index]
deck.remove(this_card)
return this_card
def get_player_bet():
print("Your bank: ---> $" + str(balance))
time.sleep(0.75) # sleep
print("How much do you want to bet?")
time.sleep(0.75) # sleep
this_bet = int(raw_input())
if this_bet > balance:
print("You don't have that much. Your balance is " + str(balance))
return(get_player_bet())
print("You bet $" + str(this_bet))
time.sleep(0.75) # sleep
print("You have $" + str(balance - this_bet) + " left")
return this_bet
def offer_player_options():
print("Hit (h), Double Down (d), Stand (s)")
# print("Double down (d)")
# if player_cards[0] == player_cards[1]:
# print("Split (p)")
play = raw_input()
return play
def print_player_cards(cards):
print("Your cards: " + str(cards))
def print_dealer_cards(cards):
print("Dealer's cards: " + str(cards))
def end_round(outcome, bet):
global balance
if outcome == "lose":
print("You lose!")
balance = balance - bet
elif outcome == "win":
print("*** You win! ***")
time.sleep(0.75) # sleep
print("You won $" + str(bet) + "!")
time.sleep(0.75) # sleep
balance = balance + bet
time.sleep(0.75) # sleep
print("Starting a new round...")
time.sleep(1.5) # sleep
elif outcome == "push":
print("Push!")
else:
print("Error")
return 1
def get_card_value(card):
# returns int for value of card
this_card_value = 0
card = card[:len(card)-1]
if card in face_cards[0:3]: #J Q K
this_card_value = 10
elif card == "A":
this_card_value = 11
else:
this_card_value = card
return this_card_value
def calculate_hand_value(hand):
current_hand_value = 0
num_aces = 0
for card in hand:
this_card_value = int(get_card_value(card))
current_hand_value += this_card_value
if this_card_value == 11:
num_aces = num_aces + 1
for i in range(num_aces):
if current_hand_value > 21:
current_hand_value -= 10
return current_hand_value
def check_for_blackjack(hand):
if calculate_hand_value(hand) == 21:
return True
else:
return False
def player_turn(hand):
global bet
# offer player options
print("You have " + str(calculate_hand_value(hand)) + " against the dealer's " + str(get_card_value(dealer_cards[0])))
time.sleep(0.75) # sleep
if calculate_hand_value(hand) > 21:
print("You busted!")
time.sleep(0.75) # sleep
return calculate_hand_value(hand)
play = offer_player_options()
if play == "s":
print("You stand at " + str(calculate_hand_value(hand)))
time.sleep(0.75) # sleep
return calculate_hand_value(hand)
elif play == "h":
hand.insert(len(hand), deal_card())
print_player_cards(hand)
time.sleep(0.75) # sleep
return(player_turn(hand))
elif play == "d":
bet = bet * 2
print("Doubling down!")
time.sleep(0.75) # sleep
hand.insert(len(hand), deal_card())
time.sleep(0.75) # sleep
print_player_cards(hand)
print("You have " + str(calculate_hand_value(hand)) + " against the dealer's " + str(get_card_value(dealer_cards[0])))
time.sleep(0.75) # sleep
return calculate_hand_value(hand)
else:
print("Invalid selection, try again.")
time.sleep(0.75) # sleep
return(player_turn(hand))
def dealer_turn(dealer_cards):
print("Dealer has " + str(calculate_hand_value(dealer_cards)))
time.sleep(0.75) # sleep
print_dealer_cards(dealer_cards)
time.sleep(0.75) # sleep
while calculate_hand_value(dealer_cards) < 17:
print("Dealer hits")
dealer_cards.insert(len(dealer_cards), deal_card())
time.sleep(0.75) # sleep
print_dealer_cards(dealer_cards)
time.sleep(0.75) # sleep
print("Dealer has " + str(calculate_hand_value(dealer_cards)))
time.sleep(0.75) # sleep
if calculate_hand_value(dealer_cards) > 21:
print("Dealer busted!")
time.sleep(0.75) # sleep
return calculate_hand_value(dealer_cards)
time.sleep(0.75) # sleep
print("Dealer stands at " + str(calculate_hand_value(dealer_cards)))
time.sleep(0.75) # sleep
return calculate_hand_value(dealer_cards)
print("Welcome to Blackjack!") # only happens once
time.sleep(0.75) # sleep
deck = all_cards
balance = 100
while balance > 0: # game takes place inside this loop; play until out of $
bet = get_player_bet()
time.sleep(0.75) # sleep
# initialize empty hands each round
player_cards = list()
dealer_cards = list()
# deal 2 cards per player
player_cards.insert(len(player_cards), deal_card())
dealer_cards.insert(len(dealer_cards), deal_card())
player_cards.insert(len(player_cards), deal_card())
dealer_cards.insert(len(dealer_cards), deal_card())
print("**************")
print_player_cards(player_cards)
print("Dealer's cards: " + str(dealer_cards[0])) # keep dealer's second card hidden
print("**************")
time.sleep(1.25) # sleep
# if dealer has blackjack, player loses
if check_for_blackjack(dealer_cards):
# TODO: offer insurance
print("Dealer has blackjack!")
print_player_cards(player_cards)
print_dealer_cards(dealer_cards)
end_round("lose", bet)
# is player has blackjack, player wins
elif check_for_blackjack(player_cards):
print("You got blackjack!")
print_player_cards(player_cards)
print_dealer_cards(dealer_cards)
bet = bet * 1.5 # 3:2 payout
end_round("win", bet)
# assuming no blackjacks, now the player goes
else:
player_outcome = player_turn(player_cards) # returns the value of player's hand, or 0 for bust
if player_outcome > 21:
print("You busted!")
end_round("lose", bet)
else:
dealer_outcome = dealer_turn(dealer_cards)
if dealer_outcome > 21:
end_round("win", bet)
else:
if dealer_outcome > player_outcome:
end_round("lose", bet)
elif dealer_outcome == player_outcome:
end_round("push", bet)
elif dealer_outcome < player_outcome:
end_round("win", bet)
else:
print("Error - scores don't make sense!")
print("You're out of money!")
time.sleep(0.75) # sleep
print("Just like Vegas...")
time.sleep(0.75) # sleep
print("Time to go home to your shitty hotel room!")
time.sleep(0.75) # sleep
print("GAME OVER")