Skip to content

JavaScript / TypeScript Reference ​

Auto-generated from js/src/index.d.ts, worker.d.ts, and types.d.ts.

sh
npm install dealbreaker

Module Exports (dealbreaker) ​

init(moduleOrPath?: InitInput): Promise<void> ​

Initialize the WebAssembly module. Safe to call multiple times.

In Node.js, the module is initialized automatically on import, so calling init() is optional. In browsers and bundlers, await init() must resolve before calling presets() or constructing a Solver.

presets(): PresetInfo[] ​

List all built-in game presets.

formatStep(step: Step | string): string ​

Format a single step as human-readable text (e.g. "tableau 4 -> cell 1").

class Solver ​

One game's compiled rules. Create once, reuse across many positions.

MethodDescription
constructor(rules: Rules | string)Compile a game from a Rules object or rules JSON string.
static fromPreset(id: string): SolverCompile a built-in preset by id (see presets()).
static fromPresetWith(id: string, patch: Record<string, unknown> | string): SolverCompile a built-in preset with an RFC 7396 JSON Merge Patch applied to its rules.
rules(): RulesThe game's compiled rules.
deal(seed: number): StateDataDeal a position from a seed (0 to 2^53).
dealMicrosoft(n: number): StateDataDeal Microsoft FreeCell deal n (1 to 2^31 - 1).
dealOrdered(cards: readonly string[] | string): StateDataDeal an explicit card order (cards[0] dealt first), e.g. ["AS", "2S", ...].
validate(state: StateData | string): voidValidate a position against this game's pack and pile capacities. Throws if invalid.
solve(state: StateData | string, options?: SolveOptions | string): SolveResultSolve a position.
best(state: StateData | string, options?: SolveOptions | string): MaxResultFor games played for points: find the line of play that ends with the most points.
score(state: StateData | string): number | nullPoints for a position, or null for games not played for points.
hint(state: StateData | string, options?: HintOptions | string): Hint | nullSuggest a next step without peeking at face-down cards, or null if no win was found.
hints(state: StateData | string, options?: HintOptions | string): HintReportSuggest up to max_candidates ranked next steps without peeking at face-down cards.
grade(state: StateData | string, options?: GradeOptions | string): DealGradeGrade a single deal's solvability and difficulty.
findDeal(options?: FindDealOptions | string): FoundDeal | nullSearch sequentially for a solvable deal (optionally matching a target difficulty).
safeSteps(state: StateData | string): Step[]Greedily collect provably safe forced moves without searching.
autocomplete(state: StateData | string): AutoCompleteDetect whether state can be auto-completed trivially and return the steps to play.
describeStep(state: StateData | string, step: Step | string): DescribeStepResultPlay one step and return rich details (cards moved, destination card, flipped cards) + next state.
describeSteps(state: StateData | string, steps: readonly Step[] | string): StepDetails[]Replay a list of steps and return rich player-visible details for each step.
lastWinnable( state: StateData | string, history: readonly Step[] | string, options?: SolveOptions | string, ): LastWinnableFind the most recent winnable position along history played from state.
legalSteps(state: StateData | string): Step[]Every legal single step in this position.
playStep(state: StateData | string, step: Step | string): StateDataPlay one step and return the resulting position. Throws if the step is illegal.
isWon(state: StateData | string): booleanWhether the position is won.
render(state: StateData | string): stringRender a position as a human-readable ASCII board.
free(): voidFree the underlying WebAssembly instance.

class WorkerSolver (dealbreaker/worker) ​

Runs a Dealbreaker Solver inside a dedicated Web Worker so searches never block the UI thread. Passing an AbortSignal (or timeout_ms) cancels an in-flight search and transparently respawns the worker on the next call. Call terminate() to permanently stop the worker.

MethodDescription
static fromPreset(id: string, wasmUrl?: URL | string): Promise<WorkerSolver>
static fromPresetWith( id: string, patch: Record<string, unknown> | string, wasmUrl?: URL | string, ): Promise<WorkerSolver>
static fromRules(rules: Rules | string, wasmUrl?: URL | string): Promise<WorkerSolver>
rules(): Promise<Rules>
deal(seed: number): Promise<StateData>
dealMicrosoft(n: number): Promise<StateData>
dealOrdered(cards: readonly string[] | string): Promise<StateData>
validate(state: StateData | string): Promise<void>
solve( state: StateData | string, options?: SolveOptions | string, callOptions?: WorkerCallOptions, ): Promise<SolveResult>
best( state: StateData | string, options?: SolveOptions | string, callOptions?: WorkerCallOptions, ): Promise<MaxResult>
score(state: StateData | string): Promise<number | null>
hint( state: StateData | string, options?: HintOptions | string, callOptions?: WorkerCallOptions, ): Promise<Hint | null>
hints( state: StateData | string, options?: HintOptions | string, callOptions?: WorkerCallOptions, ): Promise<HintReport>
grade( state: StateData | string, options?: GradeOptions | string, callOptions?: WorkerCallOptions, ): Promise<DealGrade>
findDeal( options?: FindDealOptions | string, callOptions?: WorkerCallOptions, ): Promise<FoundDeal | null>
safeSteps(state: StateData | string): Promise<Step[]>
autocomplete(state: StateData | string): Promise<AutoComplete>
describeStep(state: StateData | string, step: Step | string): Promise<DescribeStepResult>
describeSteps(state: StateData | string, steps: readonly Step[] | string): Promise<StepDetails[]>
lastWinnable( state: StateData | string, history: readonly Step[] | string, options?: SolveOptions | string, callOptions?: WorkerCallOptions, ): Promise<LastWinnable>
legalSteps(state: StateData | string): Promise<Step[]>
playStep(state: StateData | string, step: Step | string): Promise<StateData>
isWon(state: StateData | string): Promise<boolean>
render(state: StateData | string): Promise<string>
terminate(): voidTerminate the worker immediately (also cancels any in-flight search).

Core Data & Result Types ​

For the full Rules type tree, see the Rules Schema Reference.

ts
/**
 * A position keyed by group name. Cards are bottom-first, e.g. `"AS"`, `"10H"`, `"~KC"` (face down),
 * or `"JK"` / `"~JK"` (joker). In `hint()`, unknown face-down cards may be written as `"?"` or `"~?"`.
 */
export interface StateData {
  piles: Record<string, string[][]>;
  redeals_used?: number;
  base_rank?: number;
  seed?: number;
}

export interface PileRef {
  group: string;
  index: number;
}

export type Step =
  | { type: "deal" }
  | { type: "redeal" }
  | { type: "move"; from: PileRef; to: PileRef; count: number }
  | { type: "remove"; piles: PileRef[] };

export type Strategy = "auto" | "depth_first" | "best_first";

export interface SolveOptions {
  strategy?: Strategy;
  max_states?: number;
  /** Wall-clock timeout in milliseconds (enforced on native targets). */
  timeout_ms?: number;
  autoplay?: boolean;
  shorten?: boolean;
}

export type OutcomeLabel = "solved" | "unsolvable" | "gave-up";

export interface SolveResult {
  outcome: OutcomeLabel;
  states: number;
  steps?: Step[];
}

export interface MaxResult {
  score: number;
  proven: boolean;
  states: number;
  steps: Step[];
}

export interface HintOptions {
  samples?: number;
  solve?: SolveOptions;
  seed?: number;
  /** Maximum ranked candidates returned by `hints()` (default 3). */
  max_candidates?: number;
}

export interface Hint {
  step: Step;
  wins: number;
  samples: number;
  score?: number;
}

export interface HintReport {
  candidates: Hint[];
  samples: number;
  solved_samples: number;
  unsolvable_samples: number;
  gave_up_samples: number;
}

export type Difficulty = "easy" | "medium" | "hard";

export interface DifficultyCutoffs {
  easy_max: number;
  medium_max: number;
}

export interface GradeOptions {
  solve?: SolveOptions;
  playouts?: number;
  seed?: number;
  cutoffs?: DifficultyCutoffs;
}

export type DealGrade =
  | {
      status: "solvable";
      difficulty: Difficulty;
      score: number;
      playout_win_rate?: number;
      playout_wins: number;
      playouts: number;
      states: number;
      steps: number;
    }
  | { status: "unsolvable"; states: number }
  | { status: "unknown"; states: number };

export interface FindDealOptions {
  start_seed?: number;
  max_attempts?: number;
  difficulty?: Difficulty;
  grade?: GradeOptions;
  compute_grade?: boolean;
  include_solution?: boolean;
}

export interface FoundDeal {
  seed: number;
  state: StateData;
  attempts: number;
  grade?: DealGrade;
  steps?: Step[];
}

export interface AutoComplete {
  completes_game: boolean;
  steps: Step[];
}

export interface FlippedCard {
  pile: PileRef;
  card: string;
}

export interface StepDetails {
  step: Step;
  cards?: string[];
  onto?: string;
  flipped?: FlippedCard[];
  text: string;
}

export interface DescribeStepResult {
  details: StepDetails;
  state: StateData;
}

export interface LastWinnable {
  index?: number;
  undo_steps?: number;
  states: number;
}

export interface WorkerCallOptions {
  signal?: AbortSignal;
  timeoutMs?: number;
}

export interface PresetInfo {
  id: string;
  name: string;
}

export type InitInput =
  | RequestInfo
  | URL
  | Response
  | BufferSource
  | WebAssembly.Module;

Dual-licensed under MIT or Apache-2.0.