Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. /************************************
  2. * Deck Class
  3. *
  4. * A new deck has 52 cards, each with a value and suit.
  5. *
  6. * shuffle()
  7. * draw()
  8. * score()
  9. * getWinner()
  10. *
  11. **************************************/
  12.  
  13. // creates a deck with 52 cards. Each card has a value and suit
  14. class Deck {
  15.  
  16. constructor() {
  17. this.deck = [],
  18. this.rankings = { 'Straight Flush' : 9, 'Four of a Kind' : 8, 'Full House' : 7,
  19. 'Flush' : 6, 'Straight' : 5, 'Three of a Kind' : 4,
  20. 'Two Pair' : 3, 'Pair' : 2, 'High Card' : 1 },
  21. this.cardValues = {'2' : 2, '3' : 3, '4' : 4, '5' : 5, '6' : 6, '7' : 7, '8' : 8,
  22. '9' : 9, '10' : 10, 'J' : 11, 'Q' : 12, 'K' : 13, 'A' : 14},
  23. this.faceCardValues = { 'J': 11, 'Q': 12, 'K': 13, 'A': 14 },
  24. this.highCardTieBreakers = ['Straight Flush', 'Flush', 'Straight', 'High Card'];
  25.  
  26. var suits = ['hearts', 'spades', 'diamonds', 'clubs'],
  27. values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
  28.  
  29. for (let suit of suits) {
  30. for (let value of values) {
  31. this.deck.push({ 'value': value, 'suit': suit });
  32. }
  33.  
  34. }
  35.  
  36. }
  37.  
  38. }
  39.  
  40. // removes 5 cards from deck
  41. // return a hand with 5 cards
  42. Deck.prototype.draw = function () {
  43. return this.deck.splice(0, 5);
  44. }
  45.  
  46. // shuffle up cards using Fisher-Yates algorithm
  47. // there's a lodash function, _.shuffle(). to do this more easily
  48. Deck.prototype.shuffle = function () {
  49.  
  50. var current = this.deck.length,
  51. temp, random;
  52.  
  53. while (0 !== current) {
  54. random = Math.floor(Math.random() * current);
  55. current -= 1;
  56.  
  57. temp = this.deck[current];
  58. this.deck[current] = this.deck[random];
  59. this.deck[random] = temp;
  60. }
  61.  
  62. return 0;
  63.  
  64. }
  65.  
  66. // helper for score function
  67. Deck.prototype.flushOrStraight = function (hand) {
  68. var isFlush = false,
  69. isStraight = false,
  70. values = [];
  71.  
  72. // see if hand is flush
  73. isFlush = hand.filter(function(item, i, arr) {
  74. return item.suit == arr[0].suit;
  75. }).length === hand.length;
  76.  
  77. // see if hand is straight
  78. values = hand.map(function(card, i, arr) {
  79. if ( isNaN(parseInt(card.value)) ) {
  80. return this.faceCardValues[card.value];
  81. } else {
  82. return parseInt(card.value);
  83. }
  84. }, this);
  85.  
  86. values.sort(function(a,b) { return a-b; });
  87.  
  88. if (isFlush && isStraight) {
  89. return 'Straight Flush';
  90. } else if (isFlush) {
  91. return 'Flush';
  92. } else if (isStraight) {
  93. return 'Straight';
  94. } else {
  95. return 'High Card';
  96. }
  97.  
  98. }
  99.  
  100. // determines what type of hand it is
  101. Deck.prototype.score = function (hand) {
  102. var rank = '',
  103. values = [],
  104. numberOfAKind = [];
  105.  
  106. values = hand.map(function(card, i, arr) {
  107. if ( isNaN(parseInt(card['value'])) ) {
  108. return this.faceCardValues[card.value];
  109. } else {
  110. return parseInt(card['value']);
  111. }
  112. }, this);
  113.  
  114. values.sort(function(a,b) { return a-b; });
  115.  
  116. // load numberOfAKind array. This tell us how many of each card is in a hand
  117. // values array is already sorted
  118. for (let i = 0; i < 5; i++) {
  119.  
  120. if (i == 0) {
  121. numberOfAKind.push({'value': values[i], 'count': 1});
  122. } else {
  123. if ( numberOfAKind[numberOfAKind.length - 1]['value'] == values[i] ) {
  124. numberOfAKind[numberOfAKind.length - 1]['count'] += 1;
  125. } else {
  126. numberOfAKind.push({'value': values[i], 'count': 1});
  127. }
  128. }
  129. }
  130.  
  131. // If only two types of cards then it's eith four of a kind or full housr.
  132. // Return whichever it is
  133. if (numberOfAKind.length == 2) {
  134. if (numberOfAKind[0].count == 4 || numberOfAKind[1].count == 4) {
  135. return {'hand' : 'Four of a Kind', 'numberOfAKind' : numberOfAKind};
  136. } else {
  137. return {'hand' : 'Full House', 'numberOfAKind' : numberOfAKind};
  138. }
  139. }
  140.  
  141. // If only three types of cards we have either three of a kind or two pair
  142. // Return whichever it is
  143. if (numberOfAKind.length == 3) {
  144. for (let kind of numberOfAKind) {
  145. if (kind.count == 3) {
  146. return {'hand' : 'Three of a Kind', 'numberOfAKind' : numberOfAKind};
  147. }
  148. }
  149. return {'hand' : 'Two Pair', 'numberOfAKind' : numberOfAKind};
  150. }
  151.  
  152. // If four types of cards then there must be a pair
  153. if (numberOfAKind.length == 4) {
  154. return {'hand' : 'Pair', 'numberOfAKind' : numberOfAKind};
  155. }
  156.  
  157. // If 5 types of cards then mush have flush, straight or nothing
  158. if (numberOfAKind.length == 5) {
  159. return {'hand' : this.flushOrStraight(hand), 'numberOfAKind' : numberOfAKind};
  160. }
  161.  
  162. }
  163.  
  164. // helper for getWinner function to get high Card Value
  165. Deck.prototype.getHighCard = function (hand) {
  166.  
  167. var highCardValue = hand.map(function(card) {
  168. if ( isNaN(parseInt(card.value)) ) {
  169. return this.faceCardValues[card.value];
  170. } else {
  171. return parseInt(card.value);
  172. }
  173. }, this).sort(function(a,b) { return a-b; });
  174.  
  175. return highCardValue[highCardValue.length - 1];
  176. }
  177.  
  178. // compares two hands and determines the winner
  179. Deck.prototype.getWinner = function (hand1, hand2) {
  180. var score1 = {},
  181. score2 = {},
  182. highCount = 0;
  183. highScore = 0;
  184.  
  185. score1 = this.score(hand1);
  186. score2 = this.score(hand2);
  187.  
  188. if (this.rankings[score1['hand']] > this.rankings[score2['hand']]) {
  189. return 'Hand 1 is the winner with a ' + score1['hand'] + '!';
  190. } else if (this.rankings[score1['hand']] < this.rankings[score2['hand']]) {
  191. return 'Hand 2 is the winner with a ' + score2['hand'] + '!';
  192. }
  193.  
  194. // if neither of above checks result in a winner then both players have the
  195. // same hand. Find out if one is higher or we end in a draw
  196.  
  197. // this block is only for sraight flush, flush, straight and high cards
  198. if ( this.highCardTieBreakers.indexOf(score1['hand']) > -1 ) {
  199. let highCardValue1 = this.getHighCard(hand1);
  200. let highCardValue2 = this.getHighCard(hand2);
  201.  
  202. if ( highCardValue1 > highCardValue2 ) {
  203. return 'Hand 1 is the winner with a ' + score1['hand'] + '!';
  204. } else if (highCardValue1 < highCardValue2) {
  205. return 'Hand 2 is the winner with a ' + score2['hand'] + '!';
  206. } else {
  207. return 'We have a draw! Both players have a ' + score2['hand'];
  208. }
  209. }
  210.  
  211. // for 4 of kind, full house, 3 of kind, two pair, pair
  212. for (let kind of score1.numberOfAKind) {
  213. if (kind.count > highCount) {
  214. highCount = kind.count;
  215. highScore = kind.value;
  216. }
  217. }
  218.  
  219. for (let kind of score2.numberOfAKind) {
  220. if (kind.count == highCount && kind.value == highScore) {
  221. return 'We have a draw! Both players have a ' + score2['hand'];
  222. } else if (kind.count == highCount && kind.value > highScore) {
  223. return 'Hand 2 is the winner with a ' + score2['hand'] + '!'; }
  224. }
  225.  
  226. return 'Hand 1 is the winner with a ' + score1['hand'] + '!';
  227.  
  228. }
  229.  
  230. var deck = new Deck();
  231. console.log("There are " + deck.deck.length + " cards in the deck. Let's play a hand.");
  232. deck.shuffle();
  233. var hand = deck.draw();
  234. var hand2 = deck.draw();
  235. console.log('Hand 1 is:');
  236. console.log(hand);
  237. console.log('Hand 2 is: ');
  238. console.log(hand2);
  239. console.log('There are now ' + deck.deck.length + ' cards in the deck.');
  240. console.log('Aaaaand...........');
  241. console.log(deck.getWinner(hand, hand2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement