-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblackjack.py
61 lines (45 loc) · 1.67 KB
/
blackjack.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
from api_consumer import shuffle
from game_actions import deal_cards, draw_cards, dealer_hit
from display import print_table, get_total
deck_id = shuffle()
# Get initial set of cards
cards = deal_cards(deck_id)
while True:
print_table(cards, False)
# Ask user what action they want to take
answer = input("Do you want to hit (h) or stand (s)?\n")
print("\n")
if len(answer) == 0:
continue
# Process user input
if answer.lower()[0] == "h":
cards['player'].append(draw_cards(deck_id, 1)[0])
# Repeat if the total is less than or equal to 21
if get_total(cards["player"]) <= 21:
continue
elif answer.lower()[0] != "s":
continue
# See if the user has busted
if get_total(cards["player"]) > 21:
print_table(cards, True)
print("\nSorry, you have busted.\n")
else:
# Dealer draws card(s) if necessary
dealer_hit(deck_id, cards)
print_table(cards, True)
# Print game results
if get_total(cards["dealer"]) > 21:
print ("\nDealer busts, you win!\n")
elif get_total(cards["player"]) > get_total(cards["dealer"]):
print("\nYou win!\n")
elif get_total(cards["player"]) < get_total(cards["dealer"]):
print("\nYou lost\n")
elif get_total(cards["player"]) == get_total(cards["dealer"]):
print("\nYou pushed\n")
play_again = input("Do you want to play again (y/n)?")
if len(play_again) == 0 or play_again.lower()[0] == "y":
deck_id = shuffle()
cards = deal_cards(deck_id)
continue
else:
break