-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackJack.py
297 lines (231 loc) · 11.4 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python
# coding: utf-8
# In[1]:
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
# In[2]:
def cards_player(cards_P):
clear_output()
print (f'{color.PURPLE} ________ ________ ________{color.END} ')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}|________| |________| |________|{color.END}')
print (cards_P[0]+' '+cards_P[1]+' '+cards_P[2])
print (' ')
print (' ')
# In[3]:
def cards_computer(cards_P,cards_C):
clear_output()
print (f'{color.PURPLE} ________ ________ ________{color.END} ')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}| | | | | |{color.END}')
print (f'{color.PURPLE}|________| |________| |________|{color.END}')
print (cards_P[0]+' '+cards_P[1]+' '+cards_P[2])
print (' ')
print (' ')
print (f'{color.GREEN} ________ ________ ________ {color.END}')
print (f'{color.GREEN}| | | | | |{color.END}')
print (f'{color.GREEN}| | | | | |{color.END}')
print (f'{color.GREEN}| | | | | |{color.END}')
print (f'{color.GREEN}| | | | | |{color.END}')
print (f'{color.GREEN}| | | | | |{color.END}')
print (f'{color.GREEN}| | | | | |{color.END}')
print (f'{color.GREEN}|________| |________| |________|{color.END}')
print (cards_C[0]+' '+cards_C[1]+' '+cards_C[2])
# In[4]:
def Players_Input():
while True:
if len(cards_P)!=3:
answer = ''
while answer.lower() != 'y' and answer.lower() != 'n':
answer = input('Do you want to draw a card? Y or N: ')
if answer.lower() == 'y' and len(cards_P) <= 2:
if bust_check(values_player):
break
card = random.choice(list(carddeck.items()))
if card == ('Ace_Club', 0) or card == ('Ace_Spade', 0) or card == ('Ace_Heart', 0) or card == ('Ace_Diamond', 0):
newcard = list(card)
while newcard[1] != 1 and newcard[1] != 11:
try:
newcard[1] = int(input('You have drawn an Ace! Ace has two values. Do you want 1 or 11?: '))
break
except:
print (f'{color.BOLD}\nInput one of the numbers 11 or 1{color.END}')
continue
cards_P.append(newcard[0])
values_player.append(int(newcard[1]))
print (f'\nYour card is {newcard[0]}\nYour current point count is: {sum(values_player)}')
else:
cards_P.append(card[0])
values_player.append(int(card[1]))
print (f'\nYour card is {card[0]}\nYour current point count is: {sum(values_player)}')
else:
if len(cards_P) == 0:
cards_P.append('NO CARD')
cards_P.append('NO CARD')
cards_P.append('NO CARD')
elif len(cards_P) == 1:
cards_P.append('NO CARD')
cards_P.append('NO CARD')
elif len(cards_P) == 2:
cards_P.append('NO CARD')
else:
break
else:
cards_player(cards_P)
print ('Computers turn.')
break
while True:
if bust_check(values_player):
break
while sum(values_computer) < 21 and len(cards_C)<2:
if bust_check(values_computer):
break
card = random.choice(list(carddeck.items()))
if card == ('Ace_Club', 0) or card == ('Ace_Spade', 0) or card == ('Ace_Heart', 0) or card == ('Ace_Diamond', 0):
newcard = list(card)
if (sum(values_computer)+11) <=21:
newcard[1]= 11
cards_C.append(newcard[0])
values_computer.append(int(newcard[1]))
continue
else:
newcard[1]= 1
cards_C.append(newcard[0])
values_computer.append(int(newcard[1]))
continue
else:
cards_C.append(card[0])
values_computer.append(int(card[1]))
continue
if sum(values_computer) == 21:
break
elif len(cards_C) == 2:
if sum(values_player) >= sum(values_computer):
card = random.choice(list(carddeck.items()))
cards_C.append(card[0])
values_computer.append(int(card[1]))
if bust_check(values_computer):
break
break
else:
cards_C.append('NO CARD')
break
else:
break
if len(cards_C)!= 3:
cards_C.append(' ')
cards_C.append(' ')
cards_C.append(' ')
cards_computer(cards_P, cards_C)
print (' ')
print (' ')
print (f'{color.BOLD}The final point count is:\nComputer: {sum(values_computer)}\nPlayer: {sum(values_player)}{color.END}')
if bust_check(values_computer)== False and bust_check(values_player) == False:
win_check(values_computer, values_player)
elif bust_check(values_computer)== True:
print ('\nComputer has exceeded 21! The Player wins!')
elif bust_check(values_player)== True:
print ('\nPlayer has exceeded 21! The Computer wins!')
# In[5]:
def replay():
replay = ''
while replay.lower() != 'y' and replay.lower() != 'n':
replay = input('\nDo you want to start the game? Y or N: ')
continue
if replay.lower() == 'y':
return True
else:
return False
# In[6]:
def win_check(value_computer, value_player):
if sum(value_computer) == sum(value_player):
print (f"{color.BLUE}\nIt's a TIE!{color.END}")
elif sum(value_computer) > sum(value_player):
print (f'{color.BLUE}\nCOMPUTER HAS WON THE GAME!{color.END}')
elif sum(value_computer) < sum(value_player):
print (f'{color.BLUE}\nPLAYER HAS WON THE GAME!{color.END}')
# In[7]:
def bust_check(value):
return sum(value) > 21
# In[8]:
class Player25:
def __init__(self, player, balance):
self.player = player
self.balance = balance
def amount(self):
bet_amount = 0
while bet_amount not in range(1,self.balance+1):
try:
bet_amount = int(input(f'{color.BOLD}Enter a BET smaller than or equal to {self.balance}$: {color.END}'))
except:
print(f'{color.BOLD}It has to be a NUMBER and SMALLER than your balance of {self.balance}$: {color.END}')
continue
self.bet_amount = bet_amount
print (f'\n{color.BOLD}You have betted {bet_amount}$!{color.END}')
def bet(self):
if sum(values_computer) == sum(values_player):
self.balance = self.balance
print (f'{color.GREEN}\nYour bet amount of {self.bet_amount} has been cancelled! Currently your balance is {self.balance}${color.END}')
elif sum(values_computer) > sum(values_player) and sum(values_computer) <= 21:
self.balance = self.balance - self.bet_amount
print (f'{color.GREEN}\nYour bet amount of {self.bet_amount} has been deducted! Currently your balance is {self.balance}${color.END}')
elif sum(values_player) > sum(values_computer) and sum(values_player) <= 21:
self.balance = self.balance + self.bet_amount
print (f'{color.GREEN}\nYour bet amount of {self.bet_amount} has been added! Currently your balance is {self.balance}${color.END}')
elif sum(values_computer) > sum(values_player) and sum(values_computer) >= 22:
self.balance = self.balance + self.bet_amount
print (f'{color.GREEN}\nYour bet amount of {self.bet_amount} has been added! Currently your balance is {self.balance}${color.END}')
else:
self.balance = self.balance - self.bet_amount
print (f'\n{color.GREEN}Your bet amount of {self.bet_amount} has been deducted! Currently your balance is {self.balance}${color.END}')
def negative(self):
if self.balance <= 0:
self.balance = 100
print (f"\n{color.BOLD}{color.RED}OOPSS, your balance is 0${color.END}")
print (f'{color.RED}YOU HAVE LOST ALL YOUR MONEY AND THIS GAME! TRY AGAIN WITH DEFAULT 100$ {color.END}')
else:
pass
# In[9]:
import random
from IPython.display import clear_output
print (f'{color.RED}Welcome to a BlackJack Game by Scotty!{color.END}')
print (f'\n{color.BOLD}The Rules:{color.END}\n- State your name when asked.\n- Your initial balance is 100$.\n- Your goal is to reach 21 or have more than the dealer without exceeding 21.\n- You can draw up to 3 cards.\n- Jack, Queen and King are the value of 10. Ace is 1 or 11, depends on your choice.')
print (f"\n{color.BOLD}Nice to meet you, what's your name?{color.END}")
playername = str(input(f'{color.BLUE}Your name: {color.END}'))
print (f'\n{color.BLUE}Hello {playername}! Your starting balance is 100$. Think about how much you want to bet and start the GAME! {color.END}')
balance = 100
gamestart = Player25(playername, balance)
while replay():
carddeck = {'Ace_Club':0, '2_Clubs':2,'3_Clubs':3,'4_Clubs':4,'5_Clubs':5,'6_Clubs':6,'7_Clubs':7,'8_Clubs':8,'9_Clubs':9,'10_Clubs':10,
'Jack_Clubs':10, 'Queen_Clubs':10,'King_Clubs':10,'Ace_Spade':0, '2_Spades':2,'3_Spades':3,'4_Spades':4,'5_Spades':5,'6_Spades':6,'7_Spades':7,'8_Spades':8,'9_Spades':9,'10_Spades':10,
'Jack_Spades':10, 'Queen_Spades':10,'King_Spades':10,'Ace_Heart':0, '2_Hearts':2,'3_Hearts':3,'4_Hearts':4,'5_Hearts':5,'6_Hearts':6,'7_Hearts':7,'8_Hearts':8,'9_Hearts':9,'10_Hearts':10,
'Jack_Hearts':10, 'Queen_Hearts':10,'King_Hearts':10,'Ace_Diamond':0, '2_Diamonds':2,'3_Diamonds':3,'4_Diamonds':4,'5_Diamonds':5,'6_Diamonds':6,'7_Diamonds':7,'8_Diamonds':8,'9_Diamonds':9,'10_Diamonds':10,
'Jack_Diamonds':10, 'Queen_Diamonds':10,'King_Diamonds':10}
values_player = [0]
values_computer = [0]
cards_P=[]
cards_C=[]
gamestart.amount()
Players_Input()
gamestart.bet()
gamestart.negative()
print (f'\n{color.BOLD}Thank you for playing{color.END}')
# In[ ]: