Bridge Training
Bridge Core

Hands

Hand distribution, validation, queries, and transforms.

Import from @workspace/bridge-core/hands.

Distribution

import { distributeCards, completeHands, redistributeHands } from '@workspace/bridge-core/hands';

// Distribute a full deck into 4 hands
const hands = distributeCards(config, seededRandom);

// Complete partially filled hands with remaining cards
const completed = completeHands(partialHands, config, seededRandom);

// Redistribute cards while keeping specified hands fixed
const redistributed = redistributeHands(hands, config, seededRandom);
FunctionDescription
distributeCards(config, random)Distributes a full deck randomly into 4 hands
completeHands(hands, config, random)Fills incomplete hands with remaining cards from the deck
redistributeHands(hands, config, random)Redistributes cards while respecting constraints
filterHandsByConfig(hands, config)Filters out cards that are invalid for the current config

Validation

import { areComplete, areEmpty, numCards, hasTooManyCards } from '@workspace/bridge-core/hands';

numCards(hand);                    // Total cards in a single hand
areEmpty(hands);                   // Whether all hands are empty
areComplete(hands, config);        // Whether all hands have the correct number of cards
hasTooManyCards(hands, config);    // Whether any hand exceeds the card limit
FunctionDescription
numCards(hand)Counts total cards in a hand
areEmpty(hands)Checks if all four hands are empty
isHandComplete(hand, configOrN)Checks if a single hand has the right number of cards
areComplete(hands, config)Checks if all four hands are complete
hasTooManyCards(hands, config)Checks if any hand has too many cards

Queries

import { findCard, findDuplicatedCards, validateCardOwnership } from '@workspace/bridge-core/hands';

findCard(hands, 'SA');                    // { player: 'N', suit: 'S' } or null
findDuplicatedCards(hands);                // ['SA', 'HK'] (cards appearing in multiple hands)
validateCardOwnership(hands, 'SA', 'N');   // true if North holds the Ace of Spades
handsAreDifferent(hands1, hands2);         // true if hands differ

Transforms

FunctionDescription
deepCopyHands(hands)Creates a deep clone of all four hands
removeCardFromHand(hands, card, player)Removes a specific card from a player's hand
rotateHandsClockwise(hands)Rotates all hands one position clockwise
clearHands(hands)Empties all four hands

On this page