-
Notifications
You must be signed in to change notification settings - Fork 1
/
display.py
56 lines (46 loc) · 1.57 KB
/
display.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
# coding=utf-8
suits = {
"CLUBS": "♣",
"DIAMONDS": "♦",
"HEARTS": "♥",
"SPADES": "♠"
}
def print_table(cards, show_all_dealer_cards):
if show_all_dealer_cards:
card_names = list(map(lambda card: get_card_name(card), cards["dealer"]))
print("Dealer has:\n{dealers_cards} \n(Total: {total})\n".format(
dealers_cards="\n".join(card_names),
total=get_total(cards["dealer"])
))
else:
print("\nDealer is showing:\n{card}\n\n".format(
card=get_card_name(cards["dealer"][0])
))
card_names = list(map(lambda card: get_card_name(card), cards["player"]))
print("You have:\n{players_cards}\n(Total: {total})\n".format(
players_cards="\n".join(card_names),
total=get_total(cards["player"])
))
def get_card_name(card):
return "{value} {suit}".format(
value=card["value"],
suit=suits[card["suit"]]
)
def get_total(cards):
value = 0
# Add up all cards
for card in cards:
if card["value"] == "JACK" or card["value"] == "QUEEN" or card["value"] == "KING":
value += 10
elif card["value"] == "ACE":
value += 11
else:
value += int(card["value"])
# Special case, handle aces because they can be 1 or 11. If total is greater than 21, try to reduce
if value > 21:
for card in cards:
if card["value"] == "ACE":
value -= 10 # 11 - 1 is 10
if value <= 21:
break
return value