-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlackjack.py
146 lines (129 loc) · 3.54 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
from Card import Card
playerhand=[]
dealerhand=[]
money=1000
def intro():
'''
Checks whether the user wants to play and what they wish to bet
'''
global money
print('Do you want to play blackjack?')
try:
ans = input()
ans=ans.strip()
ans = ans.upper()
except KeyboardInterrupt:
quit()
except:
print('An error occured try again')
intro()
if ans[0]=='Y':
while True:
try:
bet = int(input(f'How much do you want to bet you have ${money}?'))
if bet<=0:
print('That\'s too low')
continue
elif bet>money:
print('You can\'t bet that much it\'s too high')
continue
else:
money-=bet
print(f'Money has been changed to {money}')
except KeyboardInterrupt:
exit()
except:
print('An error occured. Please try again')
game()
else:
print('Have a good day!')
exit()
def makecards():
'''
Initailizes the dealer and player\'s hands with cards at the begining of the game
'''
global playerhand,dealerhand,money
playerhand.append(Card())
playerhand.append(Card())
dealerhand.append(Card())
if(Card.sum(playerhand)>21):
playerhand=[]
dealerhand=[]
makecards()
elif Card.sum(playerhand)==21:
print(f'You won!')
showhands()
money+=(1000-money)*2
print(f'Money has been changed to {money}')
intro()
def showhands():
'''
Shows the cards the dealer and player hold and the total value to the hands of both players
'''
for card in dealerhand:
print(card)
print(f'The sum of the dealer\'s hand is {Card.sum(dealerhand)}')
for card in playerhand:
print(card)
print(f'The sum of the the player\'s hand is {Card.sum(playerhand)}')
def hitstay():
'''
Checks to see if the player wants to hit or stay and adds cards to the appropriate hand
'''
global playerhand,dealerhand
print('Do you want to hit or stay?')
try:
ans = input()
ans=ans.upper()
except KeyboardInterrupt:
quit()
except:
print('An error occured. Please try again')
hitstay()
if ans[0]=='H':
playerhand.append(Card())
elif ans[0]=='S':
dealerhand.append(Card())
else:
print('That was not a option. Please enter \'Hit\' or \'Stay\'')
hitstay()
def checkwin():
'''
Checks to see if someone has won or lost
'''
global playerhand,dealerhand,money
if Card.sum(playerhand)==21:
print('Congrats you won')
money+=(1000-money)*2
print(f'Money has been changed to {money}')
dealerhand=[]
playerhand=[]
intro()
elif Card.sum(dealerhand)==21:
print('The dealer won!')
dealerhand=[]
playerhand=[]
intro()
elif Card.sum(playerhand)>21:
print('You went over 21 and lost!')
dealerhand=[]
playerhand=[]
intro()
elif Card.sum(dealerhand)>21:
print('Dealer Bust')
money+=(1000-money)*2
print(f'Money has been changed to {money}')
dealerhand=[]
playerhand=[]
intro()
def game():
'''
Runs the main game loop
'''
makecards()
while True:
showhands()
hitstay()
checkwin()
if __name__ == '__main__':
intro()