-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.py
166 lines (145 loc) · 5.34 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import random
print("================================================")
print("\tWelcome to the blackjack game !")
print("===============================================")
print(
'''
.------. _ _ _ _ _
|A_ _ |. | | | | | | (_) | |
|( \/ ).-----. | |__ | | __ _ ___| | ___ __ _ ___| | __
| \ /|K /\ | | '_ \| |/ _` |/ __| |/ / |/ _` |/ __| |/ /
| \/ | / \ | | |_) | | (_| | (__| <| | (_| | (__| <
`-----| \ / | |_.__/|_|\__,_|\___|_|\_\ |\__,_|\___|_|\_\\
| \/ K| _/ |
`------' |__/
'''
)
# Define card ranks, suits, and values
ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
values = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10,
'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}
coins = 50
print("\t\tYou have 50 coins !")
# Define function to create deck of cards
def create_deck():
deck = []
for rank in ranks:
for suit in suits:
card = (rank, suit)
deck.append(card)
return deck
#Himel Sarder
#Department of Computer Science and Engineering
#Bangamata Sheikh Fojilatunnesa Mujib Science and Technology University, Jamalpur, Bangladesh
# Define function to deal a card
def deal_card(deck):
card = random.choice(deck)
deck.remove(card)
return card
# Define function to calculate hand value
def calculate_hand_value(hand):
value = 0
for card in hand:
rank = card[0]
value += values[rank]
return value
# Define function to display hand
def display_hand(hand):
for card in hand:
print(card[0], 'of', card[1])
# Define function to check if hand is a blackjack
def is_blackjack(hand):
return len(hand) == 2 and calculate_hand_value(hand) == 21
# Define function to check if hand is bust
def is_bust(hand):
return calculate_hand_value(hand) > 21
# Main function to play the game
def play_blackjack(coins):
print("================================================")
print()
if coins < 10:
print("Sorry, you don't have enough coins to play!")
print("=====================================")
print("\t\t\tGame over!")
print("=====================================")
return
deck = create_deck()
player_hand = [deal_card(deck), deal_card(deck)]
dealer_hand = [deal_card(deck), deal_card(deck)]
print("\nYour hand:")
display_hand(player_hand)
print("\nDealer's hand:")
print(dealer_hand[0][0], 'of', dealer_hand[0][1])
# Player's turn
while True:
choice = input("\nDo you want to hit or stand? (h/s): ").lower()
if choice == 'h':
player_hand.append(deal_card(deck))
print("\nYour hand:")
display_hand(player_hand)
if is_blackjack(player_hand):
print("Congratulations! You have a Blackjack!")
coins += 10
break
elif is_bust(player_hand):
print()
print("Sorry, you busted! Dealer wins.")
coins -= 10
break
elif choice == 's':
break
else:
print("Invalid choice! Please enter 'h' or 's'.")
#Himel Sarder
#Department of Computer Science and Engineering
#Bangamata Sheikh Fojilatunnesa Mujib Science and Technology University, Jamalpur, Bangladesh
# Dealer's turn
if not is_bust(player_hand):
print("\nDealer's hand:")
display_hand(dealer_hand)
while calculate_hand_value(dealer_hand) < 17:
dealer_hand.append(deal_card(deck))
print("\nDealer hits...")
display_hand(dealer_hand)
if is_bust(dealer_hand):
print()
print("Dealer busted! You win!")
coins += 10
break
if calculate_hand_value(dealer_hand) <= 21:
if calculate_hand_value(player_hand) > calculate_hand_value(dealer_hand):
print()
print("You win!")
coins += 10
elif calculate_hand_value(player_hand) < calculate_hand_value(dealer_hand):
print()
print("Dealer wins!")
coins -= 10
else:
print("It's a tie!")
'''
else:
print()
print("Dealer busted! You win!")
coins += 10
'''
#Himel Sarder
#Department of Computer Science and Engineering
#Bangamata Sheikh Fojilatunnesa Mujib Science and Technology University, Jamalpur, Bangladesh
print()
print(f"Current coins: {coins}")
print()
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again == 'yes':
play_blackjack(coins)
else:
print("Thanks for playing!")
print("=====================================")
print(f"\t\tFinal coins: {coins}")
print("=====================================")
# Play the game
play_blackjack(coins)
#Himel Sarder
#Department of Computer Science and Engineering
#Bangamata Sheikh Fojilatunnesa Mujib Science and Technology University, Jamalpur, Bangladesh