Bridge Training
Bridge Core

Utils

HCP computation, PBN conversion, seeded random, and vulnerability helpers.

Import from @workspace/bridge-core/utils.

HCP Computation

HCP Values

import { HCP_VALUES } from '@workspace/bridge-core/utils';

HCP_VALUES; // { A: 4, K: 3, Q: 2, J: 1 }

computeHandHCP

Computes the High Card Points for a single hand.

import { computeHandHCP } from '@workspace/bridge-core/utils';

const hcp = computeHandHCP({
  S: ['SA', 'SK'],
  H: ['HQ'],
  D: ['DJ', 'DT'],
  C: ['C9'],
});
// 4 + 3 + 2 + 1 = 10

computeCardCounts

Computes HCP and suit distribution for all four hands.

import { computeCardCounts } from '@workspace/bridge-core/utils';

const counts = computeCardCounts(hands);
counts.N.points;          // HCP for North
counts.N.colorsCount.S;   // Number of spades in North's hand

Prop

Type

Prop

Type

PBN Conversion

handToPBN

Converts a single hand to PBN notation.

import { handToPBN } from '@workspace/bridge-core/utils';

handToPBN({ S: 'AKQ', H: 'JT9', D: '876', C: '543' });
// 'AKQ.JT9.876.543'

handToPBN({ S: '', H: 'AK', D: '', C: 'QJ' });
// '-.AK.-.QJ'

handsToPBN

Converts all four hands to PBN deal notation.

import { handsToPBN } from '@workspace/bridge-core/utils';

const pbn = handsToPBN(hands, 'N');
// 'N:AKQ.JT9.876.543 T98.765.432.AKQ ...'

The format is StartPlayer:hand1 hand2 hand3 hand4 where hands follow clockwise order from the start player. Each hand uses dot-separated suits (Spades.Hearts.Diamonds.Clubs).

Seeded Random

Deterministic pseudo-random number generation using the Linear Congruential Generator (LCG) algorithm. Useful for reproducible card distributions.

createSeededRandom

import { createSeededRandom } from '@workspace/bridge-core/utils';

const random = createSeededRandom(42);
random.next(); // 0.000007826369... (deterministic)
random.next(); // 0.131537788...

randomInt

import { randomInt } from '@workspace/bridge-core/utils';

const random = createSeededRandom(42);
randomInt(random, 10); // Random integer in [0, 10)

shuffleArray

Shuffles an array deterministically using the Fisher-Yates algorithm. Returns a new array.

import { shuffleArray, createSeededRandom } from '@workspace/bridge-core/utils';

const random = createSeededRandom(42);
const shuffled = shuffleArray([1, 2, 3, 4, 5], random);
// Deterministic order based on seed

SeededRandom Type

type SeededRandom = {
  next(): number; // Returns a number between 0 and 1
};

Vulnerability Helpers

getPlayerVulnerabilityStatus

Determines a player's vulnerability status based on the deal vulnerability setting.

import { getPlayerVulnerabilityStatus } from '@workspace/bridge-core/utils';

getPlayerVulnerabilityStatus('N', 'NS');   // 'vulnerable'
getPlayerVulnerabilityStatus('E', 'NS');   // 'non-vulnerable'
getPlayerVulnerabilityStatus('N', 'ALL');  // 'vulnerable'
getPlayerVulnerabilityStatus('N', 'NONE'); // 'non-vulnerable'
getPlayerVulnerabilityStatus('N', 'UNSET'); // 'unset'

isVulnerabilitySet

Checks whether a vulnerability value has been explicitly set (i.e., is not UNSET).

import { isVulnerabilitySet } from '@workspace/bridge-core/utils';

isVulnerabilitySet('NS');    // true
isVulnerabilitySet('NONE');  // true
isVulnerabilitySet('UNSET'); // false

VulnerabilityStatus Type

type VulnerabilityStatus = 'vulnerable' | 'non-vulnerable' | 'unset';

On this page