-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.py
529 lines (374 loc) · 14.3 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/python
# -*- coding: utf-8 -*-
__version__ = "1.0"
__author__ = "Radon Gas"
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 Radon Gas (radonintro1234)'
"""
Author : Radon Gas (radonintro1234)
Github : https://github.com/radonintro1234
License : MIT
Copyright (c) 2020 Radon Gas (radonintro1234)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
from random import shuffle
suits = ("Hearts", "Clubs", "Diamonds", "Spades")
ranks = ("Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace")
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}
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
class Card:
"""
This Class creates a Card of a particular Suit and Rank.
"""
def __init__(self,rank,suit):
self.rank = rank
self.suit = suit
self.value = values[rank]
def __str__(self):
"""
Printing a Card
"""
return self.rank + " of " + self.suit
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
class Deck:
"""
This Class creates a DECK, to be used while playing a game.
"""
def __init__(self):
self.cardlist = []
#CREATING A DECK {LIST} OF CARDS FOR A GAME.
for suit in suits:
for rank in ranks:
current_card = Card(rank,suit)
self.cardlist.append(current_card)
def __str__(self):
"""
PRINTING A DECK.
"""
deck_cards = ''
for x in range(len(self.cardlist)):
deck_cards += self.cardlist[x].__str__() + "\n"
return f"This Deck has {str(len(self.cardlist))} Cards.\n" + deck_cards
def shuffle_deck(self):
"""
Function For Shuffling a DECK.
"""
shuffle(self.cardlist)
def deal_one(self):
"""
Function for Dealing a Card.
"""
return self.cardlist.pop(0)
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
class Player():
"""
This class is used to generate a Player.
"""
def __init__(self,name,chips):
self.name = name
self.chips = chips
self.bet = 0
def __str__(self):
"""
Printing a player Chips
"""
print_statement = 'Player {} has {} chips\n'.format(self.name,self.chips)
return print_statement
def add_chips(self,chips):
"""
Adding Chips to the Player
"""
self.chips += chips
def remove_chips(self,chips):
"""
Removing Chips from the Player
"""
if chips > self.chips:
print("Unsufficient Chips.")
print("Current balance = {}".format(self.chips))
else:
self.chips -= chips
print("Current balance = {}".format(self.chips))
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
class Hand():
"""
This class is used to generate a hand for the player and the dealer
"""
def __init__(self):
self.cards = []
self.value = 0
self.ace_count = 0
def __str__(self):
"""
Printing a hand
"""
cards_in_hand = 'Card list is : \n'
for x in range(len(self.cards)):
cards_in_hand += str(self.cards[x]) + "\n"
return "This hand has a value of {}.\n\n".format(self.value) + cards_in_hand
def add_card(self,card):
"""
Adding a Card to a hand.
"""
self.cards.append(card)
self.value += card.value
if card.rank == "Ace":
self.ace_count += 1
while self.value > 21 and self.ace_count > 0:
self.value -= 10
self.ace_count -= 1
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
def take_bet(player):
"""
Function to take a bet from the player.
"""
while True:
try:
current_bet = int(input("How many Chips would you like to bet : "))
except:
print("Enter a Valid Number of Chips.")
else:
if current_bet > player.chips:
print("Unsufficient Amount. Please try again.")
else:
player.bet += current_bet
player.chips -= current_bet
break
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
def create_player():
"""
Funtion to create a Player.
"""
global player
while True:
player_name = input("\n\nEnter Your Name : ")
if player_name != '':
break
else:
print("Enter a valid name.\n")
while True:
try:
start_amount = int(input("Enter starting number of Chips : "))
except:
print("Please Enter a valid Number.\n")
else:
break
player = Player(player_name,start_amount)
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
def adjust_winnings(winner):
"""
Function to adjust chips at the End of the game.
"""
if winner == "player":
player.chips += int(player.bet*1.5)
player.bet = 0
elif winner == "tie" :
player.chips += player.bet
player.bet = 0
else:
player.bet = 0
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
def hit_or_stand(player_hand,deck_1):
"""
Function to give Player a choice to HIT or STAND.
"""
global player_playing
while True:
temp = input("HIT OR STAND? : ")
if temp == '':
print("Please Choose a valid option.\n")
if temp[0].lower() == 'h':
player_hand.add_card(deck_1.deal_one())
break
elif temp[0].lower() == 's':
player_playing = False
break
else :
print("Please Choose a valid option.\n")
if temp[0].lower() == 'h':
return "h"
else :
return "s"
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
def player_busted():
"""
Final Winner : Dealer
"""
global winner
print("\nPlayer Busted.")
print("Dealer Wins!\n")
winner = "dealer"
def dealer_busted():
"""
Final Winner : Player
"""
global winner
print("\nDealer Busted.")
print("Player Wins!\n")
winner = "player"
def player_dealer_tie():
"""
Final Winner : Tie
"""
global winner
print("IT'S A TIE!!\n")
winner = "tie"
def player_wins():
"""
Final Winner : Player
"""
global winner
print("Player Wins!\n")
winner = "player"
def dealer_wins():
"""
Final Winner : Dealer
"""
global winner
print("Dealer Wins!")
winner = "dealer"
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
def show_some_cards(player_hand, dealer_hand):
"""
Function to show both hands while Hiding one of dealers Cards.
"""
print("\nPlayer Cards are : ")
for card in player_hand.cards:
print(" " + str(card))
print("\nDealer Cards are : ")
print(" " + str(dealer_hand.cards[0]))
print("**Card is Hidden.**\n")
print("\n--------------------------------------------------------------------------------------------------------------------\n")
def show_all_cards(player_hand, dealer_hand):
"""
Function to show both hands
"""
print("\nPlayer Cards are : ")
for card in player_hand.cards:
print(" " + str(card))
print("\nDealer Cards are : ")
for card in dealer_hand.cards:
print(" " + str(card))
print("\n--------------------------------------------------------------------------------------------------------------------\n")
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
def main(player):
"""
Starting the main game.
"""
#INITIALIZE A DECK OF CARDS AND SHUFFLE THEM.
deck_1=Deck()
deck_1.shuffle_deck()
#CREATE PLAYER AND DEALER HANDS.
player_hand = Hand()
dealer_hand = Hand()
print("\n--------------------------------------------------------------------------------------------------------------------\n")
print(player)
#TAKE A BET FROM THE PLAYER.
take_bet(player)
#DEAL TWO CARDS TO PLAYER AND DEALER EACH.
player_hand.add_card(deck_1.deal_one())
player_hand.add_card(deck_1.deal_one())
dealer_hand.add_card(deck_1.deal_one())
dealer_hand.add_card(deck_1.deal_one())
#SHOW PLAYERS CARDS AND HIDE DEALERS ONE CARD.
show_some_cards(player_hand, dealer_hand)
#ASK THE PLAYER TO HIT OR STAND.
player_playing = True
while player_playing == True:
if player_hand.value == 21:
break
if player_hand.value > 21:
player_busted()
break
req = hit_or_stand(player_hand, deck_1)
if req == 's':
break
show_some_cards(player_hand, dealer_hand)
show_all_cards(player_hand, dealer_hand)
#DEALERS TURN
dealer_playing = True
while dealer_playing:
if player_hand.value <= 21:
while dealer_hand.value <17 :
print("\nDealer Hits......")
dealer_hand.add_card(deck_1.deal_one())
show_all_cards(player_hand, dealer_hand)
dealer_playing = False
if dealer_hand.value > 21:
dealer_busted()
break
elif player_hand.value == dealer_hand.value:
player_dealer_tie()
break
elif player_hand.value > dealer_hand.value:
player_wins()
break
else:
dealer_wins()
break
else:
break
adjust_winnings(winner)
print("\n" + str(player))
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
def play_again():
"""
Asking the Player to play again.
"""
while True:
print("\n--------------------------------------------------------------------------------------------------------------------\n")
temp = input("\nWant to play again? : ")
if temp[0].lower() == 'y':
return True
break
elif temp[0].lower() == 'n':
print("\n--------------------------------------------------------------------------------------------------------------------\n")
print("\nThank You for playing...\n")
print("\n--------------------------------------------------------------------------------------------------------------------\n")
return False
break
else :
print("Please Choose a valid option.\n")
'''
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'''
if __name__ == '__main__':
playing = True
create_player()
while playing:
main(player)
playing=play_again()