Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. function HandScorer() {
  2.  
  3. function score(hand) {
  4. var matches = {},
  5. handByNumber = matchingNumbers(hand),
  6. handBySuit = matchingSuits(hand);
  7.  
  8. scorers.forEach(function (scorer) {
  9. scorer(hand, handByNumber, handBySuit, matches);
  10. });
  11.  
  12. return matches;
  13. }
  14.  
  15. var matchingNumbers = matcherBuilder(function (card) {
  16. return card.number()
  17. });
  18. var matchingSuits = matcherBuilder(function (card) {
  19. return card.suit()
  20. });
  21.  
  22. var fourOfAKind = nOfNumber(4, 'four of a kind');
  23. var threeOfAKind = nOfNumber(3, '3 of a kind');
  24. var twoOfAKind = nOfNumber(2, '2 of a kind');
  25. var onePair = nOfNumber(2, '2 of a kind');
  26.  
  27. var fullHouse = function (hand, handByNumber, handByType, matches) {
  28. if (matches['three of a kind'].length && matches['three of a kind'].length) {
  29. matches['full house'] = {
  30. 'three of a kind': matches['three of a kind'],
  31. 'two of a kind': matches['two of a kind']
  32. }
  33. }
  34. };
  35.  
  36. var straight = function (hand, handByNumber, handByType, matches) {
  37. var sequential = [];
  38. var previous;
  39. Object.keys(handByNumber).map(function (number) {
  40. return parseInt(number)
  41. }).sort()
  42. .forEach(function (number) {
  43. if (previous && (previous + 1) == number) {
  44. sequential.push(number)
  45. } else {
  46. sequential = [number]
  47. }
  48. previous = number;
  49. });
  50. return matches['straight'] = sequential;
  51. };
  52.  
  53. var flush = function (hand, handByNumber, handBySuit, matches) {
  54. return matches['flush'] = Object.keys(handBySuit).reduce(function (matching, card) {
  55. if (handBySuit[card] == 5) {
  56. matching.push(card)
  57. }
  58. }, [])
  59. };
  60.  
  61. function nOfNumber(n, name) {
  62. return function (hand, handByNumber, handByType, matches) {
  63. matches[name] = Object.keys(handByNumber).reduce(function (matching, card) {
  64. if (handByNumber[card] == n) {
  65. matching.push(card)
  66. }
  67. }, [])
  68. }
  69. }
  70.  
  71. function matcherBuilder(criteriaResolver) {
  72. return function (hand) {
  73. return hand
  74. .map(criteriaResolver)
  75. .reduce(function (matches, card) {
  76. matches[card] = (matches[card] || 0) + 1;
  77. return matches;
  78. }, {});
  79. }
  80. }
  81.  
  82. var scorers = [fourOfAKind, threeOfAKind, twoOfAKind, onePair, fullHouse, straight, flush];
  83.  
  84. return {score: score}
  85. }
  86.  
  87. HandScorer.types = {
  88. ROYAL_FLUSH: 'royal flush',
  89. STRAIGHT_FLUSH: 'straight flush',
  90. FOUR: 'four of a kind',
  91. FULL_HOUSE: 'full house',
  92. FLUSH: 'flush',
  93. STRAIGHT: 'straight',
  94. THREE: 'three of a kind',
  95. TWO: 'two of a kind',
  96. ONE: 'one of a kind',
  97. HIGH: 'high card'
  98. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement