Skip to content

Instantly share code, notes, and snippets.

Created July 20, 2016 20:17
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 anonymous/a98a8adbd376e24317d9262871f6fb7c to your computer and use it in GitHub Desktop.
Save anonymous/a98a8adbd376e24317d9262871f6fb7c to your computer and use it in GitHub Desktop.
# Blackjack helper
# Coder Radio ep 214 coding challenge
# Value table
TABLE = {
'2': [ 'H','H','D','D','H','S','S','S','S','S','H','H','H','H','H','S','S','S','SP','SP','H','D','SP','SP','SP','SP','S','SP' ],
'3': [ 'H','D','D','D','H','S','S','S','S','S','H','H','H','H','D','DS','S','S','SP','SP','H','D','SP','SP','SP','SP','S','SP' ],
'4': [ 'H','D','D','D','S','S','S','S','S','S','H','H','D','D','D','DS','S','S','SP','SP','H','D','SP','SP','SP','SP','S','SP' ],
'5': [ 'H','D','D','D','S','S','S','S','S','S','D','D','D','D','D','DS','S','S','SP','SP','SP','D','SP','SP','SP','SP','S','SP' ],
'6': [ 'H','D','D','D','S','S','S','S','S','S','D','D','D','D','D','DS','S','S','SP','SP','SP','D','SP','SP','SP','SP','S','SP' ],
'7': [ 'H','H','D','D','H','H','H','H','H','S','H','H','H','H','H','S','S','S','SP','SP','H','D','H','SP','SP','S','S','SP' ],
'8': [ 'H','H','D','D','H','H','H','H','H','S','H','H','H','H','H','S','S','S','H','H','H','D','H','H','SP','SP','S','SP' ],
'9': [ 'H','H','D','D','H','H','H','H','X/H','S','H','H','H','H','H','H','S','S','H','H','H','D','H','H','SP','SP','S','SP' ],
'10': [ 'H','H','H','D','H','H','H','X/H','X/H','S','H','H','H','H','H','H','S','S','H','H','H','H','H','H','SP','S','S','SP' ],
'a': [ 'H','H','H','D','H','H','H','X/H','X/H','X/S','H','H','H','H','H','H','S','S','H','H','H','H','H','H','X/P','S','S','SP' ],
}
DEFINITIONS = {
"H":"Hit.",
"S":"Stand.",
"D":"Double if allowed, otherwise Hit.",
"DS":"Double if allowed, otherwise Stand.",
"SP":"Split.",
"X/H":"Surrender if allowed, otherwise Hit.",
"X/P":"Surrender if allowed, otherwise Split.",
"X/S":"Surrender if allowed, otherwise Stand."
}
def getUserHand():
cards=input("Please enter your hand: ")
if cards in ("8 9 10 11 12 13 14 15 16 17 22 33 44 55 66 77 88 99 1010 a2 a3 a4 a5 a6 a7 a8 a9 aa".split()):
cards=list(cards)
return cards
else:
print("Please enter a valid hand.")
getUserHand()
def getIndex(cards):
if len(cards) == 4:
index = 26
elif len(cards) == 1:
if card[0] == 8:
index = 0
else:
index = 1
elif cards[0] == cards[1]:
if cards[0] == "a":
index = 27
else:
index = len(cards)-int(cards[0])
elif cards[0] == "a":
index = int(cards[1])+8
else:
index = int(cards[1])+2
return index
def getDealerCard():
card = input("Enter the dealer's card: ")
if card in ("2 3 4 5 6 7 8 9 10 a".split()):
return card
else:
print("Please enter a valid card.")
getDealerCard()
def getAdvice(dealerCard, userIndex):
advice = TABLE[dealerCard][userIndex]
return advice
# Get the users hand and convert to index
userHand = getUserHand()
userIndex = getIndex(userHand)
# Get Dealers card
dealerCard = getDealerCard()
# Look up advise from table and convert to definition
advice = getAdvice(dealerCard, userIndex)
adviceMessage = DEFINITIONS[advice]
# Output
print("You should",adviceMessage,)
@JDMcE
Copy link

JDMcE commented Jul 20, 2016

Should have made an account first ...
oops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment