@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.
Files changed (57) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +395 -77
  3. package/dist/board.d.ts +216 -12
  4. package/dist/board.js +204 -12
  5. package/dist/colours.d.ts +80 -8
  6. package/dist/colours.js +51 -6
  7. package/dist/computer.d.ts +138 -8
  8. package/dist/computer.js +109 -7
  9. package/dist/dice.d.ts +53 -4
  10. package/dist/dice.js +42 -3
  11. package/dist/draw.d.ts +43 -3
  12. package/dist/draw.js +33 -2
  13. package/dist/element-define.d.ts +1 -1
  14. package/dist/element-define.js +4 -13
  15. package/dist/element.d.ts +233 -3
  16. package/dist/element.js +402 -48
  17. package/dist/evaluate.d.ts +76 -4
  18. package/dist/evaluate.js +65 -3
  19. package/dist/game.d.ts +408 -23
  20. package/dist/game.js +328 -18
  21. package/dist/geometry.d.ts +50 -3
  22. package/dist/geometry.js +20 -1
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.js +1 -0
  25. package/dist/match.d.ts +66 -4
  26. package/dist/match.js +55 -4
  27. package/dist/mount.d.ts +75 -4
  28. package/dist/mount.js +25 -3
  29. package/dist/moves.d.ts +48 -1
  30. package/dist/moves.js +35 -1
  31. package/dist/notation.d.ts +92 -4
  32. package/dist/notation.js +82 -3
  33. package/dist/playStyle.d.ts +6 -0
  34. package/dist/playStyle.js +6 -0
  35. package/dist/plays.d.ts +108 -3
  36. package/dist/plays.js +96 -3
  37. package/dist/positionId.d.ts +29 -1
  38. package/dist/positionId.js +29 -1
  39. package/dist/random.d.ts +33 -3
  40. package/dist/random.js +22 -2
  41. package/dist/record.d.ts +150 -7
  42. package/dist/record.js +94 -5
  43. package/dist/rules.d.ts +130 -10
  44. package/dist/rules.js +99 -9
  45. package/dist/side.d.ts +38 -3
  46. package/dist/side.js +30 -3
  47. package/dist/sound.d.ts +61 -3
  48. package/dist/sound.js +27 -2
  49. package/dist/strings.d.ts +42 -2
  50. package/dist/strings.js +34 -2
  51. package/dist/style.d.ts +6 -0
  52. package/dist/style.js +6 -0
  53. package/dist/variants.d.ts +113 -8
  54. package/dist/variants.js +62 -5
  55. package/dist/version.d.ts +9 -2
  56. package/dist/version.js +9 -2
  57. package/package.json +27 -19
package/dist/record.d.ts CHANGED
@@ -35,22 +35,104 @@ import type { Position } from "./board.ts";
35
35
  * they are asked for. `result` is optional, and when it is there it must be
36
36
  * the result the replay arrives at.
37
37
  */
38
- /** The record format's version, the number after `sugoroku` on the first line. */
38
+ /** The record format's version, the number after `sugoroku` on the first line.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * import { RECORD_VERSION } from "@johnmorrisdotca/sugoroku";
43
+ * console.log(RECORD_VERSION);
44
+ * ```
45
+ */
39
46
  export declare const RECORD_VERSION = 1;
40
- /** A seed in a record: all digits means a number, anything else is text. */
47
+ /** A seed in a record: all digits means a number, anything else is text.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import type { RecordSeed } from "@johnmorrisdotca/sugoroku";
52
+ *
53
+ * const value: RecordSeed = "sample";
54
+ * console.log(value);
55
+ * ```
56
+ */
41
57
  export type RecordSeed = number | string;
42
- /** The header lines of a record: its version, the variant, the rules and the seed. */
58
+ /** The header lines of a record: its version, the variant, the rules and the seed.
59
+ *
60
+ * @param settings - The resolved variant and complete rules.
61
+ * @param seed - A reproducible numeric or text seed.
62
+ * @returns The header lines of a record: its version, the variant, the rules and the seed.
63
+ * @example
64
+ * ```ts
65
+ * import { formatRecordHeader, settingsFor } from "@johnmorrisdotca/sugoroku";
66
+ *
67
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
68
+ * console.log(formatRecordHeader(settings,42));
69
+ * ```
70
+ */
43
71
  export declare function formatRecordHeader(settings: Settings, seed?: RecordSeed): string[];
44
- /** One line of a game: the opening throw. */
72
+ /** One line of a game: the opening throw.
73
+ *
74
+ * @param throws - The opening die for white, then black.
75
+ * @returns One line of a game: the opening throw.
76
+ * @example
77
+ * ```ts
78
+ * import { formatOpening } from "@johnmorrisdotca/sugoroku";
79
+ *
80
+ * console.log(formatOpening([3,1]));
81
+ * ```
82
+ */
45
83
  export declare const formatOpening: (throws: readonly [number, number]) => string;
46
- /** One line of a game: a turn, with its dice and its play. */
84
+ /** One line of a game: a turn, with its dice and its play.
85
+ *
86
+ * @param side - The side being examined: white or black.
87
+ * @param dice - Dice values as thrown, or a reproducible source specified by the signature.
88
+ * @param moves - Legal checker moves in their played order.
89
+ * @returns One line of a game: a turn, with its dice and its play.
90
+ * @example
91
+ * ```ts
92
+ * import { formatTurn, legalPlaysOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
93
+ *
94
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
95
+ * const game = rollOpening(newGame(settings),[3,1]);
96
+ * const play = legalPlaysOf(game)[0];
97
+ * console.log(formatTurn("white",[3,1],play.moves));
98
+ * ```
99
+ */
47
100
  export declare const formatTurn: (side: Side, dice: readonly number[], moves: readonly Move[]) => string;
48
- /** The line that closes a game, with what it came to. */
101
+ /** The line that closes a game, with what it came to.
102
+ *
103
+ * @param result - The completed native game result to add to a match.
104
+ * @returns The line that closes a game, with what it came to.
105
+ * @example
106
+ * ```ts
107
+ * import { concede, formatResult, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
108
+ *
109
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
110
+ * const game = rollOpening(newGame(settings),[3,1]);
111
+ * const result = concede(game,"black","single").result!;
112
+ * console.log(formatResult(result));
113
+ * ```
114
+ */
49
115
  export declare function formatResult(result: GameResult): string;
50
116
  /**
51
117
  * A record written as a game is played. Call its methods as the game goes, in
52
118
  * the order things happen, and `text()` is the record so far; a game not yet
53
119
  * finished is a record that replays to a game still in play.
120
+ *
121
+ * @param settings - The resolved variant and complete rules to record.
122
+ * @param seed - Optional seed whose exact dice sequence replay must verify.
123
+ * @example
124
+ * ```ts
125
+ * import { GameRecorder, legalPlaysOf, newGame, rollOpening, settingsFor, startGame } from "@johnmorrisdotca/sugoroku";
126
+ *
127
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
128
+ * const game = rollOpening(newGame(settings),[3,1]);
129
+ * const play = legalPlaysOf(game)[0];
130
+ * const record = new GameRecorder(settings);
131
+ * record.startGame();
132
+ * record.opening([3,1]);
133
+ * record.turn("white",[3,1],play.moves);
134
+ * console.log(record.text());
135
+ * ```
54
136
  */
55
137
  export declare class GameRecorder {
56
138
  #private;
@@ -69,7 +151,22 @@ export declare class GameRecorder {
69
151
  /** The record, one line after another. */
70
152
  text(): string;
71
153
  }
72
- /** One game of a replayed record, finished. */
154
+ /** One game of a replayed record, finished.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * import { concede, newGame, rollOpening, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
159
+ * import type { ReplayedGame } from "@johnmorrisdotca/sugoroku";
160
+ *
161
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
162
+ * const spec = settings.variant;
163
+ * const position = startPosition(spec);
164
+ * const game = rollOpening(newGame(settings),[3,1]);
165
+ * const result = concede(game,"black","single").result!;
166
+ * const value: ReplayedGame = {result,turns:[1,0],position};
167
+ * console.log(value);
168
+ * ```
169
+ */
73
170
  export type ReplayedGame = {
74
171
  readonly result: GameResult;
75
172
  /** The turns each side took. */
@@ -77,6 +174,26 @@ export type ReplayedGame = {
77
174
  /** The position the game ended in. */
78
175
  readonly position: Position;
79
176
  };
177
+ /** A successfully validated record, its settings, completed games and optional current game.
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * import { GameRecorder, legalPlaysOf, newGame, replayRecord, rollOpening, settingsFor, startGame } from "@johnmorrisdotca/sugoroku";
182
+ * import type { ReplayOk } from "@johnmorrisdotca/sugoroku";
183
+ *
184
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
185
+ * const game = rollOpening(newGame(settings),[3,1]);
186
+ * const play = legalPlaysOf(game)[0];
187
+ * const record = new GameRecorder(settings);
188
+ * record.startGame();
189
+ * record.opening([3,1]);
190
+ * record.turn("white",[3,1],play.moves);
191
+ * const replay = replayRecord(record.text());
192
+ * if(!replay.ok) throw new Error(replay.reason);
193
+ * const value: ReplayOk = replay;
194
+ * console.log(value);
195
+ * ```
196
+ */
80
197
  export type ReplayOk = {
81
198
  readonly ok: true;
82
199
  readonly settings: Settings;
@@ -88,6 +205,16 @@ export type ReplayOk = {
88
205
  /** The game still in play when the record ends, or null if the last was finished. */
89
206
  readonly current: GameState | null;
90
207
  };
208
+ /** A refused record with its one-based line number and reason.
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * import type { ReplayError } from "@johnmorrisdotca/sugoroku";
213
+ *
214
+ * const value: ReplayError = {ok:false,line:1,reason:"Unknown record version"};
215
+ * console.log(value);
216
+ * ```
217
+ */
91
218
  export type ReplayError = {
92
219
  readonly ok: false;
93
220
  /** The line the record went wrong on, counting from 1. */
@@ -100,5 +227,21 @@ export type ReplayError = {
100
227
  * match's score are worked out, not read: a `result` line is only checked. Where
101
228
  * the record has a seed, the dice are checked to be the seed's. Gives the games
102
229
  * and the match, or the first line that goes wrong, and why.
230
+ *
231
+ * @param text - Native move notation, position text or full record specified by the signature.
232
+ * @returns Replay a record against the rules. Every move is checked to be legal when it was made, every double to have been allowed, and each game's result and the match's score are worked out, not read: a `result` line is only checked. Where the record has a seed, the dice are checked to be the seed's. Gives the games and the match, or the first line that goes wrong, and why.
233
+ * @example
234
+ * ```ts
235
+ * import { GameRecorder, legalPlaysOf, newGame, replayRecord, rollOpening, settingsFor, startGame } from "@johnmorrisdotca/sugoroku";
236
+ *
237
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
238
+ * const game = rollOpening(newGame(settings),[3,1]);
239
+ * const play = legalPlaysOf(game)[0];
240
+ * const record = new GameRecorder(settings);
241
+ * record.startGame();
242
+ * record.opening([3,1]);
243
+ * record.turn("white",[3,1],play.moves);
244
+ * console.log(replayRecord(record.text()).ok);
245
+ * ```
103
246
  */
104
247
  export declare function replayRecord(text: string): ReplayOk | ReplayError;
package/dist/record.js CHANGED
@@ -48,10 +48,29 @@ import { isVariantKey } from "./variants.js";
48
48
  * they are asked for. `result` is optional, and when it is there it must be
49
49
  * the result the replay arrives at.
50
50
  */
51
- /** The record format's version, the number after `sugoroku` on the first line. */
51
+ /** The record format's version, the number after `sugoroku` on the first line.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * import { RECORD_VERSION } from "@johnmorrisdotca/sugoroku";
56
+ * console.log(RECORD_VERSION);
57
+ * ```
58
+ */
52
59
  export const RECORD_VERSION = 1;
53
60
  const onOff = (value) => (value ? "on" : "off");
54
- /** The header lines of a record: its version, the variant, the rules and the seed. */
61
+ /** The header lines of a record: its version, the variant, the rules and the seed.
62
+ *
63
+ * @param settings - The resolved variant and complete rules.
64
+ * @param seed - A reproducible numeric or text seed.
65
+ * @returns The header lines of a record: its version, the variant, the rules and the seed.
66
+ * @example
67
+ * ```ts
68
+ * import { formatRecordHeader, settingsFor } from "@johnmorrisdotca/sugoroku";
69
+ *
70
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
71
+ * console.log(formatRecordHeader(settings,42));
72
+ * ```
73
+ */
55
74
  export function formatRecordHeader(settings, seed) {
56
75
  if (typeof seed === "string" && /^\d+$/.test(seed))
57
76
  throw new Error("A seed of digits is a number: pass it as a number.");
@@ -61,11 +80,49 @@ export function formatRecordHeader(settings, seed) {
61
80
  lines.push(`seed ${seed}`);
62
81
  return lines;
63
82
  }
64
- /** One line of a game: the opening throw. */
83
+ /** One line of a game: the opening throw.
84
+ *
85
+ * @param throws - The opening die for white, then black.
86
+ * @returns One line of a game: the opening throw.
87
+ * @example
88
+ * ```ts
89
+ * import { formatOpening } from "@johnmorrisdotca/sugoroku";
90
+ *
91
+ * console.log(formatOpening([3,1]));
92
+ * ```
93
+ */
65
94
  export const formatOpening = (throws) => `open ${throws[0]} ${throws[1]}`;
66
- /** One line of a game: a turn, with its dice and its play. */
95
+ /** One line of a game: a turn, with its dice and its play.
96
+ *
97
+ * @param side - The side being examined: white or black.
98
+ * @param dice - Dice values as thrown, or a reproducible source specified by the signature.
99
+ * @param moves - Legal checker moves in their played order.
100
+ * @returns One line of a game: a turn, with its dice and its play.
101
+ * @example
102
+ * ```ts
103
+ * import { formatTurn, legalPlaysOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
104
+ *
105
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
106
+ * const game = rollOpening(newGame(settings),[3,1]);
107
+ * const play = legalPlaysOf(game)[0];
108
+ * console.log(formatTurn("white",[3,1],play.moves));
109
+ * ```
110
+ */
67
111
  export const formatTurn = (side, dice, moves) => `${side} ${dice.join("")}: ${formatPlay(moves)}`;
68
- /** The line that closes a game, with what it came to. */
112
+ /** The line that closes a game, with what it came to.
113
+ *
114
+ * @param result - The completed native game result to add to a match.
115
+ * @returns The line that closes a game, with what it came to.
116
+ * @example
117
+ * ```ts
118
+ * import { concede, formatResult, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
119
+ *
120
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
121
+ * const game = rollOpening(newGame(settings),[3,1]);
122
+ * const result = concede(game,"black","single").result!;
123
+ * console.log(formatResult(result));
124
+ * ```
125
+ */
69
126
  export function formatResult(result) {
70
127
  return `result ${result.winner ?? "none"} ${result.how} ${result.kind} ${result.points}`;
71
128
  }
@@ -73,6 +130,22 @@ export function formatResult(result) {
73
130
  * A record written as a game is played. Call its methods as the game goes, in
74
131
  * the order things happen, and `text()` is the record so far; a game not yet
75
132
  * finished is a record that replays to a game still in play.
133
+ *
134
+ * @param settings - The resolved variant and complete rules to record.
135
+ * @param seed - Optional seed whose exact dice sequence replay must verify.
136
+ * @example
137
+ * ```ts
138
+ * import { GameRecorder, legalPlaysOf, newGame, rollOpening, settingsFor, startGame } from "@johnmorrisdotca/sugoroku";
139
+ *
140
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
141
+ * const game = rollOpening(newGame(settings),[3,1]);
142
+ * const play = legalPlaysOf(game)[0];
143
+ * const record = new GameRecorder(settings);
144
+ * record.startGame();
145
+ * record.opening([3,1]);
146
+ * record.turn("white",[3,1],play.moves);
147
+ * console.log(record.text());
148
+ * ```
76
149
  */
77
150
  export class GameRecorder {
78
151
  constructor(settings, seed) {
@@ -153,6 +226,22 @@ const SIDE = /^(white|black)\s+(.*)$/;
153
226
  * match's score are worked out, not read: a `result` line is only checked. Where
154
227
  * the record has a seed, the dice are checked to be the seed's. Gives the games
155
228
  * and the match, or the first line that goes wrong, and why.
229
+ *
230
+ * @param text - Native move notation, position text or full record specified by the signature.
231
+ * @returns Replay a record against the rules. Every move is checked to be legal when it was made, every double to have been allowed, and each game's result and the match's score are worked out, not read: a `result` line is only checked. Where the record has a seed, the dice are checked to be the seed's. Gives the games and the match, or the first line that goes wrong, and why.
232
+ * @example
233
+ * ```ts
234
+ * import { GameRecorder, legalPlaysOf, newGame, replayRecord, rollOpening, settingsFor, startGame } from "@johnmorrisdotca/sugoroku";
235
+ *
236
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
237
+ * const game = rollOpening(newGame(settings),[3,1]);
238
+ * const play = legalPlaysOf(game)[0];
239
+ * const record = new GameRecorder(settings);
240
+ * record.startGame();
241
+ * record.opening([3,1]);
242
+ * record.turn("white",[3,1],play.moves);
243
+ * console.log(replayRecord(record.text()).ok);
244
+ * ```
156
245
  */
157
246
  export function replayRecord(text) {
158
247
  const lines = text.split(/\r?\n/);
package/dist/rules.d.ts CHANGED
@@ -2,6 +2,16 @@ import { type VariantKey, type VariantSpec } from "./variants.ts";
2
2
  /**
3
3
  * HOW A GAME IS SCORED AND STAKED, beside the variant's board. A variant says
4
4
  * where the checkers stand and how they move; these say what a win is worth.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { settingsFor } from "@johnmorrisdotca/sugoroku";
9
+ * import type { Rules } from "@johnmorrisdotca/sugoroku";
10
+ *
11
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
12
+ * const value: Rules = settings.rules;
13
+ * console.log(value);
14
+ * ```
5
15
  */
6
16
  export type Rules = {
7
17
  /**
@@ -20,21 +30,82 @@ export type Rules = {
20
30
  /** Whether a gammon is worth two points and a backgammon three, instead of one. */
21
31
  gammons: boolean;
22
32
  };
23
- /** The rules a variant is played with when nothing else is asked: a single game with no cube and no gammons. */
33
+ /** The rules a variant is played with when nothing else is asked: a single game with no cube and no gammons.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { DEFAULT_RULES } from "@johnmorrisdotca/sugoroku";
38
+ * console.log(DEFAULT_RULES);
39
+ * ```
40
+ */
24
41
  export declare const DEFAULT_RULES: Rules;
25
- /** What goes wrong with a set of rules, as sentences; empty when they are fine. */
42
+ /** What goes wrong with a set of rules, as sentences; empty when they are fine.
43
+ *
44
+ * @param rules - The complete or partial native rules specified by the signature.
45
+ * @param variant - The variant key or resolved specification.
46
+ * @returns What goes wrong with a set of rules, as sentences; empty when they are fine.
47
+ * @example
48
+ * ```ts
49
+ * import { rulesProblems, settingsFor } from "@johnmorrisdotca/sugoroku";
50
+ *
51
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
52
+ * const spec = settings.variant;
53
+ * console.log(rulesProblems(settings.rules,spec));
54
+ * ```
55
+ */
26
56
  export declare function rulesProblems(rules: Rules, variant?: VariantSpec): string[];
27
- /** The rules with anything left out filled from `DEFAULT_RULES`. Throws if the result is not a set of rules that can be played. */
57
+ /** The rules with anything left out filled from `DEFAULT_RULES`. Throws if the result is not a set of rules that can be played.
58
+ *
59
+ * @param rules - The complete or partial native rules specified by the signature.
60
+ * @param variant - The variant key or resolved specification.
61
+ * @returns The rules with anything left out filled from `DEFAULT_RULES`. Throws if the result is not a set of rules that can be played.
62
+ * @example
63
+ * ```ts
64
+ * import { resolveRules, settingsFor } from "@johnmorrisdotca/sugoroku";
65
+ *
66
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
67
+ * const spec = settings.variant;
68
+ * console.log(resolveRules({points:5,cube:true},spec));
69
+ * ```
70
+ */
28
71
  export declare function resolveRules(rules?: Partial<Rules>, variant?: VariantSpec): Rules;
29
- /** Whether the cube is in play in a game of a match, given whether this is the Crawford game. */
72
+ /** Whether the cube is in play in a game of a match, given whether this is the Crawford game.
73
+ *
74
+ * @param rules - The complete or partial native rules specified by the signature.
75
+ * @param crawfordGame - Whether this is the match’s cube-free Crawford game.
76
+ * @returns Whether the cube is in play in a game of a match, given whether this is the Crawford game.
77
+ * @example
78
+ * ```ts
79
+ * import { cubeInPlay, settingsFor } from "@johnmorrisdotca/sugoroku";
80
+ *
81
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
82
+ * console.log(cubeInPlay(settings.rules,false));
83
+ * ```
84
+ */
30
85
  export declare function cubeInPlay(rules: Rules, crawfordGame: boolean): boolean;
31
- /** The most the cube can show. */
86
+ /** The most the cube can show.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * import { CUBE_LIMIT } from "@johnmorrisdotca/sugoroku";
91
+ * console.log(CUBE_LIMIT);
92
+ * ```
93
+ */
32
94
  export declare const CUBE_LIMIT = 64;
33
95
  /**
34
96
  * A NAMED WAY OF PLAYING: a variant with its rules. The names on the sites
35
97
  * these games were played on (ItsYourTurn.com and GoldToken.com) each lead to
36
98
  * one of these through `presetByName`, and `docs/VARIANTS.md` says how each
37
99
  * was read.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * import type { Preset } from "@johnmorrisdotca/sugoroku";
104
+ * import { PRESETS } from "@johnmorrisdotca/sugoroku";
105
+ *
106
+ * const value: Preset = PRESETS[0];
107
+ * console.log(value);
108
+ * ```
38
109
  */
39
110
  export type Preset = {
40
111
  /** In kebab case. */
@@ -44,16 +115,65 @@ export type Preset = {
44
115
  /** The names this goes by, as the sites print them. */
45
116
  names: readonly string[];
46
117
  };
47
- /** Every named way of playing, in the order of `docs/VARIANTS.md`. */
118
+ /** Every named way of playing, in the order of `docs/VARIANTS.md`.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * import { PRESETS } from "@johnmorrisdotca/sugoroku";
123
+ * console.log(PRESETS);
124
+ * ```
125
+ */
48
126
  export declare const PRESETS: readonly Preset[];
49
- /** The preset a site's printed name leads to, or undefined. Case and spacing around the name do not matter. */
127
+ /** The preset a site's printed name leads to, or undefined. Case and spacing around the name do not matter.
128
+ *
129
+ * @param name - The colour property, preset name or sound identifier specified by this signature.
130
+ * @returns The matching preset, or undefined for an unknown name.
131
+ * @example
132
+ * ```ts
133
+ * import { presetByName } from "@johnmorrisdotca/sugoroku";
134
+ *
135
+ * console.log(presetByName("backgammon")?.key);
136
+ * ```
137
+ */
50
138
  export declare function presetByName(name: string): Preset | undefined;
51
- /** The preset with this key, or undefined. */
139
+ /** The preset with this key, or undefined.
140
+ *
141
+ * @param key - The native variant, preset or localized-label key specified by the signature.
142
+ * @returns The matching preset, or undefined for an unknown key.
143
+ * @example
144
+ * ```ts
145
+ * import { PRESETS, presetByKey } from "@johnmorrisdotca/sugoroku";
146
+ *
147
+ * console.log(presetByKey(PRESETS[0].key)?.names);
148
+ * ```
149
+ */
52
150
  export declare function presetByKey(key: string): Preset | undefined;
53
- /** The settings of a game: the variant's row and the rules. What every game and match is made from. */
151
+ /** The settings of a game: the variant's row and the rules. What every game and match is made from.
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * import { settingsFor } from "@johnmorrisdotca/sugoroku";
156
+ * import type { Settings } from "@johnmorrisdotca/sugoroku";
157
+ *
158
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
159
+ * const value: Settings = settings;
160
+ * console.log(value);
161
+ * ```
162
+ */
54
163
  export type Settings = {
55
164
  variant: VariantSpec;
56
165
  rules: Rules;
57
166
  };
58
- /** Settings from a variant key (or a row) and rules, left-out rules filled in. */
167
+ /** Settings from a variant key (or a row) and rules, left-out rules filled in.
168
+ *
169
+ * @param variant - The variant key or resolved specification.
170
+ * @param rules - The complete or partial native rules specified by the signature.
171
+ * @returns Settings from a variant key (or a row) and rules, left-out rules filled in.
172
+ * @example
173
+ * ```ts
174
+ * import { settingsFor } from "@johnmorrisdotca/sugoroku";
175
+ *
176
+ * console.log(settingsFor("nackgammon",{points:5}).variant.key);
177
+ * ```
178
+ */
59
179
  export declare function settingsFor(variant: VariantKey | VariantSpec, rules?: Partial<Rules>): Settings;
package/dist/rules.js CHANGED
@@ -1,7 +1,27 @@
1
1
  import { isVariantKey, variantSpec } from "./variants.js";
2
- /** The rules a variant is played with when nothing else is asked: a single game with no cube and no gammons. */
2
+ /** The rules a variant is played with when nothing else is asked: a single game with no cube and no gammons.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { DEFAULT_RULES } from "@johnmorrisdotca/sugoroku";
7
+ * console.log(DEFAULT_RULES);
8
+ * ```
9
+ */
3
10
  export const DEFAULT_RULES = { points: 1, cube: false, crawford: true, jacoby: false, beaver: false, gammons: false };
4
- /** What goes wrong with a set of rules, as sentences; empty when they are fine. */
11
+ /** What goes wrong with a set of rules, as sentences; empty when they are fine.
12
+ *
13
+ * @param rules - The complete or partial native rules specified by the signature.
14
+ * @param variant - The variant key or resolved specification.
15
+ * @returns What goes wrong with a set of rules, as sentences; empty when they are fine.
16
+ * @example
17
+ * ```ts
18
+ * import { rulesProblems, settingsFor } from "@johnmorrisdotca/sugoroku";
19
+ *
20
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
21
+ * const spec = settings.variant;
22
+ * console.log(rulesProblems(settings.rules,spec));
23
+ * ```
24
+ */
5
25
  export function rulesProblems(rules, variant) {
6
26
  const problems = [];
7
27
  if (!Number.isInteger(rules.points) || rules.points < 0)
@@ -18,7 +38,20 @@ export function rulesProblems(rules, variant) {
18
38
  problems.push("a game played to lose has no cube and no gammons");
19
39
  return problems;
20
40
  }
21
- /** The rules with anything left out filled from `DEFAULT_RULES`. Throws if the result is not a set of rules that can be played. */
41
+ /** The rules with anything left out filled from `DEFAULT_RULES`. Throws if the result is not a set of rules that can be played.
42
+ *
43
+ * @param rules - The complete or partial native rules specified by the signature.
44
+ * @param variant - The variant key or resolved specification.
45
+ * @returns The rules with anything left out filled from `DEFAULT_RULES`. Throws if the result is not a set of rules that can be played.
46
+ * @example
47
+ * ```ts
48
+ * import { resolveRules, settingsFor } from "@johnmorrisdotca/sugoroku";
49
+ *
50
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
51
+ * const spec = settings.variant;
52
+ * console.log(resolveRules({points:5,cube:true},spec));
53
+ * ```
54
+ */
22
55
  export function resolveRules(rules = {}, variant) {
23
56
  const resolved = { ...DEFAULT_RULES, ...rules };
24
57
  const problems = rulesProblems(resolved, variant);
@@ -26,15 +59,41 @@ export function resolveRules(rules = {}, variant) {
26
59
  throw new Error(`These rules cannot be played: ${problems.join("; ")}.`);
27
60
  return resolved;
28
61
  }
29
- /** Whether the cube is in play in a game of a match, given whether this is the Crawford game. */
62
+ /** Whether the cube is in play in a game of a match, given whether this is the Crawford game.
63
+ *
64
+ * @param rules - The complete or partial native rules specified by the signature.
65
+ * @param crawfordGame - Whether this is the match’s cube-free Crawford game.
66
+ * @returns Whether the cube is in play in a game of a match, given whether this is the Crawford game.
67
+ * @example
68
+ * ```ts
69
+ * import { cubeInPlay, settingsFor } from "@johnmorrisdotca/sugoroku";
70
+ *
71
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
72
+ * console.log(cubeInPlay(settings.rules,false));
73
+ * ```
74
+ */
30
75
  export function cubeInPlay(rules, crawfordGame) {
31
76
  return rules.cube && rules.points !== 1 && !crawfordGame;
32
77
  }
33
- /** The most the cube can show. */
78
+ /** The most the cube can show.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * import { CUBE_LIMIT } from "@johnmorrisdotca/sugoroku";
83
+ * console.log(CUBE_LIMIT);
84
+ * ```
85
+ */
34
86
  export const CUBE_LIMIT = 64;
35
87
  const MATCH = (points) => ({ points, cube: true, crawford: true, jacoby: false, beaver: false, gammons: true });
36
88
  const SINGLE = DEFAULT_RULES;
37
- /** Every named way of playing, in the order of `docs/VARIANTS.md`. */
89
+ /** Every named way of playing, in the order of `docs/VARIANTS.md`.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * import { PRESETS } from "@johnmorrisdotca/sugoroku";
94
+ * console.log(PRESETS);
95
+ * ```
96
+ */
38
97
  export const PRESETS = [
39
98
  { key: "backgammon", variant: "backgammon", rules: SINGLE, names: ["Backgammon", "Backgammon Level 2", "Backgammon Level 3", "Casual Backgammon"] },
40
99
  { key: "backgammon-3", variant: "backgammon", rules: MATCH(3), names: ["Backgammon (3 Point)"] },
@@ -59,16 +118,47 @@ export const PRESETS = [
59
118
  { key: "hypergammon-5", variant: "hypergammon", rules: MATCH(5), names: ["Hypergammon (5 Point)"] },
60
119
  { key: "tabula", variant: "tabula", rules: SINGLE, names: ["Tabula"] },
61
120
  ];
62
- /** The preset a site's printed name leads to, or undefined. Case and spacing around the name do not matter. */
121
+ /** The preset a site's printed name leads to, or undefined. Case and spacing around the name do not matter.
122
+ *
123
+ * @param name - The colour property, preset name or sound identifier specified by this signature.
124
+ * @returns The matching preset, or undefined for an unknown name.
125
+ * @example
126
+ * ```ts
127
+ * import { presetByName } from "@johnmorrisdotca/sugoroku";
128
+ *
129
+ * console.log(presetByName("backgammon")?.key);
130
+ * ```
131
+ */
63
132
  export function presetByName(name) {
64
133
  const wanted = name.trim().toLowerCase();
65
134
  return PRESETS.find((preset) => preset.names.some((one) => one.toLowerCase() === wanted));
66
135
  }
67
- /** The preset with this key, or undefined. */
136
+ /** The preset with this key, or undefined.
137
+ *
138
+ * @param key - The native variant, preset or localized-label key specified by the signature.
139
+ * @returns The matching preset, or undefined for an unknown key.
140
+ * @example
141
+ * ```ts
142
+ * import { PRESETS, presetByKey } from "@johnmorrisdotca/sugoroku";
143
+ *
144
+ * console.log(presetByKey(PRESETS[0].key)?.names);
145
+ * ```
146
+ */
68
147
  export function presetByKey(key) {
69
148
  return PRESETS.find((preset) => preset.key === key);
70
149
  }
71
- /** Settings from a variant key (or a row) and rules, left-out rules filled in. */
150
+ /** Settings from a variant key (or a row) and rules, left-out rules filled in.
151
+ *
152
+ * @param variant - The variant key or resolved specification.
153
+ * @param rules - The complete or partial native rules specified by the signature.
154
+ * @returns Settings from a variant key (or a row) and rules, left-out rules filled in.
155
+ * @example
156
+ * ```ts
157
+ * import { settingsFor } from "@johnmorrisdotca/sugoroku";
158
+ *
159
+ * console.log(settingsFor("nackgammon",{points:5}).variant.key);
160
+ * ```
161
+ */
72
162
  export function settingsFor(variant, rules = {}) {
73
163
  if (typeof variant === "string" && !isVariantKey(variant))
74
164
  throw new Error(`${variant} is not a variant`);