-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblackjack.py
More file actions
134 lines (123 loc) · 3.73 KB
/
blackjack.py
File metadata and controls
134 lines (123 loc) · 3.73 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
import random
import time
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
def deal(deck):
hand = []
for i in range(2):
random.shuffle(deck)
card = deck.pop()
if card == 11:card = "J"
if card == 12:card = "Q"
if card == 13:card = "K"
if card == 14:card = "A"
hand.append(card)
return hand
def play_again():
time.sleep(1)
again = input("Do you want to play again? (Y/N) : ").lower()
if again == "y":
dealer_hand = []
player_hand = []
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
game()
else:
print("Bye!")
time.sleep(1)
exit()
def total(hand):
total = 0
for card in hand:
if card == "J" or card == "Q" or card == "K":
total+= 10
elif card == "A":
if total >= 11: total+= 1
else: total+= 11
else: total += card
return total
def hit(hand):
card = deck.pop()
if card == 11:card = "J"
if card == 12:card = "Q"
if card == 13:card = "K"
if card == 14:card = "A"
hand.append(card)
return hand
def print_results(dealer_hand, player_hand):
print ("The dealer has a " + str(dealer_hand) + " for a total of " + str(total(dealer_hand)))
time.sleep(1)
print ("You have a " + str(player_hand) + " for a total of " + str(total(player_hand)))
def blackjack(dealer_hand, player_hand):
if total(player_hand) == 21:
print_results(dealer_hand, player_hand)
time.sleep(1)
print ("Congratulations! You got a Blackjack!\n")
time.sleep(1)
play_again()
elif total(dealer_hand) == 21:
print_results(dealer_hand, player_hand)
time.sleep(1)
print ("Sorry, you lose. The dealer got a blackjack.\n")
time.sleep(1)
play_again()
def both(dealer_hand, play_again):
if total(player_hand) >=22 and total(dealer_hand) >=22:
print_results(dealer_hand, player_hand)
time.sleep(1)
print("Both you and the dealer bust!")
def score(dealer_hand, player_hand):
if total(player_hand) == 21:
print_results(dealer_hand, player_hand)
time.sleep(1)
print ("Congratulations! You got a Blackjack!\n")
elif total(player_hand) >=22 and total(dealer_hand) >=22:
print_results(dealer_hand, player_hand)
time.sleep(1)
print("Both you and the dealer bust!\n")
elif total(dealer_hand) == 21:
print_results(dealer_hand, player_hand)
time.sleep(1)
print ("Sorry, you lose. The dealer got a blackjack.\n")
elif total(player_hand) > 21:
print_results(dealer_hand, player_hand)
time.sleep(1)
print ("Sorry. You busted. You lose.\n")
elif total(dealer_hand) > 21:
print_results(dealer_hand, player_hand)
time.sleep(1)
print ("Dealer busts. You win!\n")
elif total(player_hand) < total(dealer_hand):
print_results(dealer_hand, player_hand)
time.sleep(1)
print("Sorry. Your score isn't higher than the dealer. You lose.\n")
elif total(player_hand) > total(dealer_hand):
print_results(dealer_hand, player_hand)
time.sleep(1)
print ("Congratulations. Your score is higher than the dealer. You win!\n")
def game():
choice = 0
print ("WELCOME TO BLACKJACK!\n")
dealer_hand = deal(deck)
player_hand = deal(deck)
while choice != "q":
print ("The dealer is showing a " + str(dealer_hand[0]))
time.sleep(1)
print ("You have a " + str(player_hand) + " for a total of " + str(total(player_hand)))
blackjack(dealer_hand, player_hand)
time.sleep(1)
choice = input("Do you want to [H]it, [S]tand, or [F]old: ").lower()
if choice == "h":
hit(player_hand)
while total(dealer_hand) < 17:
hit(dealer_hand)
score(dealer_hand, player_hand)
play_again()
elif choice == "s":
while total(dealer_hand) < 17:
hit(dealer_hand)
score(dealer_hand, player_hand)
play_again()
elif choice == "f":
print ("Bye!")
exit()
if __name__ == "__main__":
game()