[189cd8f] | 1 | export const hands = [
|
---|
| 2 | 'Royal Flush',
|
---|
| 3 | 'Straight Flush',
|
---|
| 4 | 'Four of a Kind',
|
---|
| 5 | 'Full House',
|
---|
| 6 | 'Flush',
|
---|
| 7 | 'Straight',
|
---|
| 8 | 'Three of a Kind',
|
---|
| 9 | 'Two Pairs',
|
---|
| 10 | 'Pair',
|
---|
| 11 | 'High Card',
|
---|
| 12 | ]
|
---|
| 13 |
|
---|
| 14 | const order = "23456789XJQKA"
|
---|
| 15 | export function getHandDetails(hand) {
|
---|
| 16 | const cards = hand
|
---|
| 17 | const faces = cards.map(a => String.fromCharCode([77 - order.indexOf(a[1])])).sort()
|
---|
| 18 | const suits = cards.map(a => a[0]).sort()
|
---|
| 19 | const counts = faces.reduce(count, {})
|
---|
| 20 | const duplicates = Object.values(counts).reduce(count, {})
|
---|
| 21 | const flush = suits[0] === suits[4]
|
---|
| 22 | const first = faces[0].charCodeAt(1)
|
---|
| 23 | const straight = faces.every((f, index) => f.charCodeAt(1) - first === index)
|
---|
| 24 | let rank =
|
---|
| 25 | (flush && straight && 1) ||
|
---|
| 26 | (duplicates[4] && 2) ||
|
---|
| 27 | (duplicates[3] && duplicates[2] && 3) ||
|
---|
| 28 | (flush && 4) ||
|
---|
| 29 | (straight && 5) ||
|
---|
| 30 | (duplicates[3] && 6) ||
|
---|
| 31 | (duplicates[2] > 1 && 7) ||
|
---|
| 32 | (duplicates[2] && 8) ||
|
---|
| 33 | 9;
|
---|
| 34 |
|
---|
| 35 | return { hand: hands[rank], highCard: faces.sort(byCountFirst).join("") }
|
---|
| 36 |
|
---|
| 37 | function byCountFirst(a, b) {
|
---|
| 38 | //Counts are in reverse order - bigger is better
|
---|
| 39 | const countDiff = counts[b] - counts[a]
|
---|
| 40 | if (countDiff) return countDiff // If counts don't match return
|
---|
| 41 | return b > a ? -1 : b === a ? 0 : 1
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | function count(c, a) {
|
---|
| 45 | c[a] = (c[a] || 0) + 1
|
---|
| 46 | return c
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | export function compareHands(h1, h2) {
|
---|
| 51 | let d1 = getHandDetails(h1)
|
---|
| 52 | let d2 = getHandDetails(h2)
|
---|
| 53 | if (hands.indexOf(d1.hand) === hands.indexOf(d2.hand)) {
|
---|
| 54 | if (d1.highCard < d2.highCard) {
|
---|
| 55 | return d2;
|
---|
| 56 | } else if (d1.highCard > d2.highCard) {
|
---|
| 57 | return d1;
|
---|
| 58 | } else {
|
---|
| 59 | return d1;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | return hands.indexOf(d1.hand) < hands.indexOf(d2.hand) ? d2 : d1;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | export function getBestHandDetails(playerCards, tableCards) {
|
---|
| 66 | let combinations = getCardCombinations(playerCards, tableCards);
|
---|
| 67 |
|
---|
| 68 | let h1 = combinations[0];
|
---|
| 69 | let bestHand = h1;
|
---|
| 70 | combinations.forEach(combination => {
|
---|
| 71 | bestHand = compareHands(h1, combination);
|
---|
| 72 | h1 = combination;
|
---|
| 73 | })
|
---|
| 74 |
|
---|
| 75 | return bestHand;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | export function getCardCombinations(playerCards, tableCards) {
|
---|
| 79 | let combinations = [];
|
---|
| 80 |
|
---|
| 81 | combinations.push([playerCards[0], tableCards[0], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 82 | combinations.push([playerCards[0], tableCards[0], tableCards[1], tableCards[2], tableCards[4]])
|
---|
| 83 |
|
---|
| 84 | combinations.push([playerCards[0], tableCards[0], tableCards[1], tableCards[4], tableCards[3]])
|
---|
| 85 | combinations.push([playerCards[0], tableCards[0], tableCards[4], tableCards[2], tableCards[3]])
|
---|
| 86 | combinations.push([playerCards[0], tableCards[4], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | combinations.push([playerCards[1], tableCards[0], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 90 | combinations.push([playerCards[1], tableCards[0], tableCards[1], tableCards[2], tableCards[4]])
|
---|
| 91 |
|
---|
| 92 | combinations.push([playerCards[1], tableCards[0], tableCards[1], tableCards[4], tableCards[3]])
|
---|
| 93 | combinations.push([playerCards[1], tableCards[0], tableCards[4], tableCards[2], tableCards[3]])
|
---|
| 94 | combinations.push([playerCards[1], tableCards[4], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[1], tableCards[2]])
|
---|
| 98 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[1], tableCards[3]])
|
---|
| 99 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[1], tableCards[4]])
|
---|
| 100 |
|
---|
| 101 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[2], tableCards[3]])
|
---|
| 102 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[2], tableCards[4]])
|
---|
| 103 | combinations.push([playerCards[0], playerCards[1], tableCards[0], tableCards[3], tableCards[4]])
|
---|
| 104 |
|
---|
| 105 | combinations.push([playerCards[0], playerCards[1], tableCards[1], tableCards[2], tableCards[3]])
|
---|
| 106 | combinations.push([playerCards[0], playerCards[1], tableCards[1], tableCards[2], tableCards[4]])
|
---|
| 107 | combinations.push([playerCards[0], playerCards[1], tableCards[1], tableCards[3], tableCards[4]])
|
---|
| 108 |
|
---|
| 109 | combinations.push([playerCards[0], playerCards[1], tableCards[2], tableCards[3], tableCards[4]])
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | return combinations;
|
---|
| 113 | } |
---|