Repository navigation
Expand file tree
/
Copy pathpyjack.py
More file actions
214 lines (193 loc) · 6.17 KB
/
Copy pathpyjack.py
File metadata and controls
214 lines (193 loc) · 6.17 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
### PYJACK v1.0.0 BY RUBIKTOR012 ###
# TODO:
# - Look for and fix bugs.
# ! Can't double down if previous game ended on a bust
# - Maybe: Make the chips value persist even when quiting the program.
# - Very Maybe: Add a GUI using pygame.
import os
import platform
import random
deck = []
handDealer = []
handPlayer = []
score = 0
chips = 500
bet = 0
firstTurn = True
def clear():
if platform.system() == "Windows":
os.system("cls")
elif platform.system() == "Linux" or "macosx" or "Darwin":
os.system("clear")
else:
print("ERROR - Cannot clear screen: Unrecognized OS")
def shuffleDeck():
deck.clear()
for i in range(4): # Add one of each card, 4 times
for j in ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]:
deck.append(j)
random.shuffle(deck)
def dealInit():
handDealer.clear()
handPlayer.clear()
for i in range(2): # Deals 2 cards to the player and dealer
handDealer.append(deck.pop(0))
handPlayer.append(deck.pop(0))
print(f"The dealer's first card is: {handDealer[0]}")
print(f"Your hand: {handPlayer}")
print(f"Score: {calcScore(handPlayer)}")
def aceFix(targetList):
counter = 0
for i in targetList: # Checks if any of the cards are aces, if they are, pops and appends to the end of the sequence, so aces can be scored last.
if i == "A":
targetList.append(targetList.pop(counter))
counter += 1
def calcScore(targetList):
score = 0
scoreList = []
aceFix(scoreList)
for i in targetList:
match i:
case "2":
score += 2
case "3":
score += 3
case "4":
score += 4
case "5":
score += 5
case "6":
score += 6
case "7":
score += 7
case "8":
score += 8
case "9":
score += 9
case "10" | "J" | "Q" | "K":
score += 10
case "A": # Since this is scored last, aces will always use their highest possible non-bust value.
if score <= 10:
score += 11
else:
score += 1
return score
def betPrompt():
global bet
bet = 0
while bet <= 0 or bet > chips:
try:
bet = int(input(f"How much would you like to bet? You currently have {chips} chips. (Min. 1): "))
if bet <= 0 or bet > chips:
clear()
print("Invalid amount")
except ValueError:
clear()
print("Invalid amount")
def hit(targetList):
targetList.append(deck.pop(0))
def stand(targetList):
winCheck()
def doubleDown():
global bet
#print(bet)
bet += bet
#print(bet)
hit(handPlayer)
if calcScore(handPlayer) > 21:
checkBust(handPlayer)
else:
winCheck()
def checkBust(targetList):
global chips
global firstTurn
if calcScore(handPlayer) > 21:
print("Busted!")
print("You lose!")
chips -= bet
print(f"You now have {chips} chips.")
firstTurn = True
playAgain()
def checkBlackjack():
if calcScore(handPlayer) == 21:
winCheck()
def winCheck():
global chips
global bet
while calcScore(handDealer) < 17:
hit(handDealer)
print("-------------------------------------")
print(f"Dealer's hand: {handDealer}")
print(f"Your score is {calcScore(handPlayer)}, and the dealer's score is {calcScore(handDealer)}, which means...")
if calcScore(handPlayer) > 21:
print("Busted!")
print("You lose!")
chips -= bet
print(f"You now have {chips} chips.")
elif calcScore(handPlayer) == calcScore(handDealer):
print("It's a draw!")
elif calcScore(handPlayer) == 21 and calcScore(handDealer) != 21:
print("You win by blackjack!")
chips += round(bet * 1.5)
elif calcScore(handDealer) > 21:
print("The dealer has busted, and you win!")
chips += bet
elif calcScore(handPlayer) < calcScore(handDealer):
print("You lose!")
chips -= bet
elif calcScore(handPlayer) > calcScore(handDealer):
print("You win!")
chips += bet
else:
print("No clue, there seems to be an error. Odd...")
print(f"You have {chips} chips.")
playAgain()
def playAgain():
answer = input("Would you like to play again? [Y/n]: ")
match answer:
case "N" | "n":
exit()
case "Y" | "y" | _:
initGame()
def initGame():
global firstTurn
clear()
shuffleDeck()
betPrompt()
dealInit()
checkBlackjack()
firstTurn = True
initGame()
while True:
if firstTurn == True:
playerInput = input("Hit (H), Stand (S) or Double Down (D)?: ")
else:
playerInput = input("Hit (H) or Stand (S)?: ")
match playerInput:
case "H" | "h":
clear()
hit(handPlayer)
print(f"The dealer's first card is: {handDealer[0]}")
print(f"Your hand: {handPlayer}")
print(f"Score: {calcScore(handPlayer)}")
checkBust(handPlayer)
checkBlackjack()
firstTurn = False
case "S" | "s":
stand(handPlayer)
case "D" | "d":
if firstTurn == True:
doubleDown()
else:
clear()
print(f"The dealer's first card is: {handDealer[0]}")
print(f"Your hand: {handPlayer}")
print(f"Score: {calcScore(handPlayer)}")
print("!!! Invalid input !!!")
case _:
clear()
print(f"The dealer's first card is: {handDealer[0]}")
print(f"Your hand: {handPlayer}")
print(f"Score: {calcScore(handPlayer)}")
print("!!! Invalid input !!!")
input("What? The game exited the game loop! Report this to the developer and press Enter to exit.")