BrainJar.com: Blackjack (2024)

Blackjack

Play the game or view thesource code.

The Blackjack Hand Object

The Hand object is designed to represent an individualblackjack hand and the cards in it. It provides methods for actions like addinga card to the hand, removing a card and totaling the its score.

When the game is first initialized, one of these objects is created for eachpossible hand. This includes the dealer's hand and an array of hands for theplayer (in case of splits). The constructor function is shown below.

Hand.prototype.leftIncr = 2.5; // For positioning cards.Hand.prototype.topIncr = 0.2;Hand.prototype.rollEvery = 5;function Hand(id) { this.cards = new Array(); // Get page elements based on id. this.fieldNode = document.getElementById(id); this.cardsNode = document.getElementById(id + "Cards"); this.scoreTextNode = document.getElementById(id + "Score").firstChild; if (id != "dealer") { this.betTextNode = document.getElementById(id + "Bet").firstChild; this.resultTextNode = document.getElementById(id + "Result").firstChild; } this.reset = handReset; this.addCard = handAddCard; this.removeCard = handRemoveCard; this.getScore = handGetScore; this.clearCards = handClearCards; // Initialize as an empty hand. this.reset();}

To create a hand, an ID string is passed to the function so it can find theelements reserved for that particular hand's playing area on the page. Thesereferences are saved for later use.

This only needs to be done once. Other properties of the Handobject are only relevant to an individual round. These are defined and set inthe reset() method which is used to initialize a hand forplay.

function handReset() { // Remove any cards and initialize properties. this.clearCards(); this.cards = new Array(); this.blackjack = false; this.split = false; this.doubled = false; this.surrender = false; this.left = 0; this.top = 0; this.scoreTextNode.nodeValue = "\u00a0"; if (this.betTextNode) { this.betTextNode.parentNode.className = "textBox dollars"; this.betTextNode.nodeValue = "\u00a0"; } if (this.resultTextNode) this.resultTextNode.nodeValue = "\u00a0";}

First it removes any cards used in the previous round. Then it initializesseveral flags and values that will be used during the course of play. Finally,it resets any text displays in the hand's playing area. The flags used duringgame play will be discussed shortly but first let's look at how the cards aredisplayed.

Displaying Cards

To add a card to a hand, the addCard() method is used. It addsthe given Card object to the hand's cards array andcreates a DOM node for displaying it. This node is dynamically added tocardArea class DIV assigned to the hand (stored as the propertycardsNode) so it will appear on the page. Note that it allows anoption to display the card face down.

function handAddCard(card, down) { var n; var node; // Add the given card to the hand. n = this.cards.length; this.cards[n] = card; // Create a card node for display, set as face down if requested. node = this.cards[n].createNode(); if (down) node.firstChild.style.visibility = "hidden"; // Add the card display to the associated card area on the page. node.style.left = this.left + "em"; node.style.top = this.top + "em"; this.cardsNode.appendChild(node); this.left += this.leftIncr; if (this.cards.length % this.rollEvery == 0) this.top = 0; else this.top += this.topIncr;}

Likewise, the removeCard() method will remove the lastCard object in the cards array and remove the lastcard node from the cardsNode DIV so that it no longer appears onthe page.

function handRemoveCard() { var card; // Remove the last card in the array and save it. card = null; if (this.cards.length > 0) { card = this.cards.pop(); // Remove the card node from the display and reset position. this.cardsNode.removeChild(this.cardsNode.lastChild); this.left -= this.leftIncr; this.top -= this.topIncr; } // Return the card. return card;}

The card that was removed is returned by the function. This makes it usefulfor handling splits, as we'll see later.

Finally, there is the clearCards() method which provides aquick way to remove all the card nodes from a playing area.

function handClearCards() { // Remove the card nodes in the associated card area. while (this.cardsNode.lastChild) this.cardsNode.removeChild(this.cardsNode.lastChild);}

This method is actually used only indirectly via the reset()method to initialize the hand for play.

Scoring

Since the object of the game is to score points by totaling the value ofeach card in a hand, a method is defined to calculate this total. While itseems simple enough to just add up each card's value, the fact that aces canbe counted as one or eleven complicates the task a bit. ThegetScore() method is defined to handle this.

function handGetScore() { var i, total; total = 0; // Total card values counting Aces as one. for (i = 0; i < this.cards.length; i++) if (this.cards[i].rank == "A") total++; else { if (this.cards[i].rank == "J" || this.cards[i].rank == "Q" || this.cards[i].rank == "K") total += 10; else total += parseInt(this.cards[i].rank, 10); } // Change as many ace values to 11 as possible. for (i = 0; i < this.cards.length; i++) if (this.cards[i].rank == "A" && total <= 11) total += 10; return total;}

Basically, it first totals each card value, counting any ace as one point.Then it goes through the cards again looking only at aces. If it finds one andit can be counted as eleven without pushing the total over 21, it does so. Thisway, the method returns the highest score possible, without going over 21, forthe hand.

Prev | Next

BrainJar.com: Blackjack (2024)

References

Top Articles
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 5932

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.