@johnmorrisdotca/sugoroku 1.1.0 → 1.2.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.
- package/CHANGELOG.md +20 -0
- package/README.md +6 -4
- package/dist/board.js +1 -1
- package/dist/colours.js +6 -1
- package/dist/computer.d.ts +1 -1
- package/dist/computer.js +9 -4
- package/dist/draw-entry.d.ts +3 -3
- package/dist/draw-entry.js +2 -2
- package/dist/draw.js +30 -11
- package/dist/element.d.ts +3 -122
- package/dist/element.js +327 -405
- package/dist/elementFrame.d.ts +21 -0
- package/dist/elementFrame.js +110 -0
- package/dist/evaluate.js +5 -2
- package/dist/game.js +16 -2
- package/dist/geometry.js +44 -27
- package/dist/index.d.ts +11 -11
- package/dist/index.js +9 -9
- package/dist/match.js +14 -2
- package/dist/mount.d.ts +2 -2
- package/dist/mount.js +80 -354
- package/dist/mountView.d.ts +58 -0
- package/dist/mountView.js +459 -0
- package/dist/notation.js +6 -2
- package/dist/play-entry.d.ts +4 -4
- package/dist/play-entry.js +1 -1
- package/dist/positionElement.d.ts +107 -0
- package/dist/positionElement.js +303 -0
- package/dist/positionId.js +4 -1
- package/dist/record.d.ts +1 -1
- package/dist/record.js +27 -31
- package/dist/rules.js +34 -5
- package/dist/strings-fr.data.d.ts +2 -0
- package/dist/strings-fr.data.js +60 -0
- package/dist/strings.d.ts +4 -4
- package/dist/strings.js +10 -5
- package/dist/variants.js +19 -2
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +11 -11
|
@@ -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
|
+
{ ja: "双六の局面", fr: "Position de backgammon" }[(this.getAttribute("lang") ?? this.ownerDocument.documentElement.lang).slice(0, 2)],
|
|
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
|
+
}
|
package/dist/positionId.js
CHANGED
|
@@ -99,7 +99,10 @@ export function positionFromId(id, onRoll, checkers = 15) {
|
|
|
99
99
|
const sextets = [...id].map((char) => ALPHABET.indexOf(char));
|
|
100
100
|
const bytes = [];
|
|
101
101
|
for (let g = 0; g < 14; g += 4) {
|
|
102
|
-
const group = (sextets[g] << 18) |
|
|
102
|
+
const group = (sextets[g] << 18) |
|
|
103
|
+
(sextets[g + 1] << 12) |
|
|
104
|
+
((sextets[g + 2] ?? 0) << 6) |
|
|
105
|
+
(sextets[g + 3] ?? 0);
|
|
103
106
|
bytes.push((group >> 16) & 255);
|
|
104
107
|
if (g + 2 < 14)
|
|
105
108
|
bytes.push((group >> 8) & 255);
|
package/dist/record.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import type { Position } from "./board.ts";
|
|
1
2
|
import { type GameResult, type GameState, type WinKind } from "./game.ts";
|
|
2
3
|
import { type Match } from "./match.ts";
|
|
3
4
|
import type { Move } from "./moves.ts";
|
|
4
5
|
import { type Settings } from "./rules.ts";
|
|
5
6
|
import { type Side } from "./side.ts";
|
|
6
|
-
import type { Position } from "./board.ts";
|
|
7
7
|
/**
|
|
8
8
|
* GAMES AS TEXT. A record is plain lines a server can store and read back, and
|
|
9
9
|
* replay move by move against the rules to be sure every move was legal and
|
package/dist/record.js
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
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");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _GameRecorder_lines, _GameRecorder_games;
|
|
13
1
|
import { seededDice } from "./dice.js";
|
|
14
|
-
import { agreeDraw, beaverDouble, concede, dropDouble, offerDouble, playTurn, rollDice, rollOpening, takeDouble } from "./game.js";
|
|
2
|
+
import { agreeDraw, beaverDouble, concede, dropDouble, offerDouble, playTurn, rollDice, rollOpening, takeDouble, } from "./game.js";
|
|
15
3
|
import { finishGame, newMatch, startGame } from "./match.js";
|
|
16
4
|
import { formatPlay, parsePlay } from "./notation.js";
|
|
17
5
|
import { settingsFor } from "./rules.js";
|
|
@@ -75,7 +63,16 @@ export function formatRecordHeader(settings, seed) {
|
|
|
75
63
|
if (typeof seed === "string" && /^\d+$/.test(seed))
|
|
76
64
|
throw new Error("A seed of digits is a number: pass it as a number.");
|
|
77
65
|
const { rules, variant } = settings;
|
|
78
|
-
const lines = [
|
|
66
|
+
const lines = [
|
|
67
|
+
`sugoroku ${RECORD_VERSION}`,
|
|
68
|
+
`variant ${variant.key}`,
|
|
69
|
+
`points ${rules.points}`,
|
|
70
|
+
`cube ${onOff(rules.cube)}`,
|
|
71
|
+
`crawford ${onOff(rules.crawford)}`,
|
|
72
|
+
`jacoby ${onOff(rules.jacoby)}`,
|
|
73
|
+
`beaver ${onOff(rules.beaver)}`,
|
|
74
|
+
`gammons ${onOff(rules.gammons)}`,
|
|
75
|
+
];
|
|
79
76
|
if (seed !== undefined)
|
|
80
77
|
lines.push(`seed ${seed}`);
|
|
81
78
|
return lines;
|
|
@@ -148,55 +145,54 @@ export function formatResult(result) {
|
|
|
148
145
|
* ```
|
|
149
146
|
*/
|
|
150
147
|
export class GameRecorder {
|
|
148
|
+
#lines;
|
|
149
|
+
#games = 0;
|
|
151
150
|
constructor(settings, seed) {
|
|
152
|
-
|
|
153
|
-
_GameRecorder_games.set(this, 0);
|
|
154
|
-
__classPrivateFieldSet(this, _GameRecorder_lines, formatRecordHeader(settings, seed), "f");
|
|
151
|
+
this.#lines = formatRecordHeader(settings, seed);
|
|
155
152
|
}
|
|
156
153
|
/** Begin the next game of the match. */
|
|
157
154
|
startGame() {
|
|
158
|
-
|
|
159
|
-
|
|
155
|
+
this.#games += 1;
|
|
156
|
+
this.#lines.push(`game ${this.#games}`);
|
|
160
157
|
}
|
|
161
158
|
opening(throws) {
|
|
162
|
-
|
|
159
|
+
this.#lines.push(formatOpening(throws));
|
|
163
160
|
}
|
|
164
161
|
turn(side, dice, moves) {
|
|
165
|
-
|
|
162
|
+
this.#lines.push(formatTurn(side, dice, moves));
|
|
166
163
|
}
|
|
167
164
|
double(side) {
|
|
168
|
-
|
|
165
|
+
this.#lines.push(`${side} doubles`);
|
|
169
166
|
}
|
|
170
167
|
take(side) {
|
|
171
|
-
|
|
168
|
+
this.#lines.push(`${side} takes`);
|
|
172
169
|
}
|
|
173
170
|
drop(side) {
|
|
174
|
-
|
|
171
|
+
this.#lines.push(`${side} drops`);
|
|
175
172
|
}
|
|
176
173
|
beaver(side) {
|
|
177
|
-
|
|
174
|
+
this.#lines.push(`${side} beavers`);
|
|
178
175
|
}
|
|
179
176
|
concede(side, kind) {
|
|
180
|
-
|
|
177
|
+
this.#lines.push(`${side} concedes ${kind}`);
|
|
181
178
|
}
|
|
182
179
|
drawn() {
|
|
183
|
-
|
|
180
|
+
this.#lines.push("draw");
|
|
184
181
|
}
|
|
185
182
|
result(result) {
|
|
186
|
-
|
|
183
|
+
this.#lines.push(formatResult(result));
|
|
187
184
|
}
|
|
188
185
|
/** The record, one line after another. */
|
|
189
186
|
text() {
|
|
190
|
-
return `${
|
|
187
|
+
return `${this.#lines.join("\n")}\n`;
|
|
191
188
|
}
|
|
192
189
|
}
|
|
193
|
-
_GameRecorder_lines = new WeakMap(), _GameRecorder_games = new WeakMap();
|
|
194
190
|
class RecordError extends Error {
|
|
195
191
|
}
|
|
196
192
|
const fail = (message) => {
|
|
197
193
|
throw new RecordError(message);
|
|
198
194
|
};
|
|
199
|
-
const flag = (value) =>
|
|
195
|
+
const flag = (value) => value === "on" ? true : value === "off" ? false : fail(`"${value}" is not on or off`);
|
|
200
196
|
function parseSettings(entries) {
|
|
201
197
|
const named = entries.get("variant") ?? fail("the record names no variant");
|
|
202
198
|
const variant = isVariantKey(named) ? named : fail(`"${named}" is not a variant`);
|
package/dist/rules.js
CHANGED
|
@@ -7,7 +7,14 @@ import { isVariantKey, variantSpec } from "./variants.js";
|
|
|
7
7
|
* console.log(DEFAULT_RULES);
|
|
8
8
|
* ```
|
|
9
9
|
*/
|
|
10
|
-
export const DEFAULT_RULES = {
|
|
10
|
+
export const DEFAULT_RULES = {
|
|
11
|
+
points: 1,
|
|
12
|
+
cube: false,
|
|
13
|
+
crawford: true,
|
|
14
|
+
jacoby: false,
|
|
15
|
+
beaver: false,
|
|
16
|
+
gammons: false,
|
|
17
|
+
};
|
|
11
18
|
/** What goes wrong with a set of rules, as sentences; empty when they are fine.
|
|
12
19
|
*
|
|
13
20
|
* @param rules - The complete or partial native rules specified by the signature.
|
|
@@ -84,7 +91,14 @@ export function cubeInPlay(rules, crawfordGame) {
|
|
|
84
91
|
* ```
|
|
85
92
|
*/
|
|
86
93
|
export const CUBE_LIMIT = 64;
|
|
87
|
-
const MATCH = (points) => ({
|
|
94
|
+
const MATCH = (points) => ({
|
|
95
|
+
points,
|
|
96
|
+
cube: true,
|
|
97
|
+
crawford: true,
|
|
98
|
+
jacoby: false,
|
|
99
|
+
beaver: false,
|
|
100
|
+
gammons: true,
|
|
101
|
+
});
|
|
88
102
|
const SINGLE = DEFAULT_RULES;
|
|
89
103
|
/** Every named way of playing, in the order of `docs/VARIANTS.md`.
|
|
90
104
|
*
|
|
@@ -95,12 +109,27 @@ const SINGLE = DEFAULT_RULES;
|
|
|
95
109
|
* ```
|
|
96
110
|
*/
|
|
97
111
|
export const PRESETS = [
|
|
98
|
-
{
|
|
112
|
+
{
|
|
113
|
+
key: "backgammon",
|
|
114
|
+
variant: "backgammon",
|
|
115
|
+
rules: SINGLE,
|
|
116
|
+
names: ["Backgammon", "Backgammon Level 2", "Backgammon Level 3", "Casual Backgammon"],
|
|
117
|
+
},
|
|
99
118
|
{ key: "backgammon-3", variant: "backgammon", rules: MATCH(3), names: ["Backgammon (3 Point)"] },
|
|
100
|
-
{
|
|
119
|
+
{
|
|
120
|
+
key: "backgammon-5",
|
|
121
|
+
variant: "backgammon",
|
|
122
|
+
rules: MATCH(5),
|
|
123
|
+
names: ["Backgammon (5 Point)", "Pro Backgammon", "Pro Backgammon Level 2"],
|
|
124
|
+
},
|
|
101
125
|
{ key: "backgammon-7", variant: "backgammon", rules: MATCH(7), names: ["Backgammon (7 Point)"] },
|
|
102
126
|
{ key: "backgammon-9", variant: "backgammon", rules: MATCH(9), names: ["Backgammon (9 Point)", "Pro Backgammon-9"] },
|
|
103
|
-
{
|
|
127
|
+
{
|
|
128
|
+
key: "backgammon-race",
|
|
129
|
+
variant: "backgammon-race",
|
|
130
|
+
rules: SINGLE,
|
|
131
|
+
names: ["Backgammon Race", "Backgammon Race Level 2"],
|
|
132
|
+
},
|
|
104
133
|
{ key: "backgammon-race-5", variant: "backgammon-race", rules: MATCH(5), names: ["Pro Backgammon Race"] },
|
|
105
134
|
{ key: "anti-backgammon", variant: "anti-backgammon", rules: SINGLE, names: ["Anti-Backgammon"] },
|
|
106
135
|
{ key: "nackgammon", variant: "nackgammon", rules: SINGLE, names: ["Nackgammon"] },
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** Sugoroku's words in French: the same names as the English table in `strings.ts`. A line that may name "you" puts the side first, before a colon, so no verb has to agree with it. */
|
|
2
|
+
export const FR = {
|
|
3
|
+
variant_backgammon: "Backgammon",
|
|
4
|
+
"variant_backgammon-race": "Backgammon, course",
|
|
5
|
+
"variant_anti-backgammon": "Anti-backgammon",
|
|
6
|
+
variant_nackgammon: "Nackgammon",
|
|
7
|
+
"variant_long-gammon": "Long Gammon",
|
|
8
|
+
variant_hypergammon: "Hypergammon",
|
|
9
|
+
variant_tabula: "Tabula",
|
|
10
|
+
white: "Blanc",
|
|
11
|
+
black: "Noir",
|
|
12
|
+
computer: "Ordinateur",
|
|
13
|
+
you: "Vous",
|
|
14
|
+
board: "Plateau\u00a0: {variant}",
|
|
15
|
+
roll: "Lancer les dés",
|
|
16
|
+
rollOpening: "Lancer pour commencer",
|
|
17
|
+
done: "Terminé",
|
|
18
|
+
undo: "Annuler",
|
|
19
|
+
double: "Doubler",
|
|
20
|
+
take: "Accepter",
|
|
21
|
+
drop: "Refuser",
|
|
22
|
+
beaver: "Beaver",
|
|
23
|
+
nextGame: "Partie suivante",
|
|
24
|
+
newMatch: "Nouveau match",
|
|
25
|
+
waitingDice: "En attente des dés",
|
|
26
|
+
opening: "Lancez les dés\u00a0: le dé le plus haut commence.",
|
|
27
|
+
openingTie: "Égalité, {die} chacun. Relancez.",
|
|
28
|
+
started: "Premier à jouer\u00a0: {side}, avec {dice}.",
|
|
29
|
+
toRoll: "À {side} de lancer.",
|
|
30
|
+
toRollYou: "À vous de lancer.",
|
|
31
|
+
toPlay: "À {side} de jouer\u00a0: {dice}.",
|
|
32
|
+
toPlayYou: "À vous de jouer\u00a0: {dice}.",
|
|
33
|
+
played: "Tour joué. Appuyez sur Terminé.",
|
|
34
|
+
noMove: "{side} ne peut pas jouer.",
|
|
35
|
+
noMoveYou: "Aucun coup possible. Appuyez sur Terminé.",
|
|
36
|
+
thinking: "{side} réfléchit…",
|
|
37
|
+
doubled: "{side}\u00a0: videau doublé à {value}.",
|
|
38
|
+
answerDouble: "{side} double le videau à {value}. Accepter, ou refuser et perdre {points}?",
|
|
39
|
+
beavered: "{side}\u00a0: beaver, le videau passe à {value}.",
|
|
40
|
+
answerBeaver: "{side} fait un beaver\u00a0: le videau passe à {value}. Accepter, ou refuser et perdre {points}?",
|
|
41
|
+
tookDouble: "{side}\u00a0: double accepté, le videau est à {value}.",
|
|
42
|
+
singleGame: "une partie simple",
|
|
43
|
+
gammon: "un gammon",
|
|
44
|
+
backgammon: "un backgammon",
|
|
45
|
+
wonBy: "Gagnant\u00a0: {side}, {kind}. {points}.",
|
|
46
|
+
wonDrop: "Gagnant\u00a0: {side}, le double a été refusé. {points}.",
|
|
47
|
+
wonConcede: "Gagnant\u00a0: {side}, l'adversaire a abandonné. {points}.",
|
|
48
|
+
drawn: "La partie est nulle.",
|
|
49
|
+
point: "{n} point",
|
|
50
|
+
points: "{n} points",
|
|
51
|
+
matchWon: "Gagnant du match\u00a0: {side}, {a} à {b}.",
|
|
52
|
+
matchDrawn: "Le match est nul.",
|
|
53
|
+
score: "{white} {a}, {black} {b}",
|
|
54
|
+
matchTo: "match en {points}",
|
|
55
|
+
money: "partie libre",
|
|
56
|
+
crawford: "Partie Crawford\u00a0: pas de videau",
|
|
57
|
+
cubeMiddle: "videau au centre",
|
|
58
|
+
cubeHeld: "videau {value}, propriétaire\u00a0: {side}",
|
|
59
|
+
dice: "dés {dice}",
|
|
60
|
+
};
|
package/dist/strings.d.ts
CHANGED
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
* console.log(value);
|
|
16
16
|
* ```
|
|
17
17
|
*/
|
|
18
|
-
export type SugorokuLanguage = "en" | "ja";
|
|
19
|
-
/** The native English and
|
|
18
|
+
export type SugorokuLanguage = "en" | "ja" | "fr";
|
|
19
|
+
/** The native English, Japanese and French labels, announcements and interpolation templates.
|
|
20
20
|
*
|
|
21
21
|
* @example
|
|
22
22
|
* ```ts
|
|
@@ -39,10 +39,10 @@ export declare const SUGOROKU_STRINGS: Record<SugorokuLanguage, Record<string, s
|
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
41
|
export declare function sugorokuSay(language: SugorokuLanguage, key: string, values?: Record<string, string | number>): string;
|
|
42
|
-
/** The language an element or its page is in: its own `lang`, or the nearest above,
|
|
42
|
+
/** The language an element or its page is in: its own `lang`, or the nearest above, Japanese if that begins with `ja`, French if `fr`, and English otherwise.
|
|
43
43
|
*
|
|
44
44
|
* @param element - The host element whose inherited language is read; null selects the fallback.
|
|
45
|
-
* @returns The language an element or its page is in: its own `lang`, or the nearest above,
|
|
45
|
+
* @returns The language an element or its page is in: its own `lang`, or the nearest above, Japanese if that begins with `ja`, French if `fr`, and English otherwise.
|
|
46
46
|
* @example
|
|
47
47
|
* ```ts no-run
|
|
48
48
|
* import { sugorokuLanguageOf } from "@johnmorrisdotca/sugoroku/play";
|
package/dist/strings.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { FR } from "./strings-fr.data.js";
|
|
2
|
+
/** The native English, Japanese and French labels, announcements and interpolation templates.
|
|
2
3
|
*
|
|
3
4
|
* @example
|
|
4
5
|
* ```ts
|
|
@@ -125,6 +126,7 @@ export const SUGOROKU_STRINGS = {
|
|
|
125
126
|
cubeHeld: "キューブ{value}({side})",
|
|
126
127
|
dice: "サイコロ {dice}",
|
|
127
128
|
},
|
|
129
|
+
fr: FR,
|
|
128
130
|
};
|
|
129
131
|
/** A line in a language with its `{name}`s filled in. A name left unfilled stays as written, so a missing value is visible.
|
|
130
132
|
*
|
|
@@ -143,10 +145,10 @@ export function sugorokuSay(language, key, values = {}) {
|
|
|
143
145
|
const line = SUGOROKU_STRINGS[language][key] ?? SUGOROKU_STRINGS.en[key] ?? key;
|
|
144
146
|
return line.replace(/\{(\w+)\}/g, (whole, name) => (name in values ? String(values[name]) : whole));
|
|
145
147
|
}
|
|
146
|
-
/** The language an element or its page is in: its own `lang`, or the nearest above,
|
|
148
|
+
/** The language an element or its page is in: its own `lang`, or the nearest above, Japanese if that begins with `ja`, French if `fr`, and English otherwise.
|
|
147
149
|
*
|
|
148
150
|
* @param element - The host element whose inherited language is read; null selects the fallback.
|
|
149
|
-
* @returns The language an element or its page is in: its own `lang`, or the nearest above,
|
|
151
|
+
* @returns The language an element or its page is in: its own `lang`, or the nearest above, Japanese if that begins with `ja`, French if `fr`, and English otherwise.
|
|
150
152
|
* @example
|
|
151
153
|
* ```ts no-run
|
|
152
154
|
* import { sugorokuLanguageOf } from "@johnmorrisdotca/sugoroku/play";
|
|
@@ -157,6 +159,9 @@ export function sugorokuSay(language, key, values = {}) {
|
|
|
157
159
|
* ```
|
|
158
160
|
*/
|
|
159
161
|
export function sugorokuLanguageOf(element) {
|
|
160
|
-
const tag = element?.closest("[lang]")?.getAttribute("lang") ??
|
|
161
|
-
|
|
162
|
+
const tag = element?.closest("[lang]")?.getAttribute("lang") ??
|
|
163
|
+
element?.ownerDocument?.documentElement.getAttribute("lang") ??
|
|
164
|
+
"en";
|
|
165
|
+
const lower = tag.toLowerCase();
|
|
166
|
+
return lower.startsWith("ja") ? "ja" : lower.startsWith("fr") ? "fr" : "en";
|
|
162
167
|
}
|
package/dist/variants.js
CHANGED
|
@@ -58,7 +58,16 @@ export const VARIANTS = {
|
|
|
58
58
|
],
|
|
59
59
|
},
|
|
60
60
|
/** The Roman game: three dice, no doubles, both sides entering the same way round the same track. */
|
|
61
|
-
tabula: {
|
|
61
|
+
tabula: {
|
|
62
|
+
...BASE,
|
|
63
|
+
key: "tabula",
|
|
64
|
+
layout: [],
|
|
65
|
+
dice: 3,
|
|
66
|
+
doubles: "none",
|
|
67
|
+
direction: "same",
|
|
68
|
+
opening: "die-each",
|
|
69
|
+
holdBelow: 12,
|
|
70
|
+
},
|
|
62
71
|
};
|
|
63
72
|
/** The variants' keys, in the order the demo lists them.
|
|
64
73
|
*
|
|
@@ -68,7 +77,15 @@ export const VARIANTS = {
|
|
|
68
77
|
* console.log(VARIANT_KEYS);
|
|
69
78
|
* ```
|
|
70
79
|
*/
|
|
71
|
-
export const VARIANT_KEYS = [
|
|
80
|
+
export const VARIANT_KEYS = [
|
|
81
|
+
"backgammon",
|
|
82
|
+
"backgammon-race",
|
|
83
|
+
"anti-backgammon",
|
|
84
|
+
"nackgammon",
|
|
85
|
+
"long-gammon",
|
|
86
|
+
"hypergammon",
|
|
87
|
+
"tabula",
|
|
88
|
+
];
|
|
72
89
|
/** Whether `key` names a variant.
|
|
73
90
|
*
|
|
74
91
|
* @param key - The native variant, preset or localized-label key specified by the signature.
|
package/dist/version.d.ts
CHANGED