-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
193 lines (162 loc) · 6.67 KB
/
game.py
File metadata and controls
193 lines (162 loc) · 6.67 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
from constants import chips
from card_components import Deck, Hand
from players import Player, Dealer
from account import Account
import time
class BlackjackGame:
def __init__(self, player_name, initial_balance=10000):
self.deck = Deck()
self.player = Player(player_name)
self.dealer = Dealer("Dealer")
self.player_account = Account(player_name, initial_balance)
self.current_bet = 0
self.player_hand = None
self.dealer_hand = None
def place_bet(self):
while True:
print("\nAvailable chips:", ', '.join(f"{color}(${value})" for color, value in chips.items()))
print(f"Current balance: {self.player_account.get_balance()}")
bet_input = input("\nEnter your bet using chip colors (e.g., '2 black 1 green' for $225): ")
total_bet = 0
try:
bet_parts = bet_input.lower().split()
for i in range(0, len(bet_parts), 2):
count = int(bet_parts[i])
color = bet_parts[i + 1]
if color not in chips:
print("Invalid chip color!")
else:
total_bet += count * chips[color]
break
if total_bet <= float(self.player_account.get_balance().replace('$', '')):
self.current_bet = total_bet
self.player_account.withdraw(total_bet)
print(f"\nBet placed: ${total_bet}")
break
else:
print("Insufficient funds!")
except (ValueError, IndexError):
print("Invalid bet format! Use: <number> <color> [<number> <color> ...]")
def deal_initial_cards(self):
self.deck.shuffle()
self.player_hand = Hand()
self.dealer_hand = Hand()
# Deal cards alternately
self.player_hand.add_card(self.deck.deal_one())
self.dealer_hand.add_card(self.deck.deal_one())
self.player_hand.add_card(self.deck.deal_one())
self.dealer_hand.add_card(self.deck.deal_one())
def player_turn(self):
while True:
print("\nYour hand:")
self.player_hand.print_hand()
print(f"Your total: {self.player_hand.value}")
print("\nDealer's hand:")
self.dealer_hand.print_hand(hide_first=True)
if self.player_hand.value == 21:
print("BLACKJACK!")
time.sleep(2)
return
if self.player_hand.value > 21:
print("Bust!")
time.sleep(1.5)
return
action = input("\nWhat would you like to do? (hit/stand/double): ").lower()
if action == 'hit':
self.player_hand.add_card(self.deck.deal_one())
elif action == 'double':
if len(self.player_hand.cards) == 2:
if self.current_bet <= float(self.player_account.get_balance().replace('$', '')):
self.player_account.withdraw(self.current_bet)
self.current_bet *= 2
self.player_hand.add_card(self.deck.deal_one())
print("\nYour final hand:")
self.player_hand.print_hand()
print(f"Your total: {self.player_hand.value}")
return
else:
print("Insufficient funds to double down!")
else:
print("Can only double down on initial hand!")
elif action == 'stand':
return
def dealer_turn(self):
print("\nDealer's full hand:")
self.dealer_hand.print_hand()
while self.dealer_hand.value < 17:
self.dealer_hand.add_card(self.deck.deal_one())
print("\nDealer hits:")
self.dealer_hand.print_hand()
print(f"Dealer's total: {self.dealer_hand.value}")
def determine_winner(self):
player_value = self.player_hand.value
dealer_value = self.dealer_hand.value
print(f"\nYour total: {player_value}")
print(f"Dealer's total: {dealer_value}")
if player_value > 21:
print("You bust! Dealer wins!")
time.sleep(1.5)
return False
elif dealer_value > 21:
print("Dealer busts! You win!")
self.player_account.deposit(self.current_bet * 2)
time.sleep(1.5)
return True
elif player_value > dealer_value:
print("You win!")
self.player_account.deposit(self.current_bet * 2)
time.sleep(1.5)
return True
elif dealer_value > player_value:
print("Dealer wins!")
time.sleep(1.5)
return False
else:
print("Push!")
self.player_account.deposit(self.current_bet)
return None
def play_again(self):
yes = ['Yes', 'yes', 'Y', 'y']
no = ['No', 'no', 'N', 'n']
correct_response = False
while not correct_response:
replay = input('Would you like to play again ([y]es, [n]o)? ')
if not replay.isascii():
print('Sorry, that\'s not a word/letter...')
continue
if replay in yes:
print('Let me give you a hand...')
time.sleep(1.5)
return True
elif replay in no:
print(f"\nFinal balance: {self.player_account.get_balance()}")
print("Tanoshū gozaimashita! Jaa mata ne...")
time.sleep(1)
return False
else:
print('You must answer, "[y]es, [n]o."')
def play(self):
game_on = True
while game_on:
# Start new round
print("\n" + "=" * 50)
print(f"Grab a seat, {self.player.name}!")
self.place_bet()
self.deal_initial_cards()
self.player_turn()
if self.player_hand.value <= 21:
self.dealer_turn()
self.determine_winner()
if self.player_account.get_balance() == "$ 0":
print(f"You have no more chips (balance: {self.player_account.get_balance()})!")
time.sleep(2)
print("I'mma need you to part like Moses...")
time.sleep(2)
print("Jaa mata ne!")
break
game_on = self.play_again()
if not game_on:
break
# Reset deck if running low
if len(self.deck.all_cards) < 15:
self.deck = Deck()