-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack.py
More file actions
138 lines (123 loc) · 4.87 KB
/
blackjack.py
File metadata and controls
138 lines (123 loc) · 4.87 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
import random
playing: bool = True
def deal_cards(cards_arr, amount: int, display_cards_arr) -> None:
for card in range(amount):
random_card_value: int = random.randrange(1,13)
if random_card_value == 1:
display_cards_arr.append("[A]")
cards_arr.append(1)
elif random_card_value == 11:
display_cards_arr.append("[J]")
cards_arr.append(10)
elif random_card_value == 12:
display_cards_arr.append("[Q]")
cards_arr.append(10)
elif random_card_value == 13:
display_cards_arr.append("[K]")
cards_arr.append(10)
else:
display_cards_arr.append("[" + str(random_card_value) + "]")
cards_arr.append(random_card_value)
def calc_total_cards(cards_arr) -> int:
total_cards: int = 0
for card in cards_arr:
total_cards += card
return total_cards
money: int = 500
while playing:
print("------------------------------------")
print("Current Balance:", money)
bet_money: int = -1
if money <= 0:
print("You ran out of money :(")
playing = False
break
else:
while bet_money > money or bet_money < 0 or bet_money == 0:
try:
bet_money = int(input("How Much Money Would You Like To Bet?: "))
except ValueError:
bet_money = -1
player_cards = []
dealer_cards = []
display_player_cards = []
display_dealer_cards = []
deal_cards(player_cards, 1, display_player_cards)
total_player_cards: int = calc_total_cards(player_cards)
print("Your Cards:", display_player_cards)
stand: bool = False
while not stand:
print("------------------------------------")
choice = str(input("Would you like to hit or stand? (H/S): "))
if choice == "H" or choice == "h":
deal_cards(player_cards, 1, display_player_cards)
total_player_cards = calc_total_cards(player_cards)
print("Your Cards:", display_player_cards)
if total_player_cards == 21:
stand = True
elif total_player_cards > 21:
stand = True
elif len(player_cards) > 5:
stand = True
elif choice == "S" or choice == "s":
stand = True
print("------------------------------------")
deal_cards(dealer_cards, 3, display_dealer_cards)
total_player_cards = calc_total_cards(player_cards)
total_dealer_cards: int = calc_total_cards(dealer_cards)
if total_dealer_cards < 15:
deal_cards(dealer_cards, 1, display_dealer_cards)
total_dealer_cards = calc_total_cards(dealer_cards)
for card in player_cards:
if card == 1:
valid: bool = False
while not valid:
ace_choice: str = str(input("What would you like your ace to be worth (A: 1) (B: 11): "))
if ace_choice == "A" or ace_choice == "a":
valid = True
elif ace_choice == "B" or ace_choice == "b":
valid = True
total_player_cards += 10
for card in dealer_cards:
if card == 1:
if total_dealer_cards + 10 <= 21:
total_dealer_cards += 10
else:
pass
won: bool = False
draw: bool = False
print("------------------------------------")
print("Your Cards:", display_player_cards, total_player_cards)
print("Dealer Cards:", display_dealer_cards, total_dealer_cards)
print("------------------------------------")
if total_player_cards == total_dealer_cards:
draw = True
elif total_player_cards > 21 and total_dealer_cards > 21:
draw = True
elif total_player_cards <= 21 and total_player_cards > total_dealer_cards:
won = True
elif total_dealer_cards <= 21:
won = False
else:
won = True
if draw:
print("Draw!")
print("Money:", money)
else:
if won:
print("You Beat The Dealer!")
money += bet_money
print("Money:", money)
else:
print("You Lost!")
money -= bet_money
print("Money:", money)
print("------------------------------------")
play_again_choice: str = input("Would you like to play again? (Y/N): ")
while play_again_choice != "Y" and play_again_choice != "y" and play_again_choice != "N" and play_again_choice != "n":
play_again_choice = input("Invalid choice. Please enter Y or N: ")
if play_again_choice == "Y" or play_again_choice == "y":
playing = True
elif play_again_choice == "N" or play_again_choice == "n":
playing = False
print("------------------------------------\n")