-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
203 lines (182 loc) · 8.16 KB
/
main.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
import random
import sys
game_deck = ([2, 3, 4, 5, 6, 7, 8, 9, 10,
'Jack', 'Queen', 'King', 'Ace']*4)
round_count = 0
class Player:
def __init__(self, name, p_cards, p_score, win_loss):
self.name = name
self.p_cards = p_cards
self.p_score = p_score
self.win_loss = win_loss
# Function for player to draw initial cards
def draw_hand(self, p_cards=None):
global game_deck
if p_cards is None:
p_cards = []
# Need to shuffle deck, otherwise cards will be in order defined in game_deck
random.shuffle(game_deck)
p_cards.append(game_deck[0])
p_cards.append(game_deck[1])
for c in p_cards:
game_deck.remove(c)
self.p_cards = p_cards
# Calculate score function, which is called after every time a player is hit to check for win (21) condition
def calculate_score(self, p_cards, p_score) -> int:
if isinstance(p_score, tuple):
p_score = int(p_score)
else:
p_score = 0
for c in p_cards:
if isinstance(c, (int)):
p_score += c
else:
if c == 'Jack' or c == 'Queen' or c == 'King':
p_score += 10
else:
# Special logic for player to select either 1 or 11 as value for a drawn Ace
print(f"{self.name}'s score is currently: {p_score} and your cards are: {p_cards}")
print("Would you like your Ace to be scored as a 1 or an 11?")
choice = input()
if choice == '1' or '11:':
choice = int(choice)
p_score += choice
for p in range(len(p_cards)):
if p_cards[p] == 'Ace':
p_cards[p] = choice
else:
print("Selection invalid, choosing 1 by default")
p_score =+ 1
self.p_score = p_score
print(f"This is {self.name}'s current score: {p_score}")
# Player hits need to alter the global game deck and remove drawn cards from pool
def player_hit(p_cards):
global game_deck
player1.p_cards.append(game_deck[0])
game_deck.remove(player1.p_cards[len(player1.p_cards)-1])
def dealer_hit(p_cards):
global game_deck
dealer.p_cards.append(game_deck[0])
game_deck.remove(dealer.p_cards[len(dealer.p_cards)-1])
# Function to reset the game_deck, player score/cards, and dealer score/cards after round
def player_reset(p_cards, p_score):
global game_deck
game_deck.clear()
game_deck = ([2, 3, 4, 5, 6, 7, 8, 9, 10,
'Jack', 'Queen', 'King', 'Ace']*4)
global round_count
round_count += 1
player1.p_cards.clear()
player1.p_score = 0
def dealer_reset(p_cards, p_score):
dealer.p_cards.clear()
dealer.p_score == 0
# Initializes game and defines player
def game_init() -> Player:
print("Hello and welcome to the casino! What is your name?")
player_name = input()
player1 = Player(f"{player_name}", None, 0, win_loss={'Wins': 0, 'Losses': 0})
return player1
player1 = game_init()
def game_actions(p_cards, p_score):
if player1.p_cards == None or player1.p_cards == []:
print("Here are your cards...")
player1.draw_hand()
print(player1.p_cards)
player1.calculate_score(player1.p_cards, player1.p_score)
while player1.p_score < 21:
try:
print("Select action: Hit | Stand | View Cards | Quit")
action = input()
if action == 'Hit' or action == 'hit':
player1.player_hit()
print(f"You drew a: {player1.p_cards[(len(player1.p_cards)-1)]}")
player1.calculate_score(player1.p_cards, player1.p_score)
if player1.p_score == 21:
print("21! You win!")
player1.win_loss['Wins'] +=1
print("Play again? Y/N")
play_again = input()
if play_again == 'Y' or play_again == 'y':
player1.player_reset(p_score)
return game_actions(p_cards, p_score)
elif play_again == 'N' or play_again == 'n':
# Run player_reset to increment the round counter
player1.player_reset(p_score)
sys.exit()
elif player1.p_score > 21:
print("Bust! You lose!")
player1.win_loss['Losses'] +=1
print("Play again? Y/N")
play_again = input()
if play_again == 'Y' or play_again == 'y':
player1.player_reset(p_score)
return game_actions(p_cards, p_score)
elif play_again == 'N' or play_again == 'n':
# Run player_reset to increment the round counter
player1.player_reset(p_score)
sys.exit()
elif action == 'Stand' or action == 'stand':
print("Awaiting dealer turn...")
if round_count > 1:
return dealer_loop(p_cards = dealer.p_cards, p_score = dealer.p_score)
else:
break
elif action == 'View Cards' or action == 'view cards':
print(player1.p_cards)
elif action == 'Quit' or action == 'quit':
sys.exit()
else:
print("I'm sorry. That command is not recognized, please select another option...")
return game_actions(p_cards = None, p_score = 0)
except:
print("Something went wrong")
sys.exit()
game_actions(p_cards = player1.p_cards, p_score = player1.p_score)
# Creates dealer instance before dealer turn
dealer = Player("Dealer", None, 0, None)
def dealer_loop(p_cards, p_score):
dealer.draw_hand()
print(dealer.p_cards)
dealer.calculate_score(dealer.p_cards, dealer.p_score)
try:
# Dealer will hit until they exceed player's score
# Player will need to choose 11 or 1 for dealer
while dealer.p_score <= player1.p_score:
dealer.dealer_hit()
print("The dealer draws a card...")
print(dealer.p_cards)
dealer.calculate_score(dealer.p_cards, dealer.p_score)
if dealer.p_score > 21:
print('Dealer busts! You win!')
player1.win_loss['Wins'] +=1
print("Play again? Y/N")
play_again = input()
if play_again == 'Y' or play_again == 'y':
player1.player_reset(p_score)
dealer.player_reset(p_score)
return game_actions(p_cards, p_score)
elif play_again == 'N' or play_again == 'n':
# Run player_reset to increment the round counter
player1.player_reset(p_score)
sys.exit()
elif 21 >= dealer.p_score > player1.p_score:
print('Dealer wins!')
player1.win_loss['Losses'] +=1
print("Play again? Y/N")
play_again = input()
if play_again == 'Y' or play_again == 'y':
player1.player_reset(p_score)
dealer.player_reset(p_score)
return game_actions(p_cards, p_score)
elif play_again == 'N' or play_again == 'n':
# Run player_reset to increment the round counter
player1.player_reset(p_score)
sys.exit()
return game_actions(p_cards, p_score)
except:
global round_count
print("Game summary:" + "\n" + f"Wins:{player1.win_loss['Wins']}" + "\n" +
f"Losses:{player1.win_loss['Losses']}" + "\n" + f"Rounds:{round_count}")
sys.exit()
dealer_loop(p_cards = dealer.p_cards, p_score = dealer.p_score)