testsAndMisc/poker-stakes/src/engine/deck.ts
Krzysztof kuhy Rudnicki 72a5e28456 Track poker-stakes so the file-length cap can reach it
The directory has existed on disk with 58 untracked files, four of them over
the 250-line cap. An untracked file is invisible to every pre-commit hook, so
those violations could not be fixed or gated while it stayed that way -- and
its CI workflow was untracked too, so nothing ran against it.

Landed with its already-written evidence artifact and a contract, since this
crosses the 4-file threshold.

Verified before staging: 58 plain text source/config files, no binaries, no
node_modules, no build output. npm run check passes -- typecheck, eslint
--max-warnings 0, and 220 tests at 100% statements/branches/functions/lines.

Splitting its four oversized files is deliberately a separate commit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-16 15:19:07 +02:00

53 lines
1.6 KiB
TypeScript

import type { Rng } from './rng'
import { nextInt } from './rng'
import type { Card, Rank, Suit } from './types'
const SUITS: readonly Suit[] = ['clubs', 'diamonds', 'hearts', 'spades']
const RANKS: readonly Rank[] = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
/** Builds a deterministic 52-card deck in fixed suit/rank order. */
export const buildDeck = (): readonly Card[] => {
const deck: Card[] = []
for (const suit of SUITS) {
for (const rank of RANKS) {
deck.push({ rank, suit })
}
}
return deck
}
/**
* Selection-sampling shuffle: repeatedly pulls a random remaining card into
* the output. Uses splice (never indexed reads) so no element access is ever
* typed `Card | undefined` under noUncheckedIndexedAccess.
*/
export const shuffleDeck = (rng: Rng, deck: readonly Card[]): Card[] => {
const pool: Card[] = [...deck]
const out: Card[] = []
while (pool.length > 0) {
for (const card of pool.splice(nextInt(rng, pool.length), 1)) {
out.push(card)
}
}
return out
}
export interface DealCursor {
deck: readonly Card[]
next: number
}
export const createDealCursor = (deck: readonly Card[]): DealCursor => ({ deck, next: 0 })
/** Deals `count` cards from the cursor, returning the cards and an advanced cursor. */
export const dealCards = (
cursor: DealCursor,
count: number,
): { cards: Card[]; cursor: DealCursor } => {
if (cursor.next + count > cursor.deck.length) {
throw new Error('dealCards: not enough cards remaining in deck')
}
const cards = cursor.deck.slice(cursor.next, cursor.next + count)
return { cards, cursor: { deck: cursor.deck, next: cursor.next + count } }
}