Created
June 11, 2022 15:52
-
-
Save robertsosinski/e3212260ddd28a7eec52f37906f812a3 to your computer and use it in GitHub Desktop.
Blackjack Strategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// All potential actions that can be taken | |
let action = [ | |
'hit', // 0 | |
'stand', // 1 | |
'double', // 2 | |
'split', // 3 | |
'blackjack' // 4 | |
]; | |
// Used for hard hands, when the player hand does not have an Ace (11) | |
let hardStrategy = [ | |
// dealer card V | |
// 2, 3, 4, 5, 6, 7, 8, 9, T, A | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // 5 <- player cards (both cards together are worth) | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // 6 | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // 7 | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // 8 | |
[0, 2, 2, 2, 2, 0, 0, 0, 0, 0], // 9 | |
[2, 2, 2, 2, 2, 2, 2, 2, 0, 0], // 10 | |
[2, 2, 2, 2, 2, 2, 2, 2, 2, 0], // 11 | |
[0, 0, 1, 1, 1, 0, 0, 0, 0, 0], // 12 | |
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], // 13 | |
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], // 14 | |
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], // 15 | |
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], // 16 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], // 17 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], // 18 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], // 19 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], // 20 | |
]; | |
// Used for soft hands, when the player hand has an Ace (11) | |
let softStrategy = [ | |
// dealer card V | |
// 2, 3, 4, 5, 6, 7, 8, 9, T, A | |
[0, 0, 0, 2, 2, 0, 0, 0, 0, 0], // 11,2 <- player cards (one 11 card and one other card) | |
[0, 0, 0, 2, 2, 0, 0, 0, 0, 0], // 11,3 | |
[0, 0, 2, 2, 2, 0, 0, 0, 0, 0], // 11,4 | |
[0, 0, 2, 2, 2, 0, 0, 0, 0, 0], // 11,5 | |
[0, 2, 2, 2, 2, 0, 0, 0, 0, 0], // 11,6 | |
[1, 2, 2, 2, 2, 1, 1, 0, 0, 0], // 11,7 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], // 11,8 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], // 11,9 | |
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4], // 11,10 <- blackjack | |
]; | |
// Used for split hands, when the player hand as two cards with the same value, | |
// such as two 3 cards, two 10 cards, or two Ace cards. | |
let splitStrategy = [ | |
// dealer card V | |
// 2, 3, 4, 5, 6, 7, 8, 9, T, A | |
[3, 3, 3, 3, 3, 3, 0, 0, 0, 0], // 2s <- player cards (both cards are the same) | |
[3, 3, 3, 3, 3, 3, 0, 0, 0, 0], // 3s | |
[0, 0, 0, 3, 3, 0, 0, 0, 0, 0], // 4s | |
[2, 2, 2, 2, 2, 2, 2, 2, 0, 0], // 5s | |
[3, 3, 3, 3, 3, 0, 0, 0, 0, 0], // 6s | |
[3, 3, 3, 3, 3, 3, 0, 0, 0, 0], // 7s | |
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3], // 8s | |
[3, 3, 3, 3, 3, 1, 3, 3, 1, 1], // 9s | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], // 10s | |
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3], // 11s | |
]; | |
/** | |
* Provides the best action for a blackjack hand. | |
* Example 1: blackjack(2, 7, 9) -> 'hit' | |
* Example 2: blackjack(3, 8, 6) -> 'double' | |
* Example 3: blackjack(8, 8, 8) -> 'split' | |
* Example 4: blackjack(11, 11, 9) -> 'blackjack' | |
* | |
* @param {Number} one the player's first card. | |
* @param {Number} two the player's second card. | |
* @param {Number} deal the dealer's stud card. | |
* | |
* @return {String} the appropiate action to take, see `action` array on line #2 above. | |
*/ | |
function blackjack(one, two, deal) { | |
var play, pidx | |
let didx = deal - 2 | |
// use split strategy | |
if (one === two) { | |
play = splitStrategy | |
pidx = one - 2; | |
} else { | |
// use soft strategy | |
if (one === 11 || two == 11) { | |
play = softStrategy; | |
if (one === 11) { | |
pidx = two - 2; | |
} else { | |
pidx = one - 2; | |
} | |
// use hard strategy | |
} else { | |
play = hardStrategy | |
pidx = (one + two) - 5; | |
} | |
} | |
return action[play[pidx][didx]]; | |
} | |
exports.blackjack = blackjack; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# All potential actions that can be taken | |
action = [ | |
'hit', # 0 | |
'stand', # 1 | |
'double', # 2 | |
'split', # 3 | |
'blackjack' # 4 | |
] | |
# Used for hard hands, when the player hand does not have an Ace (11) | |
hard_strategy = [ | |
# dealer card V | |
# 2, 3, 4, 5, 6, 7, 8, 9, T, A | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 5 <- player cards (both cards together are worth) | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 6 | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 7 | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 8 | |
[0, 2, 2, 2, 2, 0, 0, 0, 0, 0], # 9 | |
[2, 2, 2, 2, 2, 2, 2, 2, 0, 0], # 10 | |
[2, 2, 2, 2, 2, 2, 2, 2, 2, 0], # 11 | |
[0, 0, 1, 1, 1, 0, 0, 0, 0, 0], # 12 | |
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], # 13 | |
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], # 14 | |
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], # 15 | |
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], # 16 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # 17 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # 18 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # 19 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # 20 | |
] | |
# Used for soft hands, when the player hand has an Ace (11) | |
soft_strategy = [ | |
# dealer card V | |
# 2, 3, 4, 5, 6, 7, 8, 9, T, A | |
[0, 0, 0, 2, 2, 0, 0, 0, 0, 0], # 11,2 <- player cards (one 11 card and one other card) | |
[0, 0, 0, 2, 2, 0, 0, 0, 0, 0], # 11,3 | |
[0, 0, 2, 2, 2, 0, 0, 0, 0, 0], # 11,4 | |
[0, 0, 2, 2, 2, 0, 0, 0, 0, 0], # 11,5 | |
[0, 2, 2, 2, 2, 0, 0, 0, 0, 0], # 11,6 | |
[1, 2, 2, 2, 2, 1, 1, 0, 0, 0], # 11,7 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # 11,8 | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # 11,9 | |
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4], # 11,10 <- blackjack | |
] | |
# Used for split hands, when the player hand as two cards with the same value, | |
# such as two 3 cards, two 10 cards, or two Ace cards. | |
split_strategy = [ | |
# dealer card V | |
# 2, 3, 4, 5, 6, 7, 8, 9, T, A | |
[3, 3, 3, 3, 3, 3, 0, 0, 0, 0], # 2s <- player cards (both cards are the same) | |
[3, 3, 3, 3, 3, 3, 0, 0, 0, 0], # 3s | |
[0, 0, 0, 3, 3, 0, 0, 0, 0, 0], # 4s | |
[2, 2, 2, 2, 2, 2, 2, 2, 0, 0], # 5s | |
[3, 3, 3, 3, 3, 0, 0, 0, 0, 0], # 6s | |
[3, 3, 3, 3, 3, 3, 0, 0, 0, 0], # 7s | |
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3], # 8s | |
[3, 3, 3, 3, 3, 1, 3, 3, 1, 1], # 9s | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # 10s | |
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3], # 11s | |
] | |
def blackjack(one, two, deal): | |
""" | |
Provides the best action for a blackjack hand. | |
Example 1: blackjack(2, 7, 9) -> 'hit' | |
Example 2: blackjack(3, 8, 6) -> 'double' | |
Example 3: blackjack(8, 8, 8) -> 'split' | |
Example 4: blackjack(11, 11, 9) -> 'blackjack' | |
Parameters: | |
one (int) the player's first card. | |
two (int) the player's second card. | |
deal (int) the dealer's stud card. | |
Returns: | |
(str): the appropiate action to take, see `action` list on line #2 above. | |
""" | |
didx = deal - 2 | |
# use split strategy | |
if one == two: | |
play = split_strategy | |
pidx = one - 2 | |
else: | |
# use soft strategy | |
if one == 11 or two == 11: | |
play = soft_strategy | |
if one == 11: | |
pidx = two - 2 | |
else: | |
pidx = one - 2 | |
# use hard strategy | |
else: | |
play = hard_strategy | |
pidx = (one + two) - 5 | |
return action[play[pidx][didx]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment