@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/colours.js CHANGED
@@ -1,4 +1,11 @@
1
- /** The names of the colours, as the custom properties spell them (`--sg-` and the name in kebab case). */
1
+ /** The names of the colours, as the custom properties spell them (`--sg-` and the name in kebab case).
2
+ *
3
+ * @example
4
+ * ```ts
5
+ * import { SUGOROKU_COLOUR_NAMES } from "@johnmorrisdotca/sugoroku/draw";
6
+ * console.log(SUGOROKU_COLOUR_NAMES);
7
+ * ```
8
+ */
2
9
  export const SUGOROKU_COLOUR_NAMES = [
3
10
  "frame",
4
11
  "felt",
@@ -18,12 +25,29 @@ export const SUGOROKU_COLOUR_NAMES = [
18
25
  "cube",
19
26
  "cubeInk",
20
27
  ];
21
- /** The custom property a colour is set on. */
28
+ /** The custom property a colour is set on.
29
+ *
30
+ * @param name - The colour property, preset name or sound identifier specified by this signature.
31
+ * @returns The custom property a colour is set on.
32
+ * @example
33
+ * ```ts
34
+ * import { colourProperty } from "@johnmorrisdotca/sugoroku/draw";
35
+ *
36
+ * console.log(colourProperty("pointA"));
37
+ * ```
38
+ */
22
39
  export function colourProperty(name) {
23
40
  return `--sg-${name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`;
24
41
  }
25
42
  const POINTS_CLASSIC = { pointA: "#e7d8b1", pointB: "#8e3a2b" };
26
- /** The boards the drawing knows by name, set against any theme. Each changes only the surfaces: the frame, the felt, the points, the bar and the trays. */
43
+ /** The boards the drawing knows by name, set against any theme. Each changes only the surfaces: the frame, the felt, the points, the bar and the trays.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * import { SUGOROKU_BOARDS } from "@johnmorrisdotca/sugoroku/draw";
48
+ * console.log(SUGOROKU_BOARDS);
49
+ * ```
50
+ */
27
51
  export const SUGOROKU_BOARDS = {
28
52
  green: { frame: "#5b3a1f", felt: "#2f5d4a", ...POINTS_CLASSIC, bar: "#4a2f19", tray: "#24493a" },
29
53
  blue: { frame: "#2c3a52", felt: "#2865a6", pointA: "#dfe6ef", pointB: "#16345c", bar: "#1f2b40", tray: "#1d4d80" },
@@ -31,14 +55,35 @@ export const SUGOROKU_BOARDS = {
31
55
  black: { frame: "#1d1f22", felt: "#2f3236", pointA: "#d8d4c6", pointB: "#a3342e", bar: "#141618", tray: "#222427" },
32
56
  wood: { frame: "#6b4a2b", felt: "#e2ba7a", pointA: "#f3dfae", pointB: "#7a4a22", bar: "#6b4a2b", tray: "#c4954f" },
33
57
  };
34
- /** The names of the boards. */
58
+ /** The names of the boards.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * import { SUGOROKU_BOARD_NAMES } from "@johnmorrisdotca/sugoroku/draw";
63
+ * console.log(SUGOROKU_BOARD_NAMES);
64
+ * ```
65
+ */
35
66
  export const SUGOROKU_BOARD_NAMES = ["green", "blue", "red", "black", "wood"];
36
- /** The checkers the drawing knows by name. `contrast` is for readers who tell colours apart with difficulty: pale and near-black, with strong rims. */
67
+ /** The checkers the drawing knows by name. `contrast` is for readers who tell colours apart with difficulty: pale and near-black, with strong rims.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * import { SUGOROKU_CHECKER_SETS } from "@johnmorrisdotca/sugoroku/draw";
72
+ * console.log(SUGOROKU_CHECKER_SETS);
73
+ * ```
74
+ */
37
75
  export const SUGOROKU_CHECKER_SETS = {
38
76
  classic: { white: "#f6f0df", whiteEdge: "#b3a888", black: "#2b2724", blackEdge: "#0d0b0a" },
39
77
  "red-and-white": { white: "#f6f0df", whiteEdge: "#b3a888", black: "#b3332b", blackEdge: "#5d1712" },
40
78
  "gold-and-blue": { white: "#e8bf4a", whiteEdge: "#8d6a14", black: "#27508f", blackEdge: "#102446" },
41
79
  contrast: { white: "#ffffff", whiteEdge: "#000000", black: "#111111", blackEdge: "#ffffff" },
42
80
  };
43
- /** The names of the checker sets. */
81
+ /** The names of the checker sets.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * import { SUGOROKU_CHECKER_SET_NAMES } from "@johnmorrisdotca/sugoroku/draw";
86
+ * console.log(SUGOROKU_CHECKER_SET_NAMES);
87
+ * ```
88
+ */
44
89
  export const SUGOROKU_CHECKER_SET_NAMES = ["classic", "red-and-white", "gold-and-blue", "contrast"];
@@ -20,10 +20,36 @@ import { type Side } from "./side.ts";
20
20
  * take when the doubler's is 2 or more above the taker's. In a position where the sides are still in
21
21
  * contact (`careful` and `strong` only), the evaluation is turned into a
22
22
  * chance of winning (`winChance`) and a side doubles at 70% and takes at 25%.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import type { Strength } from "@johnmorrisdotca/sugoroku";
27
+ *
28
+ * const value: Strength = "greedy";
29
+ * console.log(value);
30
+ * ```
23
31
  */
24
32
  export type Strength = "random" | "greedy" | "careful" | "strong";
25
- /** The strengths, weakest first. */
33
+ /** The strengths, weakest first.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { STRENGTHS } from "@johnmorrisdotca/sugoroku";
38
+ * console.log(STRENGTHS);
39
+ * ```
40
+ */
26
41
  export declare const STRENGTHS: readonly Strength[];
42
+ /** Strength, tie-breaking randomness, lookahead time budget and evaluation weights for computer decisions.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import type { ComputerOptions } from "@johnmorrisdotca/sugoroku";
47
+ * import { seededRandom } from "@johnmorrisdotca/sugoroku";
48
+ *
49
+ * const value: ComputerOptions = {strength:"greedy",random:seededRandom(42)};
50
+ * console.log(value);
51
+ * ```
52
+ */
27
53
  export type ComputerOptions = {
28
54
  /** The strength. Default `strong`. */
29
55
  strength?: Strength;
@@ -38,15 +64,81 @@ export type ComputerOptions = {
38
64
  /** Weights for the evaluation, to play with a different taste. Default `EVALUATION_WEIGHTS`. */
39
65
  weights?: EvaluationWeights;
40
66
  };
41
- /** The play the computer makes for the rest of the turn of the side on turn, from the plays the rules leave it. */
67
+ /** The play the computer makes for the rest of the turn of the side on turn, from the plays the rules leave it.
68
+ *
69
+ * @param game - The current immutable native game state.
70
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
71
+ * @returns The play the computer makes for the rest of the turn of the side on turn, from the plays the rules leave it.
72
+ * @example
73
+ * ```ts
74
+ * import { choosePlay, newGame, rollOpening, seededRandom, settingsFor } from "@johnmorrisdotca/sugoroku";
75
+ *
76
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
77
+ * const game = rollOpening(newGame(settings),[3,1]);
78
+ * console.log(choosePlay(game,{strength:"greedy",random:seededRandom(42)}).moves);
79
+ * ```
80
+ */
42
81
  export declare function choosePlay(game: GameState, options?: ComputerOptions): Play;
43
- /** The chance, 0 to 1, that `side` wins from here, by the evaluation, or by the Keith count's rule of thumb in a race (as 0.5 plus a share of the count's lead). */
82
+ /** The chance, 0 to 1, that `side` wins from here, by the evaluation, or by the Keith count's rule of thumb in a race (as 0.5 plus a share of the count's lead).
83
+ *
84
+ * @param game - The current immutable native game state.
85
+ * @param side - The side being examined: white or black.
86
+ * @returns The chance, 0 to 1, that `side` wins from here, by the evaluation, or by the Keith count's rule of thumb in a race (as 0.5 plus a share of the count's lead).
87
+ * @example
88
+ * ```ts
89
+ * import { chanceOfWinning, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
90
+ *
91
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
92
+ * const game = rollOpening(newGame(settings),[3,1]);
93
+ * console.log(chanceOfWinning(game,"white"));
94
+ * ```
95
+ */
44
96
  export declare function chanceOfWinning(game: GameState, side: Side): number;
45
- /** Whether the computer, to move before it rolls, should offer a double. */
97
+ /** Whether the computer, to move before it rolls, should offer a double.
98
+ *
99
+ * @param game - The current immutable native game state.
100
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
101
+ * @returns Whether the computer, to move before it rolls, should offer a double.
102
+ * @example
103
+ * ```ts
104
+ * import { endTurn, legalPlaysOf, newGame, playTurn, rollOpening, settingsFor, wantsToDouble } from "@johnmorrisdotca/sugoroku";
105
+ *
106
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
107
+ * const game = rollOpening(newGame(settings),[3,1]);
108
+ * const play = legalPlaysOf(game)[0];
109
+ * const beforeRoll = playTurn(game,play.moves);
110
+ * console.log(wantsToDouble(beforeRoll,{strength:"greedy"}));
111
+ * ```
112
+ */
46
113
  export declare function wantsToDouble(game: GameState, options?: ComputerOptions): boolean;
47
- /** Whether the computer, doubled, should take. */
114
+ /** Whether the computer, doubled, should take.
115
+ *
116
+ * @param game - The current immutable native game state.
117
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
118
+ * @returns Whether the computer, doubled, should take.
119
+ * @example
120
+ * ```ts
121
+ * import { endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor, wantsToTake } from "@johnmorrisdotca/sugoroku";
122
+ *
123
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
124
+ * const game = rollOpening(newGame(settings),[3,1]);
125
+ * const play = legalPlaysOf(game)[0];
126
+ * const beforeRoll = playTurn(game,play.moves);
127
+ * const offered = offerDouble(beforeRoll);
128
+ * console.log(wantsToTake(offered,{strength:"greedy"}));
129
+ * ```
130
+ */
48
131
  export declare function wantsToTake(game: GameState, options?: ComputerOptions): boolean;
49
- /** What the computer does next in a game, for the side it plays. */
132
+ /** What the computer does next in a game, for the side it plays.
133
+ *
134
+ * @example
135
+ * ```ts
136
+ * import type { ComputerAction } from "@johnmorrisdotca/sugoroku";
137
+ *
138
+ * const value: ComputerAction = {kind:"roll"};
139
+ * console.log(value);
140
+ * ```
141
+ */
50
142
  export type ComputerAction = {
51
143
  kind: "throw";
52
144
  } | {
@@ -63,16 +155,54 @@ export type ComputerAction = {
63
155
  } | {
64
156
  kind: "drop";
65
157
  };
66
- /** What the side that has to act in a game would do, if the computer were playing it. Nothing is done: the game is not changed. Null when the game is over. */
158
+ /** What the side that has to act in a game would do, if the computer were playing it. Nothing is done: the game is not changed. Null when the game is over.
159
+ *
160
+ * @param game - The current immutable native game state.
161
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
162
+ * @returns The phase-appropriate computer action, or null when the game is over.
163
+ * @example
164
+ * ```ts
165
+ * import { computerAction, newGame, rollOpening, seededRandom, settingsFor } from "@johnmorrisdotca/sugoroku";
166
+ *
167
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
168
+ * const game = rollOpening(newGame(settings),[3,1]);
169
+ * console.log(computerAction(game,{strength:"greedy",random:seededRandom(42)}));
170
+ * ```
171
+ */
67
172
  export declare function computerAction(game: GameState, options?: ComputerOptions): ComputerAction | null;
68
173
  /**
69
174
  * Carry out one step of what the computer would do, with dice from `dice`.
70
175
  * Returns the game after it, and what was done, so a caller can write it down
71
176
  * or show it. Which side plays is up to the caller: this plays for whichever side is to act.
177
+ *
178
+ * @param game - The current immutable native game state.
179
+ * @param dice - Dice values as thrown, or a reproducible source specified by the signature.
180
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
181
+ * @returns The next state and selected computer action, or null when no action remains.
182
+ * @example
183
+ * ```ts
184
+ * import { newGame, rollOpening, seededDice, seededRandom, settingsFor, stepComputer } from "@johnmorrisdotca/sugoroku";
185
+ *
186
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
187
+ * const game = rollOpening(newGame(settings),[3,1]);
188
+ * console.log(stepComputer(game,seededDice(42),{strength:"greedy",random:seededRandom(42)})?.game.phase);
189
+ * ```
72
190
  */
73
191
  export declare function stepComputer(game: GameState, dice: DiceSource, options?: ComputerOptions): {
74
192
  game: GameState;
75
193
  action: ComputerAction;
76
194
  } | null;
77
- /** Whether a side is the one the computer would act for at this moment of a game: the side on turn, or the side that must answer a double. */
195
+ /** Whether a side is the one the computer would act for at this moment of a game: the side on turn, or the side that must answer a double.
196
+ *
197
+ * @param game - The current immutable native game state.
198
+ * @returns The side to make the next decision, or null during opening or after the game ends.
199
+ * @example
200
+ * ```ts
201
+ * import { newGame, rollOpening, settingsFor, sideToAct } from "@johnmorrisdotca/sugoroku";
202
+ *
203
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
204
+ * const game = rollOpening(newGame(settings),[3,1]);
205
+ * console.log(sideToAct(game));
206
+ * ```
207
+ */
78
208
  export declare function sideToAct(game: GameState): Side | null;
package/dist/computer.js CHANGED
@@ -4,7 +4,14 @@ import { canDouble, endTurn, legalPlaysOf, offerDouble, playTurn, rollDice, roll
4
4
  import { EVALUATION_WEIGHTS, evaluateFor, keithCount, winChance } from "./evaluate.js";
5
5
  import { legalPlays } from "./plays.js";
6
6
  import { otherSide } from "./side.js";
7
- /** The strengths, weakest first. */
7
+ /** The strengths, weakest first.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { STRENGTHS } from "@johnmorrisdotca/sugoroku";
12
+ * console.log(STRENGTHS);
13
+ * ```
14
+ */
8
15
  export const STRENGTHS = ["random", "greedy", "careful", "strong"];
9
16
  /** How many of its best plays each strength looks ahead on. 0 is none. */
10
17
  const LOOKAHEAD = { random: 0, greedy: 0, careful: 4, strong: 10 };
@@ -55,7 +62,20 @@ function lookedAhead(spec, position, side, weights) {
55
62
  return total / weight;
56
63
  }
57
64
  const now = () => (typeof performance === "undefined" ? Date.now() : performance.now());
58
- /** The play the computer makes for the rest of the turn of the side on turn, from the plays the rules leave it. */
65
+ /** The play the computer makes for the rest of the turn of the side on turn, from the plays the rules leave it.
66
+ *
67
+ * @param game - The current immutable native game state.
68
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
69
+ * @returns The play the computer makes for the rest of the turn of the side on turn, from the plays the rules leave it.
70
+ * @example
71
+ * ```ts
72
+ * import { choosePlay, newGame, rollOpening, seededRandom, settingsFor } from "@johnmorrisdotca/sugoroku";
73
+ *
74
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
75
+ * const game = rollOpening(newGame(settings),[3,1]);
76
+ * console.log(choosePlay(game,{strength:"greedy",random:seededRandom(42)}).moves);
77
+ * ```
78
+ */
59
79
  export function choosePlay(game, options = {}) {
60
80
  const strength = options.strength ?? "strong";
61
81
  const random = options.random ?? Math.random;
@@ -97,7 +117,20 @@ function pickFromTies(order, random) {
97
117
  const tied = order.filter((entry) => entry.score >= top - 1e-9);
98
118
  return tied[Math.floor(random() * tied.length)].play;
99
119
  }
100
- /** The chance, 0 to 1, that `side` wins from here, by the evaluation, or by the Keith count's rule of thumb in a race (as 0.5 plus a share of the count's lead). */
120
+ /** The chance, 0 to 1, that `side` wins from here, by the evaluation, or by the Keith count's rule of thumb in a race (as 0.5 plus a share of the count's lead).
121
+ *
122
+ * @param game - The current immutable native game state.
123
+ * @param side - The side being examined: white or black.
124
+ * @returns The chance, 0 to 1, that `side` wins from here, by the evaluation, or by the Keith count's rule of thumb in a race (as 0.5 plus a share of the count's lead).
125
+ * @example
126
+ * ```ts
127
+ * import { chanceOfWinning, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
128
+ *
129
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
130
+ * const game = rollOpening(newGame(settings),[3,1]);
131
+ * console.log(chanceOfWinning(game,"white"));
132
+ * ```
133
+ */
101
134
  export function chanceOfWinning(game, side) {
102
135
  const spec = game.settings.variant;
103
136
  return winChance(evaluateFor(spec, game.position, side));
@@ -109,7 +142,22 @@ function raceCube(game, doubler, redouble) {
109
142
  const lead = x - y;
110
143
  return { double: lead <= (redouble ? 3 : 4), take: lead >= 2 };
111
144
  }
112
- /** Whether the computer, to move before it rolls, should offer a double. */
145
+ /** Whether the computer, to move before it rolls, should offer a double.
146
+ *
147
+ * @param game - The current immutable native game state.
148
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
149
+ * @returns Whether the computer, to move before it rolls, should offer a double.
150
+ * @example
151
+ * ```ts
152
+ * import { endTurn, legalPlaysOf, newGame, playTurn, rollOpening, settingsFor, wantsToDouble } from "@johnmorrisdotca/sugoroku";
153
+ *
154
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
155
+ * const game = rollOpening(newGame(settings),[3,1]);
156
+ * const play = legalPlaysOf(game)[0];
157
+ * const beforeRoll = playTurn(game,play.moves);
158
+ * console.log(wantsToDouble(beforeRoll,{strength:"greedy"}));
159
+ * ```
160
+ */
113
161
  export function wantsToDouble(game, options = {}) {
114
162
  const strength = options.strength ?? "strong";
115
163
  if (strength === "random" || !canDouble(game))
@@ -128,7 +176,23 @@ export function wantsToDouble(game, options = {}) {
128
176
  return false;
129
177
  return chanceOfWinning(game, side) >= 0.7;
130
178
  }
131
- /** Whether the computer, doubled, should take. */
179
+ /** Whether the computer, doubled, should take.
180
+ *
181
+ * @param game - The current immutable native game state.
182
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
183
+ * @returns Whether the computer, doubled, should take.
184
+ * @example
185
+ * ```ts
186
+ * import { endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor, wantsToTake } from "@johnmorrisdotca/sugoroku";
187
+ *
188
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
189
+ * const game = rollOpening(newGame(settings),[3,1]);
190
+ * const play = legalPlaysOf(game)[0];
191
+ * const beforeRoll = playTurn(game,play.moves);
192
+ * const offered = offerDouble(beforeRoll);
193
+ * console.log(wantsToTake(offered,{strength:"greedy"}));
194
+ * ```
195
+ */
132
196
  export function wantsToTake(game, options = {}) {
133
197
  const strength = options.strength ?? "strong";
134
198
  if (strength === "random")
@@ -147,7 +211,20 @@ export function wantsToTake(game, options = {}) {
147
211
  return true;
148
212
  return chanceOfWinning(game, taker) >= 0.25;
149
213
  }
150
- /** What the side that has to act in a game would do, if the computer were playing it. Nothing is done: the game is not changed. Null when the game is over. */
214
+ /** What the side that has to act in a game would do, if the computer were playing it. Nothing is done: the game is not changed. Null when the game is over.
215
+ *
216
+ * @param game - The current immutable native game state.
217
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
218
+ * @returns The phase-appropriate computer action, or null when the game is over.
219
+ * @example
220
+ * ```ts
221
+ * import { computerAction, newGame, rollOpening, seededRandom, settingsFor } from "@johnmorrisdotca/sugoroku";
222
+ *
223
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
224
+ * const game = rollOpening(newGame(settings),[3,1]);
225
+ * console.log(computerAction(game,{strength:"greedy",random:seededRandom(42)}));
226
+ * ```
227
+ */
151
228
  export function computerAction(game, options = {}) {
152
229
  switch (game.phase) {
153
230
  case "opening":
@@ -167,6 +244,19 @@ export function computerAction(game, options = {}) {
167
244
  * Carry out one step of what the computer would do, with dice from `dice`.
168
245
  * Returns the game after it, and what was done, so a caller can write it down
169
246
  * or show it. Which side plays is up to the caller: this plays for whichever side is to act.
247
+ *
248
+ * @param game - The current immutable native game state.
249
+ * @param dice - Dice values as thrown, or a reproducible source specified by the signature.
250
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
251
+ * @returns The next state and selected computer action, or null when no action remains.
252
+ * @example
253
+ * ```ts
254
+ * import { newGame, rollOpening, seededDice, seededRandom, settingsFor, stepComputer } from "@johnmorrisdotca/sugoroku";
255
+ *
256
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
257
+ * const game = rollOpening(newGame(settings),[3,1]);
258
+ * console.log(stepComputer(game,seededDice(42),{strength:"greedy",random:seededRandom(42)})?.game.phase);
259
+ * ```
170
260
  */
171
261
  export function stepComputer(game, dice, options = {}) {
172
262
  const action = computerAction(game, options);
@@ -191,7 +281,19 @@ export function stepComputer(game, dice, options = {}) {
191
281
  return { game: endTurn(game), action };
192
282
  }
193
283
  }
194
- /** Whether a side is the one the computer would act for at this moment of a game: the side on turn, or the side that must answer a double. */
284
+ /** Whether a side is the one the computer would act for at this moment of a game: the side on turn, or the side that must answer a double.
285
+ *
286
+ * @param game - The current immutable native game state.
287
+ * @returns The side to make the next decision, or null during opening or after the game ends.
288
+ * @example
289
+ * ```ts
290
+ * import { newGame, rollOpening, settingsFor, sideToAct } from "@johnmorrisdotca/sugoroku";
291
+ *
292
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
293
+ * const game = rollOpening(newGame(settings),[3,1]);
294
+ * console.log(sideToAct(game));
295
+ * ```
296
+ */
195
297
  export function sideToAct(game) {
196
298
  if (game.phase === "over" || game.phase === "opening")
197
299
  return null;
package/dist/dice.d.ts CHANGED
@@ -1,4 +1,14 @@
1
- /** Where dice come from: each call is the number on one die, 1 to 6. */
1
+ /** Where dice come from: each call is the number on one die, 1 to 6.
2
+ *
3
+ * @example
4
+ * ```ts
5
+ * import type { DiceSource } from "@johnmorrisdotca/sugoroku";
6
+ * import { seededDice } from "@johnmorrisdotca/sugoroku";
7
+ *
8
+ * const value: DiceSource = seededDice(42);
9
+ * console.log(value);
10
+ * ```
11
+ */
2
12
  export type DiceSource = () => number;
3
13
  /**
4
14
  * A SEEDED DICE SEQUENCE: the same seed is the same dice, one die after
@@ -7,11 +17,50 @@ export type DiceSource = () => number;
7
17
  * dice it should have had. Dice are taken in the order a game asks for them:
8
18
  * the opening throw (white's die, then black's, and again for a tie), then
9
19
  * every roll in turn, each as many dice as the variant rolls.
20
+ *
21
+ * @param seed - A reproducible numeric or text seed.
22
+ * @returns A SEEDED DICE SEQUENCE: the same seed is the same dice, one die after another, in every browser and every Node, so a game can be played again from its seed and a server can tell whether the dice a game says it had are the dice it should have had. Dice are taken in the order a game asks for them: the opening throw (white's die, then black's, and again for a tie), then every roll in turn, each as many dice as the variant rolls.
23
+ * @example
24
+ * ```ts
25
+ * import { rollFrom, seededDice } from "@johnmorrisdotca/sugoroku";
26
+ *
27
+ * console.log(rollFrom(seededDice(42),2));
28
+ * ```
10
29
  */
11
30
  export declare function seededDice(seed: number | string): DiceSource;
12
- /** Dice that are not seeded: from the device's own random numbers. */
31
+ /** Dice that are not seeded: from the device's own random numbers.
32
+ *
33
+ * @returns Dice that are not seeded: from the device's own random numbers.
34
+ * @example
35
+ * ```ts
36
+ * import { randomDice } from "@johnmorrisdotca/sugoroku";
37
+ *
38
+ * console.log(typeof randomDice());
39
+ * ```
40
+ */
13
41
  export declare function randomDice(): DiceSource;
14
- /** A roll of `count` dice from a source. */
42
+ /** A roll of `count` dice from a source.
43
+ *
44
+ * @param source - A die source returning a valid integer from 1 to 6.
45
+ * @param count - The number of dice to throw.
46
+ * @returns A roll of `count` dice from a source.
47
+ * @example
48
+ * ```ts
49
+ * import { rollFrom, seededDice } from "@johnmorrisdotca/sugoroku";
50
+ *
51
+ * console.log(rollFrom(seededDice(42),2));
52
+ * ```
53
+ */
15
54
  export declare function rollFrom(source: DiceSource, count?: number): number[];
16
- /** An opening throw from a source: one die for white and one for black. */
55
+ /** An opening throw from a source: one die for white and one for black.
56
+ *
57
+ * @param source - A die source returning a valid integer from 1 to 6.
58
+ * @returns An opening throw from a source: one die for white and one for black.
59
+ * @example
60
+ * ```ts
61
+ * import { openingFrom, seededDice } from "@johnmorrisdotca/sugoroku";
62
+ *
63
+ * console.log(openingFrom(seededDice(42)));
64
+ * ```
65
+ */
17
66
  export declare function openingFrom(source: DiceSource): [number, number];
package/dist/dice.js CHANGED
@@ -6,12 +6,30 @@ import { seededRandom } from "./random.js";
6
6
  * dice it should have had. Dice are taken in the order a game asks for them:
7
7
  * the opening throw (white's die, then black's, and again for a tie), then
8
8
  * every roll in turn, each as many dice as the variant rolls.
9
+ *
10
+ * @param seed - A reproducible numeric or text seed.
11
+ * @returns A SEEDED DICE SEQUENCE: the same seed is the same dice, one die after another, in every browser and every Node, so a game can be played again from its seed and a server can tell whether the dice a game says it had are the dice it should have had. Dice are taken in the order a game asks for them: the opening throw (white's die, then black's, and again for a tie), then every roll in turn, each as many dice as the variant rolls.
12
+ * @example
13
+ * ```ts
14
+ * import { rollFrom, seededDice } from "@johnmorrisdotca/sugoroku";
15
+ *
16
+ * console.log(rollFrom(seededDice(42),2));
17
+ * ```
9
18
  */
10
19
  export function seededDice(seed) {
11
20
  const random = seededRandom(seed);
12
21
  return () => 1 + Math.floor(random() * 6);
13
22
  }
14
- /** Dice that are not seeded: from the device's own random numbers. */
23
+ /** Dice that are not seeded: from the device's own random numbers.
24
+ *
25
+ * @returns Dice that are not seeded: from the device's own random numbers.
26
+ * @example
27
+ * ```ts
28
+ * import { randomDice } from "@johnmorrisdotca/sugoroku";
29
+ *
30
+ * console.log(typeof randomDice());
31
+ * ```
32
+ */
15
33
  export function randomDice() {
16
34
  const crypto = globalThis.crypto;
17
35
  if (crypto?.getRandomValues !== undefined) {
@@ -26,11 +44,32 @@ export function randomDice() {
26
44
  }
27
45
  return () => 1 + Math.floor(Math.random() * 6);
28
46
  }
29
- /** A roll of `count` dice from a source. */
47
+ /** A roll of `count` dice from a source.
48
+ *
49
+ * @param source - A die source returning a valid integer from 1 to 6.
50
+ * @param count - The number of dice to throw.
51
+ * @returns A roll of `count` dice from a source.
52
+ * @example
53
+ * ```ts
54
+ * import { rollFrom, seededDice } from "@johnmorrisdotca/sugoroku";
55
+ *
56
+ * console.log(rollFrom(seededDice(42),2));
57
+ * ```
58
+ */
30
59
  export function rollFrom(source, count = 2) {
31
60
  return Array.from({ length: count }, source);
32
61
  }
33
- /** An opening throw from a source: one die for white and one for black. */
62
+ /** An opening throw from a source: one die for white and one for black.
63
+ *
64
+ * @param source - A die source returning a valid integer from 1 to 6.
65
+ * @returns An opening throw from a source: one die for white and one for black.
66
+ * @example
67
+ * ```ts
68
+ * import { openingFrom, seededDice } from "@johnmorrisdotca/sugoroku";
69
+ *
70
+ * console.log(openingFrom(seededDice(42)));
71
+ * ```
72
+ */
34
73
  export function openingFrom(source) {
35
74
  const white = source();
36
75
  return [white, source()];
package/dist/draw.d.ts CHANGED
@@ -15,7 +15,16 @@ import { type VariantKey, type VariantSpec } from "./variants.ts";
15
15
  * counts, 1 to 24), `data-bar` and `data-tray` with the side they belong to, and
16
16
  * `data-checker` on each checker. Nothing in the drawing can be selected.
17
17
  */
18
- /** What to draw with. */
18
+ /** What to draw with.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * import type { SugorokuDrawOptions } from "@johnmorrisdotca/sugoroku/draw";
23
+ *
24
+ * const value: SugorokuDrawOptions = {variant:"backgammon",view:"white",numbers:true};
25
+ * console.log(value);
26
+ * ```
27
+ */
19
28
  export type SugorokuDrawOptions = {
20
29
  /** The variant, for how its sides' points line up. Default `backgammon`. */
21
30
  variant?: VariantKey | VariantSpec;
@@ -58,12 +67,43 @@ export type SugorokuDrawOptions = {
58
67
  /** Put `SUGOROKU_STYLE` inside the drawing, so it stands alone as an image. A page with the style in already leaves this off. */
59
68
  style?: boolean;
60
69
  };
61
- /** How many checkers are drawn on a stack before the last one carries the count. */
70
+ /** How many checkers are drawn on a stack before the last one carries the count.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * import { MOST_DRAWN } from "@johnmorrisdotca/sugoroku/draw";
75
+ * console.log(MOST_DRAWN);
76
+ * ```
77
+ */
62
78
  export declare const MOST_DRAWN = 5;
63
- /** The inline custom properties of a drawing: the named board and checkers, then any colour given. */
79
+ /** The inline custom properties of a drawing: the named board and checkers, then any colour given.
80
+ *
81
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
82
+ * @returns The inline custom properties of a drawing: the named board and checkers, then any colour given.
83
+ * @example
84
+ * ```ts
85
+ * import { colourStyle } from "@johnmorrisdotca/sugoroku/draw";
86
+ *
87
+ * console.log(colourStyle({board:"green",checkers:"contrast"}));
88
+ * ```
89
+ */
64
90
  export declare function colourStyle(options: Pick<SugorokuDrawOptions, "board" | "checkers" | "colours">): string;
65
91
  /**
66
92
  * The board as SVG text for `position`. The variant says how the sides' points
67
93
  * line up; pass the same one the game is played with.
94
+ *
95
+ * @param position - The native checker counts, bar, reserve and borne-off counts.
96
+ * @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
97
+ * @returns The board as SVG text for `position`. The variant says how the sides' points line up; pass the same one the game is played with.
98
+ * @example
99
+ * ```ts
100
+ * import { settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
101
+ * import { drawSugoroku } from "@johnmorrisdotca/sugoroku/draw";
102
+ *
103
+ * const settings = settingsFor("backgammon",{points:5,cube:true});
104
+ * const spec = settings.variant;
105
+ * const position = startPosition(spec);
106
+ * console.log(drawSugoroku(position,{variant:"backgammon",label:"Starting board"}).startsWith("<svg"));
107
+ * ```
68
108
  */
69
109
  export declare function drawSugoroku(position: Position, options?: SugorokuDrawOptions): string;