BrainJar.com: Blackjack (2024)

Blackjack

Play the game or view thesource code.

2. The Player's Turn

At this point, the player may choose one of several options: surrender,double down, hit, stand or (if dealt a pair) split. Each option and the actionstaken by the associated code are described below.

Surrender

This one is easy. When a player surrenders, he or she is forfeiting theround in exchange for losing only half of his or her bet.

function playerSurrender() { // Unhighlight the player's hand. removeClassName(player[0].fieldNode, "activeField"); // Note the surrender and end the round. player[0].surrender = true; endRound();}

The playerSurrender() function simply flags the hand as havingbeen surrendered and calls endRound() to end the round. Note thatthis option is only available after the initial deal.

Split

This one is a little more complicated. If the player chooses to split apair, a new hand has to be put into play for the player. TheplayerSplit() function handles this.

function playerSplit() { var m, n; var card, node; // Enable/disable buttons. DisablePlayButtons(); // Update the number of player hands. m = curPlayerHand; n = numPlayerHands; numPlayerHands++; // Note the split. player[m].split = true; player[n].split = true; // Remove the second card from the current hand and add it to a // new hand. card = player[m].removeCard(); player[m].scoreTextNode.nodeValue = player[m].getScore(); player[n].addCard(card, false); player[n].scoreTextNode.nodeValue = player[n].getScore(); player[n].fieldNode.style.display = ""; // Update bet and credits. player[n].bet = player[m].bet; credits -= player[n].bet; updateBetDisplay(n); updateBetDisplay(n + 1); // Give the current hand a second card. setTimeout(playerHit, dealTimeDelay);}

First it increments the global numPlayerHands and marks boththe original and new hands as split. Then it removes the second card from theoriginal hand and places it in the new one. The player must place a bet on thisnew hand equal to the bet on the original so credits are deducted and the newbet is placed.

The last step is to add a second card to the current hand to replace the onethat was split off, so the playerHit() function is called. Again,a timer is used to cause a slight delay. Note that the buttons for play havebeen disabled. So the user can't press any while the timer is running.

The playerHit() function is also called when the "Hit" button ispressed (see below). Once it runs and exits, the game in left in step 2 so thatplayer can play the original hand with the new second card. The new handcreated by the split will be handled later.

Double

A player can choose to "double down" meaning that he or she doubles thecurrent bet on a hand and risks taking one and only one additional card. TheplayerDouble() function updates the bet and credits appropriately,flags the hand as a double down and calls playerHit() to add theadditional card.

function playerDouble() { // Double the player's bet and deal a single card. player[curPlayerHand].bet *= 2; credits -= defaultBet; updateBetDisplay(curPlayerHand); player[curPlayerHand].doubled = true; player[curPlayerHand].top = 0; playerHit();}

You might notice that it also alters the position for this new card beforeit is dealt, to give the user a visual cue.

Hit

The playerHit() function handles adding a card to a hand. Itwill first disable some of the other options and then deal a new card to thecurrent hand.

function playerHit() { var n, p; // Enable/disable buttons. DisablePlayButtons(); document.forms["controls"].elements["hit"].disabled = false; document.forms["controls"].elements["stand"].disabled = false; // Give the player another card and find total. n = curPlayerHand; player[n].addCard(getNextCard(), false); p = player[n].getScore(); ...

At this point we need to evaluate the hand and check flags to determine whatto do next. Remember that playerHit() may have been called inresponse to the user pressing the "Hit" button, by theplayerSplit() or playerDouble() functions. Where itwas called from may affect the next course of action.

First we check the player's total to see if he or she has busted. If so, the hand is done so it will call startNextHand() and exit (movingto step 3).

 // If the player has busted, go to the next hand. if (p > 21) { player[n].scoreTextNode.nodeValue = "Busted (" + p + ")"; startNextHand(); return; } else player[n].scoreTextNode.nodeValue = p;

Likewise, if the player has reached a score of 21, or if the player doubleddown on the hand, we can also move on to the next step.

 // If the player has reached 21, or is doubling down, go on to the // next hand. if (p == 21 || player[n].doubled) { startNextHand(); return; }

The last check is to determine if the function was initiated from theplayerSplit() function in order to add a new second card to asplit hand.

 // Handle second card on split hands. if (player[n].split && player[n].cards.length == 2) { // If Aces were split, go on to next hand. if (player[n].split && player[n].cards[0].rank == "A") { startNextHand(); return; } // Enable/disable buttons. document.forms["controls"].elements["double"].disabled = false; if (canSplit()) document.forms["controls"].elements["split"].disabled = false; }

If this hand was just split, we check to see if it was a pair of aces. Sinceaces cannot be resplit and receive only one additional card, play should moveonto the next hand (step 3). Of course, the next hand will be the one with theother ace that was split off, so this process will end up being repeated forthat hand.

Otherwise, we treat this hand as though it were an initial deal, re-enablingthe "Double" option and, if another pair was dealt, the "Split" option as well.In this case, we're basically starting over at step 2 with the currenthand.

Stand

This one is easy to implement. The playerStand() functionsimply calls startNextHand() to move on to step 3.

function playerStand() { // Go on to the next hand. startNextHand();}

We'll look at this function next.

Prev | Next

BrainJar.com: Blackjack (2024)
Top Articles
Latest Posts
Article information

Author: Msgr. Benton Quitzon

Last Updated:

Views: 6468

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Msgr. Benton Quitzon

Birthday: 2001-08-13

Address: 96487 Kris Cliff, Teresiafurt, WI 95201

Phone: +9418513585781

Job: Senior Designer

Hobby: Calligraphy, Rowing, Vacation, Geocaching, Web surfing, Electronics, Electronics

Introduction: My name is Msgr. Benton Quitzon, I am a comfortable, charming, thankful, happy, adventurous, handsome, precious person who loves writing and wants to share my knowledge and understanding with you.