// Make your card constructor again here, but make sure to use private
// variables!
//http://j...content-available-to-author-only...r.org/ <---if you want to properly indent your code
var isBetween = function (A, B, C) {
return (A <= B && B <= C);
};
function Card(s, n) {
var suit = s;
var number = n;
this.getSuit = function () {
return suit;
};
this.getNumber = function () {
return number;
};
}
Card.prototype.getValue = function () {
return this.getNumber() === 1 ? 11 : isBetween(11, this.getNumber(), 13) ? 10 : this.getNumber();
};
var deal = function () {
return new Card(-~(Math.floor(Math.random() * 4)), -~(Math.floor(Math.random() * 13)));
};
function Deck() {
var accept = {
1: [],
2: [],
3: [],
4: []
};
for (var s in accept) {
for (var i = 0; i < 13; ++i)
accept[s][i] = 1;
}
this.TryAddDeck = function (newCard) {
if (accept[newCard.getSuit()][newCard.getNumber() - 1]) {
accept[newCard.getSuit()][newCard.getNumber() - 1] = 0;
return 1;
}
return 0;
};
}
function Hand() {
var pack = new Deck(),
deck = [],
l = deck.length,
string = '';
this.good = 1;
this.printHand = function () {
var suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'];
var face = {
1: 'Ace',
11: 'Jack',
12: 'Queen',
13: 'King'
};
while (l < deck.length) {
var num = deck[l].getNumber();
string += (l ? ', ' : '') + (num ^ 1 && num < 11 ? num : face[num]) + ' of ' + suits[deck[l].getSuit() - 1];
++l;
}
return string;
};
var close = function () {
this.good = 0;
alert("You have used up all the cards in your deck!\n");
};
var addToDeck = function (myCard) {
deck[deck.length] = myCard;
};
this.hitMe = function () {
var c;
if (deck.length ^ 52) {
while (!pack.TryAddDeck((c = deal())));
addToDeck(c);
} else close();
};
this.score = function () {
var s = 0,
bust = deck.length,
n;
for (var c in deck) {
s += (n = deck[c].getValue());
bust -= ((n ^ 11) !== 0);
}
while (s > 21 && bust--) s -= 10;
return s;
};
this.hitMe();
this.hitMe();
}
var playAsDealer = function () {
var myHand = new Hand();
while (myHand.score() < 17)
myHand.hitMe();
return myHand;
};
var playAsUser = function () {
var myHand = new Hand();
while (myHand.good && confirm('\tIn your deck you have:\n' +
'\t-------------------------------------\n' +
myHand.printHand() +
"\n\n\tDo you want to hit?")) {
myHand.hitMe();
}
return myHand;
};
var declareWinner = function (userHand, dealerHand) {
var u = userHand.score(),
d = dealerHand.score();
console.log('User Hand\n' + userHand.printHand() +
'\nScore = ' + u + '\n\nDealer Hand\n' +
dealerHand.printHand() +
'\nScore = ' + d + '\n');
return (u === d || (u > 21 && d > 21)) ? "You tied!" : ((isBetween(0, d, 21) && d > u) || (u > 21)) ? "You lose!" : "You win!";
};
var playGame = function () {
console.log(declareWinner(playAsUser(), playAsDealer()));
};
playGame();
Ly8gTWFrZSB5b3VyIGNhcmQgY29uc3RydWN0b3IgYWdhaW4gaGVyZSwgYnV0IG1ha2Ugc3VyZSB0byB1c2UgcHJpdmF0ZQovLyB2YXJpYWJsZXMhCi8vaHR0cDovL2ouLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLnIub3JnLyA8LS0taWYgeW91IHdhbnQgdG8gcHJvcGVybHkgaW5kZW50IHlvdXIgY29kZQoKdmFyIGlzQmV0d2VlbiA9IGZ1bmN0aW9uIChBLCBCLCBDKSB7CiAgICByZXR1cm4gKEEgPD0gQiAmJiBCIDw9IEMpOwp9OwoKZnVuY3Rpb24gQ2FyZChzLCBuKSB7CiAgICB2YXIgc3VpdCA9IHM7CiAgICB2YXIgbnVtYmVyID0gbjsKICAgIHRoaXMuZ2V0U3VpdCA9IGZ1bmN0aW9uICgpIHsKICAgICAgICByZXR1cm4gc3VpdDsKICAgIH07CiAgICB0aGlzLmdldE51bWJlciA9IGZ1bmN0aW9uICgpIHsKICAgICAgICByZXR1cm4gbnVtYmVyOwogICAgfTsKfQpDYXJkLnByb3RvdHlwZS5nZXRWYWx1ZSA9IGZ1bmN0aW9uICgpIHsKICAgIHJldHVybiB0aGlzLmdldE51bWJlcigpID09PSAxID8gMTEgOiBpc0JldHdlZW4oMTEsIHRoaXMuZ2V0TnVtYmVyKCksIDEzKSA/IDEwIDogdGhpcy5nZXROdW1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AogICAgICAgICAgICAxMTogJ0phY2snLAogICAgICAgICAgICAxMjogJ1F1ZWVuJywKICAgICAgICAgICAgMTM6ICdLaW5nJwogICAgICAgIH07CiAgICAgICAgd2hpbGUgKGwgPCBkZWNrLmxlbmd0aCkgewogICAgICAgICAgICB2YXIgbnVtID0gZGVja1tsXS5nZXROdW1iZXIoKTsKICAgICAgICAgICAgc3RyaW5nICs9IChsID8gJywgJyA6ICcnKSArIChudW0gXiAxICYmIG51bSA8IDExID8gbnVtIDogZmFjZVtudW1dKSArICcgb2YgJyArIHN1aXRzW2RlY2tbbF0uZ2V0U3VpdCgpIC0gMV07CiAgICAgICAgICAgICsrbDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHN0cmluZzsKICAgIH07CgogICAgdmFyIGNsb3NlID0gZnVuY3Rpb24gKCkgewogICAgICAgIHRoaXMuZ29vZCA9IDA7CiAgICAgICAgYWxlcnQoIllvdSBoYXZlIHVzZWQgdXAgYWxsIHRoZSBjYXJkcyBpbiB5b3VyIGRlY2shXG4iKTsKICAgIH07CgogICAgdmFyIGFkZFRvRGVjayA9IGZ1bmN0aW9uIChteUNhcmQpIHsKICAgICAgICBkZWNrW2RlY2subGVuZ3RoXSA9IG15Q2FyZDsKICAgIH07CgogICAgdGhpcy5oaXRNZSA9IGZ1bmN0aW9uICgpIHsKICAgICAgICB2YXIgYzsKICAgICAgICBpZiAoZGVjay5sZW5ndGggXiA1MikgewogICAgICAgICAgICB3aGlsZSAoIXBhY2suVHJ5QWRkRGVjaygoYyA9IGRlYWwoKSkpKTsKICAgICAgICAgICAgYWRkVG9EZWNrKGMpOwogICAgICAgIH0gZWxzZSBjbG9zZSgpOwogICAgfTsKCiAgICB0aGlzLnNjb3JlID0gZnVuY3Rpb24gKCkgewogICAgICAgIHZhciBzID0gMCwKICAgICAgICAgICAgYnVzdCA9IGRlY2subGVuZ3RoLAogICAgICAgICAgICBuOwogICAgICAgIGZvciAodmFyIGMgaW4gZGVjaykgewogICAgICAgICA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