-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.py
More file actions
55 lines (44 loc) · 1.15 KB
/
Copy pathrules.py
File metadata and controls
55 lines (44 loc) · 1.15 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
'''
rules.py
A compendium of blackjack rules and numbers
'''
from typing import Tuple
CARDS_PER_SUIT = 13
SUITS_PER_DECK = 4
CARDS_PER_DECK = CARDS_PER_SUIT * SUITS_PER_DECK
CARD_VALUES = {
"2" : 2, "3" : 3, "4" : 4, "5" : 5, "6" : 6, "7" : 7, "8" : 8, "9" : 9,
"X" : 10, "A" : 1
}
CARD_INDEXES = {
"2" : 0, "3" : 1, "4" : 2, "5" : 3, "6" : 4, "7" : 5, "8" : 6, "9" : 7,
"X" : 8, "A" : 9
}
CARD_FACES = "23456789XXXXA"
HAND_VALUE = Tuple[int, bool]
def hand_value(cards:str) -> HAND_VALUE:
'''
Returns the value of a hand
cards is a string with the alphabet of '23456789XA'
If the hand is soft the higher value is returned.
The return value is a pair consisting of the
value and the softness.
'''
total = 0
has_aces = False
for face in cards:
card_value = CARD_VALUES[face]
if card_value == 1:
has_aces = True
total += card_value
if total <= 11 and has_aces:
return total + 10, True
else:
return total, False
def is_blackjack(cards) -> bool:
if len(cards) == 2:
if cards[0] == 'A' and cards[1] == 'X':
return True
elif cards[0] == 'X' and cards[1] == 'A':
return True
return False