[new_dealer (change_dealer) dtmf-3] [repeat] [help dtmf-0]
pc1= , pc2= , dc1=
} [no dtmf-2] {
} [no dtmf-2] {
/* B J . J S - Encapsulates a game of Blackjack. Can be used anywhere JavaScript is supported - in a Voice application, in a Web application, etc. a card object consists of num - an absolute number from 1..52 type - 2..10, j, q, k, a value - 2...11 suit - c (clubs), h (hearts), s (spades), d (diamonds) */ function Card(iNum, sType, iValue, sSuit) { this.num = iNum; this.type = sType; this.value = iValue; this.suit = sSuit; } // Diagnostic Card.prototype.Dump = function() { return "num=" + this.num + ", type=" + this.type + ", value=" + this.value + ", suit=" + this.suit; } // return the card's value Card.prototype.GetValue = function() { return this.value; } // return the card's type Card.prototype.GetType = function() { return this.type; } // return the concatenation of cards type and suit (e.g. "as" == "ace of spades") // the audio files for each card use this naming scheme Card.prototype.GetTypeSuit = function() { return this.type + this.suit; } // a blackjack object function Blackjack() { /* a tuple consisting of card type and its corresponding value a card is picked at random from 1 to 52 the random value is normalized down to 0..12 to determine its corresponding tuple */ this.aCardInfos = new Array( ["a", 11], // 0 ["2", 2], // 1 ["3", 3], // 2 ["4", 4], ["5", 5], ["6", 6], ["7", 7], ["8", 8], ["9", 9], ["10", 10], ["j", 10], ["q", 10], ["k", 10]); // 12 } // Initialize the array of dealt cards and each player's hand Blackjack.prototype.Init = function() { this.aDealtCards = new Array(); // cards dealt to the dealer and player this.hPlayers = { "caller" : [], // array of cards dealt to the player "dealer" : []}; // array of cards dealt to the dealer this.iIterations = 0; // number of iterations to generate valid card } // deal a card to sWho Blackjack.prototype.Hit = function(sWho) { var aHand = this.hPlayers[sWho]; if (!aHand) { return null; } var oCard = this.GetCard(); aHand[aHand.length] = oCard; this.aDealtCards[this.aDealtCards.length] = oCard; return oCard; } // return the last card dealt to sWho Blackjack.prototype.GetLastCardOf = function(sWho) { var aHand = this.hPlayers[sWho]; if (!aHand) { return null; } return aHand[aHand.length-1]; } // return the total value of the players hand Blackjack.prototype.GetTotalOf = function(sWho) { var aHand = this.hPlayers[sWho]; if (!aHand) { return 0; } var iAces = 0, iTotal = 0; for(var i=0; i 21 && iAces > 0) { iAces--; iTotal -= 10; } return iTotal; } // manufacture a random card from a deck of 52. Blackjack.prototype.GetCard = function() { var aDealtCards = this.aDealtCards; var sSuit = "d"; // make a card by picking a number at random from 1..52 var iNum = Math.floor(Math.random()*52) + 1; // make sure the card hasn't already been dealt for(var i=0; i= 1 && iNum <= 13) { sSuit = 'd'; } else if(iNum >= 14 && iNum <= 26) { sSuit = 'c'; } else if(iNum >= 17 && iNum <= 39) { sSuit = 'h'; } else if(iNum >= 40 && iNum <= 52) { sSuit = 's'; } return new Card(iNum, aCard[0], aCard[1], sSuit); } // Finish up the hand Blackjack.prototype.Finish = function() { var iDealerTotal = this.GetTotalOf("dealer"); // dealer must take cards until she hits at least 17 while(iDealerTotal < 17) { oBJ.Hit("dealer"); iDealerTotal = oBJ.GetTotalOf("dealer"); } return true; } // 0 == lose, 1 == win, 2 == draw/push Blackjack.prototype.GetGameResult = function() { var iPlayerTotal = this.GetTotalOf("caller"); var iDealerTotal = this.GetTotalOf("dealer"); // figure out who won if (iPlayerTotal > 21 && iDealerTotal > 21) { return 0; } else if (iPlayerTotal > 21 && iDealerTotal <= 21) { return 0; } else if (iPlayerTotal <= 21 && iDealerTotal > 21) { return 1; } else // (iPlayerTotal <= 21 && iDealerTotal <= 21) { if(iDealerTotal == iPlayerTotal) { return 2; } else if (iDealerTotal > iPlayerTotal) { return 0; } else // (iDealerTotal < iPlayerTotal) { return 1; } } }