Skip to content

Commit

Permalink
fix(core): not detecting "high" straight
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinfreund committed Mar 12, 2024
1 parent 4960015 commit abec0af
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const RANK_TO_CHIP_MAP: Record<Rank, number> = {
}

export const RANK_TO_INDEX_MAP: Record<Rank, number> = {
'Ace': 14,
'King': 13,
'Queen': 12,
'Jack': 11,
Expand All @@ -116,7 +117,6 @@ export const RANK_TO_INDEX_MAP: Record<Rank, number> = {
'4': 4,
'3': 3,
'2': 2,
'Ace': 1,
}

export const PLAYED_CARD_RETRIGGER_JOKER_NAMES: JokerName[] = [
Expand Down
32 changes: 32 additions & 0 deletions src/lib/getHand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,24 @@ describe('getHand', () => {
{ rank: 'King', suit: 'Hearts' },
],
},
{
message: 'High straight',
initialCards: [
{ rank: 'King', suit: 'Clubs' },
{ rank: 'Queen', suit: 'Clubs' },
{ rank: 'Ace', suit: 'Hearts' },
{ rank: 'Jack', suit: 'Spades' },
{ rank: '10', suit: 'Diamonds' },
],
expectedPlayedHand: 'Straight',
expectedScoringCards: [
{ rank: 'King', suit: 'Clubs' },
{ rank: 'Queen', suit: 'Clubs' },
{ rank: 'Ace', suit: 'Hearts' },
{ rank: 'Jack', suit: 'Spades' },
{ rank: '10', suit: 'Diamonds' },
],
},
{
message: 'Low straight',
initialCards: [
Expand All @@ -429,6 +447,20 @@ describe('getHand', () => {
{ rank: '3', suit: 'Diamonds' },
],
},
{
message: 'Not a low straight',
initialCards: [
{ rank: 'King', suit: 'Clubs' },
{ rank: '4', suit: 'Clubs' },
{ rank: 'Ace', suit: 'Hearts' },
{ rank: '2', suit: 'Spades' },
{ rank: '3', suit: 'Diamonds' },
],
expectedPlayedHand: 'High Card',
expectedScoringCards: [
{ rank: 'Ace', suit: 'Hearts' },
],
},
{
message: 'Straight + Four Fingers + Shortcut',
initialCards: [
Expand Down
36 changes: 30 additions & 6 deletions src/lib/getHand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,15 @@ export function straight (cards: Card[], hasFourFingers: boolean, hasShortcut: b
const gap = hasShortcut ? 2 : 1
const cardIndexes = cards
.filter(({ enhancement }) => enhancement !== 'stone')
.map(({ rank }) => RANK_TO_INDEX_MAP[rank])
.map(({ rank }) => {
if (rank === 'Ace') {
// Count an Ace both for its natural rank (i.e. succeeding a “King”) and for its special rank (i.e. preceeding a “2”).
return [RANK_TO_INDEX_MAP[rank], 1]
}

return [RANK_TO_INDEX_MAP[rank]]
})
.flat()
cardIndexes.sort((a, b) => a - b)

const gaps = Array.from({ length: gap }, (_, index) => index + 1)
Expand All @@ -162,19 +170,35 @@ export function straight (cards: Card[], hasFourFingers: boolean, hasShortcut: b
let isStraight = false
for (let i = 1; i < cardIndexes.length; i++) {
const cardIndex = cardIndexes[i]!
const isWithinGap = gaps.some((gap) => previousCardIndex + gap === cardIndex)

if (isWithinGap) {
scoringCardIndexes.push(cardIndex)

if (gaps.some((gap) => previousCardIndex + gap === cardIndex)) {
if (scoringCardIndexes.length === straightnessThreshold) {
// Mark the hand as a straight but continue evaluating to determine _all scoring_ cards, not just the ones needed to form a straight.
isStraight = true
}
} else if (!isStraight) {
// If the hand isn't already a straight and the straightness is broken, start over by clearing the preliminary list of scoring card indexes and assuming the next card is the start of a straight.
scoringCardIndexes.length = 0
scoringCardIndexes.push(cardIndex)
}

previousCardIndex = cardIndex
}

if (scoringCardIndexes.length === straightnessThreshold) {
isStraight = true
}
if (!isStraight) {
return []
}

return isStraight ? cards.filter(({ rank }) => scoringCardIndexes.includes(RANK_TO_INDEX_MAP[rank]!)) : []
return cards.filter(({ rank }) => {
if (rank === 'Ace' && scoringCardIndexes.includes(1)) {
return true
}

return scoringCardIndexes.includes(RANK_TO_INDEX_MAP[rank]!)
})
}

export function twoPair (cards: Card[]): Card[] {
Expand Down

0 comments on commit abec0af

Please sign in to comment.