-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.py
212 lines (163 loc) · 6.01 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
import random, sys
HEARTS = chr(9829)
DIAMONDS = chr(9830)
SPADES = chr(9824)
CLUBS = chr(9827)
BACKSIDE = 'backside'
def main():
print(
'''
Rules:
Try to get as close to 21 without going over.
Kings, Queens, and Jacks are worth 10 points.
Aces are worth 1 or 11 points,
Cards 2 through 10 are worth their face value.
(H)it to take another card.
(S)tand to stop taking cards.
On your first play, you can (D)ouble down to increase your bet but must hit exactly one more time before standing.
In case of a tie, the bet is returned to the player.
The dealer stops hitting at 17.
'''
)
money = 5000
while True:
if money <= 0:
print("You're broke!")
print("Good thing you weren't playing with real money.")
print('Thanks for playing!')
sys.exit()
print('Money:', money)
bet = getBet(money)
deck = getDeck()
dealerHand = [deck.pop(), deck.pop()]
playerHand = [deck.pop(), deck.pop()]
print('Bet:', bet)
while True:
displayHands(playerHand, dealerHand, False)
print()
if getHandValue(playerHand) > 21:
break
move = getMove(playerHand, money - bet)
if move == 'D':
additionalBet = getBet(min(bet, (money - bet)))
bet += additionalBet
print('Bet increased to {}.'.format(bet))
print('Bet:', bet)
if move in ('H', 'D'):
newCard = deck.pop()
rank, suit, = newCard
print('You drew a {} of {}.'.format(rank, suit))
playerHand.append(newCard)
if getHandValue(playerHand) > 21:
continue
if move in ('S', 'D'):
break
if getHandValue(playerHand) <= 21:
while getHandValue(dealerHand) < 17:
print('Dealer hits...')
dealerHand.append(deck.pop())
displayHands(playerHand, dealerHand, False)
if getHandValue(dealerHand) > 21:
break
input('Press Enter to continue')
print('\n\n')
displayHands(playerHand, dealerHand, True)
playerValue = getHandValue(playerHand)
dealerValue = getHandValue(dealerHand)
if dealerValue > 21:
print('Dealer busts! You win ${}!'.format(bet))
money += bet
elif (playerValue > 21) or (playerValue < dealerValue):
print('You lost')
money -= bet
elif playerValue > dealerValue:
print('You won ${}!'.format(bet))
money += bet
elif playerValue == dealerValue:
print('It\'s a tie, the bet is returned to you.')
input('Press Enter to continue...')
print('\n\n')
def getBet(maxBet):
while True:
print('How much do you bet? (1-{}, or QUIT)'.format(maxBet))
bet = input('> ').upper().strip()
if bet == 'QUIT':
print('Thanks for playing!')
sys.exit()
if not bet.isdecimal():
continue
bet = int(bet)
if 1 <= bet <= maxBet:
return bet
def getDeck():
deck = []
for suit in (HEARTS, DIAMONDS, SPADES, CLUBS):
for rank in range(2, 11):
deck.append((str(rank), suit))
for rank in ('J', 'Q', 'K', 'A'):
deck.append((rank, suit))
random.shuffle(deck)
return deck
def displayHands(playerHand, dealerHand, showDealerHand):
print()
if showDealerHand:
print('DEALER:', getHandValue(dealerHand))
displayCards(dealerHand)
else:
print('DEALER: ???')
displayCards([BACKSIDE] + dealerHand[1:])
print('PLAYER:', getHandValue(playerHand))
displayCards(playerHand)
def getHandValue(cards):
value = 0
numberOfAces = 0
for card in cards:
rank = card[0]
if rank == 'A':
numberOfAces += 1
elif rank in ('K', 'Q', 'J'):
value += 10
else:
value += int(rank)
value += numberOfAces
for i in range(numberOfAces):
if value + 10 <= 21:
value += 10
return value
def displayCards(cards):
rows = ['', '', '', '', '']
for i, card in enumerate(cards):
rows[0] += ' ___ '
if card == BACKSIDE:
rows[1] += '|## | '
rows[2] += '|###| '
rows[3] += '|_##| '
else:
rank, suit = card
rows[1] += '|{} | '.format(rank.ljust(2))
rows[2] += '| {} | '.format(suit)
rows[3] += '|_{}| '.format(rank.rjust(2, '_'))
for row in rows:
print(row)
def getMove(playerHand, money):
while True:
moves = ['(H)it', '(S)tand']
if len(playerHand) == 2 and money > 0:
moves.append('(D)ouble down')
movePrompt = ', '.join(moves) + '> '
move = input(movePrompt).upper()
if move in ('H', 'S'):
return move
if move == 'D' and '(D)ouble down' in moves:
return move
if __name__ == '__main__':
main()
'''Exploring the Program
How does the program represent a single card?
If the player chooses to Hit or Double, a newCard variable is made that is used to pop one more card from the deck and append it to the player hand. If the player's hand is less than or equal to a value of 21, the dealer will hit and a new card from the deck is appended to the dealer hand.
How does the program represent a hand of cards?
A hand of cards is represented by the variables dealerHand and playerHand. These variables pop two cards from the card deck to give to the dealer and player, respectively.
What do each of the strings in the rows list represent?
The characters that make up each row
What happens when showDealerHand in the displayHands() function is set to True? What happens when it is False?
'''