|
4 | 4 |
|
5 | 5 | deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] * 4 |
6 | 6 |
|
7 | | -random.shuffle(deck) |
8 | | - |
9 | | -print( |
10 | | - " ********************************************************** " |
11 | | -) |
12 | | -print( |
13 | | - " Welcome to the game Casino - BLACK JACK ! " |
14 | | -) |
15 | | -print( |
16 | | - " ********************************************************** " |
17 | | -) |
18 | | - |
19 | | -d_cards = [] # Initialising dealer's cards |
20 | | -p_cards = [] # Initialising player's cards |
21 | | - |
22 | | -while len(d_cards) != 2: |
23 | | - random.shuffle(deck) |
24 | | - d_cards.append(deck.pop()) |
25 | | - if len(d_cards) == 2: |
26 | | - print("The cards dealer has are X ", d_cards[1]) |
27 | | - |
28 | | -# Displaying the Player's cards |
29 | | -while len(p_cards) != 2: |
30 | | - random.shuffle(deck) |
31 | | - p_cards.append(deck.pop()) |
32 | | - if len(p_cards) == 2: |
33 | | - print("The total of player is ", sum(p_cards)) |
34 | | - print("The cards Player has are ", p_cards) |
35 | | - |
36 | | -if sum(p_cards) > 21: |
37 | | - print("You are BUSTED !\n **************Dealer Wins !!******************\n") |
38 | | - exit() |
39 | 7 |
|
40 | | -if sum(d_cards) > 21: |
| 8 | +def welcome(): |
| 9 | + print( |
| 10 | + " ********************************************************** " |
| 11 | + ) |
| 12 | + print( |
| 13 | + " Welcome to the game Casino - BLACK JACK ! " |
| 14 | + ) |
41 | 15 | print( |
42 | | - "Dealer is BUSTED !\n ************** You are the Winner !!******************\n" |
| 16 | + " ********************************************************** " |
43 | 17 | ) |
44 | | - exit() |
45 | 18 |
|
46 | | -if sum(d_cards) == 21: |
47 | | - print("***********************Dealer is the Winner !!******************") |
48 | | - exit() |
49 | 19 |
|
50 | | -if sum(d_cards) == 21 and sum(p_cards) == 21: |
51 | | - print("*****************The match is tie !!*************************") |
52 | | - exit() |
| 20 | +def start_game(): |
| 21 | + random.shuffle(deck) |
53 | 22 |
|
| 23 | + d_cards = [] |
| 24 | + p_cards = [] |
54 | 25 |
|
55 | | -def dealer_choice(): |
56 | | - if sum(d_cards) < 17: |
57 | | - while sum(d_cards) < 17: |
58 | | - random.shuffle(deck) |
59 | | - d_cards.append(deck.pop()) |
| 26 | + # Dealer initial cards |
| 27 | + while len(d_cards) != 2: |
| 28 | + random.shuffle(deck) |
| 29 | + d_cards.append(deck.pop()) |
| 30 | + if len(d_cards) == 2: |
| 31 | + print("The cards dealer has are X ", d_cards[1]) |
| 32 | + |
| 33 | + # Player initial cards |
| 34 | + while len(p_cards) != 2: |
| 35 | + random.shuffle(deck) |
| 36 | + p_cards.append(deck.pop()) |
| 37 | + if len(p_cards) == 2: |
| 38 | + print("The total of player is ", sum(p_cards)) |
| 39 | + print("The cards Player has are ", p_cards) |
60 | 40 |
|
61 | | - print("Dealer has total " + str(sum(d_cards)) + "with the cards ", d_cards) |
| 41 | + if sum(p_cards) > 21: |
| 42 | + print("You are BUSTED !\n **************Dealer Wins !!******************\n") |
| 43 | + return |
62 | 44 |
|
63 | | - if sum(p_cards) == sum(d_cards): |
64 | | - print("***************The match is tie !!****************") |
65 | | - exit() |
| 45 | + if sum(d_cards) > 21: |
| 46 | + print( |
| 47 | + "Dealer is BUSTED !\n ************** You are the Winner !!******************\n" |
| 48 | + ) |
| 49 | + return |
| 50 | + |
| 51 | + if sum(d_cards) == 21 and sum(p_cards) == 21: |
| 52 | + print("*****************The match is tie !!*************************") |
| 53 | + return |
66 | 54 |
|
67 | 55 | if sum(d_cards) == 21: |
68 | | - if sum(p_cards) < 21: |
69 | | - print("***********************Dealer is the Winner !!******************") |
70 | | - elif sum(p_cards) == 21: |
71 | | - print("********************There is tie !!**************************") |
72 | | - else: |
73 | | - print("***********************Dealer is the Winner !!******************") |
| 56 | + print("***********************Dealer is the Winner !!******************") |
| 57 | + return |
74 | 58 |
|
75 | | - elif sum(d_cards) < 21: |
76 | | - if sum(p_cards) < 21 and sum(p_cards) < sum(d_cards): |
77 | | - print("***********************Dealer is the Winner !!******************") |
78 | | - if sum(p_cards) == 21: |
79 | | - print("**********************Player is winner !!**********************") |
80 | | - if sum(p_cards) < 21 and sum(p_cards) > sum(d_cards): |
81 | | - print("**********************Player is winner !!**********************") |
| 59 | + def dealer_choice(): |
| 60 | + if sum(d_cards) < 17: |
| 61 | + while sum(d_cards) < 17: |
| 62 | + random.shuffle(deck) |
| 63 | + d_cards.append(deck.pop()) |
| 64 | + |
| 65 | + print("Dealer has total " + str(sum(d_cards)) + " with the cards ", d_cards) |
82 | 66 |
|
83 | | - else: |
84 | | - if sum(p_cards) < 21: |
| 67 | + if sum(p_cards) == sum(d_cards): |
| 68 | + print("***************The match is tie !!****************") |
| 69 | + return |
| 70 | + |
| 71 | + if sum(d_cards) > 21: |
85 | 72 | print("**********************Player is winner !!**********************") |
86 | | - elif sum(p_cards) == 21: |
| 73 | + return |
| 74 | + |
| 75 | + if sum(d_cards) > sum(p_cards): |
| 76 | + print("***********************Dealer is the Winner !!******************") |
| 77 | + else: |
87 | 78 | print("**********************Player is winner !!**********************") |
| 79 | + |
| 80 | + # Player turn |
| 81 | + while sum(p_cards) < 21: |
| 82 | + k = input("Want to hit or stay?\n Press 1 for hit and 0 for stay ") |
| 83 | + |
| 84 | + if k == "1": |
| 85 | + random.shuffle(deck) |
| 86 | + p_cards.append(deck.pop()) |
| 87 | + print("You have a total of " + str(sum(p_cards)) + " with the cards ", p_cards) |
| 88 | + |
| 89 | + if sum(p_cards) > 21: |
| 90 | + print("*************You are BUSTED !*************\n Dealer Wins !!") |
| 91 | + return |
| 92 | + |
| 93 | + if sum(p_cards) == 21: |
| 94 | + print( |
| 95 | + "*******************You are the Winner !!*****************************" |
| 96 | + ) |
| 97 | + return |
88 | 98 | else: |
89 | | - print("***********************Dealer is the Winner !!******************") |
| 99 | + dealer_choice() |
| 100 | + break |
90 | 101 |
|
91 | 102 |
|
92 | | -while sum(p_cards) < 21: |
93 | | - k = input("Want to hit or stay?\n Press 1 for hit and 0 for stay ") |
94 | | - if k == 1: |
95 | | - random.shuffle(deck) |
96 | | - p_cards.append(deck.pop()) |
97 | | - print("You have a total of " + str(sum(p_cards)) + " with the cards ", p_cards) |
98 | | - if sum(p_cards) > 21: |
99 | | - print("*************You are BUSTED !*************\n Dealer Wins !!") |
100 | | - if sum(p_cards) == 21: |
101 | | - print( |
102 | | - "*******************You are the Winner !!*****************************" |
103 | | - ) |
104 | | - |
105 | | - else: |
106 | | - dealer_choice() |
107 | | - break |
| 103 | +# Run Game |
| 104 | +welcome() |
| 105 | +start_game() |
0 commit comments