-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcardgame.py
More file actions
62 lines (53 loc) · 1.3 KB
/
cardgame.py
File metadata and controls
62 lines (53 loc) · 1.3 KB
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
# Card Game API
import random
def fresh_deck():
suits = {"Spade", "Heart", "Diamond", "Club"}
ranks = {"A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"}
deck = []
for s in suits:
for r in ranks:
card = (s, r)
deck.append(card)
random.shuffle(deck)
return deck
# deck = fresh_deck()
# print(deck)
def hit(deck):
if deck == []:
deck = fresh_deck()
return deck[0], deck[1:]
# card, deck = hit(deck)
# print(card)
def count_score(cards):
score = 0
number_of_aces = 0
for card in cards:
rank = card[1]
if rank == 'A':
score += 11
number_of_aces += 1
elif rank in {'J', 'Q', 'K'}:
score += 10
else:
score += rank
while score > 21 and number_of_aces > 0:
score -= 10
number_of_aces -= 1
if score > 21:
score = 0
return score
def count_baccarat(cards):
score = 0
for card in cards:
rank = card[1]
if rank == 'A':
score = score + 1
elif rank in {'J', 'Q', 'K'}:
score = score
else:
score += rank
return score%10
def show_cards(cards, message):
for card in cards:
message = message + '\n' + str(card[0]) + ' '+ str(card[1])
return message