☰
✕
Главная
© 2002 - 2025
Diary.ru
Я сделал свой блэкджек!
xe-xe-my-strange-life
| суббота, 15 декабря 2012
код
function Deck() { var fullDeck = []; var index; for(index = 0; index < 52; index++) //Create the deck. fullDeck[index] = index; this.giveCard = function() {//whichCard = position in array of card to deal, 0-51. var whichCard = Math.floor(Math.random()*fullDeck.length); var dealThis; //The actual card to be dealt. dealThis = fullDeck[whichCard]; //Pick a card. fullDeck.splice(whichCard,1); //Take it out of the deck. return dealThis; } } function Card(suit,number) { var suit = suit; var number = number; this.getSuit = function() {return suit;}; this.getNumber = function() {return number;}; this.getValue = function() { if(number > 10) //Face card. return 10; if(number == 1) //Ace. return 11; if((number > 1) && (number <= 10)) return number; } } function Hand() { var privateHand = []; var currentCard; var currentScore = 0; privateHand[0] = deal(); privateHand[1] = deal(); this.getHand = function() {return privateHand;} this.score = function() { var isAce; currentScore = 0; for(currentCard = 0; currentCard < privateHand.length; currentCard++) { isAce = false; if(privateHand[currentCard].getValue() == 11) //It's an Ace. isAce = true; if((isAce) && (currentScore + privateHand[currentCard].getValue() > 21)) currentScore += 1; //Have an Ace and its 11 points would cause a bust. else currentScore += privateHand[currentCard].getValue(); } return currentScore; } this.printHand = function() { var handName = " "; var currentCard; var number; var suit; for(currentCard = 0; currentCard < privateHand.length; currentCard++) { number = privateHand[currentCard].getNumber(); suit = privateHand[currentCard].getSuit(); switch(number) { case 1: handName += "Ace"; break; case 11: handName += "Jack"; break; case 12: handName += "Queen"; break; case 13: handName += "King"; break; default: handName += number; break; } handName += " of "; switch(suit) { case 1: handName += "clubs"; break; case 2: handName += "diamonds"; break; case 3: handName += "hearts"; break; case 4: handName += "spades"; break; default: ; break; } if(currentCard != privateHand.length - 1) handName += ", "; } return handName; } this.hitMe = function() { hitCard = deal(); privateHand.push(hitCard); } } var deal = function() { var thisDeck = new Deck; var thisNumber = thisDeck.giveCard(); var thisSuit = findSuit(thisNumber); //Need raw number (0-51) to determine suit. var thisCard = new Card(thisSuit,thisNumber % 13 + 1); return thisCard; }; var findSuit = function(thisNumber) { if((thisNumber >= 0) && (thisNumber <= 12)) return 1; if((thisNumber >= 13) && (thisNumber <= 25)) return 2; if((thisNumber >= 26) && (thisNumber <= 38)) return 3; if((thisNumber >= 39) && (thisNumber <= 51)) return 4; } var playAsDealer = function() { var dealerHand = new Hand(); while(dealerHand.score() < 17) dealerHand.hitMe(); return dealerHand; }; var playAsUser = function() { var myHand = new Hand; var hit; do { hit = confirm("Your hand is " +myHand.printHand() +" do you want to hit?"); if(hit) if(myHand.score() <= 21) myHand.hitMe(); else { alert("You busted! Cannot hit!"); hit = false; } } while(hit); return myHand; } var declareWinner = function(userHand,dealerHand) { var dScore = dealerHand.score(); var pScore = userHand.score(); if(pScore > 21) if(dScore > 21) return "Both bust! Tie!"; else return "Player bust! You lose!"; else if(dScore > 21) return "Dealer bust! You win!"; else if(dScore > pScore) return "You lose!"; else if(dScore < pScore) return "You win!"; else return "It's a tie!"; } var playGame = function() { var dealerHand = playAsDealer(); var playerHand = playAsUser(); console.log("You have: " +playerHand.score() +" dealer has " +dealerHand.score()); console.log(declareWinner(playerHand,dealerHand)); } playGame();
Осталось поэтесс привести!
Смотрите также
Завораживающая "Музыка в темноте" из Города Белых Ночей в Городе Белых Ночей! )))
Протоиерей Виктор Гурьев, из "Пролога"
Не отправляются комментарии
ЗФБ
про кино
№ 4933