-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
115 lines (95 loc) Β· 4.18 KB
/
Game.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
from deck import Deck
from hand import Hand
class Game:
def play(self):
game_number = 0
games_to_play = 0
while games_to_play <= 0:
try:
games_to_play = int(input("Enter number of games to play (0 or any key to exit): "))
if games_to_play == 0:
print("Thank you! Hope to see you again!")
return 0
except:
print("Thank you! Hope to see you again!")
return 0
while game_number < games_to_play:
game_number += 1
deck = Deck()
deck.shuffle()
player_hand = Hand()
dealer_hand = Hand(dealer=True)
for i in range(2):
player_hand.add_card(deck.deal(1))
dealer_hand.add_card(deck.deal(1))
print()
print("|--------------------" * 1)
print(f"| Game {game_number} of {games_to_play}")
print("|--------------------" * 1)
print()
player_hand.display()
dealer_hand.display()
if self.check_winner(player_hand, dealer_hand):
continue
choice = ""
while player_hand.get_value() < 21 and choice not in ["s", "stand"]:
choice = input("Please choose 'Hit' or 'Stand' (h/s): ").lower()
print()
while choice not in ["h", "s", "hit", "stand"]:
choice = input("Please enter 'Hit' or 'Stand' (h/s) ").lower()
print()
if choice in ["hit", "h"]:
player_hand.add_card(deck.deal(1))
player_hand.display()
if self.check_winner(player_hand, dealer_hand):
continue
player_hand_value = player_hand.get_value()
dealer_hand_value = dealer_hand.get_value()
while dealer_hand_value < 17:
dealer_hand.add_card(deck.deal(1))
dealer_hand_value = dealer_hand.get_value()
dealer_hand.display(show_all_dealer_cards=True)
if self.check_winner(player_hand, dealer_hand):
continue
self.show_values(player_hand,dealer_hand)
# print("----- RESULT -----")
# print("Your hand:", player_hand_value)
# print("Dealer's hand:", dealer_hand_value)
self.check_winner(player_hand, dealer_hand, True)
print("\nThanks for playing!")
def show_values(self, player_hand,dealer_hand):
print("----- RESULT -----")
print("PLAYER:", player_hand.get_value())
print("DEALER:", dealer_hand.get_value())
print("------------------")
def check_winner(self, player_hand, dealer_hand, game_over=False):
if not game_over:
if player_hand.get_value() > 21:
self.show_values(player_hand,dealer_hand)
print("Busted! Dealer wins. π")
return True
elif dealer_hand.get_value() > 21:
self.show_values(player_hand,dealer_hand)
print("Dealer busted. You win! ππ")
return True
elif dealer_hand.is_blackjack() and player_hand.is_blackjack():
self.show_values(player_hand,dealer_hand)
print("Push (Tie). Bets off. π")
return True
elif player_hand.is_blackjack():
self.show_values(player_hand,dealer_hand)
print("Blackjack! You win! ππ")
return True
elif dealer_hand.is_blackjack():
self.show_values(player_hand,dealer_hand)
print("Dealer has blackjack. Dealer wins. π")
return True
else:
if player_hand.get_value() > dealer_hand.get_value():
print("You win! ππ")
elif player_hand.get_value() == dealer_hand.get_value():
print("Push (Tie). Bets off. π")
else:
print("Dealer wins. π")
return True
return False