-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.py
281 lines (226 loc) · 8.09 KB
/
blackjack.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
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
"""
Game to play BlackJack against an AI dealer on the command line.
"""
import os
import random
# Variables used to create, display and assess deck of cards
suits = {'\u2660', # Hearts
'\u2661', # Spades
'\u2662', # Clubs
'\u2663', # Diamonds
}
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7,
'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}
short_ranks = {'Two': '2', 'Three': '3', 'Four': '4', 'Five': '5', 'Six': '6', 'Seven': '7',
'Eight': '8', 'Nine': '9', 'Ten': 'T', 'Jack': 'J', 'Queen': 'Q', 'King': 'K', 'Ace': 'A'}
class Card():
""" Card class to play game with """
def __init__(self, suit, rank):
# suit, rank
self.suit = suit
self.rank = rank
self.value = values[rank]
self.short_rank = short_ranks[rank]
def __str__(self):
return self.short_rank + self.suit
class Deck():
""" Deck class to deal cards from """
def __init__(self):
self.all_cards = []
# generates deck object
for suit in suits:
for rank in ranks:
# creates card objects
created_card = Card(suit, rank)
self.all_cards.append(created_card)
def shuffle(self):
random.shuffle(self.all_cards)
def deal_one(self):
return self.all_cards.pop(0)
class Player():
""" Play class for user and dealer """
def __init__(self, name, bankroll):
self.name = name
self.bankroll = bankroll
self.stake = 0
self.hand = []
def play_card(self):
return self.hand.pop(0)
def take_cards(self, cards):
try:
# for multiple cards
self.hand.extend(cards)
except:
# for single card
self.hand.append(cards)
def score(self):
return blackjack_score(self.hand)
def __str__(self):
return f'Player {self.name} has ${self.bankroll:.2f} chips.'
"""
Main logic to play Blackjack
"""
def main():
print('Welcome to BlackJack.')
active_player = True
while active_player:
# set up game for new player
player_name = input('Enter your name: ')
player_bankroll = num_input('What is your balance (in dollars)? ')
player = Player(player_name, player_bankroll)
dealer = Player("Dealer", 0)
play_blackjack = get_user_choice('Ready to play BlackJack?')
# play a hand of blackjack
while play_blackjack:
# hand set up
dealer.hand = []
player.hand = []
game_deck = Deck()
game_deck.shuffle()
show_board(player)
print("Card's are shuffled.")
# place bet
stake = num_input("How much do you want to bet (in dollars)?",
player.bankroll)
player.bankroll -= stake
# deal cards
for _ in range(2):
player.take_cards(game_deck.deal_one())
dealer.take_cards(game_deck.deal_one())
dealer_seen = [dealer.hand[0], "??"]
show_board(player, dealer_seen, stake, player.hand)
print("Cards are dealt.")
# check for blackjack
if player.score() == dealer.score() == 21:
show_board(player, dealer.hand, stake, player.hand)
print("You and the dealer have BLACKJACK!")
elif dealer.score() == 21:
show_board(player, dealer.hand, stake, player.hand)
print("Dealer has BLACKJACK!")
elif player.score() == 21:
show_board(player, dealer_seen, stake, player.hand)
print("You have BLACKJACK!")
# when no blackjack
else:
# player actions
print(f"Your score is {player.score()}")
extra_card = get_user_choice("Hit or stick?", "H", "S")
while extra_card:
player.take_cards(game_deck.deal_one())
show_board(player, dealer_seen, stake, player.hand)
if player.score() > 21:
print("BUST")
break
print(f"Your score is {player.score()}")
extra_card = get_user_choice("Hit or stick?", "H", "S")
# if player not bust
if player.score() <= 21:
# dealer actions
show_board(player, dealer.hand, stake, player.hand)
print(f"Dealer turns over {dealer.hand[1]}.")
print(f"Dealer's score is {dealer.score()}.")
while dealer.score() < 17:
print("Dealer will take another card")
input("Press enter to continue...")
dealer.take_cards(game_deck.deal_one())
show_board(player, dealer.hand, stake, player.hand)
print(f"Dealer's score is {dealer.score()}.")
if dealer.score() > 21:
print("BUST")
# payout
dealer_score = dealer.score()
player_score = player.score()
if dealer_score == player_score:
print(f"It's a tie. Take back your stake of ${stake:.2f}.")
player.bankroll += stake
elif len(player.hand) == 2 and player_score == 21:
print(f"You win ${stake * 3/2:.2f}!")
player.bankroll += stake * 5/2
elif (dealer_score < player_score <= 21 or
player_score <= 21 < dealer_score):
print(f"You win ${stake:.2f}!")
player.bankroll += stake * 2
else:
print("You lose.")
print(f"Your bankroll is ${player.bankroll:.2f}.")
# play again?
if player.bankroll == 0:
print("You have no money! Please leave the table.")
play_blackjack = False
else:
play_blackjack = get_user_choice('Play another hand?')
# another player?
active_player = get_user_choice('Anyone else want to play?')
print('Goodbye.')
"""
Getting user input
"""
def get_user_choice(question, option1='Y', option2='N'):
answer = None
while True:
answer = input(f'{question} ({option1}/{option2}): ').lower()
if answer == option1.lower():
return True
elif answer == option2.lower():
return False
else:
print(f"Type '{option1}' or '{option2}'.")
def num_input(question, cap=0):
while True:
answer = input(f"{question} ")
try:
answer = float(answer)
except:
print("Enter a number in digits.")
continue
if cap == 0 or cap >= answer:
return answer
else:
print(f"Number cannot exceed {cap}. Try again.")
"""
Display functions
"""
def show_hand(cards):
hand = ''
top = ''
bottom = ''
space = ''
for card in cards:
hand += f'| {str(card)} | '
top += ' ____ '
bottom += '|____| '
space += '| | '
print(top)
print(space)
print(hand)
print(bottom)
def show_board(player, dealer_hand=[], stake=0, player_hand=[]):
os.system('cls' if os.name == 'nt' else 'clear')
print("-" * 50)
print("Dealer's hand")
show_hand(dealer_hand)
print('')
if stake:
print(f"Stake ${stake:.2f}")
else:
print('')
show_hand(player_hand)
print(f"{player.name}'s hand")
print(f"Stack ${player.bankroll:.2f}")
print("-" * 50)
"""
Assessing score
"""
def blackjack_score(hand):
score = 0
for card in hand:
score += card.value
num_aces = len([card for card in hand if card.rank == "Ace"])
while score > 21 and num_aces:
num_aces -= 1
score -= 10
return score
if __name__ == '__main__':
main()