-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackjack
More file actions
173 lines (118 loc) · 5.18 KB
/
Copy pathBlackjack
File metadata and controls
173 lines (118 loc) · 5.18 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
171
172
import random
from blackjackart import logo
print(logo)
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
def user_score_calc(user_deck):
""" Calculates user score"""
user_score = sum(user_deck)
return user_score
def dealer_score_calc(dealer_deck):
""" Calculates computer score"""
dealer_score = sum(dealer_deck)
return dealer_score
def draw_dealer(dealer_deck):
""" Draws random cards for computer"""
dealer_deck += random.sample(cards,1)
while sum(dealer_deck) < 17:
dealer_deck += random.sample(cards,1)
return dealer_deck
return dealer_deck
def draw_user(user_deck):
""" Draws random cards for user"""
user_deck += random.sample(cards,1)
if sum(user_deck) > 21:
for ace in range(len(user_deck)):
if user_deck[ace] == 11:
user_deck [ace] = user_deck[ace] = 1
return user_deck
return user_deck
def replay():
""" Function for asking user if they want to play"""
ask_user = input("Do you want to play a game of Blackjack? Type 'y' or 'n': ")
return ask_user
def start():
""" Function to play Blackjack"""
user_deck = []
dealer_deck =[]
ask_user = ''
user_deck = random.sample(cards,2)
dealer_deck = random.sample(cards,2)
play = True
if play == True:
ask_user = replay()
if ask_user == 'n':
play = False
while play is True:
user_score = user_score_calc(user_deck)
dealer_score = dealer_score_calc(dealer_deck)
print(f"Your cards: {user_deck}, current score: {user_score}")
print(f"Computer's first card: {dealer_deck[0]}")
if user_score == 21 and len(user_deck) == 2 and dealer_score != 21:
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("Win with a Blackjack 😎")
play = False
break
if dealer_score == 21 and len(dealer_deck) == 2:
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("Lose, opponent has Blackjack 😱")
play = False
break
if user_score != 21 and dealer_score != 21 and user_score == dealer_score:
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("Draw 🙃")
play = False
break
if user_score > dealer_score and dealer_score> 17: #NEW CODE
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("You win 😃")
if play == True and sum(user_deck) != 21 or sum(dealer_deck) != 21:
draw_again = input("Type 'y' to get another card, type 'n' to pass: ")
if draw_again == 'y' and user_score < 21:
user_deck = draw_user(user_deck)
user_score = user_score_calc(user_deck)
if user_score > 21:
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("You went over. You lose 😭")
play = False
break
if user_score == 21:
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("Win with a Blackjack 😎")
play = False
break
if draw_again == 'n' and dealer_score < 17:
dealer_deck = draw_dealer(dealer_deck)
dealer_score = dealer_score_calc(dealer_deck)
if user_score != 21 and dealer_score != 21 and dealer_score == user_score:
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("Draw 🙃")
play = False
break
if draw_again == 'n' and user_score < 21 and dealer_score > 21:
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("Opponent went over. You win 😁")
play = False
break
if draw_again == 'n' and dealer_score < 21:
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("You lose 😤")
play = False
break
if draw_again == 'n' and dealer_score == 21:
print(f"Your final hand: {user_deck}, final score: {user_score}")
print(f"Computer's final hand: {dealer_deck}, final score: {dealer_score}")
print("Lose, opponent has Blackjack 😱")
play = False
break
if ask_user == 'y':
start()
start()