-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackjack.py
292 lines (256 loc) · 9.14 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
""" This is the python script imitating the Blackjack game """
import random
import os
# Cards
values = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8,
'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}
suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
LOOP = 0
# Objects
class Cards():
""" This is the class for cards"""
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
self.value = values[rank]
def __str__(self):
return f'{self.rank} of {self.suit}'
class Deck():
""" This is the class for a new deck """
def __init__(self):
self.all_cards = []
for cardsuit in suits:
for cardrank in ranks:
new_cards = Cards(cardsuit, cardrank)
self.all_cards.append(new_cards)
self.shuffle()
def draw(self):
""" This method is for drawing cards """
return self.all_cards.pop(0)
def shuffle(self):
""" This method is for shuffling the starting Deck """
random.shuffle(self.all_cards)
class Player():
""" This is the class for the player hand"""
def __init__(self, name, money):
self.inhand = []
self.value = 0
self.money = money
self.name = name
self.ace = []
def drawcard(self, card):
""" This is the method for drawing card or HIT """
self.inhand.append(card)
if card.value == 11:
self.ace.append(card)
self.value += card.value
self.valuecalc()
def __str__(self) -> str:
return f'Player {self.name} with value {self.value}, with {len(self.ace)} ACES'
def valuecalc(self):
""" This is for the recalculation of hand value (Ace) """
for _ in self.ace:
if self.value > 21:
self.value -= 10
self.ace.pop(0)
def checkburst(self):
""" This method is for checking whether a player has 'burst' """
if self.value > 21:
return True
else:
return False
def bet(self):
while True:
print(f'Your current money: {self.money}')
bet_money = input('Please place your bet: ')
if bet_money.isdigit() is False:
print('Please enter a valid number!')
elif int(bet_money) > self.money:
print('You can not bet more than what you have!')
else:
bet_money = int(bet_money)
self.money -= bet_money
return bet_money
def win(self, money):
self.money += money
def check_lose(self):
if self.money <= 0:
return True
else:
return False
def clear_card(self):
self.inhand.clear()
self.value = 0
self.ace = []
class Dealer(Player):
""" This class is for computer dealer """
def __init__(self, dealer_money):
super().__init__('Dealer', dealer_money)
def reveal_cards(self):
return self.inhand[-1]
def lose(self, money):
self.money -= money
self.check_lose()
# Function
def clear_screen():
try:
os.system('cls')
except:
os.system('clear')
def player_drawing():
print('Dealer cards: ')
print('FACEDOWN')
print(dealer.reveal_cards())
print('-------------------------------------------------------')
while True:
print('Your cards: ')
for card in player.inhand:
print(card)
choice = input('DO YOU WANT TO HIT, Y or N?: ')
if choice == 'N':
return 'Ok'
elif choice == 'Y':
player.drawcard(card_pile.draw())
if player.checkburst() is True:
return 'Burst'
else:
pass
else:
print('Please enter N or Y valid.')
def dealer_drawing():
while True:
if dealer.value < 11:
dealer.drawcard(card_pile.draw())
if dealer.checkburst() is True:
return 'Burst'
else:
pass
elif 11 <= dealer.value < 14:
if random.choice([0, 1, 1, 1]) == 1:
dealer.drawcard(card_pile.draw())
if dealer.checkburst() is True:
return 'Burst'
else:
pass
else:
return 'Ok'
elif 14 <= dealer.value < 16:
if random.choice([0, 1]) == 1:
dealer.drawcard(card_pile.draw())
if dealer.checkburst() is True:
return 'Burst'
else:
pass
else:
return 'Ok'
elif 16 <= dealer.value < 18:
if random.choice([0, 1, 0, 0]) == 1:
dealer.drawcard(card_pile.draw())
if dealer.checkburst() is True:
return 'Burst'
else:
pass
else:
return 'Ok'
elif 18 <= dealer.value < 20:
if random.choice([0, 1, 0, 0, 0]) == 1:
dealer.drawcard(card_pile.draw())
if dealer.checkburst() is True:
return 'Burst'
else:
pass
else:
return 'Ok'
else:
if dealer.checkburst() is True:
return 'Burst'
else:
return 'Ok'
# MAIN SCRIPT START HERE
while True:
LOOP = 0
while True:
player_asked_money = input(
'Please enter the value of money you want to start with: ')
dealer_asked_money = input(
'Please enter the value of money you want the dealer to start with: ')
if player_asked_money.isdigit() is True and dealer_asked_money.isdigit() is True:
player = Player('player', int(player_asked_money))
dealer = Dealer(int(dealer_asked_money))
card_pile = Deck()
print(
'_____________________________________________________________________________________')
break
print('Please enter a number!')
while True:
LOOP += 1
card_pile = Deck()
bet_ammount = player.bet()
print('------------------------------------------------------------------------')
player.clear_card()
dealer.clear_card()
player.drawcard(card_pile.draw())
player.drawcard(card_pile.draw())
dealer.drawcard(card_pile.draw())
dealer.drawcard(card_pile.draw())
if player_drawing() == 'Burst':
print('*********************************************')
print('Your cards: ')
for pcard in player.inhand:
print(pcard)
# if both burst then player lose.
print('*********************************************')
print('You BURST!')
dealer.win(bet_ammount)
print('You have lost this round.')
elif dealer_drawing() == 'Burst':
print('*********************************************')
print("Dealer's cards: ")
for dcard in dealer.inhand:
print(dcard)
print('*********************************************')
print('Dealer BURST!')
dealer.lose(bet_ammount)
player.win(2*bet_ammount)
print('You have won this round.')
else:
print('*********************************************')
print('Your cards: ')
for pcard in player.inhand:
print(pcard)
print('*********************************************')
print("Dealer's cards: ")
for dcard in dealer.inhand:
print(dcard)
print('*********************************************')
if (21 - player.value) > (21 - dealer.value): # Player lose
dealer.win(bet_ammount)
print('You have lost this round.')
elif (21 - player.value) < (21 - dealer.value): # Player win
dealer.lose(bet_ammount)
player.win(2*bet_ammount)
print('You have won this round.')
else:
print('You have drawn with the dealer.')
player.win(bet_ammount)
if player.check_lose() is True:
print('You have LOST the game because you ran out of money')
break
elif dealer.check_lose() is True:
print('You have WON the game because Dealer ran out of money.')
break
else:
pass
print(f'Your money: {player.money}\nDealer money: {dealer.money}')
print('......................................................................................................')
print('New round')
print(f'You have played {LOOP} rounds.')
exit_choice = input(
"Input 'new' if you want to play new game. Otherwise, enter anything to exit: ")
if exit_choice == 'new':
clear_screen()
print('NEW GAME')
else:
break