Created
January 14, 2013 15:51
-
-
Save xwlee/3a590fc01ae4e6fd9c61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Card Constructor | |
function Card(s, n) { | |
var suit = s; | |
var number = n; | |
this.getNumber = function() { | |
return number; | |
}; | |
this.getSuit = function() { | |
return suit; | |
}; | |
this.getValue = function() { | |
if (number > 10) { | |
return 10; | |
} else if (number === 1) { | |
return 11; | |
} else { | |
return number; | |
} | |
}; | |
} | |
function deal() { | |
var randSuit = Math.floor(Math.random() * 4) + 1; | |
var randNumber = Math.floor(Math.random() * 13) + 1; | |
return new Card(randSuit, randNumber); | |
} | |
function Hand() { | |
var card1 = deal(), card2 = deal(); | |
var cards = [card1, card2]; | |
this.getHand = function() { | |
return cards; | |
}; | |
this.score = function() { | |
var sum = 0; | |
var aceCount = 0; | |
for (var i = 0; i < cards.length; i++) { | |
if (cards[i].getValue() === 11) { | |
aceCount++; | |
} | |
sum += cards[i].getValue(); | |
} | |
while (sum > 21 && aceCount > 0) { | |
sum -= 10; | |
aceCount--; | |
} | |
return sum; | |
}; | |
this.printHand = function() { | |
var string = ""; | |
for (var i = 0, length = cards.length; i < length; i++) { | |
string += cards[i].getNumber() + " of suit " + cards[i].getSuit(); | |
string += (i === length - 1) ? "" : ", "; | |
} | |
return string; | |
}; | |
this.hitMe = function() { | |
cards.push(deal()); | |
}; | |
} | |
function playAsDealer() { | |
var hand = new Hand(); | |
while (hand.score() < 17) { | |
hand.hitMe(); | |
} | |
return hand; | |
} | |
function playAsUser() { | |
var hand = new Hand(); | |
var decision = confirm("Your hand is " + hand.printHand() + ": Hit OK to hit (take another card) or cancel to stand"); | |
while(decision) { | |
hand.hitMe(); | |
decision = confirm("Your hand is " + hand.printHand() + ": Hit OK to hit (take another card) or cancel to stand"); | |
} | |
return hand; | |
} | |
function declareWinner(userHand, dealerHand) { | |
var userScore = userHand.score(); | |
var dealerScore = dealerHand.score(); | |
if (userScore > 21) { | |
if (dealerScore > 21) { | |
return "You tie!"; | |
} else { | |
return "You lose!"; | |
} | |
} else { | |
if (dealerScore > 21) { | |
return "You win!"; | |
} else { | |
if (userScore > dealerScore) { | |
return "You win!"; | |
} else if (userScore === dealerScore) { | |
return "You tie!"; | |
} else { | |
return "You lose!"; | |
} | |
} | |
} | |
} | |
function playGame() { | |
var dealerHand = playAsDealer(); | |
var userHand = playAsUser(); | |
var winnerMessage = declareWinner(userHand, dealerHand); | |
console.log("Dealer has " + dealerHand.printHand()); | |
console.log("Dealer score is " + dealerHand.score()); | |
console.log("User has " + userHand.printHand()); | |
console.log("User score is " + userHand.score()); | |
console.log(winnerMessage); | |
} | |
playGame(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment