Bookmark and Share


Play the game or view the source code.

Initializing the Game

Before the game can be played, some things need to be set up. Several constants and global variables are defined and the initGame() function is set to be called automatically via the window.onload event.

// Constants.

var numPacks      =    4;
var numShuffles   =   10;

var maxSplits     =    3;

var minBet        =    5;
var maxBet        =  100;
var initCredit    = 1000;
var initBet       =   10;

var dealTimeDelay =  750;

// Globals.

var deck;
var burnCard;

var dealer;
var player = new Array(maxSplits + 1);
var curPlayerHand, numPlayerHands;

var credits, defaultBet;
var creditsTextNode, defaultTextNode;

var dealRoundCounter;

// Initialize game on page load.

window.onload = initGame;

First the constants. This includes such things as the number of card packs to use in the deck, how many times the deck should be shuffled, the number of splits allowed, the amount of credits allotted the player and some betting values.

Then some globals are defined for the card deck, the dealer and player hands and the credits and default bet amounts. The initGame() function takes care of initializing these.

function initGame() {

  var i;

  // Locate credits and default bet text nodes on the page.

  creditsTextNode = document.getElementById("credits").firstChild;
  defaultTextNode = document.getElementById("default").firstChild;

  // Initialize player's credits and bet amount.

  credits    = initCredit;
  defaultBet = initBet;

  // Initialize card deck.

  deck = new Stack();

  // Create dealer and player hands.

  dealer = new Hand("dealer");
  for (i = 0; i < player.length; i++)
    player[i] = new Hand("player" + i);

The curPlayerHand and numPlayerHands global variables will be used to keep track of the hands in the players[] array during play. dealTimeDelay and dealRoundCounter will be discussed later on.

Game Play

Because the game is interactive, much of the program flow is event driven. For the most part, the script only executes in response to the user pressing one of the form buttons and then exits. To control the logical flow, the script may enable and disable individual buttons based on the action being performed and other, variable circumstances.

Between Rounds

Initially, only the "Deal," "Increase Bet" and "Decrease Bet" buttons are enabled. Both the "Increase" and "Decrease" buttons execute the function changeBet() when pressed.

function changeBet(n) {

  // Increase or decrease the default bet.

  defaultBet += n;
  defaultBet = Math.max(Math.min(defaultBet, maxBet), minBet);
  defaultTextNode.nodeValue = "Default Bet: " + formatDollar(defaultBet);

  // Reset the increase/decrease buttons.


This function does nothing more than adjust the default bet amount. EnableBetButtons() handles the enabling of the "Increase Bet" and "Decrease Bet" buttons based on the current bet amount and the minimum and maximum allowed.

function EnableBetButtons() {

  // Enable the increase and decrease bet buttons provided the current bet
  // amount is within the allowed min/max value.

  document.forms["controls"].elements["increase"].disabled =
    (defaultBet >= maxBet);
  document.forms["controls"].elements["decrease"].disabled =
    (defaultBet <= minBet);

When "Deal" is pressed, the real fun begins. It fires the startRound() function which initially disables these three buttons and begins a new round of play.

Playing a Round

Below is an outline of the steps taken in any single round. Note that step 2 is never executed directly. Instead, the user must press one of the enabled form buttons to select one of the options and execute the function associated with it.

  1. Start a round by dealing two cards to the player and the dealer. If either has a blackjack, Go to step 5. Otherwise exit (leaving the game at step 2).
  2. The player may choose one of these options:
    • Surrender: Go to step 5.
    • Double: Give player one card. If player busts, Go to step 5. Otherwise go to step 3.
    • Hit: Give player another card. If player busts, go to step 5. Otherwise exit (leaving the game in step 2).
    • Stand: Go to step 3.
    • Split: Create a new hand, add a card to the current hand and exit (leaving the game in step 2).
  3. If the player split and has another hand to play, add a card to it exit (leaving the game in step 2). Otherwise go to step 4.
  4. Player the dealer's hand, adding cards if necessary until the dealer reaches 17 or more. Then go to step 5.
  5. End the round, show the results and pay off bets if appropriate.

After the last step, the round is over and the only buttons enabled are again "Deal," "Increase Bet" and "Decrease Bet." Next we'll look at the functions associated with each step above.