-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack.py
More file actions
909 lines (686 loc) · 35 KB
/
blackjack.py
File metadata and controls
909 lines (686 loc) · 35 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
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
"""The game logic tied together."""
from MainPackage import main_script, messages
# clears the terminal
print("\n" * 100)
# display the welcoming message
messages.welcome()
print()
# variable to determine if the game should start or not
start = "None"
# variable to determine if the game is on or not
game_on = True
# variable to store the current round
round = 0
# variable to store the threshold limit of the bank account to win the game
threshold = 0
print("Type 'help' to see the rules.")
while game_on:
if start == "None":
# asks the player what they want to do
start = input("Would you like to start the game? (yes/no) ")
# start the game
if start.lower() in ("y", "yes"):
print()
# Configuration Phase:
if round == 0:
# create the player
player = main_script.Player()
# create the computer dealer (automatically creates the deck)
dealer = main_script.Dealer()
print()
# create the bank account for the player asking for the intial balance
bank = main_script.Bank(player.name)
# account balance goal that will end the game if met
# loop to verify if the player input is an integer
while True:
try:
threshold = int(
input(
"What's the balance goal to win the game? (Recommended "
f"{bank.balance * 20}) "
)
)
# raise an error if the threshold value is too low
if threshold < bank.balance * 10:
raise TypeError("Value is too low.")
# warn the player that the threshold value is too low
except TypeError:
print(f"Sorry, the goal cannot be lower than ${bank.balance * 10}.")
print()
# warn the player that their input is invalid
except ValueError:
print("Invalid input, plase try again!")
print()
else:
print()
break
# Play Phase:
# variable to determine if the game is still going
main_phase = True
# variable to determine how much the player wants to bet
bet = 0
# list of Aces in hand
aces_list = []
# variable to store the value of cards in hand
hand_value = 0
# variable to store the value of cards with Aces in hand for the player
player_ace_hand_val = 0
# variable to store the value of cards with Aces in hand for the dealer
dealer_ace_hand_val = 0
# variable to get the player input to hit or stay
hit_stay = "None"
# variable to determine if someone hit a blackjack
blackjack = False
# variable to determine if someone hit a bust
bust = False
# variable to determine if a draw happened
draw = False
# variable to continue the game or not
game_continue = "None"
# shuffle the deck
if round == 0:
dealer.shuffle()
# reset some needed variables and objects for the next round
else:
# give both the player's and dealer's hand back to the deck
dealer.french_deck += player.hand + dealer.hand
# shuffle the deck
dealer.shuffle()
# reset both the player's and dealer's hands
player.hand = []
dealer.hand = []
# wish good luck on the player
print(f"{dealer.name} shuffles the deck. The game is about to begin!")
print("May lady luck smile your way!")
print()
# player input to continue to the game's main phase
input("Press Enter to continue! ")
print()
# loop of the main play phase
while main_phase:
# cleans the terminal
print("\n" * 100)
# increase the round each time the main_phase loops
round += 1
# display the round for the player
print(f"Round {round}")
print()
# display the bank account data to the player
print(f"Bank owner: {bank.owner}")
print(f"Current balance: {bank.balance}")
print()
# ask the player for a bet amount, storing it in the bet variable
while True:
# display a recommended amount if it is the first round
if round == 1:
try:
bet = int(
input(
"How much would you like to bet? (Recommended: "
f"{int(bank.balance / 10)}) "
)
)
# warn the player that their input is invalid
except ValueError:
print("Invalid input, plase try again!")
print()
continue
elif round > 1:
try:
bet = int(input("How much would you like to bet? "))
# warn the player that their input is invalid
except ValueError:
print("Invalid input, plase try again!")
print()
continue
# warn the player that the bet value is too high
if bet > bank.balance:
print(f"Sorry, the bet cannot be higher than ${bank.balance}.")
print()
continue
if bet < 1:
print("Sorry, the bet cannot be lower than $1.")
print()
continue
else:
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
break
# deals 2 cards if it is the beginning of a round
while (len(player.hand) and len(dealer.hand)) < 2:
# dealer deals a card to the player
player.add_cards(dealer.deal())
# dealing announce with correct grammar depending on the card
if ("Ace" in main_script.Card.__str__(player.new_card)) or (
"Eight" in main_script.Card.__str__(player.new_card)
):
print(f"{dealer.name} deals an {player.new_card} to you.")
else:
print(f"{dealer.name} deals a {player.new_card} to you.")
# dealer deals a card to itself
dealer.add_cards(dealer.deal())
# hide the dealer's second card in hand
if len(dealer.hand) == 2:
print(f"{dealer.name} deals a face down card to itself.")
# dealing announce with correct grammar depending on the card
elif ("Ace" in main_script.Card.__str__(dealer.new_card)) or (
"Eight" in main_script.Card.__str__(dealer.new_card)
):
print(f"{dealer.name} deals an {dealer.new_card} to itself.")
else:
print(f"{dealer.name} deals a {dealer.new_card} to itself.")
print()
# otherwise deals and show hands normally
else:
# don't show the dealer's second card if it's hand length is < 3
if len(dealer.hand) == 2:
print(
f"{dealer.name} current hand is: {dealer.hand[0]} and a "
"face down card."
)
# show first card's value in the dealers hand
print(f"Total card value: {dealer.hand[0].value}")
print()
# variable to compare to the player hand value
dealer_ace_hand_val = dealer.hand[0].value
# show the player's current hand
print(player)
# variable to store the value of cards in hand
hand_value = sum(card.value for card in player.hand)
# list of Aces in hand
aces_list = [
main_script.Card.__str__(card)
for card in player.hand
if "Ace" in main_script.Card.__str__(card)
]
if len(aces_list) <= 1:
# variable to store the value of cards with Aces in hand
player_ace_hand_val = hand_value
if player_ace_hand_val > 21:
player_ace_hand_val = hand_value - (10 * (len(aces_list)))
elif len(aces_list) >= 2:
# variable to store the value of cards with Aces in hand
player_ace_hand_val = hand_value - (10 * (len(aces_list) - 1))
if player_ace_hand_val > 21:
player_ace_hand_val = hand_value - (10 * (len(aces_list)))
# show the current player hand value
print("Total card value: " f"{player_ace_hand_val}")
print()
if player_ace_hand_val == 21:
print("IT'S A BLACKJACK!")
print()
input("Press Enter to continue! ")
# change hit_stay to 's' so the computer deals to itself
# (needed, because a draw can happen with 2 blackjacks)
hit_stay = "s"
# check for a blackjack or a bust
while True:
if hit_stay == "None":
# ask the player to hit or stay
hit_stay = input("Choose: (Hit/Stay) ")
if hit_stay.lower() in ("h", "hit"):
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# dealer deals a card to the player
player.add_cards(dealer.deal())
# deal announce with correct grammar depending on the card
if ("Ace" in main_script.Card.__str__(player.new_card)) or (
"Eight" in main_script.Card.__str__(player.new_card)
):
print(f"{dealer.name} deals an {player.new_card} to you.")
else:
print(f"{dealer.name} deals a {player.new_card} to you.")
print()
# don't show the dealer's second card if it's hand length is < 3
print(
f"{dealer.name} current hand is: {dealer.hand[0]} and "
"a face down card."
)
# show first card's value in the dealers hand
print(f"Total card value: {dealer.hand[0].value}")
print()
# show the player's current hand
print(player)
# variable to store the value of cards in hand
hand_value = sum(card.value for card in player.hand)
# list of Aces in hand
aces_list = [
main_script.Card.__str__(card)
for card in player.hand
if "Ace" in main_script.Card.__str__(card)
]
if len(aces_list) <= 1:
# variable to store the value of cards with Aces in hand
player_ace_hand_val = hand_value
if player_ace_hand_val > 21:
player_ace_hand_val = hand_value - (
10 * (len(aces_list))
)
elif len(aces_list) >= 2:
# variable to store the value of cards with Aces in hand
player_ace_hand_val = hand_value - (
10 * (len(aces_list) - 1)
)
if player_ace_hand_val > 21:
player_ace_hand_val = hand_value - (
10 * (len(aces_list))
)
# print the total card value of the player
print("Total card value: " f"{player_ace_hand_val}")
print()
# reset hit_stay so it asks for player input every time
hit_stay = "None"
if player_ace_hand_val == 21:
print("IT'S A BLACKJACK!")
print()
input("Press Enter to continue! ")
# change hit_stay to 's' so the computer deals to itself
# (needed, because a draw can happen with 2 blackjacks)
hit_stay = "s"
elif player_ace_hand_val > 21:
print("BUST!")
print()
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# readjust the player balance
bank.balance -= bet
print("THE DEALER WINS THE ROUND!")
print()
# display the player's loss and new balance
print(f"{player.name} loses ${bet}.")
print(f"Your new balance is: ${bank.balance}.")
print()
break
elif hit_stay.lower() in ("s", "stay"):
while dealer_ace_hand_val < 18:
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# reveal the face down card
if len(dealer.hand) == 2:
print(f"{dealer.name} reveals the face down card.")
# check for an Ace
if (
"Ace" in main_script.Card.__str__(dealer.hand[1])
) or (
"Eight" in main_script.Card.__str__(dealer.hand[1])
):
print(
"It's an "
f"{main_script.Card.__str__(dealer.hand[1])}"
)
else:
print(
"It's a "
f"{main_script.Card.__str__(dealer.hand[1])}"
)
print()
# show the dealer's current hand
print(dealer)
# variable to store the value of cards in hand
hand_value = sum(card.value for card in dealer.hand)
# list of Aces in hand
aces_list = [
main_script.Card.__str__(card)
for card in dealer.hand
if "Ace" in main_script.Card.__str__(card)
]
if len(aces_list) <= 1:
# variable to store value of cards with Aces in hand
dealer_ace_hand_val = hand_value
if dealer_ace_hand_val > 21:
dealer_ace_hand_val = hand_value - (
10 * (len(aces_list))
)
elif len(aces_list) >= 2:
# variable to store value of cards with Aces in hand
dealer_ace_hand_val = hand_value - (
10 * (len(aces_list) - 1)
)
if dealer_ace_hand_val > 21:
dealer_ace_hand_val = hand_value - (
10 * (len(aces_list))
)
# print the total card value of the dealer
print("Total card value: " f"{dealer_ace_hand_val}")
print()
if dealer_ace_hand_val == 21:
print("IT'S A BLACKJACK!")
print()
# show the player's current hand
print(player)
# print the total card value of the player
print("Total card value: " f"{player_ace_hand_val}")
print()
if player_ace_hand_val == 21:
print("IT'S A BLACKJACK!")
print()
# asks input from the player
if (
dealer_ace_hand_val < 18
and player_ace_hand_val >= dealer_ace_hand_val
):
input("Press Enter to continue! ")
else:
break
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# dealer deals a card to itself
dealer.add_cards(dealer.deal())
# deal announce with correct grammar depending on the card
if ("Ace" in main_script.Card.__str__(dealer.new_card)) or (
"Eight" in main_script.Card.__str__(dealer.new_card)
):
print(
f"{dealer.name} deals an {dealer.new_card} "
"to itself."
)
else:
print(
f"{dealer.name} deals a {dealer.new_card} "
"to itself."
)
print()
# show the dealer's current hand
print(dealer)
# variable to store the value of cards in hand
hand_value = sum(card.value for card in dealer.hand)
# list of Aces in hand
aces_list = [
main_script.Card.__str__(card)
for card in dealer.hand
if "Ace" in main_script.Card.__str__(card)
]
if len(aces_list) <= 1:
# variable to store the value of cards with Aces in hand
dealer_ace_hand_val = hand_value
if dealer_ace_hand_val > 21:
dealer_ace_hand_val = hand_value - (
10 * (len(aces_list))
)
elif len(aces_list) >= 2:
# variable to store the value of cards with Aces in hand
dealer_ace_hand_val = hand_value - (
10 * (len(aces_list) - 1)
)
if dealer_ace_hand_val > 21:
dealer_ace_hand_val = hand_value - (
10 * (len(aces_list))
)
# print the total card value of the player
print("Total card value: " f"{dealer_ace_hand_val}")
print()
if dealer_ace_hand_val == 21:
print("IT'S A BLACKJACK!")
print()
blackjack = True
elif dealer_ace_hand_val > 21:
print("BUST!")
print()
bust = True
# show the player's current hand
print(player)
# variable to store the value of cards in hand
hand_value = sum(card.value for card in player.hand)
# list of Aces in hand
aces_list = [
main_script.Card.__str__(card)
for card in player.hand
if "Ace" in main_script.Card.__str__(card)
]
if len(aces_list) <= 1:
# variable to store the value of cards with Aces in hand
player_ace_hand_val = hand_value
if player_ace_hand_val > 21:
player_ace_hand_val = hand_value - (
10 * (len(aces_list))
)
elif len(aces_list) >= 2:
# variable to store the value of cards with Aces in hand
player_ace_hand_val = hand_value - (
10 * (len(aces_list) - 1)
)
if player_ace_hand_val > 21:
player_ace_hand_val = hand_value - (
10 * (len(aces_list))
)
# print the total card value of the player
print("Total card value: " f"{player_ace_hand_val}")
print()
if player_ace_hand_val == 21:
print("IT'S A BLACKJACK!")
print()
blackjack = True
elif player_ace_hand_val > 21:
print("BUST!")
print()
bust = True
# break the loop if the player's hand value is lower than
# the dealer's hand value
if (
player_ace_hand_val < dealer_ace_hand_val
and dealer_ace_hand_val < 18
):
break
# ask for player input
elif dealer_ace_hand_val < 18:
input("Press Enter to continue! ")
# check for the round winner after both hands are shown
if (
player_ace_hand_val == dealer_ace_hand_val
and dealer_ace_hand_val >= 18
and dealer_ace_hand_val <= 21
):
# readjust the player balance
print("DRAW!")
print()
draw = True
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# display the player's loss and new balance
print(f"{player.name} gets the bet back.")
print(f"Your balance is: ${bank.balance}.")
print()
elif (
player_ace_hand_val < dealer_ace_hand_val
and dealer_ace_hand_val <= 21
):
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# readjust the player balance
bank.balance -= bet
print("THE DEALER WINS THE ROUND!")
print()
# display the player's loss and new balance
print(f"{player.name} loses ${bet}.")
print(f"Your new balance is: ${bank.balance}.")
print()
elif (
player_ace_hand_val > dealer_ace_hand_val
and dealer_ace_hand_val >= 18
and dealer_ace_hand_val <= 21
):
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# readjust the player balance
bank.balance += bet
print("THE PLAYER WINS THE ROUND!")
print()
# display the player's loss and new balance
print(f"{player.name} wins ${bet * 2}.")
print(f"Your new balance is: ${bank.balance}.")
print()
elif blackjack is True:
if dealer_ace_hand_val < 21 or dealer_ace_hand_val > 21:
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# readjust the player balance
bank.balance += bet
print("THE PLAYER WINS THE ROUND!")
print()
# display the player's loss and new balance
print(f"{player.name} wins ${bet * 2}.")
print(f"Your new balance is: ${bank.balance}.")
print()
elif player_ace_hand_val < 21 or player_ace_hand_val > 21:
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# readjust the player balance
bank.balance -= bet
print("THE DEALER WINS THE ROUND!")
print()
# display the player's loss and new balance
print(f"{player.name} loses ${bet}.")
print(f"Your new balance is: ${bank.balance}.")
print()
elif bust is True:
if dealer_ace_hand_val > 21:
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# readjust the player balance
bank.balance += bet
print("THE PLAYER WINS THE ROUND!")
print()
# display the player's loss and new balance
print(f"{player.name} wins ${bet * 2}.")
print(f"Your new balance is: ${bank.balance}.")
print()
elif player_ace_hand_val > 21:
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display the round for the player
print(f"Round {round}")
print()
# readjust the player balance
bank.balance -= bet
print("THE DEALER WINS THE ROUND!")
print()
# display the player's loss and new balance
print(f"{player.name} loses ${bet}.")
print(f"Your new balance is: ${bank.balance}.")
print()
break
elif hit_stay.lower() not in ("h", "hit", "s", "stay"):
print("Invalid input, please try again!")
print()
# reset the variable to avoid an infinite loop
hit_stay = "None"
continue
while True:
# check for the threshold to end the game
if bank.balance >= threshold:
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display winning message
messages.win()
print()
if bank.balance == threshold:
print(
"CONGRATULATIONS, YOU HIT THE TRESHOLD OF "
f"${threshold} AND WON THE GAME!"
)
print()
elif bank.balance > threshold:
print(
"CONGRATULATIONS, YOU SURPASSED THE TRESHOLD OF "
f"${threshold} WITH A BALANCE OF ${bank.balance} "
"AND WON THE GAME!"
)
# set the continue to no, so it instantly ends the game
game_continue = "n"
elif bank.balance <= 0:
input("Press Enter to continue! ")
# cleans the terminal
print("\n" * 100)
# display losing message
messages.lose()
print()
if bank.balance == 0:
print(f"YOUR BALANCE HIT 0!")
# set the continue to no, so it instantly ends the game
game_continue = "n"
# check if there is already something assigned to game_continue
if game_continue == "None":
# ask the player if the game should continue or not
game_continue = input(
"Would you like to continue playing? (Yes/No) "
)
if game_continue.lower() in ("y", "yes"):
print("\n" * 100)
print(f"Round {round + 1}")
main_phase = False
break
elif game_continue.lower() in ("n", "no"):
game_on = False
main_phase = False
break
elif game_continue.lower() not in ("y", "yes", "n", "no"):
print("Invalid input, please try again!")
print()
# reset the variable to avoid an infinite loop
game_continue = "None"
continue
# end the game
elif start.lower() in ("n", "no"):
game_on = False
# show the rules
elif start.lower() in ("h", "help"):
# clears the terminal
print("\n" * 100)
messages.rules()
# reset the variable to avoid an infinite loop
start = "None"
# clears the terminal
print("\n" * 100)
# display the welcoming message
messages.welcome()
print()
print("Type 'help' to see the rules.")
# invalid input warning
elif start.lower() not in ("y", "yes", "n", "no", "h", "help"):
print("Invalid input, please try again!")
print()
# reset the variable to avoid an infinite loop
start = "None"
continue