@johnmorrisdotca/sugoroku 1.0.1 → 1.0.2
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 +14 -0
- package/README.md +318 -10
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,20 @@ All notable changes to this project are written here. The format follows
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [1.0.2] - 2026-10-06
|
|
10
|
+
|
|
11
|
+
Nothing that was exported has changed.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- The README takes the family's one layout, fully: a hero picture of the demo on a desk and on a phone in light and dark, a picture of the opening position of six variants, an Install section, an Examples section of twelve examples whose output is what they print (the notation, a record written and replayed, a record refused, the computer at four strengths, and more), and an Accessibility section. Its pictures are in `docs/images` (WebP, light and dark) and are retaken with `pnpm screenshots:readme` (it replaces `pnpm pictures` and the two JPEGs `docs/desktop.jpg` and `docs/phone.jpg`); they are not in the tarball, and `pnpm test:package` fails if one is.
|
|
16
|
+
- `pnpm test:readme` type-checks and runs every TypeScript and JavaScript example in the README against the built package, as a CI job of its own, and `pnpm check` holds the README to the family's lint (sections in order, a language on every fence, pictures with alt text, widths and captions, no marketing words, at most 64,000 characters, because npm shows only the first 65,536).
|
|
17
|
+
- Repository only: the package and everything it exports are unchanged. `CONTRIBUTING.md` is the family's one text with a section of its own for Sugoroku, held to the master in johnmorrisdotca/.github by `src/family.test.js`; `ci.yml` and `pages.yml` are the family's one text (`pnpm check`, the demo, and the package on Linux, macOS and Windows), and any jobs of the package's own after them.
|
|
18
|
+
|
|
19
|
+
### Fixed
|
|
20
|
+
|
|
21
|
+
- The API reference page wraps a long entry path instead of running about 2 px wider than a 360 px screen. Nothing the package exports has changed.
|
|
22
|
+
|
|
9
23
|
## [1.0.1] - 2026-10-05
|
|
10
24
|
|
|
11
25
|
Nothing that was exported has changed.
|
package/README.md
CHANGED
|
@@ -8,14 +8,29 @@ The full rules, the doubling cube and match play with the Crawford rule, Nackgam
|
|
|
8
8
|
<a href="https://www.npmjs.com/package/@johnmorrisdotca/sugoroku"><img alt="npm" src="https://img.shields.io/npm/v/@johnmorrisdotca/sugoroku?color=2f5d4a"></a>
|
|
9
9
|
<a href="./LICENSE"><img alt="MIT licence" src="https://img.shields.io/badge/licence-MIT-2f5d4a"></a>
|
|
10
10
|
<img alt="No dependencies" src="https://img.shields.io/badge/dependencies-0-2f5d4a">
|
|
11
|
+
<img alt="TypeScript" src="https://img.shields.io/badge/types-TypeScript-3178c6">
|
|
11
12
|
</p>
|
|
12
13
|
|
|
13
14
|
<p align="center"><a href="https://johnmorrisdotca.github.io/sugoroku/"><strong>Play a game →</strong></a> · <a href="https://johnmorrisdotca.github.io/sugoroku/api.html">API reference</a> · <a href="docs/VARIANTS.md">The variants and their sources</a></p>
|
|
14
15
|
|
|
15
|
-
<
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
<table align="center">
|
|
17
|
+
<tr>
|
|
18
|
+
<td align="center" valign="top">
|
|
19
|
+
<picture>
|
|
20
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/hero-desk-dark.webp">
|
|
21
|
+
<img src="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/hero-desk-light.webp" alt="The demo on a desk, in English: the page's header with its language chooser and cloth swatches, the game chooser (Backgammon, Race, Anti, Nackgammon, Long Gammon, Hypergammon, Tabula), the player choices, and a backgammon board in the middle of a match to 3 with the doubling cube at 2, black and white checkers on a green board with red and cream points, and the Roll dice, Double and Undo buttons under it." width="720">
|
|
22
|
+
</picture>
|
|
23
|
+
<br><em>A match to 3 against the computer, the cube at 2, on a desk.</em>
|
|
24
|
+
</td>
|
|
25
|
+
<td align="center" valign="top">
|
|
26
|
+
<picture>
|
|
27
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/hero-phone-dark.webp">
|
|
28
|
+
<img src="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/hero-phone-light.webp" alt="The demo on a phone, in Japanese, in the device's light or dark: the board standing up on its end, with the same match in progress and the three buttons under it." width="220">
|
|
29
|
+
</picture>
|
|
30
|
+
<br><em>The same board standing up on a phone, in Japanese.</em>
|
|
31
|
+
</td>
|
|
32
|
+
</tr>
|
|
33
|
+
</table>
|
|
19
34
|
|
|
20
35
|
Sugoroku is the rules engine for backgammon and the games played on its board: the
|
|
21
36
|
classic game, and the relatives that ItsYourTurn.com and GoldToken.com played under their own
|
|
@@ -71,15 +86,90 @@ And in a page, a game to play against the computer, by touch and mouse, with not
|
|
|
71
86
|
- **English and Japanese** in the board's words and the demo.
|
|
72
87
|
- **No dependencies**, no network requests, and nothing stored outside the page it is in.
|
|
73
88
|
|
|
89
|
+
### What's in it
|
|
90
|
+
|
|
91
|
+
Each picture is a board drawn by the package's `drawSugoroku`, taken from [the demo](https://johnmorrisdotca.github.io/sugoroku/) with `pnpm screenshots:readme`, in light and dark: the opening position of six of the seven variants (Anti-Backgammon starts as the classic does). The hero is a seeded match, played up by the package's own computer. The pictures come again.
|
|
92
|
+
|
|
93
|
+
<table>
|
|
94
|
+
<tr>
|
|
95
|
+
<td align="center" valign="top" width="50%">
|
|
96
|
+
<picture>
|
|
97
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/backgammon-desk-dark.webp">
|
|
98
|
+
<img src="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/backgammon-desk-light.webp" alt="The classic backgammon board at the start: fifteen black and fifteen white checkers on the points of a green board with red and cream triangles, the bar down the middle, a cube in the middle of the left rail and a tray each side." width="400">
|
|
99
|
+
</picture>
|
|
100
|
+
<br><em><strong>Backgammon</strong>: fifteen each, two dice, bear off first.</em>
|
|
101
|
+
</td>
|
|
102
|
+
<td align="center" valign="top" width="50%">
|
|
103
|
+
<picture>
|
|
104
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/nackgammon-desk-dark.webp">
|
|
105
|
+
<img src="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/nackgammon-desk-light.webp" alt="A Nackgammon board at the start: the classic position with four back checkers on each side's 24-point instead of two." width="400">
|
|
106
|
+
</picture>
|
|
107
|
+
<br><em><strong>Nackgammon</strong>: four back checkers each, for a longer game.</em>
|
|
108
|
+
</td>
|
|
109
|
+
</tr>
|
|
110
|
+
<tr>
|
|
111
|
+
<td align="center" valign="top" width="50%">
|
|
112
|
+
<picture>
|
|
113
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/long-gammon-desk-dark.webp">
|
|
114
|
+
<img src="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/long-gammon-desk-light.webp" alt="A Long Gammon board at the start: all fifteen checkers of each side stacked on one point, the 24-point." width="400">
|
|
115
|
+
</picture>
|
|
116
|
+
<br><em><strong>Long Gammon</strong>: all fifteen start on the 24-point.</em>
|
|
117
|
+
</td>
|
|
118
|
+
<td align="center" valign="top" width="50%">
|
|
119
|
+
<picture>
|
|
120
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/hypergammon-desk-dark.webp">
|
|
121
|
+
<img src="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/hypergammon-desk-light.webp" alt="A Hypergammon board at the start: three checkers each, on the 24, 23 and 22 points." width="400">
|
|
122
|
+
</picture>
|
|
123
|
+
<br><em><strong>Hypergammon</strong>: three checkers each.</em>
|
|
124
|
+
</td>
|
|
125
|
+
</tr>
|
|
126
|
+
<tr>
|
|
127
|
+
<td align="center" valign="top" width="50%">
|
|
128
|
+
<picture>
|
|
129
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/tabula-desk-dark.webp">
|
|
130
|
+
<img src="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/tabula-desk-light.webp" alt="A Tabula board at the start: fifteen checkers on each side waiting on the bar, and the track empty, in a dark-themed board." width="400">
|
|
131
|
+
</picture>
|
|
132
|
+
<br><em><strong>Tabula</strong>: the Roman game, three dice, both sides one way round.</em>
|
|
133
|
+
</td>
|
|
134
|
+
<td align="center" valign="top" width="50%">
|
|
135
|
+
<picture>
|
|
136
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/backgammon-race-desk-dark.webp">
|
|
137
|
+
<img src="https://raw.githubusercontent.com/johnmorrisdotca/sugoroku/main/docs/images/backgammon-race-desk-light.webp" alt="A Backgammon Race board at the start: all fifteen checkers of both sides on the bar, none yet on the points." width="400">
|
|
138
|
+
</picture>
|
|
139
|
+
<br><em><strong>Backgammon Race</strong>: every checker enters from the bar.</em>
|
|
140
|
+
</td>
|
|
141
|
+
</tr>
|
|
142
|
+
</table>
|
|
143
|
+
|
|
74
144
|
## Use it in your project
|
|
75
145
|
|
|
146
|
+
### Install
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
npm install @johnmorrisdotca/sugoroku
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
```sh
|
|
153
|
+
pnpm add @johnmorrisdotca/sugoroku
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
```sh
|
|
157
|
+
yarn add @johnmorrisdotca/sugoroku
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
A page with no bundler loads the board as a tag from a CDN, naming the major version so that a release that changes what you use is one you choose:
|
|
161
|
+
|
|
162
|
+
```html
|
|
163
|
+
<script type="module" src="https://cdn.jsdelivr.net/npm/@johnmorrisdotca/sugoroku@1/dist/element-define.js"></script>
|
|
164
|
+
```
|
|
165
|
+
|
|
76
166
|
Sugoroku is three things, each usable without the others: **the rules** (a position, a game, a match, text records, dice and the computer, as plain functions), **the drawing** (SVG text), and **the page** (a mounted board or a tag). The table at the end of [API](#api) says which entry holds which.
|
|
77
167
|
|
|
78
168
|
### 1. The API alone, on a server
|
|
79
169
|
|
|
80
170
|
A server that wants to be sure what was played takes the record the board keeps and replays it:
|
|
81
171
|
|
|
82
|
-
```ts
|
|
172
|
+
```ts no-check
|
|
83
173
|
import { replayRecord } from "@johnmorrisdotca/sugoroku";
|
|
84
174
|
|
|
85
175
|
const replayed = replayRecord(textFromTheBrowser);
|
|
@@ -145,7 +235,7 @@ import "@johnmorrisdotca/sugoroku/element/define";
|
|
|
145
235
|
<sugoroku-board bind:this={board} variant="backgammon" points="5" cube="on" gammons="on" black="strong"></sugoroku-board>
|
|
146
236
|
```
|
|
147
237
|
|
|
148
|
-
```ts
|
|
238
|
+
```ts no-check
|
|
149
239
|
// Angular: a standalone component with CUSTOM_ELEMENTS_SCHEMA
|
|
150
240
|
import { Component, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
|
|
151
241
|
import "@johnmorrisdotca/sugoroku/element/define";
|
|
@@ -171,6 +261,205 @@ These recipes are written to the tag's documented attributes and events; they ar
|
|
|
171
261
|
- **No dependencies.** ES modules, an entry per concern, and `sideEffects` set so that only the define entry has an effect.
|
|
172
262
|
- **Where it runs.** See [Browser support](#browser-support).
|
|
173
263
|
|
|
264
|
+
The cookbook, with the output of each example, is under [Examples](#examples).
|
|
265
|
+
|
|
266
|
+
## Examples
|
|
267
|
+
|
|
268
|
+
Every TypeScript and JavaScript block that can run is type-checked against the built package and run by `pnpm test:readme`, so the output after `// →` is what the code prints. The rules need no page and no network, so most of these run under Node.
|
|
269
|
+
|
|
270
|
+
### A page with nothing else
|
|
271
|
+
|
|
272
|
+
Save this as a file and open it: one script and one tag. The board is the whole game, a match against the computer, played by touch or mouse:
|
|
273
|
+
|
|
274
|
+
```html
|
|
275
|
+
<!doctype html>
|
|
276
|
+
<meta charset="utf-8">
|
|
277
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
278
|
+
<title>Backgammon</title>
|
|
279
|
+
<script type="module" src="https://cdn.jsdelivr.net/npm/@johnmorrisdotca/sugoroku@1/dist/element-define.js"></script>
|
|
280
|
+
<sugoroku-board variant="nackgammon" points="5" cube gammons black="strong" seed="2026"></sugoroku-board>
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### Every play of a roll
|
|
284
|
+
|
|
285
|
+
`legalPlaysOf(game)` lists every different play of the dice in hand, so a board never has to know a rule; `formatPlay` writes one in standard notation. Here, after the opening throw of a 3 and a 2:
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
import { formatPlay, legalPlaysOf, newGame, openingFrom, rollOpening, seededDice, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
289
|
+
|
|
290
|
+
const dice = seededDice(2026); // the same dice in every browser and every Node
|
|
291
|
+
let game = newGame(settingsFor("backgammon"));
|
|
292
|
+
game = rollOpening(game, openingFrom(dice)); // a die each: the higher starts
|
|
293
|
+
console.log(game.turn, game.need); // → white [ 3, 2 ]
|
|
294
|
+
const plays = legalPlaysOf(game);
|
|
295
|
+
console.log(plays.length); // → 17
|
|
296
|
+
console.log(plays.slice(0, 3).map((play) => formatPlay(play.moves))); // → [ '24/21 24/22', '24/21 13/11', '24/21 8/6' ]
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### The computer, at four strengths
|
|
300
|
+
|
|
301
|
+
The four strengths are written for this package, and none of it is GPL. The same position, the same roll, and what each would play:
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { choosePlay, formatPlay, newGame, openingFrom, rollOpening, seededDice, settingsFor, STRENGTHS } from "@johnmorrisdotca/sugoroku";
|
|
305
|
+
|
|
306
|
+
let game = newGame(settingsFor("backgammon"));
|
|
307
|
+
game = rollOpening(game, openingFrom(seededDice(2026)));
|
|
308
|
+
for (const strength of STRENGTHS) console.log(strength, formatPlay(choosePlay(game, { strength, random: () => 0.5 }).moves));
|
|
309
|
+
// → random 13/10 6/4
|
|
310
|
+
// → greedy 13/10/8
|
|
311
|
+
// → careful 13/10/8
|
|
312
|
+
// → strong 13/10/8
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### Does the computer want the cube?
|
|
316
|
+
|
|
317
|
+
`wantsToDouble` and `chanceOfWinning` are the computer's cube: where the sides are in contact, it doubles at 70 percent. At the start of a game it does not:
|
|
318
|
+
|
|
319
|
+
```ts
|
|
320
|
+
import { chanceOfWinning, newGame, openingFrom, rollOpening, seededDice, settingsFor, wantsToDouble } from "@johnmorrisdotca/sugoroku";
|
|
321
|
+
|
|
322
|
+
let game = newGame(settingsFor("backgammon", { points: 3, cube: true }));
|
|
323
|
+
game = rollOpening(game, openingFrom(seededDice(2026)));
|
|
324
|
+
console.log(wantsToDouble(game, { strength: "strong" })); // → false
|
|
325
|
+
console.log(chanceOfWinning(game, "white").toFixed(2)); // → 0.56
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Write a record, and replay it on a server
|
|
329
|
+
|
|
330
|
+
A server that wants to be sure what was played takes the record the board keeps and replays it against the rules. `GameRecorder` writes one as a game is played; `replayRecord` checks every move, every double and every score, and the dice against the seed:
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
import { choosePlay, GameRecorder, newGame, openingFrom, replayRecord, rollOpening, seededDice, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
334
|
+
|
|
335
|
+
const settings = settingsFor("backgammon", { points: 3, cube: true, gammons: true });
|
|
336
|
+
const throws = openingFrom(seededDice(2026));
|
|
337
|
+
const game = rollOpening(newGame(settings), throws);
|
|
338
|
+
|
|
339
|
+
const recorder = new GameRecorder(settings, 2026);
|
|
340
|
+
recorder.startGame();
|
|
341
|
+
recorder.opening(throws);
|
|
342
|
+
recorder.turn(game.turn!, game.need, choosePlay(game, { strength: "careful", random: () => 0.5 }).moves);
|
|
343
|
+
const record = recorder.text();
|
|
344
|
+
console.log(record.split("\n").slice(-4, -1).join(" | ")); // → game 1 | open 3 2 | white 32: 13/10/8
|
|
345
|
+
const replayed = replayRecord(record);
|
|
346
|
+
console.log(replayed.ok, replayed.ok && replayed.current?.phase); // → true before-roll
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### Refuse a record that was changed
|
|
350
|
+
|
|
351
|
+
A move the rules do not allow, or an opening throw that is not the seed's, is found at its line:
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
import { replayRecord } from "@johnmorrisdotca/sugoroku";
|
|
355
|
+
|
|
356
|
+
const record = `sugoroku 1
|
|
357
|
+
variant backgammon
|
|
358
|
+
points 3
|
|
359
|
+
cube on
|
|
360
|
+
crawford on
|
|
361
|
+
jacoby off
|
|
362
|
+
beaver off
|
|
363
|
+
gammons on
|
|
364
|
+
seed 2026
|
|
365
|
+
game 1
|
|
366
|
+
open 3 2
|
|
367
|
+
white 32: 13/10/8
|
|
368
|
+
`;
|
|
369
|
+
const illegal = replayRecord(record.replace("13/10/8", "24/20 13/11"));
|
|
370
|
+
console.log(illegal.ok, !illegal.ok && illegal.line, !illegal.ok && illegal.reason);
|
|
371
|
+
// → false 12 "24/20 13/11" is not a legal play of 32
|
|
372
|
+
const changed = replayRecord(record.replace("open 3 2", "open 6 1"));
|
|
373
|
+
console.log(changed.ok, !changed.ok && changed.reason); // → false the opening throw is not the seed's
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### Notation, and a position ID
|
|
377
|
+
|
|
378
|
+
Plays read and write as standard notation (`24/18 13/11`, `bar/22*`, `6/off`), and the standard board's position has the 14-character ID the GNU Backgammon manual gives it:
|
|
379
|
+
|
|
380
|
+
```ts
|
|
381
|
+
import { formatPlay, newGame, openingFrom, parsePlay, positionId, rollOpening, seededDice, settingsFor, VARIANTS } from "@johnmorrisdotca/sugoroku";
|
|
382
|
+
|
|
383
|
+
let game = newGame(settingsFor("backgammon"));
|
|
384
|
+
console.log(positionId(game.position, "white")); // → 4HPwATDgc/ABMA
|
|
385
|
+
game = rollOpening(game, openingFrom(seededDice(2026)));
|
|
386
|
+
const play = parsePlay(VARIANTS.backgammon, game.position, game.turn!, [3, 2], "24/21 13/11");
|
|
387
|
+
console.log(play && formatPlay(play)); // → 24/21 13/11
|
|
388
|
+
console.log(parsePlay(VARIANTS.backgammon, game.position, game.turn!, [3, 2], "24/20 13/11")); // → null: not a play of 3 and 2
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
### The names of the two old sites
|
|
392
|
+
|
|
393
|
+
The names that ItsYourTurn and GoldToken printed are `PRESETS`, each a variant with its rules:
|
|
394
|
+
|
|
395
|
+
```ts
|
|
396
|
+
import { PRESETS, presetByName, settingsFor, VARIANT_KEYS, VARIANTS } from "@johnmorrisdotca/sugoroku";
|
|
397
|
+
|
|
398
|
+
console.log(VARIANT_KEYS.length, PRESETS.length); // → 7 22
|
|
399
|
+
const preset = presetByName("Pro Backgammon-9");
|
|
400
|
+
console.log(preset?.key, preset?.variant, preset?.rules.points); // → backgammon-9 backgammon 9
|
|
401
|
+
console.log(VARIANTS.tabula.dice); // → 3
|
|
402
|
+
console.log(settingsFor("hypergammon", { points: 3, cube: true, gammons: true }).rules.points); // → 3
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### The board as an image
|
|
406
|
+
|
|
407
|
+
`drawSugoroku` returns SVG text: a server that only checks a game never loads it, and a page can put it anywhere an image goes:
|
|
408
|
+
|
|
409
|
+
```ts
|
|
410
|
+
import { drawSugoroku } from "@johnmorrisdotca/sugoroku/draw";
|
|
411
|
+
import { newGame, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
412
|
+
|
|
413
|
+
const game = newGame(settingsFor("backgammon"));
|
|
414
|
+
const svg = drawSugoroku(game.position, { variant: "backgammon", numbers: true, board: "wood", style: true });
|
|
415
|
+
console.log(svg.startsWith("<svg"), svg.includes("class=\"sugoroku\"")); // → true true
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### Mount a board, and listen
|
|
419
|
+
|
|
420
|
+
`mountSugoroku` is the tag as a function call; it keeps the match, the cube, undo and a record for you, and says what happens as events:
|
|
421
|
+
|
|
422
|
+
```ts no-run
|
|
423
|
+
import { mountSugoroku } from "@johnmorrisdotca/sugoroku/play";
|
|
424
|
+
|
|
425
|
+
const element = document.getElementById("board")!;
|
|
426
|
+
const board = mountSugoroku(element, {
|
|
427
|
+
variant: "nackgammon",
|
|
428
|
+
rules: { points: 5, cube: true, gammons: true },
|
|
429
|
+
computer: { black: "strong" }, // either side, or both, or neither
|
|
430
|
+
seed: 2026, // the dice from a seed; left out, random
|
|
431
|
+
sound: true,
|
|
432
|
+
});
|
|
433
|
+
element.addEventListener("sugoroku-turn", (event) => console.log((event as CustomEvent).detail.notation));
|
|
434
|
+
element.addEventListener("sugoroku-end", () => fetch("/matches", { method: "POST", body: board.record() }));
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
### Dice from your own server
|
|
438
|
+
|
|
439
|
+
With `dice: "host"` the board asks the page for its dice instead of rolling, so a server can be the one that throws them:
|
|
440
|
+
|
|
441
|
+
```ts no-run
|
|
442
|
+
import { mountSugoroku } from "@johnmorrisdotca/sugoroku/play";
|
|
443
|
+
|
|
444
|
+
const element = document.getElementById("board")!;
|
|
445
|
+
const board = mountSugoroku(element, { variant: "backgammon", dice: "host" });
|
|
446
|
+
element.addEventListener("sugoroku-need-dice", async () => {
|
|
447
|
+
const { dice } = await (await fetch("/roll")).json();
|
|
448
|
+
board.roll(dice); // for example [3, 1]
|
|
449
|
+
});
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
### A look of your own
|
|
453
|
+
|
|
454
|
+
Every colour is a CSS variable on `.sugoroku`, set by the page (the table is under [Theming](#theming)); a named board (`board: "wood"`) and a set of checkers (`checkers: "contrast"`) come with the package:
|
|
455
|
+
|
|
456
|
+
```css
|
|
457
|
+
sugoroku-board .sugoroku {
|
|
458
|
+
--sg-felt: #1d4e5f;
|
|
459
|
+
--sg-frame: #4a2f1b;
|
|
460
|
+
}
|
|
461
|
+
```
|
|
462
|
+
|
|
174
463
|
## The variants
|
|
175
464
|
|
|
176
465
|
Each is a row of settings: how many checkers, where they start, how many dice, how doubles
|
|
@@ -319,7 +608,7 @@ sides are still in contact (`careful` and `strong` only; `greedy` takes and neve
|
|
|
319
608
|
there) the evaluation becomes a chance of winning (`winChance`) and it doubles at 70% and
|
|
320
609
|
takes at 25%. It never beavers, and never doubles at Tabula or in a game played to lose.
|
|
321
610
|
|
|
322
|
-
```ts
|
|
611
|
+
```ts no-check
|
|
323
612
|
import { choosePlay, computerAction, stepComputer, wantsToDouble } from "@johnmorrisdotca/sugoroku";
|
|
324
613
|
|
|
325
614
|
choosePlay(game, { strength: "careful" }); // { moves, position }: the rest of the turn, chosen
|
|
@@ -329,7 +618,7 @@ stepComputer(game, dice); // does it, with dice from a so
|
|
|
329
618
|
|
|
330
619
|
## Drawing
|
|
331
620
|
|
|
332
|
-
```ts
|
|
621
|
+
```ts no-check
|
|
333
622
|
import { drawSugoroku, SUGOROKU_STYLE } from "@johnmorrisdotca/sugoroku/draw";
|
|
334
623
|
|
|
335
624
|
const svg = drawSugoroku(game.position, {
|
|
@@ -363,7 +652,7 @@ drawing, so it stands alone as an image).
|
|
|
363
652
|
|
|
364
653
|
## Playing in a page
|
|
365
654
|
|
|
366
|
-
```ts
|
|
655
|
+
```ts no-check
|
|
367
656
|
import { mountSugoroku } from "@johnmorrisdotca/sugoroku/play";
|
|
368
657
|
|
|
369
658
|
const board = mountSugoroku(element, {
|
|
@@ -509,6 +798,19 @@ All of these are held by tests, and the ones with a name are exported.
|
|
|
509
798
|
|
|
510
799
|
The look-ahead is held to a time budget, not to a count of moves: a position of hundreds of plays, such as a double, stays well under a tenth of a second.
|
|
511
800
|
|
|
801
|
+
## Accessibility
|
|
802
|
+
|
|
803
|
+
Backgammon is played by sight and by touch, so what a person who cannot see the board hears, and what a person who cannot drag can do, matter. What is done, and what is not, in the mounted board and the tag:
|
|
804
|
+
|
|
805
|
+
- **Tap or drag.** A checker is picked up with a tap and put down with a tap on a point (the places it may go light up), or dragged; no move needs a drag, and nothing needs two fingers or a double tap. A turn may be undone move by move until Done.
|
|
806
|
+
- **The board says what is happening.** A line under the board says whose turn it is, what the dice are, and what is wanted next ("Your roll.", "Roll the dice: the higher die goes first."), and the score and the cube are a line of text above it. The words are English or Japanese by the page's `lang`.
|
|
807
|
+
- **Buttons are big.** The three buttons are one size in the same places whatever they say, and each is at least 48 pixels high.
|
|
808
|
+
- **Not by colour alone.** The two sides differ in lightness (cream and dark checkers), the cube and the dice show numbers, the checker picked up and the points it may go to are marked by a ring and a mark, and every point can carry its number (`numbers: true`).
|
|
809
|
+
- **Motion.** Nothing on the board animates. Under a request for reduced motion the computer answers at once instead of after a pause of 450 milliseconds, and the board is steady in size whatever is on it, so a page does not shift as a game goes on.
|
|
810
|
+
- **Contrast.** There are five boards and four sets of checkers, one of them `contrast`, and every colour is a CSS variable.
|
|
811
|
+
- **Without a pointer.** The engine decides every legal play (`legalPlaysOf`) and writes it in notation (`formatPlay`), so a host can offer the plays as a list or a text box; the board's own points and checkers are not yet reachable by keyboard, and a screen reader hears the board as a drawing with a status line beside it.
|
|
812
|
+
- **Known to fall short.** No keyboard play on the mounted board; the Japanese words have not been read by a native reader ([Languages](#languages)).
|
|
813
|
+
|
|
512
814
|
## Browser support
|
|
513
815
|
|
|
514
816
|
Any browser with ES2020 modules, custom elements, Pointer Events, `ResizeObserver` and CSS `color-mix`: Chrome and Edge 111, Safari 16.2, Firefox 113, all from early 2023. The element draws in the page's own DOM, with no shadow DOM. The demo is played in a real Chromium at a phone's width (with touch) and a desk's, and in WebKit, Safari's engine, at a phone's width; Firefox is not in that run. The package itself (the rules, the formats and the computer) needs no DOM: it runs in Node 22 or later (CI tests 22 and 24). Deno and Bun are not tested. The sounds need `Audio` and are off unless asked for.
|
|
@@ -638,13 +940,17 @@ Sugoroku is one of twenty-four packages, each made for the same site, each at
|
|
|
638
940
|
## Development
|
|
639
941
|
|
|
640
942
|
```sh
|
|
641
|
-
pnpm install
|
|
943
|
+
pnpm install --frozen-lockfile
|
|
642
944
|
pnpm check # lint, types and every test
|
|
643
945
|
pnpm test:package # pack, install and import it as somebody who installed it would
|
|
644
946
|
pnpm site # build the demo into site/, as the Pages workflow publishes it
|
|
645
947
|
pnpm test:demo # play the demo in Chromium and WebKit
|
|
948
|
+
pnpm test:readme # every TypeScript and JavaScript example in this README, type-checked and run
|
|
949
|
+
pnpm screenshots:readme # retake the README's pictures into docs/images (builds the demo first)
|
|
646
950
|
```
|
|
647
951
|
|
|
952
|
+
The pictures are taken on the maintainer's Mac and are retaken only when the look changes; they are in `docs/images` and are not in the package that npm installs.
|
|
953
|
+
|
|
648
954
|
## Contributing
|
|
649
955
|
|
|
650
956
|
See [CONTRIBUTING.md](./CONTRIBUTING.md). The commands are under [Development](#development).
|
|
@@ -655,6 +961,8 @@ Please follow the [code of conduct](./CODE_OF_CONDUCT.md). A record or a positio
|
|
|
655
961
|
|
|
656
962
|
See [CHANGELOG.md](./CHANGELOG.md).
|
|
657
963
|
|
|
964
|
+
The latest release is 1.0.2: the README takes the family's full layout, with pictures of the variants and examples that are run.
|
|
965
|
+
|
|
658
966
|
## Licence
|
|
659
967
|
|
|
660
968
|
MIT, © John Morris. The board is drawn in code. The sounds are from [Casino Audio by
|
package/dist/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** The package's version. */
|
|
2
|
-
export declare const VERSION = "1.0.
|
|
2
|
+
export declare const VERSION = "1.0.2";
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** The package's version. */
|
|
2
|
-
export const VERSION = "1.0.
|
|
2
|
+
export const VERSION = "1.0.2";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnmorrisdotca/sugoroku",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Sugoroku, backgammon and its variants for JavaScript and TypeScript: the rules, the doubling cube, match play, Long Gammon, Nackgammon and more, replayable games as text, a computer player, and an SVG board for any page. Zero dependencies.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"backgammon",
|
|
@@ -89,8 +89,9 @@
|
|
|
89
89
|
"prepublishOnly": "pnpm check && pnpm build",
|
|
90
90
|
"site": "pnpm build && node scripts/site.mjs",
|
|
91
91
|
"test:demo": "pnpm site && playwright test",
|
|
92
|
-
"
|
|
93
|
-
"
|
|
92
|
+
"docs:make": "node scripts/docs.mjs",
|
|
93
|
+
"screenshots:readme": "pnpm site && node scripts/readme-pictures.mjs",
|
|
94
|
+
"test:readme": "pnpm build && node scripts/check-readme-examples.mjs"
|
|
94
95
|
},
|
|
95
96
|
"devDependencies": {
|
|
96
97
|
"@eslint/js": "^9.0.0",
|