@johnmorrisdotca/sugoroku 1.0.2 → 1.1.0

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 (57) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +78 -68
  3. package/dist/board.d.ts +216 -12
  4. package/dist/board.js +204 -12
  5. package/dist/colours.d.ts +80 -8
  6. package/dist/colours.js +51 -6
  7. package/dist/computer.d.ts +138 -8
  8. package/dist/computer.js +109 -7
  9. package/dist/dice.d.ts +53 -4
  10. package/dist/dice.js +42 -3
  11. package/dist/draw.d.ts +43 -3
  12. package/dist/draw.js +33 -2
  13. package/dist/element-define.d.ts +1 -1
  14. package/dist/element-define.js +4 -13
  15. package/dist/element.d.ts +233 -3
  16. package/dist/element.js +402 -48
  17. package/dist/evaluate.d.ts +76 -4
  18. package/dist/evaluate.js +65 -3
  19. package/dist/game.d.ts +408 -23
  20. package/dist/game.js +328 -18
  21. package/dist/geometry.d.ts +50 -3
  22. package/dist/geometry.js +20 -1
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.js +1 -0
  25. package/dist/match.d.ts +66 -4
  26. package/dist/match.js +55 -4
  27. package/dist/mount.d.ts +75 -4
  28. package/dist/mount.js +25 -3
  29. package/dist/moves.d.ts +48 -1
  30. package/dist/moves.js +35 -1
  31. package/dist/notation.d.ts +92 -4
  32. package/dist/notation.js +82 -3
  33. package/dist/playStyle.d.ts +6 -0
  34. package/dist/playStyle.js +6 -0
  35. package/dist/plays.d.ts +108 -3
  36. package/dist/plays.js +96 -3
  37. package/dist/positionId.d.ts +29 -1
  38. package/dist/positionId.js +29 -1
  39. package/dist/random.d.ts +33 -3
  40. package/dist/random.js +22 -2
  41. package/dist/record.d.ts +150 -7
  42. package/dist/record.js +94 -5
  43. package/dist/rules.d.ts +130 -10
  44. package/dist/rules.js +99 -9
  45. package/dist/side.d.ts +38 -3
  46. package/dist/side.js +30 -3
  47. package/dist/sound.d.ts +61 -3
  48. package/dist/sound.js +27 -2
  49. package/dist/strings.d.ts +42 -2
  50. package/dist/strings.js +34 -2
  51. package/dist/style.d.ts +6 -0
  52. package/dist/style.js +6 -0
  53. package/dist/variants.d.ts +113 -8
  54. package/dist/variants.js +62 -5
  55. package/dist/version.d.ts +9 -2
  56. package/dist/version.js +9 -2
  57. package/package.json +25 -18
package/dist/element.js CHANGED
@@ -9,10 +9,15 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
11
  };
12
- var _SugorokuBoard_instances, _SugorokuBoard_mount, _SugorokuBoard_key, _SugorokuBoard_queued, _SugorokuBoard_side, _SugorokuBoard_look, _SugorokuBoard_refresh;
12
+ var _SugorokuFrame_widthBefore, _SugorokuFrame_widthOwned, _SugorokuFrame_labelBefore, _SugorokuBoard_instances, _SugorokuBoard_mount, _SugorokuBoard_key, _SugorokuBoard_queued, _SugorokuBoard_options, _SugorokuBoard_side, _SugorokuBoard_look, _SugorokuBoard_refresh, _SugorokuPosition_instances, _SugorokuPosition_value, _SugorokuPosition_options, _SugorokuPosition_resize, _SugorokuPosition_render;
13
+ import { drawSugoroku } from "./draw.js";
14
+ import { startPosition } from "./board.js";
15
+ import { positionFromId } from "./positionId.js";
16
+ import { VARIANTS } from "./variants.js";
13
17
  import { STRENGTHS } from "./computer.js";
14
18
  import { SUGOROKU_BOARD_NAMES, SUGOROKU_CHECKER_SET_NAMES } from "./colours.js";
15
19
  import { mountSugoroku } from "./mount.js";
20
+ import { SUGOROKU_STYLE } from "./style.js";
16
21
  import { isVariantKey } from "./variants.js";
17
22
  /**
18
23
  * THE `<sugoroku-board>` ELEMENT: a playable backgammon board in a tag, with no
@@ -44,18 +49,101 @@ import { isVariantKey } from "./variants.js";
44
49
  */
45
50
  const ElementBase = typeof HTMLElement === "undefined" ? class {
46
51
  } : HTMLElement;
47
- const isOn = (value) => value !== null && !["false", "off", "0", "no"].includes(value.toLowerCase());
52
+ const isOn = (value, fallback = false) => value === null ? fallback : !["false", "off", "0", "no"].includes(value.trim().toLowerCase());
48
53
  const oneOf = (value, allowed) => (allowed.includes(value) ? value : undefined);
49
54
  /** The attributes that name the game: changing one starts a new match. */
50
55
  const GAME_ATTRIBUTES = ["variant", "preset", "points", "cube", "crawford", "jacoby", "beaver", "gammons", "seed", "dice", "controls", "drag", "auto-end", "sound", "computer-delay"];
51
- export class SugorokuBoard extends ElementBase {
56
+ class SugorokuFrame extends ElementBase {
57
+ constructor() {
58
+ super(...arguments);
59
+ _SugorokuFrame_widthBefore.set(this, null);
60
+ _SugorokuFrame_widthOwned.set(this, false);
61
+ _SugorokuFrame_labelBefore.set(this, null);
62
+ }
63
+ frame() {
64
+ this.style.display = "block";
65
+ this.style.maxWidth = "100%";
66
+ const width = Number(this.getAttribute("width")), size = this.getAttribute("size");
67
+ const pixels = this.hasAttribute("width") && width > 0 && Number.isFinite(width) ? Math.min(width, 2000) : size === "small" ? 320 : size === "medium" ? 480 : size === "large" ? 640 : null;
68
+ if (pixels !== null) {
69
+ if (!__classPrivateFieldGet(this, _SugorokuFrame_widthOwned, "f"))
70
+ __classPrivateFieldSet(this, _SugorokuFrame_widthBefore, this.style.width, "f");
71
+ __classPrivateFieldSet(this, _SugorokuFrame_widthOwned, true, "f");
72
+ this.style.width = `${pixels}px`;
73
+ }
74
+ else if (__classPrivateFieldGet(this, _SugorokuFrame_widthOwned, "f")) {
75
+ this.style.width = __classPrivateFieldGet(this, _SugorokuFrame_widthBefore, "f") ?? "";
76
+ __classPrivateFieldSet(this, _SugorokuFrame_widthOwned, false, "f");
77
+ }
78
+ const theme = this.getAttribute("theme");
79
+ this.style.colorScheme = theme === "light" || theme === "dark" ? theme : "light dark";
80
+ this.setAttribute("data-sugoroku-embed", "");
81
+ const root = this.getRootNode(), target = typeof ShadowRoot !== "undefined" && root instanceof ShadowRoot ? root : this.ownerDocument.head;
82
+ if (target.querySelector("style[data-sugoroku-embed-style]") === null) {
83
+ const style = this.ownerDocument.createElement("style");
84
+ style.setAttribute("data-sugoroku-embed-style", "");
85
+ const palette = SUGOROKU_STYLE.match(/\.sugoroku\[data-theme="dark"\] \{([^}]+)\}/)?.[1] ?? "";
86
+ style.textContent = `@media(prefers-color-scheme:dark){[data-sugoroku-embed] .sugoroku:not([data-theme="light"]){${palette}}}`;
87
+ target.append(style);
88
+ }
89
+ if (this.hasAttribute("label")) {
90
+ __classPrivateFieldSet(this, _SugorokuFrame_labelBefore, __classPrivateFieldGet(this, _SugorokuFrame_labelBefore, "f") ?? { role: this.getAttribute("role"), label: this.getAttribute("aria-label") }, "f");
91
+ this.setAttribute("role", "group");
92
+ this.setAttribute("aria-label", this.getAttribute("label"));
93
+ }
94
+ else if (__classPrivateFieldGet(this, _SugorokuFrame_labelBefore, "f") !== null) {
95
+ for (const [name, value] of [["role", __classPrivateFieldGet(this, _SugorokuFrame_labelBefore, "f").role], ["aria-label", __classPrivateFieldGet(this, _SugorokuFrame_labelBefore, "f").label]]) {
96
+ if (value === null)
97
+ this.removeAttribute(name);
98
+ else
99
+ this.setAttribute(name, value);
100
+ }
101
+ __classPrivateFieldSet(this, _SugorokuFrame_labelBefore, null, "f");
102
+ }
103
+ }
104
+ write(name, value) { if (value === null || value === undefined)
105
+ this.removeAttribute(name);
106
+ else
107
+ this.setAttribute(name, String(value)); }
108
+ /** Symbolic display size, reflected. */
109
+ get size() { return this.getAttribute("size"); }
110
+ set size(value) { this.write("size", value); }
111
+ /** Pixel display width, reflected. */
112
+ get width() { return this.getAttribute("width"); }
113
+ set width(value) { this.write("width", value); }
114
+ /** Spoken group label, reflected. */
115
+ get label() { return this.getAttribute("label"); }
116
+ set label(value) { this.write("label", value); }
117
+ }
118
+ _SugorokuFrame_widthBefore = new WeakMap(), _SugorokuFrame_widthOwned = new WeakMap(), _SugorokuFrame_labelBefore = new WeakMap();
119
+ /** The real mounted options, checked alongside builder coverage.
120
+ * @example
121
+ * ```ts
122
+ * import { SUGOROKU_EMBED_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
123
+ * console.log(typeof SUGOROKU_EMBED_OPTION_KEYS); // object
124
+ * ```
125
+ */
126
+ export const SUGOROKU_EMBED_OPTION_KEYS = Object.keys({ board: true, checkers: true, colours: true, theme: true, home: true, view: true, numbers: true, orientation: true, variant: true, rules: true, preset: true, seed: true, dice: true, computer: true, computerDelay: true, controls: true, drag: true, autoEnd: true, language: true, names: true, sound: true, match: true, onRoll: true, onMove: true, onTurn: true, onCube: true, onEnd: true });
127
+ /** Native custom element with reflected setup and presentation.
128
+ * @example
129
+ * ```ts
130
+ * import { SugorokuBoard } from "@johnmorrisdotca/sugoroku/element";
131
+ * console.log(SugorokuBoard.observedAttributes.includes("lang")); // true
132
+ * ```
133
+ */
134
+ export class SugorokuBoard extends SugorokuFrame {
52
135
  constructor() {
53
136
  super(...arguments);
54
137
  _SugorokuBoard_instances.add(this);
55
138
  _SugorokuBoard_mount.set(this, null);
56
139
  _SugorokuBoard_key.set(this, "");
57
140
  _SugorokuBoard_queued.set(this, false);
141
+ _SugorokuBoard_options.set(this, {});
58
142
  }
143
+ /** Host mount options; callback and look changes preserve the current game. */
144
+ get options() { return __classPrivateFieldGet(this, _SugorokuBoard_options, "f"); }
145
+ set options(value) { __classPrivateFieldSet(this, _SugorokuBoard_options, value ?? {}, "f"); if (this.isConnected)
146
+ __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_refresh).call(this); }
59
147
  connectedCallback() {
60
148
  __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_refresh).call(this);
61
149
  }
@@ -70,7 +158,8 @@ export class SugorokuBoard extends ElementBase {
70
158
  __classPrivateFieldSet(this, _SugorokuBoard_queued, true, "f");
71
159
  queueMicrotask(() => {
72
160
  __classPrivateFieldSet(this, _SugorokuBoard_queued, false, "f");
73
- __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_refresh).call(this);
161
+ if (this.isConnected)
162
+ __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_refresh).call(this);
74
163
  });
75
164
  }
76
165
  /** The mounted board's handle (`mountSugoroku`), or null while it is not on a page. */
@@ -107,9 +196,10 @@ export class SugorokuBoard extends ElementBase {
107
196
  drop() {
108
197
  __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.drop();
109
198
  }
110
- beaver() {
111
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.beaver();
112
- }
199
+ /** Calling beaver() keeps the native action; assignment reflects the rule attribute. */
200
+ get beaver() { return () => __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.beaver(); }
201
+ set beaver(value) { if (typeof value !== "function")
202
+ this.write("beaver", value); }
113
203
  resign() {
114
204
  __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.resign();
115
205
  }
@@ -119,56 +209,320 @@ export class SugorokuBoard extends ElementBase {
119
209
  newMatch() {
120
210
  __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.newMatch();
121
211
  }
212
+ /** The `variant` attribute, reflected. */
213
+ get variant() { return this.getAttribute("variant"); }
214
+ set variant(value) { this.write("variant", value); }
215
+ /** The `preset` attribute, reflected. */
216
+ get preset() { return this.getAttribute("preset"); }
217
+ set preset(value) { this.write("preset", value); }
218
+ /** The `points` attribute, reflected. */
219
+ get points() { return this.getAttribute("points"); }
220
+ set points(value) { this.write("points", value); }
221
+ /** The `cube` attribute, reflected. */
222
+ get cube() { return this.getAttribute("cube"); }
223
+ set cube(value) { this.write("cube", value); }
224
+ /** The `crawford` attribute, reflected. */
225
+ get crawford() { return this.getAttribute("crawford"); }
226
+ set crawford(value) { this.write("crawford", value); }
227
+ /** The `jacoby` attribute, reflected. */
228
+ get jacoby() { return this.getAttribute("jacoby"); }
229
+ set jacoby(value) { this.write("jacoby", value); }
230
+ /** The `gammons` attribute, reflected. */
231
+ get gammons() { return this.getAttribute("gammons"); }
232
+ set gammons(value) { this.write("gammons", value); }
233
+ /** The `seed` attribute, reflected. */
234
+ get seed() { return this.getAttribute("seed"); }
235
+ set seed(value) { this.write("seed", value); }
236
+ /** The `dice` attribute, reflected. */
237
+ get dice() { return this.getAttribute("dice"); }
238
+ set dice(value) { this.write("dice", value); }
239
+ /** The `controls` attribute, reflected. */
240
+ get controls() { return this.getAttribute("controls"); }
241
+ set controls(value) { this.write("controls", value); }
242
+ /** The `drag` attribute, reflected. */
243
+ get drag() { return this.getAttribute("drag"); }
244
+ set drag(value) { this.write("drag", value); }
245
+ /** The `auto-end` attribute, reflected. */
246
+ get autoEnd() { return this.getAttribute("auto-end"); }
247
+ set autoEnd(value) { this.write("auto-end", value); }
248
+ /** The `sound` attribute, reflected. */
249
+ get sound() { return this.getAttribute("sound"); }
250
+ set sound(value) { this.write("sound", value); }
251
+ /** The `computer-delay` attribute, reflected. */
252
+ get computerDelay() { return this.getAttribute("computer-delay"); }
253
+ set computerDelay(value) { this.write("computer-delay", value); }
254
+ /** The `white` attribute, reflected. */
255
+ get white() { return this.getAttribute("white"); }
256
+ set white(value) { this.write("white", value); }
257
+ /** The `black` attribute, reflected. */
258
+ get black() { return this.getAttribute("black"); }
259
+ set black(value) { this.write("black", value); }
260
+ /** The `board` attribute, reflected. */
261
+ get board() { return this.getAttribute("board"); }
262
+ set board(value) { this.write("board", value); }
263
+ /** The `checkers` attribute, reflected. */
264
+ get checkers() { return this.getAttribute("checkers"); }
265
+ set checkers(value) { this.write("checkers", value); }
266
+ /** The `theme` attribute, reflected. */
267
+ get theme() { return this.getAttribute("theme"); }
268
+ set theme(value) { this.write("theme", value); }
269
+ /** The `home` attribute, reflected. */
270
+ get home() { return this.getAttribute("home"); }
271
+ set home(value) { this.write("home", value); }
272
+ /** The `view` attribute, reflected. */
273
+ get view() { return this.getAttribute("view"); }
274
+ set view(value) { this.write("view", value); }
275
+ /** The `numbers` attribute, reflected. */
276
+ get numbers() { return this.getAttribute("numbers"); }
277
+ set numbers(value) { this.write("numbers", value); }
278
+ /** The `orientation` attribute, reflected. */
279
+ get orientation() { return this.getAttribute("orientation"); }
280
+ set orientation(value) { this.write("orientation", value); }
122
281
  }
123
- _SugorokuBoard_mount = new WeakMap(), _SugorokuBoard_key = new WeakMap(), _SugorokuBoard_queued = new WeakMap(), _SugorokuBoard_instances = new WeakSet(), _SugorokuBoard_side = function _SugorokuBoard_side(name) {
124
- return oneOf(this.getAttribute(name), STRENGTHS) ?? null;
282
+ _SugorokuBoard_mount = new WeakMap(), _SugorokuBoard_key = new WeakMap(), _SugorokuBoard_queued = new WeakMap(), _SugorokuBoard_options = new WeakMap(), _SugorokuBoard_instances = new WeakSet(), _SugorokuBoard_side = function _SugorokuBoard_side(name) {
283
+ const value = this.getAttribute(name);
284
+ return value === null ? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").computer?.[name] ?? null : oneOf(value, STRENGTHS) ?? null;
125
285
  }, _SugorokuBoard_look = function _SugorokuBoard_look() {
126
286
  const view = this.getAttribute("view");
127
- const language = oneOf(this.getAttribute("lang"), ["en", "ja"]);
287
+ const language = oneOf(this.getAttribute("lang"), ["en", "ja"]) ?? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").language;
128
288
  return {
129
- board: oneOf(this.getAttribute("board"), SUGOROKU_BOARD_NAMES),
130
- checkers: oneOf(this.getAttribute("checkers"), SUGOROKU_CHECKER_SET_NAMES),
131
- theme: oneOf(this.getAttribute("theme"), ["auto", "light", "dark"]),
132
- home: oneOf(this.getAttribute("home"), ["left", "right"]),
133
- view: view === "white" || view === "black" || view === "auto" ? view : undefined,
134
- numbers: isOn(this.getAttribute("numbers")),
135
- orientation: oneOf(this.getAttribute("orientation"), ["auto", "landscape", "portrait"]),
289
+ board: oneOf(this.getAttribute("board"), SUGOROKU_BOARD_NAMES) ?? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").board,
290
+ checkers: oneOf(this.getAttribute("checkers"), SUGOROKU_CHECKER_SET_NAMES) ?? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").checkers,
291
+ theme: oneOf(this.getAttribute("theme"), ["auto", "light", "dark"]) ?? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").theme,
292
+ home: oneOf(this.getAttribute("home"), ["left", "right"]) ?? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").home,
293
+ view: view === "white" || view === "black" || view === "auto" ? view : __classPrivateFieldGet(this, _SugorokuBoard_options, "f").view,
294
+ numbers: isOn(this.getAttribute("numbers"), __classPrivateFieldGet(this, _SugorokuBoard_options, "f").numbers ?? false),
295
+ orientation: oneOf(this.getAttribute("orientation"), ["auto", "landscape", "portrait"]) ?? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").orientation,
136
296
  computer: { white: __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_side).call(this, "white"), black: __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_side).call(this, "black") },
137
- language,
297
+ language, colours: __classPrivateFieldGet(this, _SugorokuBoard_options, "f").colours,
138
298
  };
139
299
  }, _SugorokuBoard_refresh = function _SugorokuBoard_refresh() {
140
- const key = JSON.stringify(GAME_ATTRIBUTES.map((name) => this.getAttribute(name)));
300
+ this.frame();
301
+ const key = JSON.stringify([GAME_ATTRIBUTES.map((name) => this.getAttribute(name)), ...["variant", "rules", "preset", "seed", "dice", "controls", "drag", "autoEnd", "computerDelay", "match"].map(name => __classPrivateFieldGet(this, _SugorokuBoard_options, "f")[name])]);
141
302
  if (key === __classPrivateFieldGet(this, _SugorokuBoard_key, "f") && __classPrivateFieldGet(this, _SugorokuBoard_mount, "f") !== null) {
142
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f").set(__classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_look).call(this));
303
+ __classPrivateFieldGet(this, _SugorokuBoard_mount, "f").set({ ...__classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_look).call(this), names: __classPrivateFieldGet(this, _SugorokuBoard_options, "f").names, sound: this.hasAttribute("sound") ? isOn(this.getAttribute("sound")) : __classPrivateFieldGet(this, _SugorokuBoard_options, "f").sound });
143
304
  return;
144
305
  }
145
306
  __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.destroy();
146
- __classPrivateFieldSet(this, _SugorokuBoard_key, key, "f");
147
- const variant = this.getAttribute("variant");
148
- const rules = {};
149
- const points = this.getAttribute("points");
150
- if (points !== null && /^\d+$/.test(points))
151
- rules.points = Number(points);
152
- for (const name of ["cube", "crawford", "jacoby", "beaver", "gammons"]) {
153
- const value = this.getAttribute(name);
154
- if (value !== null)
155
- rules[name] = isOn(value);
156
- }
157
- const seed = this.getAttribute("seed");
158
- const delay = this.getAttribute("computer-delay");
159
- const sound = this.getAttribute("sound");
160
- __classPrivateFieldSet(this, _SugorokuBoard_mount, mountSugoroku(this, {
161
- variant: isVariantKey(variant) ? variant : undefined,
162
- rules,
163
- preset: this.getAttribute("preset") ?? undefined,
164
- seed: seed === null ? undefined : /^\d+$/.test(seed) ? Number(seed) : seed,
165
- dice: this.getAttribute("dice") === "host" ? "host" : "button",
166
- controls: isOn(this.getAttribute("controls") ?? "on"),
167
- drag: isOn(this.getAttribute("drag") ?? "on"),
168
- autoEnd: isOn(this.getAttribute("auto-end")),
169
- sound: sound !== null && isOn(sound === "" ? "on" : sound),
170
- computerDelay: delay !== null && /^\d+$/.test(delay) ? Number(delay) : undefined,
171
- ...__classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_look).call(this),
172
- }), "f");
307
+ __classPrivateFieldSet(this, _SugorokuBoard_mount, null, "f");
308
+ __classPrivateFieldSet(this, _SugorokuBoard_key, "", "f");
309
+ try {
310
+ const variant = this.getAttribute("variant");
311
+ const rules = { ...__classPrivateFieldGet(this, _SugorokuBoard_options, "f").rules };
312
+ const points = this.getAttribute("points");
313
+ if (points !== null && /^\d+$/.test(points))
314
+ rules.points = Number(points);
315
+ for (const name of ["cube", "crawford", "jacoby", "beaver", "gammons"]) {
316
+ const value = this.getAttribute(name);
317
+ if (value !== null)
318
+ rules[name] = isOn(value);
319
+ }
320
+ const seed = this.getAttribute("seed");
321
+ const delay = this.getAttribute("computer-delay");
322
+ const sound = this.getAttribute("sound");
323
+ __classPrivateFieldSet(this, _SugorokuBoard_mount, mountSugoroku(this, {
324
+ ...__classPrivateFieldGet(this, _SugorokuBoard_options, "f"),
325
+ variant: isVariantKey(variant) ? variant : __classPrivateFieldGet(this, _SugorokuBoard_options, "f").variant,
326
+ rules,
327
+ preset: this.getAttribute("preset") ?? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").preset,
328
+ seed: seed === null ? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").seed : /^\d+$/.test(seed) ? Number(seed) : seed,
329
+ dice: this.getAttribute("dice") === "host" ? "host" : this.getAttribute("dice") === "button" ? "button" : __classPrivateFieldGet(this, _SugorokuBoard_options, "f").dice ?? "button",
330
+ controls: isOn(this.getAttribute("controls"), __classPrivateFieldGet(this, _SugorokuBoard_options, "f").controls ?? true),
331
+ drag: isOn(this.getAttribute("drag"), __classPrivateFieldGet(this, _SugorokuBoard_options, "f").drag ?? true),
332
+ autoEnd: isOn(this.getAttribute("auto-end"), __classPrivateFieldGet(this, _SugorokuBoard_options, "f").autoEnd ?? false),
333
+ sound: sound === null ? __classPrivateFieldGet(this, _SugorokuBoard_options, "f").sound : isOn(sound === "" ? "on" : sound),
334
+ computerDelay: delay !== null && /^\d+$/.test(delay) ? Number(delay) : __classPrivateFieldGet(this, _SugorokuBoard_options, "f").computerDelay,
335
+ ...__classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_look).call(this),
336
+ onRoll: detail => __classPrivateFieldGet(this, _SugorokuBoard_options, "f").onRoll?.(detail), onMove: detail => __classPrivateFieldGet(this, _SugorokuBoard_options, "f").onMove?.(detail), onTurn: detail => __classPrivateFieldGet(this, _SugorokuBoard_options, "f").onTurn?.(detail), onCube: detail => __classPrivateFieldGet(this, _SugorokuBoard_options, "f").onCube?.(detail), onEnd: detail => __classPrivateFieldGet(this, _SugorokuBoard_options, "f").onEnd?.(detail),
337
+ }), "f");
338
+ __classPrivateFieldSet(this, _SugorokuBoard_key, key, "f");
339
+ this.frame();
340
+ this.dispatchEvent(new CustomEvent("sugoroku-load", { bubbles: true, composed: true, detail: JSON.parse(JSON.stringify({ game: this.game, match: this.match })) }));
341
+ }
342
+ catch (error) {
343
+ __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.destroy();
344
+ __classPrivateFieldSet(this, _SugorokuBoard_mount, null, "f");
345
+ __classPrivateFieldSet(this, _SugorokuBoard_key, "", "f");
346
+ const message = error instanceof Error ? error.message : String(error), status = this.ownerDocument.createElement("p");
347
+ status.setAttribute("role", "status");
348
+ status.textContent = message;
349
+ this.replaceChildren(status);
350
+ this.dispatchEvent(new CustomEvent("sugoroku-error", { bubbles: true, composed: true, detail: { message } }));
351
+ }
352
+ };
353
+ SugorokuBoard.observedAttributes = [...GAME_ATTRIBUTES, "white", "black", "board", "checkers", "theme", "home", "view", "numbers", "orientation", "lang", "size", "width", "label"];
354
+ /** Defines the default playable board, or a subclass under a custom tag. Server-safe and idempotent.
355
+ * @param tagName - The custom tag name; omitted to use the package default.
356
+ * @param registry - The custom-element registry; omitted outside a browser.
357
+ * @returns Nothing; existing definitions and server imports remain safe.
358
+ * @example
359
+ * ```ts
360
+ * import { defineBoard } from "@johnmorrisdotca/sugoroku/element";
361
+ * defineBoard();
362
+ * console.log(typeof defineBoard); // function
363
+ * ```
364
+ */
365
+ export function defineBoard(tagName = "sugoroku-board", registry = typeof customElements === "undefined" ? undefined : customElements) { if (registry !== undefined && registry.get(tagName) === undefined)
366
+ registry.define(tagName, tagName === "sugoroku-board" ? SugorokuBoard : class extends SugorokuBoard {
367
+ }); }
368
+ /**
369
+ * Reads a GNU position ID or bounded JSON position; null means invalid counts or input.
370
+ * @param text - Position ID, JSON position, or null for the variant's start.
371
+ * @param variant - The native variant used for checker counts and layout.
372
+ * @param turn - The on-roll side encoded by a GNU ID.
373
+ * @returns A complete position, or null for malformed or inconsistent input.
374
+ * @example
375
+ * ```ts
376
+ * import { readEmbedPosition } from "@johnmorrisdotca/sugoroku/element";
377
+ * console.log(readEmbedPosition("4HPwATDgc/ABMA")?.points[0].length);
378
+ * ```
379
+ */
380
+ export function readEmbedPosition(text, variant = "backgammon", turn = "white") {
381
+ if (!isVariantKey(variant) || (turn !== "white" && turn !== "black"))
382
+ return null;
383
+ const spec = VARIANTS[variant];
384
+ if (text === null)
385
+ return startPosition(spec);
386
+ if (text.length > 10000)
387
+ return null;
388
+ if (text.trim() === "")
389
+ return startPosition(spec);
390
+ if (/^[A-Za-z0-9+/]{14}$/.test(text.trim()))
391
+ return positionFromId(text.trim(), turn, spec.checkers);
392
+ try {
393
+ const data = JSON.parse(text);
394
+ const count = (value) => Number.isInteger(value) && Number(value) >= 0 && Number(value) <= spec.checkers;
395
+ const pair = (value) => Array.isArray(value) && value.length === 2 && value.every(count);
396
+ if (data === null || typeof data !== "object" || !Array.isArray(data.points) || data.points.length !== 2 || !data.points.every(row => Array.isArray(row) && row.length === 24 && row.every(count)) || !pair(data.bar) || !pair(data.off) || !pair(data.reserve))
397
+ return null;
398
+ for (const side of [0, 1])
399
+ if (data.points[side].reduce((sum, n) => sum + n, 0) + data.bar[side] + data.off[side] + data.reserve[side] !== spec.checkers)
400
+ return null;
401
+ return { points: [[...data.points[0]], [...data.points[1]]], bar: [data.bar[0], data.bar[1]], off: [data.off[0], data.off[1]], reserve: [data.reserve[0], data.reserve[1]] };
402
+ }
403
+ catch {
404
+ return null;
405
+ }
406
+ }
407
+ /** Native draw keys, checked by the shared builder.
408
+ * @example
409
+ * ```ts
410
+ * import { SUGOROKU_DRAW_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
411
+ * console.log(typeof SUGOROKU_DRAW_OPTION_KEYS); // object
412
+ * ```
413
+ */
414
+ export const SUGOROKU_DRAW_OPTION_KEYS = Object.keys({ variant: true, view: true, home: true, orientation: true, numbers: true, board: true, checkers: true, colours: true, theme: true, dice: true, cube: true, highlight: true, label: true, style: true });
415
+ /** All host position options, checked by the builder.
416
+ * @example
417
+ * ```ts
418
+ * import { SUGOROKU_POSITION_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
419
+ * console.log(typeof SUGOROKU_POSITION_OPTION_KEYS); // object
420
+ * ```
421
+ */
422
+ export const SUGOROKU_POSITION_OPTION_KEYS = Object.keys({ position: true, onChange: true, variant: true, view: true, home: true, orientation: true, numbers: true, board: true, checkers: true, colours: true, theme: true, dice: true, cube: true, highlight: true, label: true, style: true });
423
+ /** A read-only backgammon position, drawn by the native SVG writer.
424
+ * @example
425
+ * ```ts
426
+ * import { SugorokuPosition } from "@johnmorrisdotca/sugoroku/element";
427
+ * console.log(SugorokuPosition.observedAttributes.includes("lang")); // true
428
+ * ```
429
+ */
430
+ export class SugorokuPosition extends SugorokuFrame {
431
+ constructor() {
432
+ super(...arguments);
433
+ _SugorokuPosition_instances.add(this);
434
+ _SugorokuPosition_value.set(this, null);
435
+ _SugorokuPosition_options.set(this, {});
436
+ _SugorokuPosition_resize.set(this, null);
437
+ }
438
+ /** Structured current position, or null after an input error. */
439
+ get value() { return __classPrivateFieldGet(this, _SugorokuPosition_value, "f"); }
440
+ /** Native drawing options and trusted host position data. */
441
+ get options() { return __classPrivateFieldGet(this, _SugorokuPosition_options, "f"); }
442
+ set options(value) { __classPrivateFieldSet(this, _SugorokuPosition_options, value ?? {}, "f"); if (this.isConnected)
443
+ __classPrivateFieldGet(this, _SugorokuPosition_instances, "m", _SugorokuPosition_render).call(this); }
444
+ connectedCallback() { __classPrivateFieldGet(this, _SugorokuPosition_instances, "m", _SugorokuPosition_render).call(this); const Resize = this.ownerDocument.defaultView?.ResizeObserver; if (Resize !== undefined) {
445
+ __classPrivateFieldSet(this, _SugorokuPosition_resize, new Resize(() => { if (this.isConnected)
446
+ __classPrivateFieldGet(this, _SugorokuPosition_instances, "m", _SugorokuPosition_render).call(this); }), "f");
447
+ __classPrivateFieldGet(this, _SugorokuPosition_resize, "f").observe(this);
448
+ } }
449
+ disconnectedCallback() { __classPrivateFieldGet(this, _SugorokuPosition_resize, "f")?.disconnect(); __classPrivateFieldSet(this, _SugorokuPosition_resize, null, "f"); }
450
+ attributeChangedCallback() { if (this.isConnected)
451
+ __classPrivateFieldGet(this, _SugorokuPosition_instances, "m", _SugorokuPosition_render).call(this); }
452
+ /** Position ID or JSON, reflected. Assign an object to serialize it. */
453
+ get position() { return this.getAttribute("position"); }
454
+ set position(value) { this.write("position", value !== null && typeof value === "object" ? JSON.stringify(value) : value); }
455
+ /** The `variant` attribute, reflected. */
456
+ get variant() { return this.getAttribute("variant"); }
457
+ set variant(value) { this.write("variant", value); }
458
+ /** The `turn` attribute, reflected. */
459
+ get turn() { return this.getAttribute("turn"); }
460
+ set turn(value) { this.write("turn", value); }
461
+ /** The `board` attribute, reflected. */
462
+ get board() { return this.getAttribute("board"); }
463
+ set board(value) { this.write("board", value); }
464
+ /** The `checkers` attribute, reflected. */
465
+ get checkers() { return this.getAttribute("checkers"); }
466
+ set checkers(value) { this.write("checkers", value); }
467
+ /** The `theme` attribute, reflected. */
468
+ get theme() { return this.getAttribute("theme"); }
469
+ set theme(value) { this.write("theme", value); }
470
+ /** The `home` attribute, reflected. */
471
+ get home() { return this.getAttribute("home"); }
472
+ set home(value) { this.write("home", value); }
473
+ /** The `view` attribute, reflected. */
474
+ get view() { return this.getAttribute("view"); }
475
+ set view(value) { this.write("view", value); }
476
+ /** The `numbers` attribute, reflected. */
477
+ get numbers() { return this.getAttribute("numbers"); }
478
+ set numbers(value) { this.write("numbers", value); }
479
+ /** The `orientation` attribute, reflected. */
480
+ get orientation() { return this.getAttribute("orientation"); }
481
+ set orientation(value) { this.write("orientation", value); }
482
+ }
483
+ _SugorokuPosition_value = new WeakMap(), _SugorokuPosition_options = new WeakMap(), _SugorokuPosition_resize = new WeakMap(), _SugorokuPosition_instances = new WeakSet(), _SugorokuPosition_render = function _SugorokuPosition_render() {
484
+ this.frame();
485
+ try {
486
+ const variant = this.getAttribute("variant") ?? (typeof __classPrivateFieldGet(this, _SugorokuPosition_options, "f").variant === "string" ? __classPrivateFieldGet(this, _SugorokuPosition_options, "f").variant : "backgammon");
487
+ if (!isVariantKey(variant))
488
+ throw new RangeError("Choose a known backgammon variant.");
489
+ const turn = this.getAttribute("turn") ?? "white";
490
+ if (turn !== "white" && turn !== "black")
491
+ throw new RangeError("Choose white or black on roll.");
492
+ const position = this.hasAttribute("position") ? readEmbedPosition(this.getAttribute("position"), variant, turn) : __classPrivateFieldGet(this, _SugorokuPosition_options, "f").position ?? startPosition(VARIANTS[variant]);
493
+ if (position === null)
494
+ throw new RangeError("Use a valid GNU position ID or complete JSON position.");
495
+ __classPrivateFieldSet(this, _SugorokuPosition_value, position, "f");
496
+ const orientation = this.getAttribute("orientation") ?? __classPrivateFieldGet(this, _SugorokuPosition_options, "f").orientation;
497
+ const draw = { ...__classPrivateFieldGet(this, _SugorokuPosition_options, "f"), variant: this.hasAttribute("variant") ? variant : __classPrivateFieldGet(this, _SugorokuPosition_options, "f").variant ?? variant, theme: oneOf(this.getAttribute("theme"), ["auto", "light", "dark"]) ?? __classPrivateFieldGet(this, _SugorokuPosition_options, "f").theme, board: oneOf(this.getAttribute("board"), SUGOROKU_BOARD_NAMES) ?? __classPrivateFieldGet(this, _SugorokuPosition_options, "f").board, checkers: oneOf(this.getAttribute("checkers"), SUGOROKU_CHECKER_SET_NAMES) ?? __classPrivateFieldGet(this, _SugorokuPosition_options, "f").checkers, home: oneOf(this.getAttribute("home"), ["left", "right"]) ?? __classPrivateFieldGet(this, _SugorokuPosition_options, "f").home, view: oneOf(this.getAttribute("view"), ["white", "black"]) ?? __classPrivateFieldGet(this, _SugorokuPosition_options, "f").view, numbers: isOn(this.getAttribute("numbers"), __classPrivateFieldGet(this, _SugorokuPosition_options, "f").numbers ?? false), orientation: orientation === "portrait" || orientation !== "landscape" && (this.clientWidth || 600) < 560 ? "portrait" : "landscape", label: this.getAttribute("label") ?? __classPrivateFieldGet(this, _SugorokuPosition_options, "f").label ?? ((this.getAttribute("lang") ?? this.ownerDocument.documentElement.lang) === "ja" ? "双六の局面" : undefined), style: __classPrivateFieldGet(this, _SugorokuPosition_options, "f").style ?? true };
498
+ const svg = new DOMParser().parseFromString(drawSugoroku(position, draw), "image/svg+xml").documentElement;
499
+ this.replaceChildren(this.ownerDocument.importNode(svg, true));
500
+ __classPrivateFieldGet(this, _SugorokuPosition_options, "f").onChange?.(position);
501
+ const detail = JSON.parse(JSON.stringify({ variant, position }));
502
+ this.dispatchEvent(new CustomEvent("sugoroku-change", { bubbles: true, composed: true, detail }));
503
+ this.dispatchEvent(new CustomEvent("sugoroku-load", { bubbles: true, composed: true, detail }));
504
+ }
505
+ catch (error) {
506
+ __classPrivateFieldSet(this, _SugorokuPosition_value, null, "f");
507
+ const message = error instanceof Error ? error.message : String(error), status = this.ownerDocument.createElement("p");
508
+ status.setAttribute("role", "status");
509
+ status.textContent = message;
510
+ this.replaceChildren(status);
511
+ this.dispatchEvent(new CustomEvent("sugoroku-error", { bubbles: true, composed: true, detail: { message } }));
512
+ }
173
513
  };
174
- SugorokuBoard.observedAttributes = [...GAME_ATTRIBUTES, "white", "black", "board", "checkers", "theme", "home", "view", "numbers", "orientation", "lang"];
514
+ SugorokuPosition.observedAttributes = ["position", "variant", "turn", "board", "checkers", "theme", "home", "view", "numbers", "orientation", "lang", "size", "width", "label"];
515
+ /** Defines a static position, or a subclass under a host's tag. Safe on a server.
516
+ * @param tagName - The custom tag name; omitted to use the package default.
517
+ * @param registry - The custom-element registry; omitted outside a browser.
518
+ * @returns Nothing; existing definitions and server imports remain safe.
519
+ * @example
520
+ * ```ts
521
+ * import { definePosition } from "@johnmorrisdotca/sugoroku/element";
522
+ * definePosition();
523
+ * console.log(typeof definePosition); // function
524
+ * ```
525
+ */
526
+ export function definePosition(tagName = "sugoroku-position", registry = typeof customElements === "undefined" ? undefined : customElements) { if (registry !== undefined && registry.get(tagName) === undefined)
527
+ registry.define(tagName, tagName === "sugoroku-position" ? SugorokuPosition : class extends SugorokuPosition {
528
+ }); }
@@ -1,7 +1,17 @@
1
1
  import { type Position } from "./board.ts";
2
2
  import { type Side } from "./side.ts";
3
3
  import type { VariantSpec } from "./variants.ts";
4
- /** The numbers the evaluation is made of, each a number of pips. */
4
+ /** The numbers the evaluation is made of, each a number of pips.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import type { EvaluationWeights } from "@johnmorrisdotca/sugoroku";
9
+ * import { EVALUATION_WEIGHTS } from "@johnmorrisdotca/sugoroku";
10
+ *
11
+ * const value: EvaluationWeights = EVALUATION_WEIGHTS;
12
+ * console.log(value);
13
+ * ```
14
+ */
5
15
  export type EvaluationWeights = {
6
16
  /** What it is worth to have the roll, which the side that does not have it must give up. */
7
17
  onRoll: number;
@@ -32,17 +42,65 @@ export type EvaluationWeights = {
32
42
  /** How much of that the other side's checkers on the bar are worth to this side. */
33
43
  barOther: number;
34
44
  };
35
- /** The weights the computer plays with: set by hand and then held against random changes of themselves, none of which won more often over two thousand games. */
45
+ /** The weights the computer plays with: set by hand and then held against random changes of themselves, none of which won more often over two thousand games.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * import { EVALUATION_WEIGHTS } from "@johnmorrisdotca/sugoroku";
50
+ * console.log(EVALUATION_WEIGHTS);
51
+ * ```
52
+ */
36
53
  export declare const EVALUATION_WEIGHTS: EvaluationWeights;
37
54
  /**
38
55
  * The score of a position for `side`, with the other side to move, as a game
39
56
  * where bearing off first wins. Large positive or negative numbers are a game
40
57
  * won or lost; `evaluateFor` turns it over for a game played to lose.
58
+ *
59
+ * @param spec - The native variant specification.
60
+ * @param position - The native checker counts, bar, reserve and borne-off counts.
61
+ * @param side - The side being examined: white or black.
62
+ * @param weights - Optional weights for the native position evaluation.
63
+ * @returns The score of a position for `side`, with the other side to move, as a game where bearing off first wins. Large positive or negative numbers are a game won or lost; `evaluateFor` turns it over for a game played to lose.
64
+ * @example
65
+ * ```ts
66
+ * import { evaluate, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
67
+ *
68
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
69
+ * const spec = settings.variant;
70
+ * const position = startPosition(spec);
71
+ * console.log(evaluate(spec,position,"white"));
72
+ * ```
41
73
  */
42
74
  export declare function evaluate(spec: VariantSpec, position: Position, side: Side, weights?: EvaluationWeights): number;
43
- /** The score as `side` plays to win or to lose: a game played to lose is judged the other way up. */
75
+ /** The score as `side` plays to win or to lose: a game played to lose is judged the other way up.
76
+ *
77
+ * @param spec - The native variant specification.
78
+ * @param position - The native checker counts, bar, reserve and borne-off counts.
79
+ * @param side - The side being examined: white or black.
80
+ * @param weights - Optional weights for the native position evaluation.
81
+ * @returns The score as `side` plays to win or to lose: a game played to lose is judged the other way up.
82
+ * @example
83
+ * ```ts
84
+ * import { evaluateFor, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
85
+ *
86
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
87
+ * const spec = settings.variant;
88
+ * const position = startPosition(spec);
89
+ * console.log(evaluateFor(spec,position,"white"));
90
+ * ```
91
+ */
44
92
  export declare function evaluateFor(spec: VariantSpec, position: Position, side: Side, weights?: EvaluationWeights): number;
45
- /** A chance of winning, 0 to 1, that a score in pips stands for: the logistic curve, which a lead of 25 pips in a position of this kind takes to about three in four. */
93
+ /** A chance of winning, 0 to 1, that a score in pips stands for: the logistic curve, which a lead of 25 pips in a position of this kind takes to about three in four.
94
+ *
95
+ * @param score - The evaluation difference to map to a win chance.
96
+ * @returns A chance of winning, 0 to 1, that a score in pips stands for: the logistic curve, which a lead of 25 pips in a position of this kind takes to about three in four.
97
+ * @example
98
+ * ```ts
99
+ * import { winChance } from "@johnmorrisdotca/sugoroku";
100
+ *
101
+ * console.log(winChance(0));
102
+ * ```
103
+ */
46
104
  export declare function winChance(score: number): number;
47
105
  /**
48
106
  * The Keith count of a side, a pip count corrected for how badly the checkers
@@ -50,5 +108,19 @@ export declare function winChance(score: number): number;
50
108
  * 1-point, 1 for each beyond the first on the 2-point and beyond three on the
51
109
  * 3-point, and 1 for each empty point among the 4, 5 and 6, and then the side
52
110
  * on roll adds a seventh (rounded down). For races only.
111
+ *
112
+ * @param position - The native checker counts, bar, reserve and borne-off counts.
113
+ * @param side - The side being examined: white or black.
114
+ * @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
115
+ * @returns The Keith count of a side, a pip count corrected for how badly the checkers are placed for bearing off: plus 2 for each checker beyond the first on the 1-point, 1 for each beyond the first on the 2-point and beyond three on the 3-point, and 1 for each empty point among the 4, 5 and 6, and then the side on roll adds a seventh (rounded down). For races only.
116
+ * @example
117
+ * ```ts
118
+ * import { keithCount, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
119
+ *
120
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
121
+ * const spec = settings.variant;
122
+ * const position = startPosition(spec);
123
+ * console.log(keithCount(position,"white",true));
124
+ * ```
53
125
  */
54
126
  export declare function keithCount(position: Position, side: Side, onRoll: boolean): number;