1+ import os
2+ import random
3+ import time
4+
5+ deck = [2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ]* 4
6+
7+ def deal (deck ):
8+ hand = []
9+ for i in range (2 ):
10+ random .shuffle (deck )
11+ card = deck .pop ()
12+ if card == 11 :card = "J"
13+ if card == 12 :card = "Q"
14+ if card == 13 :card = "K"
15+ if card == 14 :card = "A"
16+ hand .append (card )
17+ return hand
18+
19+ def play_again ():
20+ time .sleep (1 )
21+ again = input ("Do you want to play again? (Y/N) : " ).lower ()
22+ if again == "y" :
23+ dealer_hand = []
24+ player_hand = []
25+ deck = [2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ]* 4
26+ game ()
27+ else :
28+ print ("Bye!" )
29+ time .sleep (1 )
30+ exit ()
31+
32+ def total (hand ):
33+ total = 0
34+ for card in hand :
35+ if card == "J" or card == "Q" or card == "K" :
36+ total += 10
37+ elif card == "A" :
38+ if total >= 11 : total += 1
39+ else : total += 11
40+ else : total += card
41+ return total
42+
43+ def hit (hand ):
44+ card = deck .pop ()
45+ if card == 11 :card = "J"
46+ if card == 12 :card = "Q"
47+ if card == 13 :card = "K"
48+ if card == 14 :card = "A"
49+ hand .append (card )
50+ return hand
51+
52+ def clear ():
53+ if os .name == 'nt' :
54+ os .system ('CLS' )
55+ if os .name == 'posix' :
56+ os .system ('clear' )
57+
58+ def print_results (dealer_hand , player_hand ):
59+ clear ()
60+ print ("The dealer has a " + str (dealer_hand ) + " for a total of " + str (total (dealer_hand )))
61+ time .sleep (1 )
62+ print ("You have a " + str (player_hand ) + " for a total of " + str (total (player_hand )))
63+
64+ def blackjack (dealer_hand , player_hand ):
65+ if total (player_hand ) == 21 :
66+ print_results (dealer_hand , player_hand )
67+ print ("Congratulations! You got a Blackjack!\n " )
68+ play_again ()
69+ elif total (dealer_hand ) == 21 :
70+ print_results (dealer_hand , player_hand )
71+ print ("Sorry, you lose. The dealer got a blackjack.\n " )
72+ play_again ()
73+
74+ def score (dealer_hand , player_hand ):
75+ if total (player_hand ) == 21 :
76+ print_results (dealer_hand , player_hand )
77+ print ("Congratulations! You got a Blackjack!\n " )
78+ elif total (dealer_hand ) == 21 :
79+ print_results (dealer_hand , player_hand )
80+ print ("Sorry, you lose. The dealer got a blackjack.\n " )
81+ elif total (player_hand ) > 21 :
82+ print_results (dealer_hand , player_hand )
83+ print ("Sorry. You busted. You lose.\n " )
84+ elif total (dealer_hand ) > 21 :
85+ print_results (dealer_hand , player_hand )
86+ print ("Dealer busts. You win!\n " )
87+ elif total (player_hand ) < total (dealer_hand ):
88+ print_results (dealer_hand , player_hand )
89+ print ("Sorry. Your score isn't higher than the dealer. You lose.\n " )
90+ elif total (player_hand ) > total (dealer_hand ):
91+ print_results (dealer_hand , player_hand )
92+ print ("Congratulations. Your score is higher than the dealer. You win\n " )
93+
94+ def game ():
95+ choice = 0
96+ clear ()
97+ print ("WELCOME TO BLACKJACK!\n " )
98+ dealer_hand = deal (deck )
99+ player_hand = deal (deck )
100+ while choice != "q" :
101+ print ("The dealer is showing a " + str (dealer_hand [0 ]))
102+ time .sleep (1 )
103+ print ("You have a " + str (player_hand ) + " for a total of " + str (total (player_hand )))
104+ blackjack (dealer_hand , player_hand )
105+ time .sleep (1 )
106+ choice = input ("Do you want to [H]it, [S]tand, or [F]old: " ).lower ()
107+ clear ()
108+ if choice == "h" :
109+ hit (player_hand )
110+ while total (dealer_hand ) < 17 :
111+ hit (dealer_hand )
112+ score (dealer_hand , player_hand )
113+ play_again ()
114+ elif choice == "s" :
115+ while total (dealer_hand ) < 17 :
116+ hit (dealer_hand )
117+ score (dealer_hand , player_hand )
118+ play_again ()
119+ elif choice == "f" :
120+ print ("Bye!" )
121+ exit ()
122+
123+ if __name__ == "__main__" :
124+ game ()
0 commit comments