@johnmorrisdotca/sugoroku 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +78 -68
  3. package/dist/board.d.ts +216 -12
  4. package/dist/board.js +205 -13
  5. package/dist/colours.d.ts +80 -8
  6. package/dist/colours.js +57 -7
  7. package/dist/computer.d.ts +139 -9
  8. package/dist/computer.js +118 -11
  9. package/dist/dice.d.ts +53 -4
  10. package/dist/dice.js +42 -3
  11. package/dist/draw-entry.d.ts +3 -3
  12. package/dist/draw-entry.js +2 -2
  13. package/dist/draw.d.ts +43 -3
  14. package/dist/draw.js +63 -13
  15. package/dist/element-define.d.ts +1 -1
  16. package/dist/element-define.js +4 -13
  17. package/dist/element.d.ts +116 -5
  18. package/dist/element.js +378 -102
  19. package/dist/elementFrame.d.ts +21 -0
  20. package/dist/elementFrame.js +110 -0
  21. package/dist/evaluate.d.ts +76 -4
  22. package/dist/evaluate.js +70 -5
  23. package/dist/game.d.ts +408 -23
  24. package/dist/game.js +344 -20
  25. package/dist/geometry.d.ts +50 -3
  26. package/dist/geometry.js +64 -28
  27. package/dist/index.d.ts +12 -11
  28. package/dist/index.js +10 -9
  29. package/dist/match.d.ts +66 -4
  30. package/dist/match.js +69 -6
  31. package/dist/mount.d.ts +77 -6
  32. package/dist/mount.js +105 -357
  33. package/dist/mountView.d.ts +58 -0
  34. package/dist/mountView.js +459 -0
  35. package/dist/moves.d.ts +48 -1
  36. package/dist/moves.js +35 -1
  37. package/dist/notation.d.ts +92 -4
  38. package/dist/notation.js +88 -5
  39. package/dist/play-entry.d.ts +4 -4
  40. package/dist/play-entry.js +1 -1
  41. package/dist/playStyle.d.ts +6 -0
  42. package/dist/playStyle.js +6 -0
  43. package/dist/plays.d.ts +108 -3
  44. package/dist/plays.js +96 -3
  45. package/dist/positionElement.d.ts +107 -0
  46. package/dist/positionElement.js +303 -0
  47. package/dist/positionId.d.ts +29 -1
  48. package/dist/positionId.js +33 -2
  49. package/dist/random.d.ts +33 -3
  50. package/dist/random.js +22 -2
  51. package/dist/record.d.ts +151 -8
  52. package/dist/record.js +121 -36
  53. package/dist/rules.d.ts +130 -10
  54. package/dist/rules.js +133 -14
  55. package/dist/side.d.ts +38 -3
  56. package/dist/side.js +30 -3
  57. package/dist/sound.d.ts +61 -3
  58. package/dist/sound.js +27 -2
  59. package/dist/strings.d.ts +42 -2
  60. package/dist/strings.js +37 -3
  61. package/dist/style.d.ts +6 -0
  62. package/dist/style.js +6 -0
  63. package/dist/variants.d.ts +113 -8
  64. package/dist/variants.js +81 -7
  65. package/dist/version.d.ts +9 -2
  66. package/dist/version.js +9 -2
  67. package/package.json +29 -22
@@ -0,0 +1,107 @@
1
+ import { type Position } from "./board.ts";
2
+ import { type SugorokuDrawOptions } from "./draw.ts";
3
+ import { SugorokuFrame } from "./elementFrame.ts";
4
+ import type { Side } from "./side.ts";
5
+ import { type VariantKey } from "./variants.ts";
6
+ /**
7
+ * Reads a GNU position ID or bounded JSON position; null means invalid counts or input.
8
+ * @param text - Position ID, JSON position, or null for the variant's start.
9
+ * @param variant - The native variant used for checker counts and layout.
10
+ * @param turn - The on-roll side encoded by a GNU ID.
11
+ * @returns A complete position, or null for malformed or inconsistent input.
12
+ * @example
13
+ * ```ts
14
+ * import { readEmbedPosition } from "@johnmorrisdotca/sugoroku/element";
15
+ * console.log(readEmbedPosition("4HPwATDgc/ABMA")?.points[0].length);
16
+ * ```
17
+ */
18
+ export declare function readEmbedPosition(text: string | null, variant?: VariantKey, turn?: Side): Position | null;
19
+ /** Host data and all native SVG drawing choices for a position.
20
+ * @example
21
+ * ```ts
22
+ * import type { SugorokuPositionOptions } from "@johnmorrisdotca/sugoroku/element";
23
+ * const options: SugorokuPositionOptions = {};
24
+ * console.log(Object.keys(options).length); // 0
25
+ * ```
26
+ */
27
+ export type SugorokuPositionOptions = SugorokuDrawOptions & {
28
+ position?: Position;
29
+ onChange?: (position: Position) => void;
30
+ };
31
+ /** Native draw keys, checked by the shared builder.
32
+ * @example
33
+ * ```ts
34
+ * import { SUGOROKU_DRAW_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
35
+ * console.log(typeof SUGOROKU_DRAW_OPTION_KEYS); // object
36
+ * ```
37
+ */
38
+ export declare const SUGOROKU_DRAW_OPTION_KEYS: string[];
39
+ /** All host position options, checked by the builder.
40
+ * @example
41
+ * ```ts
42
+ * import { SUGOROKU_POSITION_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
43
+ * console.log(typeof SUGOROKU_POSITION_OPTION_KEYS); // object
44
+ * ```
45
+ */
46
+ export declare const SUGOROKU_POSITION_OPTION_KEYS: string[];
47
+ /** A read-only backgammon position, drawn by the native SVG writer.
48
+ * @example
49
+ * ```ts
50
+ * import { SugorokuPosition } from "@johnmorrisdotca/sugoroku/element";
51
+ * console.log(SugorokuPosition.observedAttributes.includes("lang")); // true
52
+ * ```
53
+ */
54
+ export declare class SugorokuPosition extends SugorokuFrame {
55
+ #private;
56
+ static observedAttributes: string[];
57
+ /** Structured current position, or null after an input error. */
58
+ get value(): Position | null;
59
+ /** Native drawing options and trusted host position data. */
60
+ get options(): SugorokuPositionOptions;
61
+ set options(value: SugorokuPositionOptions);
62
+ connectedCallback(): void;
63
+ disconnectedCallback(): void;
64
+ attributeChangedCallback(): void;
65
+ /** Position ID or JSON, reflected. Assign an object to serialize it. */
66
+ get position(): string | null;
67
+ set position(value: string | Position | null);
68
+ /** The `variant` attribute, reflected. */
69
+ get variant(): string | null;
70
+ set variant(value: string | number | boolean | null | undefined);
71
+ /** The `turn` attribute, reflected. */
72
+ get turn(): string | null;
73
+ set turn(value: string | number | boolean | null | undefined);
74
+ /** The `board` attribute, reflected. */
75
+ get board(): string | null;
76
+ set board(value: string | number | boolean | null | undefined);
77
+ /** The `checkers` attribute, reflected. */
78
+ get checkers(): string | null;
79
+ set checkers(value: string | number | boolean | null | undefined);
80
+ /** The `theme` attribute, reflected. */
81
+ get theme(): string | null;
82
+ set theme(value: string | number | boolean | null | undefined);
83
+ /** The `home` attribute, reflected. */
84
+ get home(): string | null;
85
+ set home(value: string | number | boolean | null | undefined);
86
+ /** The `view` attribute, reflected. */
87
+ get view(): string | null;
88
+ set view(value: string | number | boolean | null | undefined);
89
+ /** The `numbers` attribute, reflected. */
90
+ get numbers(): string | null;
91
+ set numbers(value: string | number | boolean | null | undefined);
92
+ /** The `orientation` attribute, reflected. */
93
+ get orientation(): string | null;
94
+ set orientation(value: string | number | boolean | null | undefined);
95
+ }
96
+ /** Defines a static position, or a subclass under a host's tag. Safe on a server.
97
+ * @param tagName - The custom tag name; omitted to use the package default.
98
+ * @param registry - The custom-element registry; omitted outside a browser.
99
+ * @returns Nothing; existing definitions and server imports remain safe.
100
+ * @example
101
+ * ```ts
102
+ * import { definePosition } from "@johnmorrisdotca/sugoroku/element";
103
+ * definePosition();
104
+ * console.log(typeof definePosition); // function
105
+ * ```
106
+ */
107
+ export declare function definePosition(tagName?: string, registry?: CustomElementRegistry | undefined): void;
@@ -0,0 +1,303 @@
1
+ import { startPosition } from "./board.js";
2
+ import { SUGOROKU_BOARD_NAMES, SUGOROKU_CHECKER_SET_NAMES } from "./colours.js";
3
+ import { drawSugoroku } from "./draw.js";
4
+ import { isOn, oneOf, SugorokuFrame } from "./elementFrame.js";
5
+ import { positionFromId } from "./positionId.js";
6
+ import { isVariantKey, VARIANTS } from "./variants.js";
7
+ /**
8
+ * Reads a GNU position ID or bounded JSON position; null means invalid counts or input.
9
+ * @param text - Position ID, JSON position, or null for the variant's start.
10
+ * @param variant - The native variant used for checker counts and layout.
11
+ * @param turn - The on-roll side encoded by a GNU ID.
12
+ * @returns A complete position, or null for malformed or inconsistent input.
13
+ * @example
14
+ * ```ts
15
+ * import { readEmbedPosition } from "@johnmorrisdotca/sugoroku/element";
16
+ * console.log(readEmbedPosition("4HPwATDgc/ABMA")?.points[0].length);
17
+ * ```
18
+ */
19
+ export function readEmbedPosition(text, variant = "backgammon", turn = "white") {
20
+ if (!isVariantKey(variant) || (turn !== "white" && turn !== "black"))
21
+ return null;
22
+ const spec = VARIANTS[variant];
23
+ if (text === null)
24
+ return startPosition(spec);
25
+ if (text.length > 10000)
26
+ return null;
27
+ if (text.trim() === "")
28
+ return startPosition(spec);
29
+ if (/^[A-Za-z0-9+/]{14}$/.test(text.trim()))
30
+ return positionFromId(text.trim(), turn, spec.checkers);
31
+ try {
32
+ const data = JSON.parse(text);
33
+ const count = (value) => Number.isInteger(value) && Number(value) >= 0 && Number(value) <= spec.checkers;
34
+ const pair = (value) => Array.isArray(value) && value.length === 2 && value.every(count);
35
+ if (data === null ||
36
+ typeof data !== "object" ||
37
+ !Array.isArray(data.points) ||
38
+ data.points.length !== 2 ||
39
+ !data.points.every((row) => Array.isArray(row) && row.length === 24 && row.every(count)) ||
40
+ !pair(data.bar) ||
41
+ !pair(data.off) ||
42
+ !pair(data.reserve))
43
+ return null;
44
+ for (const side of [0, 1])
45
+ if (data.points[side].reduce((sum, n) => sum + n, 0) + data.bar[side] + data.off[side] + data.reserve[side] !==
46
+ spec.checkers)
47
+ return null;
48
+ return {
49
+ points: [[...data.points[0]], [...data.points[1]]],
50
+ bar: [data.bar[0], data.bar[1]],
51
+ off: [data.off[0], data.off[1]],
52
+ reserve: [data.reserve[0], data.reserve[1]],
53
+ };
54
+ }
55
+ catch {
56
+ return null;
57
+ }
58
+ }
59
+ /** Native draw keys, checked by the shared builder.
60
+ * @example
61
+ * ```ts
62
+ * import { SUGOROKU_DRAW_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
63
+ * console.log(typeof SUGOROKU_DRAW_OPTION_KEYS); // object
64
+ * ```
65
+ */
66
+ export const SUGOROKU_DRAW_OPTION_KEYS = Object.keys({
67
+ variant: true,
68
+ view: true,
69
+ home: true,
70
+ orientation: true,
71
+ numbers: true,
72
+ board: true,
73
+ checkers: true,
74
+ colours: true,
75
+ theme: true,
76
+ dice: true,
77
+ cube: true,
78
+ highlight: true,
79
+ label: true,
80
+ style: true,
81
+ });
82
+ /** All host position options, checked by the builder.
83
+ * @example
84
+ * ```ts
85
+ * import { SUGOROKU_POSITION_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
86
+ * console.log(typeof SUGOROKU_POSITION_OPTION_KEYS); // object
87
+ * ```
88
+ */
89
+ export const SUGOROKU_POSITION_OPTION_KEYS = Object.keys({
90
+ position: true,
91
+ onChange: true,
92
+ variant: true,
93
+ view: true,
94
+ home: true,
95
+ orientation: true,
96
+ numbers: true,
97
+ board: true,
98
+ checkers: true,
99
+ colours: true,
100
+ theme: true,
101
+ dice: true,
102
+ cube: true,
103
+ highlight: true,
104
+ label: true,
105
+ style: true,
106
+ });
107
+ /** A read-only backgammon position, drawn by the native SVG writer.
108
+ * @example
109
+ * ```ts
110
+ * import { SugorokuPosition } from "@johnmorrisdotca/sugoroku/element";
111
+ * console.log(SugorokuPosition.observedAttributes.includes("lang")); // true
112
+ * ```
113
+ */
114
+ export class SugorokuPosition extends SugorokuFrame {
115
+ static observedAttributes = [
116
+ "position",
117
+ "variant",
118
+ "turn",
119
+ "board",
120
+ "checkers",
121
+ "theme",
122
+ "home",
123
+ "view",
124
+ "numbers",
125
+ "orientation",
126
+ "lang",
127
+ "size",
128
+ "width",
129
+ "label",
130
+ ];
131
+ #value = null;
132
+ #options = {};
133
+ #resize = null;
134
+ /** Structured current position, or null after an input error. */
135
+ get value() {
136
+ return this.#value;
137
+ }
138
+ /** Native drawing options and trusted host position data. */
139
+ get options() {
140
+ return this.#options;
141
+ }
142
+ set options(value) {
143
+ this.#options = value ?? {};
144
+ if (this.isConnected)
145
+ this.#render();
146
+ }
147
+ connectedCallback() {
148
+ this.#render();
149
+ const Resize = this.ownerDocument.defaultView?.ResizeObserver;
150
+ if (Resize !== undefined) {
151
+ this.#resize = new Resize(() => {
152
+ if (this.isConnected)
153
+ this.#render();
154
+ });
155
+ this.#resize.observe(this);
156
+ }
157
+ }
158
+ disconnectedCallback() {
159
+ this.#resize?.disconnect();
160
+ this.#resize = null;
161
+ }
162
+ attributeChangedCallback() {
163
+ if (this.isConnected)
164
+ this.#render();
165
+ }
166
+ /** Position ID or JSON, reflected. Assign an object to serialize it. */
167
+ get position() {
168
+ return this.getAttribute("position");
169
+ }
170
+ set position(value) {
171
+ this.write("position", value !== null && typeof value === "object" ? JSON.stringify(value) : value);
172
+ }
173
+ /** The `variant` attribute, reflected. */
174
+ get variant() {
175
+ return this.getAttribute("variant");
176
+ }
177
+ set variant(value) {
178
+ this.write("variant", value);
179
+ }
180
+ /** The `turn` attribute, reflected. */
181
+ get turn() {
182
+ return this.getAttribute("turn");
183
+ }
184
+ set turn(value) {
185
+ this.write("turn", value);
186
+ }
187
+ /** The `board` attribute, reflected. */
188
+ get board() {
189
+ return this.getAttribute("board");
190
+ }
191
+ set board(value) {
192
+ this.write("board", value);
193
+ }
194
+ /** The `checkers` attribute, reflected. */
195
+ get checkers() {
196
+ return this.getAttribute("checkers");
197
+ }
198
+ set checkers(value) {
199
+ this.write("checkers", value);
200
+ }
201
+ /** The `theme` attribute, reflected. */
202
+ get theme() {
203
+ return this.getAttribute("theme");
204
+ }
205
+ set theme(value) {
206
+ this.write("theme", value);
207
+ }
208
+ /** The `home` attribute, reflected. */
209
+ get home() {
210
+ return this.getAttribute("home");
211
+ }
212
+ set home(value) {
213
+ this.write("home", value);
214
+ }
215
+ /** The `view` attribute, reflected. */
216
+ get view() {
217
+ return this.getAttribute("view");
218
+ }
219
+ set view(value) {
220
+ this.write("view", value);
221
+ }
222
+ /** The `numbers` attribute, reflected. */
223
+ get numbers() {
224
+ return this.getAttribute("numbers");
225
+ }
226
+ set numbers(value) {
227
+ this.write("numbers", value);
228
+ }
229
+ /** The `orientation` attribute, reflected. */
230
+ get orientation() {
231
+ return this.getAttribute("orientation");
232
+ }
233
+ set orientation(value) {
234
+ this.write("orientation", value);
235
+ }
236
+ #render() {
237
+ this.frame();
238
+ try {
239
+ const variant = this.getAttribute("variant") ??
240
+ (typeof this.#options.variant === "string" ? this.#options.variant : "backgammon");
241
+ if (!isVariantKey(variant))
242
+ throw new RangeError("Choose a known backgammon variant.");
243
+ const turn = this.getAttribute("turn") ?? "white";
244
+ if (turn !== "white" && turn !== "black")
245
+ throw new RangeError("Choose white or black on roll.");
246
+ const position = this.hasAttribute("position")
247
+ ? readEmbedPosition(this.getAttribute("position"), variant, turn)
248
+ : (this.#options.position ?? startPosition(VARIANTS[variant]));
249
+ if (position === null)
250
+ throw new RangeError("Use a valid GNU position ID or complete JSON position.");
251
+ this.#value = position;
252
+ const orientation = this.getAttribute("orientation") ?? this.#options.orientation;
253
+ const draw = {
254
+ ...this.#options,
255
+ variant: this.hasAttribute("variant") ? variant : (this.#options.variant ?? variant),
256
+ theme: oneOf(this.getAttribute("theme"), ["auto", "light", "dark"]) ?? this.#options.theme,
257
+ board: oneOf(this.getAttribute("board"), SUGOROKU_BOARD_NAMES) ?? this.#options.board,
258
+ checkers: oneOf(this.getAttribute("checkers"), SUGOROKU_CHECKER_SET_NAMES) ?? this.#options.checkers,
259
+ home: oneOf(this.getAttribute("home"), ["left", "right"]) ?? this.#options.home,
260
+ view: oneOf(this.getAttribute("view"), ["white", "black"]) ?? this.#options.view,
261
+ numbers: isOn(this.getAttribute("numbers"), this.#options.numbers ?? false),
262
+ orientation: orientation === "portrait" || (orientation !== "landscape" && (this.clientWidth || 600) < 560)
263
+ ? "portrait"
264
+ : "landscape",
265
+ label: this.getAttribute("label") ??
266
+ this.#options.label ??
267
+ ((this.getAttribute("lang") ?? this.ownerDocument.documentElement.lang) === "ja" ? "双六の局面" : undefined),
268
+ style: this.#options.style ?? true,
269
+ };
270
+ const svg = new DOMParser().parseFromString(drawSugoroku(position, draw), "image/svg+xml").documentElement;
271
+ this.replaceChildren(this.ownerDocument.importNode(svg, true));
272
+ this.#options.onChange?.(position);
273
+ const detail = JSON.parse(JSON.stringify({ variant, position }));
274
+ this.dispatchEvent(new CustomEvent("sugoroku-change", { bubbles: true, composed: true, detail }));
275
+ this.dispatchEvent(new CustomEvent("sugoroku-load", { bubbles: true, composed: true, detail }));
276
+ }
277
+ catch (error) {
278
+ this.#value = null;
279
+ const message = error instanceof Error ? error.message : String(error);
280
+ const status = this.ownerDocument.createElement("p");
281
+ status.setAttribute("role", "status");
282
+ status.textContent = message;
283
+ this.replaceChildren(status);
284
+ this.dispatchEvent(new CustomEvent("sugoroku-error", { bubbles: true, composed: true, detail: { message } }));
285
+ }
286
+ }
287
+ }
288
+ /** Defines a static position, or a subclass under a host's tag. Safe on a server.
289
+ * @param tagName - The custom tag name; omitted to use the package default.
290
+ * @param registry - The custom-element registry; omitted outside a browser.
291
+ * @returns Nothing; existing definitions and server imports remain safe.
292
+ * @example
293
+ * ```ts
294
+ * import { definePosition } from "@johnmorrisdotca/sugoroku/element";
295
+ * definePosition();
296
+ * console.log(typeof definePosition); // function
297
+ * ```
298
+ */
299
+ export function definePosition(tagName = "sugoroku-position", registry = typeof customElements === "undefined" ? undefined : customElements) {
300
+ if (registry !== undefined && registry.get(tagName) === undefined)
301
+ registry.define(tagName, tagName === "sugoroku-position" ? SugorokuPosition : class extends SugorokuPosition {
302
+ });
303
+ }
@@ -1,11 +1,39 @@
1
1
  import { type Position } from "./board.ts";
2
2
  import { type Side } from "./side.ts";
3
- /** The ID of a position with `onRoll` to move, or null if it cannot be written in this form. */
3
+ /** The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
4
+ *
5
+ * @param position - The native checker counts, bar, reserve and borne-off counts.
6
+ * @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
7
+ * @returns The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
8
+ * @example
9
+ * ```ts
10
+ * import { positionId, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
11
+ *
12
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
13
+ * const spec = settings.variant;
14
+ * const position = startPosition(spec);
15
+ * console.log(positionId(position,"white"));
16
+ * ```
17
+ */
4
18
  export declare function positionId(position: Position, onRoll: Side): string | null;
5
19
  /**
6
20
  * The position an ID stands for, with `onRoll` to move, or null if the text is
7
21
  * not an ID. The ID does not say how many checkers each side began with: pass
8
22
  * the variant's (15 for the standard board), and what is not on the board is
9
23
  * counted as borne off.
24
+ *
25
+ * @param id - A GNU-compatible position ID.
26
+ * @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
27
+ * @param checkers - The checker count expected for each side.
28
+ * @returns The position an ID stands for, with `onRoll` to move, or null if the text is not an ID. The ID does not say how many checkers each side began with: pass the variant's (15 for the standard board), and what is not on the board is counted as borne off.
29
+ * @example
30
+ * ```ts
31
+ * import { positionFromId, positionId, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
32
+ *
33
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
34
+ * const spec = settings.variant;
35
+ * const position = startPosition(spec);
36
+ * console.log(positionFromId(positionId(position,"white")!,"white")?.bar);
37
+ * ```
10
38
  */
11
39
  export declare function positionFromId(id: string, onRoll: Side, checkers?: number): Position | null;
@@ -19,7 +19,21 @@ import { otherSide, sideIndex } from "./side.js";
19
19
  * variants whose sides share one track: those give null.
20
20
  */
21
21
  const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
22
- /** The ID of a position with `onRoll` to move, or null if it cannot be written in this form. */
22
+ /** The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
23
+ *
24
+ * @param position - The native checker counts, bar, reserve and borne-off counts.
25
+ * @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
26
+ * @returns The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
27
+ * @example
28
+ * ```ts
29
+ * import { positionId, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
30
+ *
31
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
32
+ * const spec = settings.variant;
33
+ * const position = startPosition(spec);
34
+ * console.log(positionId(position,"white"));
35
+ * ```
36
+ */
23
37
  export function positionId(position, onRoll) {
24
38
  if (position.reserve[0] > 0 || position.reserve[1] > 0)
25
39
  return null;
@@ -64,6 +78,20 @@ export function positionId(position, onRoll) {
64
78
  * not an ID. The ID does not say how many checkers each side began with: pass
65
79
  * the variant's (15 for the standard board), and what is not on the board is
66
80
  * counted as borne off.
81
+ *
82
+ * @param id - A GNU-compatible position ID.
83
+ * @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
84
+ * @param checkers - The checker count expected for each side.
85
+ * @returns The position an ID stands for, with `onRoll` to move, or null if the text is not an ID. The ID does not say how many checkers each side began with: pass the variant's (15 for the standard board), and what is not on the board is counted as borne off.
86
+ * @example
87
+ * ```ts
88
+ * import { positionFromId, positionId, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
89
+ *
90
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
91
+ * const spec = settings.variant;
92
+ * const position = startPosition(spec);
93
+ * console.log(positionFromId(positionId(position,"white")!,"white")?.bar);
94
+ * ```
67
95
  */
68
96
  export function positionFromId(id, onRoll, checkers = 15) {
69
97
  if (!/^[A-Za-z0-9+/]{14}$/.test(id))
@@ -71,7 +99,10 @@ export function positionFromId(id, onRoll, checkers = 15) {
71
99
  const sextets = [...id].map((char) => ALPHABET.indexOf(char));
72
100
  const bytes = [];
73
101
  for (let g = 0; g < 14; g += 4) {
74
- const group = (sextets[g] << 18) | (sextets[g + 1] << 12) | ((sextets[g + 2] ?? 0) << 6) | (sextets[g + 3] ?? 0);
102
+ const group = (sextets[g] << 18) |
103
+ (sextets[g + 1] << 12) |
104
+ ((sextets[g + 2] ?? 0) << 6) |
105
+ (sextets[g + 3] ?? 0);
75
106
  bytes.push((group >> 16) & 255);
76
107
  if (g + 2 < 14)
77
108
  bytes.push((group >> 8) & 255);
package/dist/random.d.ts CHANGED
@@ -1,6 +1,36 @@
1
- /** A source of random numbers from 0 up to but not including 1. */
1
+ /** A source of random numbers from 0 up to but not including 1.
2
+ *
3
+ * @example
4
+ * ```ts
5
+ * import type { Random } from "@johnmorrisdotca/sugoroku";
6
+ * import { seededRandom } from "@johnmorrisdotca/sugoroku";
7
+ *
8
+ * const value: Random = seededRandom(42);
9
+ * console.log(value);
10
+ * ```
11
+ */
2
12
  export type Random = () => number;
3
- /** A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node. */
13
+ /** A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
14
+ *
15
+ * @param seed - A reproducible numeric or text seed.
16
+ * @returns A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
17
+ * @example
18
+ * ```ts
19
+ * import { hashSeed } from "@johnmorrisdotca/sugoroku";
20
+ *
21
+ * console.log(hashSeed("sample"));
22
+ * ```
23
+ */
4
24
  export declare function hashSeed(seed: number | string): number;
5
- /** The mulberry32 stream: the same seed makes the same numbers everywhere. */
25
+ /** The mulberry32 stream: the same seed makes the same numbers everywhere.
26
+ *
27
+ * @param seed - A reproducible numeric or text seed.
28
+ * @returns The mulberry32 stream: the same seed makes the same numbers everywhere.
29
+ * @example
30
+ * ```ts
31
+ * import { seededRandom } from "@johnmorrisdotca/sugoroku";
32
+ *
33
+ * console.log(seededRandom(42)());
34
+ * ```
35
+ */
6
36
  export declare function seededRandom(seed: number | string): Random;
package/dist/random.js CHANGED
@@ -1,4 +1,14 @@
1
- /** A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node. */
1
+ /** A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
2
+ *
3
+ * @param seed - A reproducible numeric or text seed.
4
+ * @returns A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
5
+ * @example
6
+ * ```ts
7
+ * import { hashSeed } from "@johnmorrisdotca/sugoroku";
8
+ *
9
+ * console.log(hashSeed("sample"));
10
+ * ```
11
+ */
2
12
  export function hashSeed(seed) {
3
13
  if (typeof seed === "number")
4
14
  return seed >>> 0;
@@ -9,7 +19,17 @@ export function hashSeed(seed) {
9
19
  }
10
20
  return hash >>> 0;
11
21
  }
12
- /** The mulberry32 stream: the same seed makes the same numbers everywhere. */
22
+ /** The mulberry32 stream: the same seed makes the same numbers everywhere.
23
+ *
24
+ * @param seed - A reproducible numeric or text seed.
25
+ * @returns The mulberry32 stream: the same seed makes the same numbers everywhere.
26
+ * @example
27
+ * ```ts
28
+ * import { seededRandom } from "@johnmorrisdotca/sugoroku";
29
+ *
30
+ * console.log(seededRandom(42)());
31
+ * ```
32
+ */
13
33
  export function seededRandom(seed) {
14
34
  let state = hashSeed(seed);
15
35
  return () => {