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

“Programming is the pursuit of laziness” — Some Wise Developer

The Idea

The Execution

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

// 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

Results

Take Away

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
}

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

Programmer — Writer. @andyhartnett12

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store