-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackJack.py
More file actions
170 lines (154 loc) · 5.14 KB
/
Copy pathBlackJack.py
File metadata and controls
170 lines (154 loc) · 5.14 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
import random, sys
HEARTS = chr(9829)
DIAMONDS = chr(9830)
SPADES = chr(9824)
CLUBS = chr(9827)
BACKSIDE = 'backside'
def main():
print('''
Blackjack, by sinister geek
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 throught 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("Your're broke!")
print("Good thing weren't playing wiuth real monmey.")
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 losts!')
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()