-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.py
135 lines (123 loc) · 5.35 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
############### Our Blackjack House Rules #####################
## The deck is unlimited in size.
## There are no jokers.
## The Jack/Queen/King all count as 10.
## The the Ace can count as 11 or 1.
## Use the following list as the deck of cards:
## cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
## The cards in the list have equal probability of being drawn.
## Cards are not removed from the deck as they are drawn.
## The computer is the dealer.
import random
import os
logo = """
.------. _ _ _ _ _
|A_ _ |. | | | | | | (_) | |
|( \/ ).-----. | |__ | | __ _ ___| | ___ __ _ ___| | __
| \ /|K /\ | | '_ \| |/ _` |/ __| |/ / |/ _` |/ __| |/ /
| \/ | / \ | | |_) | | (_| | (__| <| | (_| | (__| <
`-----| \ / | |_.__/|_|\__,_|\___|_|\_\ |\__,_|\___|_|\_\\
| \/ K| _/ |
`------' |__/
Author: Tyler Ramsbey
"""
def blackjack():
# Variables
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
user_cards = []
computer_cards = []
print(logo)
# Functions
# Deal a single random card
def deal_card():
card = random.choice(cards)
return card
# Deal two random cards
def deal_two_cards():
user_cards.append(deal_card())
user_cards.append(deal_card())
computer_cards.append(deal_card())
computer_cards.append(deal_card())
# Calculate score based on the list of cards
def calculate_score(list_of_cards):
score = sum(list_of_cards)
for card in list_of_cards:
if card == 11 and score > 21:
list_of_cards.remove(11)
list_of_cards.append(1)
score = sum(list_of_cards)
if list_of_cards == [11, 10] or list_of_cards == [10, 11]:
return 0
else:
return score
# Initial check for winner and to finish the user's turn
def calculate_winner():
user_score = calculate_score(user_cards)
computer_score = calculate_score(computer_cards)
print(f"Your cards are: {user_cards}")
# Per the rules, you only can see the computer's first card
print(f"The dealer's first card is: {computer_cards[0]}\n")
if user_score == 0:
should_continue = "You have a blackjack. You win and the game is over!"
return should_continue
elif computer_score == 0:
should_continue = "The dealer has a blackjack. You lose!"
return should_continue
elif user_score > 21:
should_continue = "You went over 21. You lose!"
return should_continue
elif user_score <= 21:
draw_card = input((f"Your current score is {user_score} do you want to draw another card? Type 'y' or 'n': "))
if draw_card == 'y':
should_continue = "user"
elif draw_card == 'n':
should_continue = "computer"
return should_continue
# After the user is done, the computer/dealer will draw cards as long as their score is less than 17 per the rules
def computers_turn():
computer_score = calculate_score(computer_cards)
if computer_score > 17:
should_continue = f"The dealer's score is {computer_score}"
return should_continue
while computer_score < 17:
computer_cards.append(deal_card())
computer_score = calculate_score(computer_cards)
if computer_score < 17:
should_continue = "computer"
if computer_score > 21:
should_continue = f"The dealer's score is {computer_score} and he went over 21. You win!"
return should_continue
else:
should_continue = computer_score
return should_continue
# Compare the scores to see who won
def compare(user_score, computer_score):
if user_score == computer_score:
print(f"It's a draw! You both have a score of {user_score}.")
elif user_score > computer_score:
print(f"You have a score of {user_score} and the dealer has a score of {computer_score} so you win!")
else:
print(f"You have a score of {user_score} and the dealer has a score of {computer_score} so you lose. Sorry!")
# Main program -- calling various functions and variables
deal_two_cards()
should_continue = calculate_winner()
while should_continue == "user":
user_cards.append(deal_card())
should_continue = calculate_winner()
while should_continue == "computer":
should_continue = computers_turn()
if should_continue != calculate_score(computer_cards):
print(should_continue)
computer_score = calculate_score(computer_cards)
if should_continue == f"The dealer's score is {computer_score}" or should_continue == computer_score:
user_score = calculate_score(user_cards)
computer_score = calculate_score(computer_cards)
compare(user_score, computer_score)
start_over = input("Do you want to start another game? Type 'y' or 'n': ")
# Recursion to start the game over
if start_over == "y":
os.system('cls')
blackjack()
else:
print("Thank you for playing!")
blackjack()