Can a programmer cheat the game of Blackjack? I couldn’t.

Or — How programming didn’t make me rich

I firmly believe in being the right kind of lazy. And such was the case when I embarked on my most recent project.

The Idea

Blackjack is a fun pastime I like to indulge in when in locations that allows me to do so. Like most people who have gambled, I have had that fantasy of really going on a roll one night and walking away a rich man.

So I got to thinking, can I game the system? If I wrote some code, I could emulate as many hands of blackjack as I wanted in no time at all and I could use the results to determine the best possible strategy to play the game and come away a big winner.

The Execution

*** Note: The core functions I used for this are included at the bottom of this article ***

I started by building a Black Jack Emulator by writing functions for: shuffling the deck, dealing the cards, hitting, staying, and the dealers hit/stay requirements. This was pretty straightforward as I stored the deck and each of the hands in arrays, then had a function for calculating the score of each hand.

This was a great first step, but I wanted to simulate 1000 hands and test varying methods of player hand and betting strategy. So I came up with a string parser so I could easily write each strategy as one string. I came up with this:

// Dealers top card > six? | Hit while your hand is under 17
d>6|hitunder17
// Dealers top card < 7 | Hit while your hand is under 12
d<7|hitunder12
// Dealer's top card > 6 | Double down if you have 11
d>6|double11

And so on. This way I could add these strings to an array and loop through them all, simulating each strategy as many times as I would like and see the outcome of each.

Needless to say, I’ve found that blackjack may not be the most profitable endeavor unless you’re brilliant at counting cards.

Results

So with a hypothetical bankroll of $500 and a hypothetical minimum bet of $5. Here are my results for each strategy over one thousand hands:

  1. Always Doubling Down on 11; If dealer has a 7 or higher, hit until hand is 16 or higher; If Dealer has a 6 or lower, (Let the dealer bust) hit until hand is greater than 11.
    Won 43.92% of hands, Lost $125.
  2. Always Doubling Down on 10, 11; If dealer has 7 or higher, hit until hand is 17 or higher; If dealer has 6 or lower, (Let the dealer bust) hit until hand is greater than 11.
    Won 45.20% of hands, Lost $69.
  3. No doubling down. Dealer 7 or higher, hit until hand is 16 or higher; Dealer 6 or lower, hit until hand is 12 or better.
    Won 44.83% of hands, Lost $110
  4. Same as #1, but don’t pocket winnings until you have won 3 hands in a row.
    Won 45% of hands, Lost $31
  5. Same as #2, but don’t pocket winnings until you have won 3 hands in a row.
    Won 43.45% of hands, Lost $293
  6. Same as #3, but don’t pocket winnings until you have won 3 hands in a row.
    Won 42.3% of hands, Lost $307

Take Away

You can’t program yourself into being a highly successful blackjack player. At least I can’t. However, writing code to check this out for me saved me countless hours and money. If I had tested these hands out at the casino I’d have lost $935, countless hours, and my dignity.

Functions Used In This Project

shuffle(cards) {
for(var i = 0; i < 30; i++) {
var rndNo = this.getRandomInt(1, 52)
var card = cards[i]
cards[i] = cards[rndNo-1]
cards[rndNo-1] = card
}
return cards
}
getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
generateDeck() {
let suits = ['Hearts', 'Spades', 'Clubs', 'Diamonds']
let values = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K']
for (let suit in suits) {
for (let value in values) {
this.cards.push(values[value])
}
}
}
score(hand) {
let score = 0
if(hand.length >= 2){
hand.forEach(v => {
score += this.cardScore(v)
})
}
if(score > 21) {
let ace = hand.indexOf('A')
if(ace > -1){
hand[ace] = 1
return this.score(hand)
}
}

return score
}
cardScore(card) {
if(card === 'A'){
return 11
}
return ['J', 'Q', 'K'].indexOf(card) > -1) ? 10 : card
}

Don’t gamble with more money that you’re willing to lose!

While you’re here. Give me a follow on twitter!

https://twitter.com/andyhartnett12

Programmer — Writer. @andyhartnett12