@johnmorrisdotca/sugoroku 1.0.1 → 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.
- package/CHANGELOG.md +32 -0
- package/README.md +395 -77
- package/dist/board.d.ts +216 -12
- package/dist/board.js +204 -12
- package/dist/colours.d.ts +80 -8
- package/dist/colours.js +51 -6
- package/dist/computer.d.ts +138 -8
- package/dist/computer.js +109 -7
- package/dist/dice.d.ts +53 -4
- package/dist/dice.js +42 -3
- package/dist/draw.d.ts +43 -3
- package/dist/draw.js +33 -2
- package/dist/element-define.d.ts +1 -1
- package/dist/element-define.js +4 -13
- package/dist/element.d.ts +233 -3
- package/dist/element.js +402 -48
- package/dist/evaluate.d.ts +76 -4
- package/dist/evaluate.js +65 -3
- package/dist/game.d.ts +408 -23
- package/dist/game.js +328 -18
- package/dist/geometry.d.ts +50 -3
- package/dist/geometry.js +20 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/match.d.ts +66 -4
- package/dist/match.js +55 -4
- package/dist/mount.d.ts +75 -4
- package/dist/mount.js +25 -3
- package/dist/moves.d.ts +48 -1
- package/dist/moves.js +35 -1
- package/dist/notation.d.ts +92 -4
- package/dist/notation.js +82 -3
- package/dist/playStyle.d.ts +6 -0
- package/dist/playStyle.js +6 -0
- package/dist/plays.d.ts +108 -3
- package/dist/plays.js +96 -3
- package/dist/positionId.d.ts +29 -1
- package/dist/positionId.js +29 -1
- package/dist/random.d.ts +33 -3
- package/dist/random.js +22 -2
- package/dist/record.d.ts +150 -7
- package/dist/record.js +94 -5
- package/dist/rules.d.ts +130 -10
- package/dist/rules.js +99 -9
- package/dist/side.d.ts +38 -3
- package/dist/side.js +30 -3
- package/dist/sound.d.ts +61 -3
- package/dist/sound.js +27 -2
- package/dist/strings.d.ts +42 -2
- package/dist/strings.js +34 -2
- package/dist/style.d.ts +6 -0
- package/dist/style.js +6 -0
- package/dist/variants.d.ts +113 -8
- package/dist/variants.js +62 -5
- package/dist/version.d.ts +9 -2
- package/dist/version.js +9 -2
- package/package.json +27 -19
package/dist/notation.js
CHANGED
|
@@ -10,11 +10,38 @@ import { sideIndex } from "./side.js";
|
|
|
10
10
|
* chain of points; and two checkers making the same move are `13/11(2)`.
|
|
11
11
|
*/
|
|
12
12
|
const point = (n) => (n === BAR ? "bar" : n === 0 ? "off" : String(n));
|
|
13
|
-
/** One move as text: `24/18`, `bar/22*`, `6/off`.
|
|
13
|
+
/** One move as text: `24/18`, `bar/22*`, `6/off`.
|
|
14
|
+
*
|
|
15
|
+
* @param move - A native legal move, including die and hit status.
|
|
16
|
+
* @returns One move as text: `24/18`, `bar/22*`, `6/off`.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { formatMove, legalPlaysOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
20
|
+
*
|
|
21
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
22
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
23
|
+
* const play = legalPlaysOf(game)[0];
|
|
24
|
+
* const move = play.moves[0];
|
|
25
|
+
* console.log(formatMove(move));
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
14
28
|
export function formatMove(move) {
|
|
15
29
|
return `${point(move.from)}/${point(move.to)}${move.hit ? "*" : ""}`;
|
|
16
30
|
}
|
|
17
|
-
/** The text of a whole turn: moves of one checker joined, equal moves counted, the farthest-back checker first. A turn of no moves is `-`.
|
|
31
|
+
/** The text of a whole turn: moves of one checker joined, equal moves counted, the farthest-back checker first. A turn of no moves is `-`.
|
|
32
|
+
*
|
|
33
|
+
* @param moves - Legal checker moves in their played order.
|
|
34
|
+
* @returns The text of a whole turn: moves of one checker joined, equal moves counted, the farthest-back checker first. A turn of no moves is `-`.
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { formatPlay, legalPlaysOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
38
|
+
*
|
|
39
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
40
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
41
|
+
* const play = legalPlaysOf(game)[0];
|
|
42
|
+
* console.log(formatPlay(play.moves));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
18
45
|
export function formatPlay(moves) {
|
|
19
46
|
if (moves.length === 0)
|
|
20
47
|
return "-";
|
|
@@ -47,6 +74,15 @@ export function formatPlay(moves) {
|
|
|
47
74
|
* The hops a written play names, in the order written: a chain of three points
|
|
48
75
|
* is two hops, and `13/11(2)` is two. Returns null for text that is not a play. `-`, `pass`,
|
|
49
76
|
* and the empty text are the play of no moves.
|
|
77
|
+
*
|
|
78
|
+
* @param text - Native move notation, position text or full record specified by the signature.
|
|
79
|
+
* @returns The hops a written play names, in the order written: a chain of three points is two hops, and `13/11(2)` is two. Returns null for text that is not a play. `-`, `pass`, and the empty text are the play of no moves.
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { parseHops } from "@johnmorrisdotca/sugoroku";
|
|
83
|
+
*
|
|
84
|
+
* console.log(parseHops("8/5 6/5"));
|
|
85
|
+
* ```
|
|
50
86
|
*/
|
|
51
87
|
export function parseHops(text) {
|
|
52
88
|
const trimmed = text.trim().toLowerCase();
|
|
@@ -80,6 +116,24 @@ export function parseHops(text) {
|
|
|
80
116
|
* in some order, each one legal when it is made, until the roll is played as
|
|
81
117
|
* the rules require. A checker that plays both dice may be written as one move,
|
|
82
118
|
* `24/14` for a 6 and a 4, as well as through the point it passes.
|
|
119
|
+
*
|
|
120
|
+
* @param spec - The native variant specification.
|
|
121
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
122
|
+
* @param side - The side being examined: white or black.
|
|
123
|
+
* @param roll - The valid dice as thrown, in throw order.
|
|
124
|
+
* @param text - Native move notation, position text or full record specified by the signature.
|
|
125
|
+
* @returns The moves a written play makes with a roll, or null if it is not a legal play of that roll. Checks every rule: it is found by making each named move in some order, each one legal when it is made, until the roll is played as the rules require. A checker that plays both dice may be written as one move, `24/14` for a 6 and a 4, as well as through the point it passes.
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* import { formatPlay, legalPlaysOf, newGame, parsePlay, rollOpening, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
129
|
+
*
|
|
130
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
131
|
+
* const spec = settings.variant;
|
|
132
|
+
* const position = startPosition(spec);
|
|
133
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
134
|
+
* const play = legalPlaysOf(game)[0];
|
|
135
|
+
* console.log(parsePlay(spec,position,"white",[3,1],formatPlay(play.moves)));
|
|
136
|
+
* ```
|
|
83
137
|
*/
|
|
84
138
|
export function parsePlay(spec, position, side, roll, text) {
|
|
85
139
|
const hops = parseHops(text);
|
|
@@ -129,11 +183,36 @@ const formatCounts = (counts) => {
|
|
|
129
183
|
* enter and those borne off, `[white, black]` each.
|
|
130
184
|
*
|
|
131
185
|
* white=24:2,13:5,8:3,6:5 black=24:2,13:5,8:3,6:5 bar=0,0 reserve=0,0 off=0,0
|
|
186
|
+
*
|
|
187
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
188
|
+
* @returns A position as text, for any variant: each side's checkers as `point:count` in that side's own numbering, highest point first, then the bar, those yet to enter and those borne off, `[white, black]` each.
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* import { formatPosition, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
192
|
+
*
|
|
193
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
194
|
+
* const spec = settings.variant;
|
|
195
|
+
* const position = startPosition(spec);
|
|
196
|
+
* console.log(formatPosition(position));
|
|
197
|
+
* ```
|
|
132
198
|
*/
|
|
133
199
|
export function formatPosition(position) {
|
|
134
200
|
return `white=${formatCounts(position.points[0])} black=${formatCounts(position.points[1])} bar=${position.bar.join(",")} reserve=${position.reserve.join(",")} off=${position.off.join(",")}`;
|
|
135
201
|
}
|
|
136
|
-
/** The position that text of `formatPosition`'s kind stands for, or null if it is not one. `bar`, `reserve` and `off` may be left out.
|
|
202
|
+
/** The position that text of `formatPosition`'s kind stands for, or null if it is not one. `bar`, `reserve` and `off` may be left out.
|
|
203
|
+
*
|
|
204
|
+
* @param text - Native move notation, position text or full record specified by the signature.
|
|
205
|
+
* @returns The position that text of `formatPosition`'s kind stands for, or null if it is not one. `bar`, `reserve` and `off` may be left out.
|
|
206
|
+
* @example
|
|
207
|
+
* ```ts
|
|
208
|
+
* import { formatPosition, parsePosition, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
209
|
+
*
|
|
210
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
211
|
+
* const spec = settings.variant;
|
|
212
|
+
* const position = startPosition(spec);
|
|
213
|
+
* console.log(parsePosition(formatPosition(position))?.bar);
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
137
216
|
export function parsePosition(text) {
|
|
138
217
|
const fields = new Map();
|
|
139
218
|
for (const token of text.trim().split(/\s+/)) {
|
package/dist/playStyle.d.ts
CHANGED
|
@@ -9,5 +9,11 @@
|
|
|
9
9
|
* (set from the drawing's own proportions), the line of words keeps room for
|
|
10
10
|
* two lines, and the buttons are three of one size in the same places whatever
|
|
11
11
|
* they say. Nothing the player touches can be selected.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { SUGOROKU_PLAY_STYLE } from "@johnmorrisdotca/sugoroku/play";
|
|
16
|
+
* console.log(SUGOROKU_PLAY_STYLE.length);
|
|
17
|
+
* ```
|
|
12
18
|
*/
|
|
13
19
|
export declare const SUGOROKU_PLAY_STYLE = "\n.sugoroku {\n --sg-frame: #5b3a1f; --sg-felt: var(--felt, #2f5d4a); --sg-point-a: #e7d8b1; --sg-point-b: #8e3a2b; --sg-bar: #4a2f19; --sg-tray: #24493a;\n --sg-white: #f6f0df; --sg-white-edge: #b3a888; --sg-black: #2b2724; --sg-black-edge: #0d0b0a;\n --sg-number: #e8dcc0; --sg-selected: #ffd23f; --sg-target: #7fe3a1; --sg-die: #fbf8f1; --sg-pip: #1f2320; --sg-cube: #fbf8f1; --sg-cube-ink: #1f2320;\n display: block; width: 100%; height: auto; overflow: visible;\n user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: manipulation; -webkit-tap-highlight-color: transparent;\n font-family: system-ui, -apple-system, \"Segoe UI\", sans-serif;\n}\n@media (prefers-color-scheme: dark) {\n :root:not([data-theme=\"light\"]) .sugoroku:not([data-theme=\"light\"]) {\n --sg-frame: #2b1c0e; --sg-felt: var(--felt, #1f4135); --sg-point-a: #bdad88; --sg-point-b: #6b2a1f; --sg-bar: #21160b; --sg-tray: #173026;\n --sg-white: #e9e3d0; --sg-white-edge: #9a9078; --sg-number: #b9ae92; --sg-die: #e9e3d0; --sg-cube: #e9e3d0;\n }\n}\n:root[data-theme=\"dark\"] .sugoroku:not([data-theme=\"light\"]), .sugoroku[data-theme=\"dark\"] {\n --sg-frame: #2b1c0e; --sg-felt: var(--felt, #1f4135); --sg-point-a: #bdad88; --sg-point-b: #6b2a1f; --sg-bar: #21160b; --sg-tray: #173026;\n --sg-white: #e9e3d0; --sg-white-edge: #9a9078; --sg-number: #b9ae92; --sg-die: #e9e3d0; --sg-cube: #e9e3d0;\n}\n.sugoroku * { user-select: none; -webkit-user-select: none; }\n.sugoroku .sg-frame { fill: var(--sg-frame); }\n.sugoroku .sg-felt { fill: var(--sg-felt); }\n.sugoroku .sg-rail, .sugoroku .sg-tray-well { fill: var(--sg-tray); }\n.sugoroku .sg-bar { fill: var(--sg-bar); }\n.sugoroku .sg-point[data-tone=\"a\"] .sg-triangle { fill: var(--sg-point-a); }\n.sugoroku .sg-point[data-tone=\"b\"] .sg-triangle { fill: var(--sg-point-b); }\n.sugoroku .sg-hit { fill: transparent; }\n.sugoroku .sg-bar-hit { fill: transparent; }\n.sugoroku .sg-tray { fill: rgba(0, 0, 0, .18); stroke: rgba(0, 0, 0, .25); stroke-width: 1; }\n.sugoroku .sg-number { fill: var(--sg-number); font-size: 11px; font-weight: 600; pointer-events: none; }\n.sugoroku .sg-checker { pointer-events: none; }\n.sugoroku .sg-checker[data-side=\"white\"] .sg-face { fill: var(--sg-white); stroke: var(--sg-white-edge); stroke-width: 2; }\n.sugoroku .sg-checker[data-side=\"black\"] .sg-face { fill: var(--sg-black); stroke: var(--sg-black-edge); stroke-width: 2; }\n.sugoroku .sg-checker[data-side=\"white\"] .sg-ring { fill: none; stroke: var(--sg-white-edge); stroke-width: 1.5; opacity: .7; }\n.sugoroku .sg-checker[data-side=\"black\"] .sg-ring { fill: none; stroke: var(--sg-black-edge); stroke-width: 1.5; opacity: .9; }\n.sugoroku .sg-gleam { fill: #fff; opacity: .16; }\n.sugoroku .sg-checker[data-side=\"white\"] .sg-count { fill: var(--sg-pip); }\n.sugoroku .sg-checker[data-side=\"black\"] .sg-count { fill: var(--sg-white); }\n.sugoroku .sg-count { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; pointer-events: none; }\n.sugoroku .sg-off[data-side=\"white\"] { fill: var(--sg-white); stroke: var(--sg-white-edge); stroke-width: 1.5; pointer-events: none; }\n.sugoroku .sg-off[data-side=\"black\"] { fill: var(--sg-black); stroke: var(--sg-black-edge); stroke-width: 1.5; pointer-events: none; }\n.sugoroku .sg-die { pointer-events: none; }\n.sugoroku .sg-die rect { fill: var(--sg-die); stroke: rgba(0, 0, 0, .35); stroke-width: 1.5; }\n.sugoroku .sg-die[data-spent=\"true\"] { opacity: .38; }\n.sugoroku .sg-pip { fill: var(--sg-pip); }\n.sugoroku .sg-cube { pointer-events: none; }\n.sugoroku .sg-cube rect { fill: var(--sg-cube); stroke: rgba(0, 0, 0, .35); stroke-width: 1.5; }\n.sugoroku .sg-cube-text { fill: var(--sg-cube-ink); font-size: 22px; font-weight: 800; pointer-events: none; }\n.sugoroku .sg-point[data-movable=\"true\"] .sg-triangle { stroke: rgba(255, 255, 255, .55); stroke-width: 3; stroke-linejoin: round; }\n.sugoroku .sg-bar-hit[data-movable=\"true\"] { fill: rgba(255, 255, 255, .14); }\n.sugoroku .sg-point[data-selected=\"true\"] .sg-triangle { stroke: var(--sg-selected); stroke-width: 4; stroke-linejoin: round; }\n.sugoroku .sg-point[data-target=\"true\"] .sg-triangle { stroke: var(--sg-target); stroke-width: 4; stroke-linejoin: round; }\n.sugoroku .sg-bar-hit[data-selected=\"true\"] { fill: var(--sg-selected); opacity: .35; }\n.sugoroku .sg-tray[data-target=\"true\"] { stroke: var(--sg-target); stroke-width: 4; fill: rgba(127, 227, 161, .18); }\n.sugoroku .sg-point[data-target=\"true\"], .sugoroku .sg-tray[data-target=\"true\"] { cursor: pointer; }\n\n.sugoroku-play {\n --sgp-ink: #1f2320; --sgp-muted: #6b6f68; --sgp-rule: #ddd6c6; --sgp-surface: #fbf8f1; --sgp-accent: #b5452c; --sgp-good: #2f7a4f;\n display: block; max-width: 100%; box-sizing: border-box; color: var(--sgp-ink); font-family: system-ui, -apple-system, \"Segoe UI\", sans-serif;\n user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;\n}\n@media (prefers-color-scheme: dark) {\n :root:not([data-theme=\"light\"]) .sugoroku-play { --sgp-ink: #ece8dc; --sgp-muted: #a09d93; --sgp-rule: #3a3d38; --sgp-surface: #1d201e; --sgp-accent: #ff8a6b; --sgp-good: #6fcf97; }\n}\n:root[data-theme=\"dark\"] .sugoroku-play { --sgp-ink: #ece8dc; --sgp-muted: #a09d93; --sgp-rule: #3a3d38; --sgp-surface: #1d201e; --sgp-accent: #ff8a6b; --sgp-good: #6fcf97; }\n.sugoroku-play *, .sugoroku-play *::before, .sugoroku-play *::after { box-sizing: border-box; }\n.sugoroku-play .sgp-board { position: relative; width: 100%; touch-action: none; user-select: none; -webkit-user-select: none; cursor: default; }\n.sugoroku-play[data-drag=\"off\"] .sgp-board { touch-action: manipulation; }\n.sugoroku-play .sgp-board svg { display: block; width: 100%; height: 100%; }\n.sugoroku-play .sgp-score { margin: 0 0 8px; font-size: .8rem; color: var(--sgp-muted); min-height: 2.7em; font-variant-numeric: tabular-nums; }\n.sugoroku-play .sgp-status { margin: 10px 0 0; min-height: 2.9em; font-size: .92rem; font-weight: 600; line-height: 1.4; }\n.sugoroku-play[data-over=\"true\"] .sgp-status { color: var(--sgp-good); }\n.sugoroku-play .sgp-controls { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 10px; }\n.sugoroku-play .sgp-controls[hidden], .sugoroku-play [hidden] { display: none !important; }\n.sugoroku-play button { font: inherit; color: inherit; user-select: none; -webkit-user-select: none; touch-action: manipulation; }\n.sugoroku-play .sgp-button { border: 1px solid var(--sgp-rule); background: var(--sgp-surface); color: var(--sgp-ink); border-radius: 999px; min-height: 48px; padding: 0 12px; font-size: .88rem; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; text-align: center; line-height: 1.2; cursor: pointer; }\n.sugoroku-play .sgp-button:hover:not(:disabled) { border-color: var(--sgp-ink); }\n.sugoroku-play .sgp-button:disabled { opacity: .34; cursor: default; }\n.sugoroku-play .sgp-button[data-primary=\"true\"]:not(:disabled) { background: var(--sgp-ink); color: var(--sgp-surface); border-color: var(--sgp-ink); }\n.sugoroku-play .sgp-button[data-attention=\"true\"]:not(:disabled) { box-shadow: 0 0 0 3px color-mix(in srgb, var(--sgp-accent) 45%, transparent); }\n.sugoroku-play .sgp-ghost { position: fixed; z-index: 10; pointer-events: none; border-radius: 50%; border: 2px solid rgba(0, 0, 0, .55); box-shadow: 0 4px 10px rgba(0, 0, 0, .4); left: 0; top: 0; }\n";
|
package/dist/playStyle.js
CHANGED
|
@@ -10,6 +10,12 @@ import { SUGOROKU_STYLE } from "./style.js";
|
|
|
10
10
|
* (set from the drawing's own proportions), the line of words keeps room for
|
|
11
11
|
* two lines, and the buttons are three of one size in the same places whatever
|
|
12
12
|
* they say. Nothing the player touches can be selected.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { SUGOROKU_PLAY_STYLE } from "@johnmorrisdotca/sugoroku/play";
|
|
17
|
+
* console.log(SUGOROKU_PLAY_STYLE.length);
|
|
18
|
+
* ```
|
|
13
19
|
*/
|
|
14
20
|
export const SUGOROKU_PLAY_STYLE = `${SUGOROKU_STYLE}
|
|
15
21
|
.sugoroku-play {
|
package/dist/plays.d.ts
CHANGED
|
@@ -5,12 +5,37 @@ import type { VariantSpec } from "./variants.ts";
|
|
|
5
5
|
/**
|
|
6
6
|
* A WHOLE TURN: the moves a side makes with a roll, in the order it makes them,
|
|
7
7
|
* and the position they leave.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { legalPlaysOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
12
|
+
* import type { Play } from "@johnmorrisdotca/sugoroku";
|
|
13
|
+
*
|
|
14
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
15
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
16
|
+
* const play = legalPlaysOf(game)[0];
|
|
17
|
+
* const value: Play = play;
|
|
18
|
+
* console.log(value);
|
|
19
|
+
* ```
|
|
8
20
|
*/
|
|
9
21
|
export type Play = {
|
|
10
22
|
readonly moves: readonly Move[];
|
|
11
23
|
readonly position: Position;
|
|
12
24
|
};
|
|
13
|
-
/** The dice a roll gives to play, highest first: a double of a game that plays doubles four times is four of them.
|
|
25
|
+
/** The dice a roll gives to play, highest first: a double of a game that plays doubles four times is four of them.
|
|
26
|
+
*
|
|
27
|
+
* @param spec - The native variant specification.
|
|
28
|
+
* @param roll - The valid dice as thrown, in throw order.
|
|
29
|
+
* @returns The dice a roll gives to play, highest first: a double of a game that plays doubles four times is four of them.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { diceToPlay, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
33
|
+
*
|
|
34
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
35
|
+
* const spec = settings.variant;
|
|
36
|
+
* console.log(diceToPlay(spec,[3,3]));
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
14
39
|
export declare function diceToPlay(spec: VariantSpec, roll: readonly number[]): number[];
|
|
15
40
|
/**
|
|
16
41
|
* WHICH DICE MUST BE PLAYED. A side must play as many of its dice as it can;
|
|
@@ -18,22 +43,102 @@ export declare function diceToPlay(spec: VariantSpec, roll: readonly number[]):
|
|
|
18
43
|
* it must play the higher: with two dice, if only one can be played, it is the
|
|
19
44
|
* larger one when it can. Returned highest first, from the dice as
|
|
20
45
|
* `diceToPlay` gives them.
|
|
46
|
+
*
|
|
47
|
+
* @param spec - The native variant specification.
|
|
48
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
49
|
+
* @param side - The side being examined: white or black.
|
|
50
|
+
* @param dice - Dice values as thrown, or a reproducible source specified by the signature.
|
|
51
|
+
* @returns WHICH DICE MUST BE PLAYED. A side must play as many of its dice as it can; where it cannot play them all and could play either of two (or more) sets, it must play the higher: with two dice, if only one can be played, it is the larger one when it can. Returned highest first, from the dice as `diceToPlay` gives them.
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { diceMustPlay, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
55
|
+
*
|
|
56
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
57
|
+
* const spec = settings.variant;
|
|
58
|
+
* const position = startPosition(spec);
|
|
59
|
+
* console.log(diceMustPlay(spec,position,"white",[3,1]));
|
|
60
|
+
* ```
|
|
21
61
|
*/
|
|
22
62
|
export declare function diceMustPlay(spec: VariantSpec, position: Position, side: Side, dice: readonly number[]): number[];
|
|
23
|
-
/** Whether every die of `need` (highest first) can be played from `position`, in some order.
|
|
63
|
+
/** Whether every die of `need` (highest first) can be played from `position`, in some order.
|
|
64
|
+
*
|
|
65
|
+
* @param spec - The native variant specification.
|
|
66
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
67
|
+
* @param side - The side being examined: white or black.
|
|
68
|
+
* @param need - The dice remaining to play.
|
|
69
|
+
* @param failed - Optional failed-position cache for the recursive legal-play search.
|
|
70
|
+
* @returns Whether every die of `need` (highest first) can be played from `position`, in some order.
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* import { canPlayAll, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
74
|
+
*
|
|
75
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
76
|
+
* const spec = settings.variant;
|
|
77
|
+
* const position = startPosition(spec);
|
|
78
|
+
* console.log(canPlayAll(spec,position,"white",[3,1]));
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
24
81
|
export declare function canPlayAll(spec: VariantSpec, position: Position, side: Side, need: readonly number[], failed?: Set<string>): boolean;
|
|
25
82
|
/**
|
|
26
83
|
* The moves that may be made next, by a side that still has the dice `need`
|
|
27
84
|
* (highest first) to play from `position`: every move that leaves the rest
|
|
28
85
|
* of `need` playable. Where one checker could be borne off by either of two
|
|
29
86
|
* dice, both moves are offered.
|
|
87
|
+
*
|
|
88
|
+
* @param spec - The native variant specification.
|
|
89
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
90
|
+
* @param side - The side being examined: white or black.
|
|
91
|
+
* @param need - The dice remaining to play.
|
|
92
|
+
* @returns The moves that may be made next, by a side that still has the dice `need` (highest first) to play from `position`: every move that leaves the rest of `need` playable. Where one checker could be borne off by either of two dice, both moves are offered.
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { nextMoves, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
96
|
+
*
|
|
97
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
98
|
+
* const spec = settings.variant;
|
|
99
|
+
* const position = startPosition(spec);
|
|
100
|
+
* console.log(nextMoves(spec,position,"white",[3,1]));
|
|
101
|
+
* ```
|
|
30
102
|
*/
|
|
31
103
|
export declare function nextMoves(spec: VariantSpec, position: Position, side: Side, need: readonly number[]): Move[];
|
|
32
104
|
/**
|
|
33
105
|
* Every different turn a side may make with a roll: one for each position
|
|
34
106
|
* that playing the roll as the rules require can leave, with one way of
|
|
35
107
|
* getting there. A side that cannot move has the one play of no moves.
|
|
108
|
+
*
|
|
109
|
+
* @param spec - The native variant specification.
|
|
110
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
111
|
+
* @param side - The side being examined: white or black.
|
|
112
|
+
* @param roll - The valid dice as thrown, in throw order.
|
|
113
|
+
* @returns Every different turn a side may make with a roll: one for each position that playing the roll as the rules require can leave, with one way of getting there. A side that cannot move has the one play of no moves.
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* import { legalPlays, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
117
|
+
*
|
|
118
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
119
|
+
* const spec = settings.variant;
|
|
120
|
+
* const position = startPosition(spec);
|
|
121
|
+
* console.log(legalPlays(spec,position,"white",[3,1]).length);
|
|
122
|
+
* ```
|
|
36
123
|
*/
|
|
37
124
|
export declare function legalPlays(spec: VariantSpec, position: Position, side: Side, roll: readonly number[]): Play[];
|
|
38
|
-
/** The position after a list of moves, made one after another.
|
|
125
|
+
/** The position after a list of moves, made one after another.
|
|
126
|
+
*
|
|
127
|
+
* @param spec - The native variant specification.
|
|
128
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
129
|
+
* @param side - The side being examined: white or black.
|
|
130
|
+
* @param moves - Legal checker moves in their played order.
|
|
131
|
+
* @returns The position after a list of moves, made one after another.
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* import { legalPlaysOf, newGame, playMoves, rollOpening, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
135
|
+
*
|
|
136
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
137
|
+
* const spec = settings.variant;
|
|
138
|
+
* const position = startPosition(spec);
|
|
139
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
140
|
+
* const play = legalPlaysOf(game)[0];
|
|
141
|
+
* console.log(playMoves(spec,position,"white",play.moves).bar);
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
39
144
|
export declare function playMoves(spec: VariantSpec, position: Position, side: Side, moves: readonly Move[]): Position;
|
package/dist/plays.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import { positionKey } from "./board.js";
|
|
2
2
|
import { applyMove, movesOfDie } from "./moves.js";
|
|
3
|
-
/** The dice a roll gives to play, highest first: a double of a game that plays doubles four times is four of them.
|
|
3
|
+
/** The dice a roll gives to play, highest first: a double of a game that plays doubles four times is four of them.
|
|
4
|
+
*
|
|
5
|
+
* @param spec - The native variant specification.
|
|
6
|
+
* @param roll - The valid dice as thrown, in throw order.
|
|
7
|
+
* @returns The dice a roll gives to play, highest first: a double of a game that plays doubles four times is four of them.
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { diceToPlay, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
11
|
+
*
|
|
12
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
13
|
+
* const spec = settings.variant;
|
|
14
|
+
* console.log(diceToPlay(spec,[3,3]));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
4
17
|
export function diceToPlay(spec, roll) {
|
|
5
18
|
const first = roll[0];
|
|
6
19
|
if (spec.doubles === "four" && roll.length === 2 && first !== undefined && roll[1] === first)
|
|
@@ -23,6 +36,21 @@ const without = (dice, index) => dice.filter((_, i) => i !== index);
|
|
|
23
36
|
* it must play the higher: with two dice, if only one can be played, it is the
|
|
24
37
|
* larger one when it can. Returned highest first, from the dice as
|
|
25
38
|
* `diceToPlay` gives them.
|
|
39
|
+
*
|
|
40
|
+
* @param spec - The native variant specification.
|
|
41
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
42
|
+
* @param side - The side being examined: white or black.
|
|
43
|
+
* @param dice - Dice values as thrown, or a reproducible source specified by the signature.
|
|
44
|
+
* @returns WHICH DICE MUST BE PLAYED. A side must play as many of its dice as it can; where it cannot play them all and could play either of two (or more) sets, it must play the higher: with two dice, if only one can be played, it is the larger one when it can. Returned highest first, from the dice as `diceToPlay` gives them.
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { diceMustPlay, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
48
|
+
*
|
|
49
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
50
|
+
* const spec = settings.variant;
|
|
51
|
+
* const position = startPosition(spec);
|
|
52
|
+
* console.log(diceMustPlay(spec,position,"white",[3,1]));
|
|
53
|
+
* ```
|
|
26
54
|
*/
|
|
27
55
|
export function diceMustPlay(spec, position, side, dice) {
|
|
28
56
|
const memo = new Map();
|
|
@@ -54,7 +82,24 @@ export function diceMustPlay(spec, position, side, dice) {
|
|
|
54
82
|
};
|
|
55
83
|
return best(position, dice);
|
|
56
84
|
}
|
|
57
|
-
/** Whether every die of `need` (highest first) can be played from `position`, in some order.
|
|
85
|
+
/** Whether every die of `need` (highest first) can be played from `position`, in some order.
|
|
86
|
+
*
|
|
87
|
+
* @param spec - The native variant specification.
|
|
88
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
89
|
+
* @param side - The side being examined: white or black.
|
|
90
|
+
* @param need - The dice remaining to play.
|
|
91
|
+
* @param failed - Optional failed-position cache for the recursive legal-play search.
|
|
92
|
+
* @returns Whether every die of `need` (highest first) can be played from `position`, in some order.
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { canPlayAll, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
96
|
+
*
|
|
97
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
98
|
+
* const spec = settings.variant;
|
|
99
|
+
* const position = startPosition(spec);
|
|
100
|
+
* console.log(canPlayAll(spec,position,"white",[3,1]));
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
58
103
|
export function canPlayAll(spec, position, side, need, failed = new Set()) {
|
|
59
104
|
const go = (at, left) => {
|
|
60
105
|
if (left.length === 0)
|
|
@@ -83,6 +128,21 @@ export function canPlayAll(spec, position, side, need, failed = new Set()) {
|
|
|
83
128
|
* (highest first) to play from `position`: every move that leaves the rest
|
|
84
129
|
* of `need` playable. Where one checker could be borne off by either of two
|
|
85
130
|
* dice, both moves are offered.
|
|
131
|
+
*
|
|
132
|
+
* @param spec - The native variant specification.
|
|
133
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
134
|
+
* @param side - The side being examined: white or black.
|
|
135
|
+
* @param need - The dice remaining to play.
|
|
136
|
+
* @returns The moves that may be made next, by a side that still has the dice `need` (highest first) to play from `position`: every move that leaves the rest of `need` playable. Where one checker could be borne off by either of two dice, both moves are offered.
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* import { nextMoves, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
140
|
+
*
|
|
141
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
142
|
+
* const spec = settings.variant;
|
|
143
|
+
* const position = startPosition(spec);
|
|
144
|
+
* console.log(nextMoves(spec,position,"white",[3,1]));
|
|
145
|
+
* ```
|
|
86
146
|
*/
|
|
87
147
|
export function nextMoves(spec, position, side, need) {
|
|
88
148
|
const moves = [];
|
|
@@ -104,6 +164,21 @@ export function nextMoves(spec, position, side, need) {
|
|
|
104
164
|
* Every different turn a side may make with a roll: one for each position
|
|
105
165
|
* that playing the roll as the rules require can leave, with one way of
|
|
106
166
|
* getting there. A side that cannot move has the one play of no moves.
|
|
167
|
+
*
|
|
168
|
+
* @param spec - The native variant specification.
|
|
169
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
170
|
+
* @param side - The side being examined: white or black.
|
|
171
|
+
* @param roll - The valid dice as thrown, in throw order.
|
|
172
|
+
* @returns Every different turn a side may make with a roll: one for each position that playing the roll as the rules require can leave, with one way of getting there. A side that cannot move has the one play of no moves.
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { legalPlays, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
176
|
+
*
|
|
177
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
178
|
+
* const spec = settings.variant;
|
|
179
|
+
* const position = startPosition(spec);
|
|
180
|
+
* console.log(legalPlays(spec,position,"white",[3,1]).length);
|
|
181
|
+
* ```
|
|
107
182
|
*/
|
|
108
183
|
export function legalPlays(spec, position, side, roll) {
|
|
109
184
|
const need = diceMustPlay(spec, position, side, diceToPlay(spec, roll));
|
|
@@ -138,7 +213,25 @@ export function legalPlays(spec, position, side, roll) {
|
|
|
138
213
|
go(position, need, []);
|
|
139
214
|
return [...found.values()];
|
|
140
215
|
}
|
|
141
|
-
/** The position after a list of moves, made one after another.
|
|
216
|
+
/** The position after a list of moves, made one after another.
|
|
217
|
+
*
|
|
218
|
+
* @param spec - The native variant specification.
|
|
219
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
220
|
+
* @param side - The side being examined: white or black.
|
|
221
|
+
* @param moves - Legal checker moves in their played order.
|
|
222
|
+
* @returns The position after a list of moves, made one after another.
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* import { legalPlaysOf, newGame, playMoves, rollOpening, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
226
|
+
*
|
|
227
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
228
|
+
* const spec = settings.variant;
|
|
229
|
+
* const position = startPosition(spec);
|
|
230
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
231
|
+
* const play = legalPlaysOf(game)[0];
|
|
232
|
+
* console.log(playMoves(spec,position,"white",play.moves).bar);
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
142
235
|
export function playMoves(spec, position, side, moves) {
|
|
143
236
|
return moves.reduce((at, move) => applyMove(spec, at, side, move), position);
|
|
144
237
|
}
|
package/dist/positionId.d.ts
CHANGED
|
@@ -1,11 +1,39 @@
|
|
|
1
1
|
import { type Position } from "./board.ts";
|
|
2
2
|
import { type Side } from "./side.ts";
|
|
3
|
-
/** The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
|
|
3
|
+
/** The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
|
|
4
|
+
*
|
|
5
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
6
|
+
* @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
|
|
7
|
+
* @returns The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { positionId, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
11
|
+
*
|
|
12
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
13
|
+
* const spec = settings.variant;
|
|
14
|
+
* const position = startPosition(spec);
|
|
15
|
+
* console.log(positionId(position,"white"));
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
4
18
|
export declare function positionId(position: Position, onRoll: Side): string | null;
|
|
5
19
|
/**
|
|
6
20
|
* The position an ID stands for, with `onRoll` to move, or null if the text is
|
|
7
21
|
* not an ID. The ID does not say how many checkers each side began with: pass
|
|
8
22
|
* the variant's (15 for the standard board), and what is not on the board is
|
|
9
23
|
* counted as borne off.
|
|
24
|
+
*
|
|
25
|
+
* @param id - A GNU-compatible position ID.
|
|
26
|
+
* @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
|
|
27
|
+
* @param checkers - The checker count expected for each side.
|
|
28
|
+
* @returns The position an ID stands for, with `onRoll` to move, or null if the text is not an ID. The ID does not say how many checkers each side began with: pass the variant's (15 for the standard board), and what is not on the board is counted as borne off.
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { positionFromId, positionId, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
32
|
+
*
|
|
33
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
34
|
+
* const spec = settings.variant;
|
|
35
|
+
* const position = startPosition(spec);
|
|
36
|
+
* console.log(positionFromId(positionId(position,"white")!,"white")?.bar);
|
|
37
|
+
* ```
|
|
10
38
|
*/
|
|
11
39
|
export declare function positionFromId(id: string, onRoll: Side, checkers?: number): Position | null;
|
package/dist/positionId.js
CHANGED
|
@@ -19,7 +19,21 @@ import { otherSide, sideIndex } from "./side.js";
|
|
|
19
19
|
* variants whose sides share one track: those give null.
|
|
20
20
|
*/
|
|
21
21
|
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
22
|
-
/** The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
|
|
22
|
+
/** The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
|
|
23
|
+
*
|
|
24
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
25
|
+
* @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
|
|
26
|
+
* @returns The ID of a position with `onRoll` to move, or null if it cannot be written in this form.
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { positionId, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
30
|
+
*
|
|
31
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
32
|
+
* const spec = settings.variant;
|
|
33
|
+
* const position = startPosition(spec);
|
|
34
|
+
* console.log(positionId(position,"white"));
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
23
37
|
export function positionId(position, onRoll) {
|
|
24
38
|
if (position.reserve[0] > 0 || position.reserve[1] > 0)
|
|
25
39
|
return null;
|
|
@@ -64,6 +78,20 @@ export function positionId(position, onRoll) {
|
|
|
64
78
|
* not an ID. The ID does not say how many checkers each side began with: pass
|
|
65
79
|
* the variant's (15 for the standard board), and what is not on the board is
|
|
66
80
|
* counted as borne off.
|
|
81
|
+
*
|
|
82
|
+
* @param id - A GNU-compatible position ID.
|
|
83
|
+
* @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
|
|
84
|
+
* @param checkers - The checker count expected for each side.
|
|
85
|
+
* @returns The position an ID stands for, with `onRoll` to move, or null if the text is not an ID. The ID does not say how many checkers each side began with: pass the variant's (15 for the standard board), and what is not on the board is counted as borne off.
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* import { positionFromId, positionId, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
89
|
+
*
|
|
90
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
91
|
+
* const spec = settings.variant;
|
|
92
|
+
* const position = startPosition(spec);
|
|
93
|
+
* console.log(positionFromId(positionId(position,"white")!,"white")?.bar);
|
|
94
|
+
* ```
|
|
67
95
|
*/
|
|
68
96
|
export function positionFromId(id, onRoll, checkers = 15) {
|
|
69
97
|
if (!/^[A-Za-z0-9+/]{14}$/.test(id))
|
package/dist/random.d.ts
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
|
-
/** A source of random numbers from 0 up to but not including 1.
|
|
1
|
+
/** A source of random numbers from 0 up to but not including 1.
|
|
2
|
+
*
|
|
3
|
+
* @example
|
|
4
|
+
* ```ts
|
|
5
|
+
* import type { Random } from "@johnmorrisdotca/sugoroku";
|
|
6
|
+
* import { seededRandom } from "@johnmorrisdotca/sugoroku";
|
|
7
|
+
*
|
|
8
|
+
* const value: Random = seededRandom(42);
|
|
9
|
+
* console.log(value);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
2
12
|
export type Random = () => number;
|
|
3
|
-
/** A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
|
|
13
|
+
/** A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
|
|
14
|
+
*
|
|
15
|
+
* @param seed - A reproducible numeric or text seed.
|
|
16
|
+
* @returns A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { hashSeed } from "@johnmorrisdotca/sugoroku";
|
|
20
|
+
*
|
|
21
|
+
* console.log(hashSeed("sample"));
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
4
24
|
export declare function hashSeed(seed: number | string): number;
|
|
5
|
-
/** The mulberry32 stream: the same seed makes the same numbers everywhere.
|
|
25
|
+
/** The mulberry32 stream: the same seed makes the same numbers everywhere.
|
|
26
|
+
*
|
|
27
|
+
* @param seed - A reproducible numeric or text seed.
|
|
28
|
+
* @returns The mulberry32 stream: the same seed makes the same numbers everywhere.
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { seededRandom } from "@johnmorrisdotca/sugoroku";
|
|
32
|
+
*
|
|
33
|
+
* console.log(seededRandom(42)());
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
6
36
|
export declare function seededRandom(seed: number | string): Random;
|
package/dist/random.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
/** A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
|
|
1
|
+
/** A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
|
|
2
|
+
*
|
|
3
|
+
* @param seed - A reproducible numeric or text seed.
|
|
4
|
+
* @returns A whole number from a seed given as a number or as text: the same seed is always the same number, in every browser and every Node.
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { hashSeed } from "@johnmorrisdotca/sugoroku";
|
|
8
|
+
*
|
|
9
|
+
* console.log(hashSeed("sample"));
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
2
12
|
export function hashSeed(seed) {
|
|
3
13
|
if (typeof seed === "number")
|
|
4
14
|
return seed >>> 0;
|
|
@@ -9,7 +19,17 @@ export function hashSeed(seed) {
|
|
|
9
19
|
}
|
|
10
20
|
return hash >>> 0;
|
|
11
21
|
}
|
|
12
|
-
/** The mulberry32 stream: the same seed makes the same numbers everywhere.
|
|
22
|
+
/** The mulberry32 stream: the same seed makes the same numbers everywhere.
|
|
23
|
+
*
|
|
24
|
+
* @param seed - A reproducible numeric or text seed.
|
|
25
|
+
* @returns The mulberry32 stream: the same seed makes the same numbers everywhere.
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { seededRandom } from "@johnmorrisdotca/sugoroku";
|
|
29
|
+
*
|
|
30
|
+
* console.log(seededRandom(42)());
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
13
33
|
export function seededRandom(seed) {
|
|
14
34
|
let state = hashSeed(seed);
|
|
15
35
|
return () => {
|