forked from ZipCodeCore/PythonFundamentals.Labs.BlackJack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
199 lines (174 loc) · 5.27 KB
/
utils.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
import time
from classes import Deck, Hand
'''
Functions needed for Black Jack Game
in alphabetical order (except end_game() which is at the end.)
'''
def blackjack(name: Hand):
'''
Lets user know who has Black Jack
'''
print(f'{name.name} has black Jack')
print(f'{name.name} Wins')
def bust(name: Hand):
'''
Lets user know who hast bust
'''
time.sleep(1)
print(f"BUST! {name.name} loses")
def dealer_sim(deck: Deck, player: Hand, dealer: Hand):
'''
After player has made all choices, this function determines if player has blackjack or bust
Or runs through the dealers scenario and decides when to hit or Stand. Then returns outcome.
'''
if player.count == 21:
return blackjack(player)
if player.count > 21:
return bust(player)
else:
print("Dealer's turns")
time.sleep(1)
show_hand(dealer)
time.sleep(2)
while dealer.count <= 17:
print('Dealer Hits..')
time.sleep(1)
hit(deck, dealer)
show_hand(dealer)
time.sleep(1)
if dealer.count > 21:
bust(dealer)
elif dealer.count == 21:
blackjack(dealer)
elif dealer.count > 17:
if dealer.count > player.count:
time.sleep(1)
print('Dealer Stands')
time.sleep(1)
show_hand(player)
time.sleep(1)
winner(dealer)
if dealer.count < player.count:
if player.count == 21:
blackjack(player)
else:
print('Dealer Stands')
time.sleep(1)
winner(player)
elif dealer.count == player.count:
push(dealer)
def first_hand(player: Hand, dealer: Hand):
'''
Shows the first hand with one dealer card not turned over. Evaluates if player has 21
'''
print(f"{dealer.name}'s hand")
print(f'[], {dealer.cards[1]}')
time.sleep(2)
print('\n')
print(f"{player.cards[0]} {player.cards[1]} {player.count}")
print(f"{player.name}'s hand")
if player.count == 21:
blackjack(player)
def hit(deck: Deck, name: Hand):
'''
Function draws a card from the deck and adds it to the hand.
and the ace difference is taken into consideration.
'''
hit = deck.deal()
name.draw_card(hit)
def play_again():
'''
Starts the game over, or lets user end game.
'''
i = input('Would you like to play again? (y/n): ')
if i == 'y':
return
elif i == 'n':
end_game()
else:
print('Invalid input')
play_again()
def play_hand(deck: Deck, name: Hand):
'''
This is where the player decides to hit or stay
'''
while name.count < 21:
I = input("h: to Hit or s: to Stand: ")
I = I.lower()
if I == 'h':
hit(deck, name)
time.sleep(1)
show_hand(name)
elif I == 's':
print("Player Stands")
break
else:
print('Invalid input')
def winner(name):
'''
Lets the player know that the player has won
'''
show_hand(name)
time.sleep(1)
print(f'{name.name} Wins')
def push(name):
'''
Calls the out come of the game, when dealer and player have the same count, push is invoked
'''
show_hand(name)
print('Push, No Winner!')
def show_hand(name: Hand):
'''
Function shows the hand specific to the instance of the parameter its given
'''
H = [card for card in name.cards]
print(f"{name.name}'s hand:{H}{name.count}")
def reset_deck(player: Hand, dealer: Hand):
player.cards = []
dealer.cards = []
def end_game():
print(" \ /")
time.sleep(.1)
print(" \ /")
time.sleep(.1)
print(" \ Thanks For Playing /")
time.sleep(.1)
print(" ] [ ,'|")
time.sleep(.1)
print(" ] [ / |")
time.sleep(.1)
print(" ]___ ___[ ,' |")
time.sleep(.1)
print(" ] ]\ /[ [ |: |")
time.sleep(.1)
print(" ] ] \ / [ [ |: |")
time.sleep(.1)
print(" ] ] ] [ [ [ |: |")
time.sleep(.1)
print(" ] ] ]__ __[ [ [ |: |")
time.sleep(.1)
print(" ] ] ] ]\ _ /[ [ [ [ |: |")
time.sleep(.1)
print(" ] ] ] ] (#) [ [ [ [ :==== '")
time.sleep(.1)
print(" ] ] ]_].nHn.[_[ [ [")
time.sleep(.1)
print(" ] ] ] HHHHH. [ [ [")
time.sleep(.1)
print(' ] ] / `HH("N \ [ [')
time.sleep(.1)
print(" ]__]/ HHH ' \[__[")
time.sleep(.1)
print(" ] NNN [")
time.sleep(.1)
print(" ] N/' [")
time.sleep(.1)
print(" ] N H [")
time.sleep(.1)
print(" / N \ ")
time.sleep(.1)
print(" / q, \ ")
time.sleep(.1)
print(" / \ ")
time.sleep(.1)
quit()