-
Notifications
You must be signed in to change notification settings - Fork 0
/
006. black jack.py
144 lines (124 loc) · 3.91 KB
/
006. black jack.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
"""
Learn about comparisons by implementing some Black Jack judging rules.
"""
"""Functions to help play and score a game of blackjack.
How to play blackjack: https://bicyclecards.com/how-to-play/blackjack/
"Standard" playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
"""
def value_of_card(card):
"""Determine the scoring value of a card.
:param card: str - given card.
:return: int - value of a given card. See below for values.
1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10
2. 'A' (ace card) = 1
3. '2' - '10' = numerical value.
"""
if card in "JQK":
return 10
if card == "A":
return 1
return int(card)
def higher_card(card_one, card_two):
"""Determine which card has a higher value in the hand.
:param card_one, card_two: str - cards dealt in hand. See below for values.
:return: str or tuple - resulting Tuple contains both cards if they are of equal value.
1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10
2. 'A' (ace card) = 1
3. '2' - '10' = numerical value.
"""
if card_one in "JQK":
one = 10
elif card_one == "A":
one = 1
else:
one = int(card_one)
if card_two in "JQK":
two = 10
elif card_two == "A":
two = 1
else:
two = int(card_two)
if one > two:
return card_one
if two > one:
return card_two
if one == two:
return card_one, card_two
def value_of_ace(card_one, card_two):
"""Calculate the most advantageous value for the ace card.
:param card_one, card_two: str - card dealt. See below for values.
:return: int - either 1 or 11 value of the upcoming ace card.
1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10
2. 'A' (ace card) = 11 (if already in hand)
3. '2' - '10' = numerical value.
"""
if card_one in "JQK":
one = 10
elif card_one == "A":
one = 11
else:
one = int(card_one)
if card_two in "JQK":
two = 10
elif card_two == "A":
two = 11
else:
two = int(card_two)
if one + two + 11 > 21:
return 1
return 11
def is_blackjack(card_one, card_two):
"""Determine if the hand is a 'natural' or 'blackjack'.
:param card_one, card_two: str - card dealt. See below for values.
:return: bool - is the hand is a blackjack (two cards worth 21).
1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10
2. 'A' (ace card) = 11 (if already in hand)
3. '2' - '10' = numerical value.
"""
if card_one == "A":
if card_two in "JQK" or card_two == "10":
return True
if card_two == "A":
if card_one in "JQK" or card_one == "10":
return True
return False
def can_split_pairs(card_one, card_two):
"""Determine if a player can split their hand into two hands.
:param card_one, card_two: str - cards dealt.
:return: bool - can the hand be split into two pairs? (i.e. cards are of the same value).
"""
if card_one in "JQK":
one = 10
elif card_one == "A":
one = 11
else:
one = int(card_one)
if card_two in "JQK":
two = 10
elif card_two == "A":
two = 11
else:
two = int(card_two)
if one == two:
return True
return False
def can_double_down(card_one, card_two):
"""Determine if a blackjack player can place a double down bet.
:param card_one, card_two: str - first and second cards in hand.
:return: bool - can the hand can be doubled down? (i.e. totals 9, 10 or 11 points).
"""
if card_one in "JQK":
one = 10
elif card_one == "A":
one = 1
else:
one = int(card_one)
if card_two in "JQK":
two = 10
elif card_two == "A":
two = 1
else:
two = int(card_two)
if one + two == 9 or one + two == 10 or one + two == 11:
return True
return False