Search code examples
javascriptarraysloopsblackjack

Blackjack javascript game infinite loop


I have created an utterly simple blackjack game that stores the first value of a shuffled array of cards into corresponding players' arrays, dealing them as actual hands. For some odd reason, I can't seem to find a way to execute the core part of the code multiple times without getting an infinite loop. For the time being, I have only tried running the quite commonplace "for" command which is meant for multiple statements, but just doesn't seem to work here.

The programm on its primitive form is as follows...

var dealerCards = [];
var playerCards = [];
var firstDeck = [];

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 (i = 0; i < 4*x; i++) {
  array.push(1,2,3,4,5,6,7,8,9,10,11,12,13);
   } 
  shuffle(array);
}

function drawCard(playersHand, playerSoft, playerHard) {
  playersHand.push(firstDeck[0]);
  firstDeck.shift();
}

function checkDeckDrawOne(playersHand) {
  if (firstDeck.length === 0) {
    createShuffledDeckNumber(firstDeck, 1);
    drawCard(playersHand);
  }else{
    for (i = 0; i < 1; i++) {
      drawCard(playersHand);
    }
  }
}

for (i = 0; i < 4; i++) {
dealerCards = [];
playerCards = [];
checkDeckDrawOne(dealerCards);
checkDeckDrawOne(dealerCards); 
checkDeckDrawOne(playerCards);
checkDeckDrawOne(playerCards);
console.log("dealerCards",dealerCards,"playerCards",playerCards);
console.log("firstDeckDrawn", firstDeck, "Number", firstDeck.length);
}

Additional Notes;

The presumed objective could be performing calculations to figure out the winner by imitating the effect of consecutive computing rounds based on a finite number of values stored in each player's array. Although, I've tried a seried of different things when it comes to emulating the real life circumstances of actually playing blackjack, this version seems to do just that, by also giving the programmer the ability to introduce counting systems like KO or HiLo. The main logic behind the whole thing is fairly simple; order x shuffled decks whenever a command that involves drawing a card is being executed unless the deck has at least one card.

It's rather fair to ponder why should I possibly bother creating multiple rounds in such a game. Reason is, that I want to create an autoplayer application that provides me with percentages on processed data.


Solution

  • Your variable i in function checkDeckDrawOne() has global scope, meaning it alters the value of i in the main loop:

    for (i = 0; i < 4; i++) {
    dealerCards = [];
    playerCards = [];
    checkDeckDrawOne(dealerCards);
    checkDeckDrawOne(dealerCards); 
    checkDeckDrawOne(playerCards);
    checkDeckDrawOne(playerCards);
    console.log("dealerCards",dealerCards,"playerCards",playerCards);
    console.log("firstDeckDrawn", firstDeck, "Number", firstDeck.length);
    }
    

    Change this:

    for (i = 0; i < 1; i++) {
      drawCard(playersHand);
    }
    

    to this:

    for (var i = 0; i < 1; i++) {
      drawCard(playersHand);
    }
    

    although why you need a loop here anyway is baffling.