-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.py
260 lines (211 loc) · 7.37 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
"""
Blackjack (a.k.a 21)
The player attempts to beat the dealer by getting a
hand value as close to 21 as possible, without going over 21.
"""
import random
import time
import cards
# Constants to identify winner of hand
TIE = 0
DEALER = 1
PLAYER = 2
# Dealer stands at 17
DEALER_MAX = 17
# The game max value for a hand
MAX = 21
# Player enters one of these two values when prompted.
HIT = 'h'
STAND = 's'
class BlackjackException(Exception):
"""Raise this exception when appropriate"""
def create_card_deck():
"""
Creates a standard deck of cards.
Each of the 4 suits has 13 cards.
Use nested FOR loops to create the cards using cards.SUITS and cards.RANKS.
(see cards.py)
:return: a standard deck of 52 cards
:rtype: list
"""
card_deck = []
for suit in cards.SUITS:
for rank in cards.RANKS:
card_deck.append(cards.Card(suit, rank))
return card_deck
def get_hand_value(hand):
"""
Computes the value of a hand using ``card.value``.
If an Ace (worth 11 by default) is present and the total hand value
exceeds the MAX, then count the Ace as 1 instead of 11.
:param hand: the cards that are held by the player or dealer
:type hand: list
:return: the value of the hand
:rtype: int
"""
number_aces = 0
hand_value_without_aces = 0
for card in hand:
if card.value != 11:
hand_value_without_aces += card.value
else:
number_aces += 1
hand_value = 0
if number_aces == 0:
hand_value = hand_value_without_aces
elif number_aces > 0:
if hand_value_without_aces < 11 - number_aces:
hand_value = hand_value_without_aces + 10 + number_aces
elif hand_value_without_aces == 11 - number_aces:
hand_value = 21
else:
hand_value = hand_value_without_aces + number_aces
return hand_value
def get_player_choice():
"""
:return: the player's decision to hit or stand.
:rtype: str
"""
user_decision = input("Do you want to hit or stand? (h/s) ").lower()
while user_decision not in (HIT, STAND):
user_decision = input("Do you want to hit or stand? (h/s) ").lower()
return user_decision
def deal_cards(deck: list, dealer_hand: list, player_hand: list):
"""
Each player is dealt two cards. Player gets the first card.
Alternate dealing between player and dealer until each has two cards.
The dealers first card needs to be dealt face down.
:param deck: the deck of cards to deal from
:param dealer_hand: an empty list representing the dealer's hand
:param player_hand: an empty list representing the player's hand
:rtype: None
"""
player_hand.append(deck.pop())
dealer_hand.append(deck.pop())
player_hand.append(deck.pop())
dealer_hand.append(deck.pop())
cards.display_cards(player_hand)
dealer_hand[1].face_down()
def dealer_turn(dealer_hand, deck, player_hand_value, delay=1):
"""
Dealer receives cards if player has not busted.
Dealer must stand at 17. Dealer must draw cards at 16 and under.
Display the value of the hand display all the dealer's cards on
after each card is dealt.
:param dealer_hand: the cards held by the dealer
:type dealer_hand: list
:param deck: the deck of cards to deal from
:type deck: list
:param player_hand_value: the value of the player's hand
:type player_hand_value: int
:param delay: a delay for visual affect
:type delay: int
:return: the value of the dealer's hand
:rtype: int
"""
hand_value = get_hand_value(dealer_hand)
print('Dealer:', hand_value)
dealer_hand[1].face_up()
cards.display_cards(dealer_hand)
time.sleep(delay)
while player_hand_value <= MAX and hand_value < DEALER_MAX:
if len(deck) != 0:
dealer_hand.append(deck.pop())
hand_value = get_hand_value(dealer_hand)
print('Dealer: ', hand_value)
cards.display_cards(dealer_hand)
else:
break
return hand_value
def player_turn(player_hand, deck):
"""
Allow the player to draw a card (hit or stand).
Return the value of the player's hand.
:param player_hand: the cards held by the player
:type player_hand: list
:param deck: the deck of cards to deal from
:type deck: list
:return: the value of the players hand
:rtype: int
"""
hand_value = get_hand_value(player_hand)
print('Player:', hand_value)
cards.display_cards(player_hand)
choice = None
if hand_value < MAX:
choice = get_player_choice()
while choice == HIT:
if len(deck) != 0:
player_hand.append(deck.pop())
hand_value = get_hand_value(player_hand)
print('Player:', hand_value)
else:
break
if hand_value <= MAX:
cards.display_cards(player_hand)
choice = get_player_choice()
elif hand_value > MAX:
cards.display_cards(player_hand)
print('BUSTED!')
break
return hand_value
def determine_winner(dealer_hand_value, player_hand_value):
"""
Returns the winner of the round based on hand value
:param dealer_hand_value: dealer's hand value
:type dealer_hand_value: int
:param player_hand_value: player's hand value
:type player_hand_value: int
:return: either TIE, DEALER or PLAYER
:rtype: int
"""
winner = None
if dealer_hand_value > MAX and player_hand_value > MAX:
raise BlackjackException
elif player_hand_value == MAX or dealer_hand_value > MAX:
winner = PLAYER
elif dealer_hand_value == MAX or player_hand_value > MAX:
winner = DEALER
elif player_hand_value == dealer_hand_value:
winner = TIE
elif dealer_hand_value < MAX and player_hand_value < MAX:
if dealer_hand_value < player_hand_value:
winner = PLAYER
else:
winner = DEALER
return winner
def main():
""" Controls the process of playing Blackjack """
deck = create_card_deck()
random.shuffle(deck)
play_again = 'y'
while play_again == 'y':
# This list holds the cards that will be dealt to the dealer
dealer_hand = []
# This list holds the cards that will be dealt to the player
player_hand = []
try:
# to the dealer and player
deal_cards(deck, dealer_hand, player_hand) # display the deal cards function
print('Dealer: ??')
cards.display_cards(dealer_hand)
player_hand_value = get_hand_value(player_hand)
dealer_hand_value = get_hand_value(dealer_hand)
player_turn(player_hand, deck)
dealer_turn(dealer_hand, deck, player_hand_value)
player_hand_value = get_hand_value(player_hand)
dealer_hand_value = get_hand_value(dealer_hand)
winner = determine_winner(dealer_hand_value, player_hand_value)
if winner == DEALER:
print("Dealer wins!")
elif winner == PLAYER:
print("Player wins!")
elif winner == TIE:
print("Tie!")
play_again = input('\nPlay again (y/n)? ')
print()
except IndexError:
print('Deck is empty. Starting over!\n')
input('Press any key to continue...')
if __name__ == '__main__':
main()