WIP rules & evaluators

This commit is contained in:
Github Readme Stats Bot 2026-01-17 19:03:21 -07:00
parent dc7787707c
commit 16d377a726
4 changed files with 80 additions and 0 deletions

18
evaluator.ts Normal file
View file

@ -0,0 +1,18 @@
import type Card from './card';
import Pile from './pile'
import type Rule from './rules/rule';
export type winner = {
index: number,
hand: Card[]
}
export default function(piles: Pile[], rule: Rule): winner | null {
if (piles.length < 1) return null
return piles.reduce((accumulator: winner, pile: Pile) => {
const priorPile = new Pile(accumulator.hand)
const priorPileEvaluated = rule(priorPile)
const pileEvaluated = rule(pile)
}, { index: -1, hand: [] })
}

40
rules/highcard.test.ts Normal file
View file

@ -0,0 +1,40 @@
import { expect, test } from 'bun:test'
import ruleHighcard from './highcard'
import Pile from '../pile'
import Card, { Index, Suit } from '../card'
test('Ten beats One', () => {
const cardTen = new Card(Index.Ten, Suit.Club)
const cardOne = new Card(Index.One, Suit.Club)
const pile = new Pile([cardTen, cardOne])
expect(ruleHighcard(pile)).toBe(cardTen)
// Ensure it isn't just lucky ordering
const pileFlipped = new Pile([cardOne, cardTen])
expect(ruleHighcard(pileFlipped)).toBe(cardTen)
})
test('Ace beats King', () => {
const cardAce = new Card(Index.Ace, Suit.Club)
const cardKing = new Card(Index.King, Suit.Club)
const pile = new Pile([cardAce, cardKing])
expect(ruleHighcard(pile)).toBe(cardAce)
// Ensure it isn't just lucky ordering
const pileFlipped = new Pile([cardKing, cardAce])
expect(ruleHighcard(pileFlipped)).toBe(cardAce)
})
test('two hands compared for a winner', () => {
const cardAce = new Card(Index.Ace, Suit.Club)
const cardKing = new Card(Index.King, Suit.Club)
const cardTwo = new Card(Index.Two, Suit.Club)
const cardOne = new Card(Index.One, Suit.Club)
const hand1 = new Pile([cardAce, cardKing])
const hand2 = new Pile([cardOne, cardTwo])
expect(ruleHighcard(hand1)).toBe(cardAce)
expect(ruleHighcard(hand2)).toBe(cardTwo)
})

15
rules/highcard.ts Normal file
View file

@ -0,0 +1,15 @@
import type Rule from './rule'
import Card from '../card'
let rule: Rule
rule = function(pile) {
if (pile.cards.length < 1) return null
return pile
.cards
.reduce((accumulator: Card, card: Card) => {
if (card.decimalIndex > accumulator.decimalIndex) return card
return accumulator
}, pile.cards[0] as Card)
}
export default rule

7
rules/rule.ts Normal file
View file

@ -0,0 +1,7 @@
import Pile from '../pile'
import Card from '../card'
export default interface Rule {
(pile: Pile): Card | null
}