fork download
  1. // Make your card constructor again here, but make sure to use private
  2. // variables!
  3. //http://j...content-available-to-author-only...r.org/ <---if you want to properly indent your code
  4.  
  5. var isBetween = function (A, B, C) {
  6. return (A <= B && B <= C);
  7. };
  8.  
  9. function Card(s, n) {
  10. var suit = s;
  11. var number = n;
  12. this.getSuit = function () {
  13. return suit;
  14. };
  15. this.getNumber = function () {
  16. return number;
  17. };
  18. }
  19. Card.prototype.getValue = function () {
  20. return this.getNumber() === 1 ? 11 : isBetween(11, this.getNumber(), 13) ? 10 : this.getNumber();
  21. };
  22.  
  23. var deal = function () {
  24. return new Card(-~(Math.floor(Math.random() * 4)), -~(Math.floor(Math.random() * 13)));
  25. };
  26.  
  27. function Deck() {
  28. var accept = {
  29. 1: [],
  30. 2: [],
  31. 3: [],
  32. 4: []
  33. };
  34. for (var s in accept) {
  35. for (var i = 0; i < 13; ++i)
  36. accept[s][i] = 1;
  37. }
  38.  
  39. this.TryAddDeck = function (newCard) {
  40. if (accept[newCard.getSuit()][newCard.getNumber() - 1]) {
  41. accept[newCard.getSuit()][newCard.getNumber() - 1] = 0;
  42. return 1;
  43. }
  44. return 0;
  45. };
  46. }
  47.  
  48. function Hand() {
  49. var pack = new Deck(),
  50. deck = [],
  51. l = deck.length,
  52. string = '';
  53. this.good = 1;
  54. this.printHand = function () {
  55. var suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'];
  56. var face = {
  57. 1: 'Ace',
  58. 11: 'Jack',
  59. 12: 'Queen',
  60. 13: 'King'
  61. };
  62. while (l < deck.length) {
  63. var num = deck[l].getNumber();
  64. string += (l ? ', ' : '') + (num ^ 1 && num < 11 ? num : face[num]) + ' of ' + suits[deck[l].getSuit() - 1];
  65. ++l;
  66. }
  67. return string;
  68. };
  69.  
  70. var close = function () {
  71. this.good = 0;
  72. alert("You have used up all the cards in your deck!\n");
  73. };
  74.  
  75. var addToDeck = function (myCard) {
  76. deck[deck.length] = myCard;
  77. };
  78.  
  79. this.hitMe = function () {
  80. var c;
  81. if (deck.length ^ 52) {
  82. while (!pack.TryAddDeck((c = deal())));
  83. addToDeck(c);
  84. } else close();
  85. };
  86.  
  87. this.score = function () {
  88. var s = 0,
  89. bust = deck.length,
  90. n;
  91. for (var c in deck) {
  92. s += (n = deck[c].getValue());
  93. bust -= ((n ^ 11) !== 0);
  94. }
  95. while (s > 21 && bust--) s -= 10;
  96. return s;
  97. };
  98.  
  99. this.hitMe();
  100. this.hitMe();
  101. }
  102.  
  103. var playAsDealer = function () {
  104. var myHand = new Hand();
  105. while (myHand.score() < 17)
  106. myHand.hitMe();
  107. return myHand;
  108. };
  109.  
  110. var playAsUser = function () {
  111. var myHand = new Hand();
  112. while (myHand.good && confirm('\tIn your deck you have:\n' +
  113. '\t-------------------------------------\n' +
  114. myHand.printHand() +
  115. "\n\n\tDo you want to hit?")) {
  116. myHand.hitMe();
  117. }
  118. return myHand;
  119. };
  120.  
  121. var declareWinner = function (userHand, dealerHand) {
  122. var u = userHand.score(),
  123. d = dealerHand.score();
  124. console.log('User Hand\n' + userHand.printHand() +
  125. '\nScore = ' + u + '\n\nDealer Hand\n' +
  126. dealerHand.printHand() +
  127. '\nScore = ' + d + '\n');
  128. return (u === d || (u > 21 && d > 21)) ? "You tied!" : ((isBetween(0, d, 21) && d > u) || (u > 21)) ? "You lose!" : "You win!";
  129. };
  130.  
  131. var playGame = function () {
  132. console.log(declareWinner(playAsUser(), playAsDealer()));
  133. };
  134. playGame();
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty