Repository navigation
Expand file tree
/
Copy pathblackjack.py
More file actions
115 lines (87 loc) · 3.42 KB
/
Copy pathblackjack.py
File metadata and controls
115 lines (87 loc) · 3.42 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
import random
import os
## Resources
# Code is from and inspired by with some improvements:
# 100 Days of Code: The Complete Python Pro Bootcamp
# https://www.udemy.com/share/103J8C3@0R0_ZWb7kJ4do0X-4zRsulmwTyefJndLSF7-DCQhp_3ojIbfiWmdBIvIMOalNv_ECg==/
ACE = 11
FACE_CARD = 10
cards = [ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, FACE_CARD, FACE_CARD, FACE_CARD]
user_buyin_balance = 0
user_bid_amount = 0
def clear_screen():
"""Clears the terminal screen."""
os.system("cls" if os.name == "nt" else "clear")
def draw_card():
"""Returns a random card from the deck."""
return random.choice(cards)
def calculate_score(cards):
"""Take a list of cards and return the score calculated from the cards."""
if sum(cards) == 21 and len(cards) == 2:
return 0
if ACE in cards and sum(cards) > 21:
cards.remove(ACE)
cards.append(1)
return sum(cards)
def compare(user_score, computer_score):
global user_buyin_balance, user_bid_amount
if user_score > 21 and computer_score > 21:
user_buyin_balance -= user_bid_amount
return "You went over. You lose"
if user_score == computer_score:
return "Draw"
elif computer_score == 0:
user_buyin_balance -= user_bid_amount
return "Lose, opponent has BlackJack"
elif user_score == 0:
user_buyin_balance += user_bid_amount
return "Win with a BlackJack"
elif user_score > 21:
user_buyin_balance -= user_bid_amount
return "You went over. You lose"
elif computer_score > 21:
user_buyin_balance += user_bid_amount
return "Opponent went over. You win"
elif user_score > computer_score:
user_buyin_balance += user_bid_amount
return "You win"
else:
user_buyin_balance -= user_bid_amount
return "You lose"
def display_hand(cards, score):
print(f"Your cards: {cards}, current score: {score}")
def play_game():
global user_buyin_balance, user_bid_amount
clear_screen()
print("Welcome to BlackJack!")
print(f"Your current remaining buyin balance is: ${user_buyin_balance}")
buyin_amount = int(input("How much would would you like to buyin? $"))
user_buyin_balance += buyin_amount
bid_amount = int(input("How much would you like to bid? $"))
user_bid_amount = bid_amount
user_cards = []
computer_cards = []
for _ in range(2):
user_cards.append(draw_card())
computer_cards.append(draw_card())
while True:
user_score = calculate_score(user_cards)
computer_score = calculate_score(computer_cards)
display_hand(user_cards, user_score)
print(f"Computer's first card: {computer_cards[0]}")
if user_score == 0 or computer_score == 0 or user_score > 21:
break
user_should_deal = input("Type 'y' to get another card, 'n' to pass: ")
if user_should_deal.lower() == 'y':
user_cards.append(draw_card())
else:
break
while computer_score != 0 and computer_score < 17:
computer_cards.append(draw_card())
computer_score = calculate_score(computer_cards)
display_hand(user_cards, user_score)
print(f"Computer's final hand: {computer_cards}, final score: {computer_score}")
print(compare(user_score, computer_score))
while input("Do you want to play a game of BlackJack? Type 'y' or 'n': ") == 'y':
play_game()
print(f"Your cash out balance is: ${user_buyin_balance:,.2f}")