-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11BlackJack.py
More file actions
185 lines (150 loc) · 5.03 KB
/
Day11BlackJack.py
File metadata and controls
185 lines (150 loc) · 5.03 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
173
174
175
176
177
178
179
180
181
182
183
184
185
import random
standard_cards_list = [
{"value": 11, "label": "A"},
{"value": 2, "label": "2"},
{"value": 3, "label": "3"},
{"value": 4, "label": "4"},
{"value": 5, "label": "5"},
{"value": 6, "label": "6"},
{"value": 7, "label": "7"},
{"value": 8, "label": "8"},
{"value": 9, "label": "9"},
{"value": 10, "label": "10"},
{"value": 10, "label": "K"},
{"value": 10, "label": "Q"},
{"value": 10, "label": "J"},
]
standard_suits_list = ["♥", "♦", "♣", "♠"]
# Generate deck
def generate_deck():
temp_deck = []
for card in standard_cards_list:
for suit in standard_suits_list:
new_card = card.copy()
new_card["suit"] = suit
temp_deck.append(new_card)
return temp_deck
def pick_card():
selected_card_index = random.randrange(0, len(deck), 1)
selected_card = deck[selected_card_index]
deck.pop(selected_card_index)
return selected_card
def show_player_hand():
hand = ''
for card in player_hand:
hand += f"{card['suit']}{card['label']} "
return hand
def show_computer_hand(show_all):
hand = ''
if show_all:
for card in computer_hand:
hand += f"{card['suit']}{card['label']} "
else:
hand += f"{computer_hand[0]['suit']}{computer_hand[0]['label']}"
return hand
def calculate_hand(hand):
total = 0
ace_in_hand = False
for card in hand:
total += card['value']
for card in hand:
if card['label'] == 'A':
ace_in_hand = True
break
if ace_in_hand and total > 21:
total = 0
# recalculate
for card in hand:
if card['label'] == 'A':
total += 1
else:
total += card['value']
return total
def is_blackjack(hand):
total = 0
for card in hand:
total += card['value']
if len(hand) == 2 and total == 21:
return True
else:
return False
play_another_game = 'y'
player_chips = 10
bet = 0
while play_another_game == 'y':
deck = []
player_hand = []
computer_hand = []
game_over = False
deck = generate_deck()
if player_chips <= 0:
game_over = True
play_another_game = 'n'
print("No more chips to bet! Game Over")
continue
play_another_game= input("Do you want to play BlackJack? y/n\n>")
if play_another_game == 'n':
continue
player_hand.append(pick_card())
player_hand.append(pick_card())
computer_hand.append(pick_card())
computer_hand.append(pick_card())
while game_over == False:
if player_chips <= 0:
game_over = True
print("No more chips to bet! Game Over")
continue
print(f"Your chips: {player_chips} $")
if bet == 0:
bet = int(input("How many chips do you want to bet?:\n>"))
while bet > player_chips or bet <= 0:
bet = int(input(f"Invalid value, you only have {player_chips} chips?:\n>"))
print(f"Your chips {player_chips}")
print(f"Your hand {show_player_hand()}")
print(f"Computer's hand: {show_computer_hand(False)}")
player_total = calculate_hand(player_hand)
computer_total = calculate_hand(computer_hand)
if player_total == 21:
game_over = True
print("You win! 21")
player_chips += bet
bet = 0
elif player_total > 21:
game_over = True
print("Sorry, you lose! You passed 21")
player_chips -= bet
bet = 0
elif is_blackjack(player_hand):
game_over = True
print("BackJack! you win.")
player_chips += bet
bet = 0
else:
hit = input("Type 'y' to get another card, type 'n' to pass:\n>")
if hit == 'y':
player_hand.append(pick_card())
else:
game_over = True
while computer_total < 17:
computer_hand.append(pick_card())
computer_total = calculate_hand(computer_hand)
print(f"Computer's hand: {show_computer_hand(True)}")
if computer_total > 21:
print("You win! Computer passed 21")
player_chips += bet
bet = 0
elif player_total == computer_total:
print("Draw!")
bet = 0
elif is_blackjack(computer_hand):
print("You lose!, Computer got a BlackJack!")
player_chips -= bet
bet = 0
elif player_total > computer_total:
print("You win! You are closer to 21 than Computer")
player_chips += bet
bet = 0
else:
print("You lose! Computer is closer to 21 than you")
player_chips -= bet
bet = 0