generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
442 lines (397 loc) · 14.1 KB
/
run.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
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
import random
import sys
import pyfiglet
import time
suits = ["Diamonds", "Hearts", "Clubs", "Spades"]
ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"]
values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
def game_introduction():
"""
Displays an introduction / welcome message
to the player, asks their name and if they
would like to read the rules or play the game.
"""
# name input and welcome messaging
print("\n")
print(pyfiglet.figlet_format("WELCOME TO\nBLACKJACK !"))
while True:
name = typing_input("Please enter your player name: \n").capitalize()
if (name != "Computer") and (name != ""):
print("\n")
print(pyfiglet.figlet_format(f"Hello {name} !"))
break
elif name == "Computer":
typing_print("\nSorry. You can't choose that name."
" Please try again...\n")
else:
typing_print("\nSorry. You chose a blank name."
" Please try again...\n")
# input for game or rules option
while True:
option = input("Would you like to read the rules 'R'"
" or play the game 'P'?: \n").lower()
if option == "r":
display_rules()
elif option == "p":
print("\n")
print(pyfiglet.figlet_format("Let's Play ..."))
play_game()
else:
print("Sorry. You can only select 'R' (rules)"
" or 'P' (play game)...\n")
def create_card_pack():
"""
Creates an ordered list (pack) of playing cards as
strings, converts list to dictionary (cardpack) to add
card values to card keys, converts dictionary (cardpack)
back to list (pack), shuffles list (pack) and converts
shuffled list (pack) to shuffled dictionary (pack)
"""
pack = []
for suit in suits:
for rank in ranks:
pack.append(rank + ' of ' + suit)
cardpack = dict(zip(pack, values*4))
pack = list(cardpack.items())
typing_print("Shuffling the pack...\n")
time.sleep(0.5)
random.shuffle(pack)
pack = dict(pack)
return pack
def create_computer_hand(pack):
"""
Selects two cards for the computer from
the shuffled pack, adds them to a new list,
prints the first card, converts list to
dictionary and sums values to get score
"""
computer_hand = []
while len(computer_hand) < 2:
computer_hand.append(pack.popitem())
typing_print("Dealing out the cards...\n")
time.sleep(0.5)
typing_print("\nThe computer's show card is: ")
time.sleep(0.5)
print(f"\n{computer_hand[0][0]}")
time.sleep(0.5)
computer_hand = dict(computer_hand)
return computer_hand
def create_player_hand(pack):
"""
Removes two cards for the player
from the shuffled dictionary (pack),
adds them to a new list (player_hand)
and converts list to dictionary
"""
player_hand = []
while len(player_hand) < 2:
player_hand.append(pack.popitem())
player_hand = dict(player_hand)
player_score = sum(player_hand.values())
# adjust score and ace values in event of two aces
player_score = player_ace_values(player_hand)
typing_print("\nYour cards are: \n")
time.sleep(0.5)
for keys, value in player_hand.items():
print(keys)
time.sleep(0.5)
your_score = (
"Your score is: "
f"{player_score}\n\n"
)
typing_print(f"\n{your_score}")
time.sleep(0.5)
return player_hand
def player_choice(pack, player_hand):
"""
Asks the player if they would like to Stick with their
current hand or Twist (receive another card)
"""
player_score = player_ace_values(player_hand)
if player_score == 21:
time.sleep(1.0)
print(pyfiglet.figlet_format("Blackjack !"))
return player_score
else:
while player_score < 21:
stick_twist = input("Would you like to Stick 'S'"
" or Twist 'T'?: \n").lower()
if stick_twist == "t":
time.sleep(0.5)
print("You chose to Twist...\n")
player_hand = twist(pack, player_hand)
player_score = sum(player_hand.values())
continue
elif stick_twist == "s":
print("")
you_chose = (
"You chose to stick with "
f"{player_score} points\n"
)
print(you_chose)
return player_score
break
else:
print("Error. You can only select Stick 'S'"
" or Twist 'T'!\n")
continue
return player_score
def twist(pack, player_hand):
"""
Draws a new card from the deck, adds to player_hand
and totals player_score
"""
player_hand_list = list(player_hand.items())
time.sleep(0.5)
typing_print("Dealing you a new card...\n")
player_hand_list.append(pack.popitem())
time.sleep(0.5)
new_card = (
f"\nYour new card is "
f"the {player_hand_list[-1][0]}\n"
)
typing_print(new_card)
player_hand = dict(player_hand_list)
player_score = sum(player_hand.values())
time.sleep(0.5)
typing_print("\nYour cards are: \n")
time.sleep(0.5)
for keys, value in player_hand.items():
print(keys)
# adjust score and ace values if score > 21
player_score = player_ace_values(player_hand)
time.sleep(0.5)
typing_print(f"\nYour score is: {player_score}\n\n")
time.sleep(0.5)
if player_score < 21:
return player_hand
elif player_score > 21:
time.sleep(1.0)
print(pyfiglet.figlet_format("BUST !"))
return player_hand
elif player_score == 21:
time.sleep(1.0)
print(pyfiglet.figlet_format("BLACKJACK !"))
return player_hand
def player_ace_values(player_hand):
"""
Checks player hand for aces if score greater is than 21
and changes one Ace value from 11 to 1, while score
remains higher than 21.
"""
player_score = sum(player_hand.values())
if player_score > 21:
for key, value in player_hand.items():
if "Ace" in key and value == 11:
updated_value = {key: 1}
player_hand.update(updated_value)
update_score = sum(player_hand.values())
if update_score > 21:
continue
else:
player_score = update_score
break
return player_score
def computer_ace_values(computer_hand):
"""
Checks computer hand for aces if score greater than 21
and changes Ace value from 11 to 1, while score
remains higher than 21
"""
computer_score = sum(computer_hand.values())
if computer_score > 21:
for key, value in computer_hand.items():
if "Ace" in key and value == 11:
updated_value = {key: 1}
computer_hand.update(updated_value)
update_score = sum(computer_hand.values())
if update_score > 21:
continue
else:
computer_score = update_score
break
return computer_score
def computer_twist(pack, computer_hand):
"""
Selects new cards for computer hand until score
reaches a random total approaching the game limit
"""
computer_score = sum(computer_hand.values())
# adjust score and ace values if score > 21
computer_score = computer_ace_values(computer_hand)
while computer_score <= random.choice(range(16, 19)):
time.sleep(0.5)
print("\nThe computer selected Twist\n")
time.sleep(0.5)
typing_print("Dealing the computer a new card\n\n")
computer_hand_list = list(computer_hand.items())
computer_hand_list.append(pack.popitem())
time.sleep(0.5)
comp_card = (
"The computer's new card is "
f"the {computer_hand_list[-1][0]}\n"
)
typing_print(comp_card)
computer_hand = dict(computer_hand_list)
computer_score = sum(computer_hand.values())
# adjust score and ace values if score > 21
computer_score = computer_ace_values(computer_hand)
time.sleep(0.5)
print("The computer chose to Stick\n")
time.sleep(0.5)
typing_print("The computer has: \n")
time.sleep(0.5)
for keys, value in computer_hand.items():
print(keys)
time.sleep(0.5)
typing_print(f"\nThe computer scored: {computer_score}\n\n")
if computer_score == 21:
time.sleep(1.0)
print(pyfiglet.figlet_format("BLACKJACK !"))
return computer_score
elif computer_score > 21:
time.sleep(1.0)
print(pyfiglet.figlet_format("BUST !"))
return computer_score
else:
return computer_score
def compare_scores(player_score, computer_score):
"""
Prints the players score again and then
compares scores to decide on the winner and
increments the game wins values in the dictionary
"""
typing_print(f"You scored: {player_score}\n\n")
time.sleep(1.0)
if player_score == 21:
if computer_score > 21:
print("You won with BlackJack!!!\n")
if "Player Wins" in game_wins:
game_wins["Player Wins"] += 1
elif computer_score == 21:
print("You both got Blackjack!!! It's a draw!\n")
else:
print("You won with BlackJack!!!\n")
if "Player Wins" in game_wins:
game_wins["Player Wins"] += 1
elif player_score > 21:
if computer_score == 21:
print("The computer got Blackjack!!! You lost the game!\n")
if "Computer Wins" in game_wins:
game_wins["Computer Wins"] += 1
elif computer_score > 21:
print("You both went BUST!!!\n")
elif computer_score < 21:
print("You lost the game!\n")
if "Computer Wins" in game_wins:
game_wins["Computer Wins"] += 1
else:
if computer_score == 21:
print("The computer got Blackjack!!! You lost the game!\n")
if "Computer Wins" in game_wins:
game_wins["Computer Wins"] += 1
elif computer_score > 21:
print("The computer went BUST!!! You won the game!\n")
if "Player Wins" in game_wins:
game_wins["Player Wins"] += 1
else:
if computer_score < player_score:
print("You won the game!\n")
if "Player Wins" in game_wins:
game_wins["Player Wins"] += 1
elif computer_score > player_score:
print("You lost the game!\n")
if "Computer Wins" in game_wins:
game_wins["Computer Wins"] += 1
else:
print("It's a draw!\n")
time.sleep(1.0)
print(f'Player Wins: {game_wins["Player Wins"]}')
print(f'Computer Wins: {game_wins["Computer Wins"]}\n')
def play_again():
"""
Asks user whether they would like to restart the game or end the game
"""
time.sleep(1.0)
new_game = input("Would you like to play again? (Y or N): \n").lower()
if new_game == "y":
print("\n")
print(pyfiglet.figlet_format("Let's Play ..."))
play_game()
elif new_game == "n":
time.sleep(0.5)
print("\n")
print(pyfiglet.figlet_format("Thanks for\nplaying\nBlackjack !!!"))
sys.exit()
else:
print("Sorry. You can only select 'Y' or 'N'. Please try again.\n")
play_again()
def play_game():
"""
Starts and runs the game
"""
pack = create_card_pack()
computer_hand = create_computer_hand(pack)
player_hand = create_player_hand(pack)
player_score = player_choice(pack, player_hand)
computer_score = computer_twist(pack, computer_hand)
compare_scores(player_score, computer_score)
play_again()
def display_rules():
"""
Displays the rules of the game
"""
print("""\n
BLACKJACK RULES AND GAMEPLAY
The aim of the game is to score more points than the computer,
without scoring more than 21 points.
Points are awarded equal to the number on the playing cards
dealt to each player.
Picture cards (Jack, Queen and King) are worth 10 points each.
The Ace is worth 11 points OR 1 point, depending on the total score.
When the game starts, players are dealt 2 cards each from the shuffled
pack of cards. You will only 'see' one of the computer's cards.
If you have a score less than 21 points, you will have the option
to either STICK with the cards you have or TWIST and receive another card.
If you score 21 points with your first 2 cards,
you score BLACKJACK and the game continues.
If you score more than 21 points, you will be BUST and the game continues.
The computer will choose to 'TWIST' or 'STICK' - which finishes the game.""")
# input to start or exit the game
option = input("\n\nPress 'P' to Play or 'X' to exit: \n").lower()
if option == "x":
print("\n")
print(pyfiglet.figlet_format("Come back\nsoon !!!"))
sys.exit()
elif option == "p":
print("\n")
print(pyfiglet.figlet_format("Let's Play ..."))
play_game()
else:
print("\nSorry. You can only select 'P' (play game)"
" or 'X' (exit game)...\n")
def typing_print(text):
"""
Controls for typing effect for print statements.
Not my code. Typing text code borrowed with thanks from:
https://www.101computing.net/python-typing-text-effect/
"""
for character in text:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
def typing_input(text):
"""
Controls for typing effect for input print statements
Not my code. Typing text code borrowed with thanks from:
https://www.101computing.net/python-typing-text-effect/
"""
for character in text:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
value = input()
return value
game_wins = {"Player Wins": 0, "Computer Wins": 0}
game_introduction()