Advertisement
Guest User

Untitled

a guest
May 26th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. const _ = require('lodash');
  2.  
  3. /*
  4. * Card
  5. */
  6.  
  7. class Card {
  8.  
  9. constructor(game, deck, options) {
  10. this.game = game;
  11. this.deck = deck;
  12.  
  13. // TODO: move game descriptions to a config file
  14. if (game === "BLACKJACK") {
  15. //CARDS in blackjack have Suits defined by the SUIT enum.
  16. //Only one suit and number is allowed per dec
  17. //WHICH HAS 4 SUITS
  18.  
  19. //Get number of cards left. Cards in 52 deck have
  20. // face value and suit and color
  21. // console.log(options.face_value, options.suit);
  22.  
  23. if (!options || !options.suit || !options['face_value']) {
  24. throw new Error(`Missing suit or face_value`);
  25. } else {
  26. this.suit = options.suit;
  27. this.value = options.face_value;
  28. }
  29.  
  30.  
  31. } else {
  32. throw new Error(`${game} is not supported, yet`);
  33. }
  34.  
  35.  
  36. }
  37.  
  38. get color() {
  39. // TODO: abstract out to a descriptor class
  40. if (this.suit == "SPADE" || this.suit == "CLOVER") {
  41. return "BLACK"
  42. } else {
  43. return "RED"
  44. }
  45. }
  46.  
  47. }
  48.  
  49. /*
  50. * Deck
  51. */
  52.  
  53. class Deck {
  54.  
  55. constructor(game) {
  56.  
  57. this.game = game;
  58. this._cards = [];
  59.  
  60. if (game === "BLACKJACK") {
  61. //BLACK JACK IS PLAYED WITH A DECK OF 52 CARDS
  62. //WHICH HAS 4 SUITS
  63.  
  64. _.each(["SPADE", "HEART", "CLOVER", "DIAMOND"], (suit) => {
  65.  
  66. _.times(13, (face_value) => {
  67. this.createCard({
  68. suit: suit,
  69. face_value: face_value + 1
  70. });
  71. });
  72.  
  73. });
  74.  
  75. } else {
  76. throw new Error(`${game} is not supported, yet`);
  77. }
  78.  
  79.  
  80. }
  81.  
  82. get cards() {
  83. return this._cards
  84. }
  85.  
  86. get length() {
  87. return this._cards.length
  88. }
  89.  
  90. createCard(options) {
  91.  
  92. // console.log(`Create (${this.length}) card for the same game ${this.game} with ${options}`);
  93. let card = new Card(this.game, this, options);
  94. this.cards.push(card);
  95.  
  96. }
  97.  
  98. PickCard() {
  99.  
  100. if (this.length > 0 ) {
  101. var idx = Math.floor(Math.random()*this.length);
  102. return this.cards.splice(idx, 1)[0];
  103. }
  104. }
  105.  
  106. }
  107.  
  108.  
  109. class BlackJackHand {
  110.  
  111.  
  112. constructor(player) {
  113. this.cards = [];
  114. this.player = player
  115. }
  116.  
  117. addCard(card) {
  118. // TODO: Check Card is of type CARD
  119. this.cards.push(card);
  120. this.checkScore();
  121.  
  122. }
  123.  
  124. checkScore() {
  125.  
  126. if (this.isBust) {
  127. console.log(`Game Over, ${this.player} busted`);
  128. }
  129.  
  130. if (this.isBlackJack) {
  131. console.log(`Game Over, ${this.player} Won!`);
  132.  
  133. }
  134. }
  135.  
  136. get isGameOver() {
  137. return this.isBust || this.isBlackJack;
  138. }
  139.  
  140. get score() {
  141. var score = 0;
  142.  
  143. _.each(this.cards, (card) => {
  144. //console.log(card, score, "FACE", card.value);
  145. score = score + card.value;
  146. });
  147.  
  148. // console.log(`${this.player} score: ${score} `);
  149. return score;
  150. }
  151.  
  152. get isBust() {
  153. return this.score > 21;
  154. }
  155.  
  156. get isBlackJack() {
  157. return this.score == 21;
  158. }
  159. }
  160.  
  161.  
  162.  
  163. var blackJackDeck = new Deck("BLACKJACK");
  164.  
  165. var playerHand = new BlackJackHand("Player");
  166.  
  167. var dealerHand = new BlackJackHand("Dealer");
  168.  
  169. while(!(playerHand.isGameOver || dealerHand.isGameOver)) {
  170.  
  171. if (blackJackDeck.length > 0) {
  172. var dealerCard = blackJackDeck.PickCard();
  173. dealerHand.addCard(dealerCard);
  174. console.log(`Dealer's card is: ${dealerCard.value} of ${dealerCard.suit} and score is ${dealerHand.score}`);
  175.  
  176. var playerCard = blackJackDeck.PickCard();
  177. playerHand.addCard(playerCard);
  178. console.log(`player's card is: ${playerCard.value} of ${playerCard.suit} and score is ${playerHand.score}`);
  179. } else {
  180. console.log('Out of cards, tie!');
  181. }
  182.  
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement