Search code examples
javascriptinfinite-loopnested-loops

Non-working nested loop in blackjack dealer AI


I'm looking for suggestions regarding my javascript function on the dealer's AI, which is fairly simple and self-explanatory and abides by the established ruleset. The code was implemented with separate soft and hard value calculators-taking place once a card is drawn, in order to render both values accessible within the GUI gameplay framework.

Critical purpose is adding continuously cards to the dealer's hand, as soon as they fail to surpass 17 and break the loop on to the winning condition function as those prerequisites are being met. Using a<4 in the for loop was kind of random as other solutions were turned down.

Former Experiments

  1. While loop. Turned out to produce an infinite loop dead end.

  2. SummerOfGOTOProject. Both full and mini versions returned a bug error on the compiler.

Perceived problem

Although both values are updated once passing through the dealerStrategy function (confirmed after extracting information using a console.log before the continue command), for some odd reason new cards are being added to the dealer's hand ignoring that compatible conditions on other loop parts are met.

var dealerValue = {initial: 0, soft: 0, hard: 0};
var dealerCards = [];
var playerValue = {initial: 0, soft: 0, hard: 0};
var playerCards = [];
var firstDeck = [];
var origValue = 0;

function shuffle(o) {
  for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
  return o;
}

function createShuffledDeckNumber(array, x) { 
  for (var i = 0; i < 4*x; i++) {
  array.push(1,2,3,4,5,6,7,8,9,10,11,12,13);
   } 
  shuffle(array);
}

function checkDeckDrawOneDealer() {
  if (firstDeck.length === 0) {
    createShuffledDeckNumber(firstDeck, 1);//Creates a number of normal shuffled decks.
    dealerCards.push(firstDeck[0]);
    dealerValue.soft = calculateHand(firstDeck[0], dealerValue.soft, 0);
    dealerValue.hard = calculateHand(firstDeck[0], dealerValue.hard, 1);
    firstDeck.shift();

  }else{
    dealerCards.push(firstDeck[0]);
    dealerValue.soft = calculateHand(firstDeck[0], dealerValue.soft, 0);
    dealerValue.hard = calculateHand(firstDeck[0], dealerValue.hard, 1);
    firstDeck.shift();
  }
}

function dealerStrategy(dS, dH) {
  for (var a = 0; a < 3; a++){  
    if (dH == dS) {
      if (dH >= 17 && dH <= 21) {break;}
      else if (dH < 17) {checkDeckDrawOneDealer(); continue;}
      else if (dH > 21) {break;}
    }
    else if (dH < dS) {
      if (dS <= 21) {
        if (dS >= 17 && dS <= 21) {break;}
        else if (dS < 17) {checkDeckDrawOneDealer(); continue;}
      }
      else if (dS > 21) {
        if (dH >= 17 && dH <= 21) {break;}
        else if (dH < 17) {checkDeckDrawOneDealer(); continue;}
        else if (dH > 21) {break;}
      }
    }
  }
}

for (i = 0; i < 5; i++) {
  console.log(i);
  dealerCards = [];
  dealerValue.soft = 0;
  dealerValue.hard = 0;
  checkDeckDrawOneDealer();
  checkDeckDrawOneDealer(); 
  dealerStrategy(dealerValue.soft, dealerValue.hard, playerValue.soft, playerValue.hard); 
  console.log("dealerCards",dealerCards);
  console.log("dS:", dealerValue.soft,"dH:", dealerValue.hard);
}

Solution

  • In dealerStrategy, you are passing in dS and dH and using them to determine behavior. When you draw additional cards within your loop, the values of dS and dH are never updated, so regardless of what card you draw, the behavior on the next loop will be the same as the first.