Skip to content

Instantly share code, notes, and snippets.

@rfolk
Last active July 22, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfolk/a1c32c4c77efb88dc2bf71143dd8f83b to your computer and use it in GitHub Desktop.
Save rfolk/a1c32c4c77efb88dc2bf71143dd8f83b to your computer and use it in GitHub Desktop.
Coder Radio Programming Challenge #001: A Black Jack helper based on http://www.blackjack-chart.com
"""
Russell Folk
Coder Radio, Programming Challenge #001
22 July 2016
"""
def get_recommendation(dealers_card, players_cards):
"""
Look up and return the appropriate recommendation for what move the player
should make according to the dealer's card on display and the player's own
hand.
Appropriate actions are: Hit (H), Stand (S), Double Down else Hit (D),
Double Down else Stand (DS), Split (SP), Surrender else Hit (X/H), Surrender
else Split (X/P), and Surrender else Stand (X/S)
Information for action comes from: www.blackjack-chart.com
"""
action_table = {
'8' : ['H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H'],
'9' : ['H', 'D', 'D', 'D', 'D', 'H', 'H', 'H', 'H', 'H'],
'10' : ['D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'H', 'H'],
'11' : ['D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D'],
'12' : ['H', 'H', 'S', 'S', 'S', 'H', 'H', 'H', 'H', 'H'],
'13' : ['S', 'S', 'S', 'S', 'S', 'H', 'H', 'H', 'H', 'H'],
'14' : ['S', 'S', 'S', 'S', 'S', 'H', 'H', 'H', 'H', 'H'],
'15' : ['S', 'S', 'S', 'S', 'S', 'H', 'H', 'H', 'X/H', 'X/H'],
'16' : ['S', 'S', 'S', 'S', 'S', 'H', 'H', 'X/H', 'X/H', 'X/H'],
'17' : ['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'X/S'],
'A8' : ['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S'],
'A9' : ['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S'],
'22' : ['SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'H', 'H', 'H', 'H'],
'33' : ['SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'H', 'H', 'H', 'H'],
'44' : ['H', 'H', 'H', 'SP', 'SP', 'H', 'H', 'H', 'H', 'H'],
'55' : ['D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'H', 'H'],
'66' : ['SP', 'SP', 'SP', 'SP', 'SP', 'H', 'H', 'H', 'H', 'H'],
'77' : ['SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'H', 'H', 'H', 'H'],
'88' : ['SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'X/P'],
'99' : ['SP', 'SP', 'SP', 'SP', 'SP', 'S', 'SP', 'SP', 'S', 'S'],
'1010': ['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S'],
'AA' : ['SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'SP', 'SP']
}
dealers_index = lookup_dealer_index(dealers_card)
players_index = lookup_player_index(players_cards)
return action_table[players_index][dealers_index]
def lookup_dealer_index(dealers_card):
"""
This will take the dealers card and translate it to the index in the list
to use for a recommendation. This will also normalize face cards (Jack,
Queen, King).
"""
if dealers_card == 'A':
# we want to return the index of the list which is an A, hence 9
return 9
elif dealers_card == 'J' or dealers_card == 'Q' or dealers_card == 'K':
# it's a face card, so return the same index as a 10, hence 10-2=8
return 8
else:
return int(dealers_card) - 2
def lookup_player_index(players_cards):
"""
This will convert the players hand into the appropriate index
"""
# normalize face cards
for index in range(len(players_cards)):
if players_cards[index] == 'J' or \
players_cards[index] == 'Q' or \
players_cards[index] == 'K':
players_cards[index] = '10'
# let's handle pairs
if len(players_cards) == 2 and players_cards[0] == players_cards[1]:
return players_cards[0] + players_cards[1]
# grab the total hand score
return get_hand_result(players_cards)
def get_hand_result(hand):
"""
This will total up the cards in your hand so that an accurate recommendation
can be given. Yay for not wanting to do the math by hand.
"""
num_aces = 0
running_total = 0
for card in players_cards:
if card == 'A':
num_aces += 1
else:
running_total += int(card)
while num_aces > 0:
if running_total < 10 and num_aces == 1:
# if we're in a special case, return that case
return 'A' + str(running_total)
elif running_total >= 10:
# there isn't a case that we want to add 11 because that's handled
# elsewhere in the code (the special case above or a 2 card hand
running_total += 1
num_aces -= 1
return str(running_total)
def get_readable_result(result):
"""
Translate the short code recommendation to a human readable sentence.
"""
if result == 'H':
return 'hit'
elif result == 'S':
return 'stand'
elif result == 'D':
return 'double down if allowed, else, hit'
elif result == 'DS':
return 'double down if allowed, else, stand'
elif result == 'SP':
return 'split'
elif result == 'X/H':
return 'surrender if allowed, else, hit'
elif result == 'X/P':
return 'surrender if allowed, else, split'
else:
return 'surrender if allowed, else, stand'
players_cards = input("What are your first cards (seperated by commas)? ")
# normalize the input
players_cards = players_cards.upper()
# split the cards into a list for processing
players_cards = players_cards.split(',')
dealers_card = input("What is the dealers card? ")
# normalize the input
dealers_card = dealers_card.upper()
result = get_recommendation(dealers_card, players_cards)
result = get_readable_result(result)
print("You should " + result + ".")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment