@johnmorrisdotca/sugoroku 1.0.2 → 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 +18 -0
- package/README.md +78 -68
- 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 +25 -18
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;
|
package/dist/draw.js
CHANGED
|
@@ -6,11 +6,28 @@ import { SUGOROKU_STYLE } from "./style.js";
|
|
|
6
6
|
import { VARIANTS } from "./variants.js";
|
|
7
7
|
const escape = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
8
8
|
const n = (value) => String(Math.round(value * 100) / 100);
|
|
9
|
-
/** How many checkers are drawn on a stack before the last one carries the count.
|
|
9
|
+
/** How many checkers are drawn on a stack before the last one carries the count.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { MOST_DRAWN } from "@johnmorrisdotca/sugoroku/draw";
|
|
14
|
+
* console.log(MOST_DRAWN);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
10
17
|
export const MOST_DRAWN = 5;
|
|
11
18
|
/** The pips of each die face, as places on a three by three grid, 0 to 8 reading across and down. */
|
|
12
19
|
const PIPS = [[], [4], [0, 8], [0, 4, 8], [0, 2, 6, 8], [0, 2, 4, 6, 8], [0, 2, 3, 5, 6, 8]];
|
|
13
|
-
/** The inline custom properties of a drawing: the named board and checkers, then any colour given.
|
|
20
|
+
/** The inline custom properties of a drawing: the named board and checkers, then any colour given.
|
|
21
|
+
*
|
|
22
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
23
|
+
* @returns The inline custom properties of a drawing: the named board and checkers, then any colour given.
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { colourStyle } from "@johnmorrisdotca/sugoroku/draw";
|
|
27
|
+
*
|
|
28
|
+
* console.log(colourStyle({board:"green",checkers:"contrast"}));
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
14
31
|
export function colourStyle(options) {
|
|
15
32
|
const colours = { ...(options.board === undefined ? {} : SUGOROKU_BOARDS[options.board]), ...(options.checkers === undefined ? {} : SUGOROKU_CHECKER_SETS[options.checkers]), ...options.colours };
|
|
16
33
|
return SUGOROKU_COLOUR_NAMES.filter((name) => colours[name] !== undefined).map((name) => `${colourProperty(name)}:${escape(String(colours[name]))}`).join(";");
|
|
@@ -27,6 +44,20 @@ function describe(position) {
|
|
|
27
44
|
/**
|
|
28
45
|
* The board as SVG text for `position`. The variant says how the sides' points
|
|
29
46
|
* line up; pass the same one the game is played with.
|
|
47
|
+
*
|
|
48
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
49
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
50
|
+
* @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.
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
54
|
+
* import { drawSugoroku } from "@johnmorrisdotca/sugoroku/draw";
|
|
55
|
+
*
|
|
56
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
57
|
+
* const spec = settings.variant;
|
|
58
|
+
* const position = startPosition(spec);
|
|
59
|
+
* console.log(drawSugoroku(position,{variant:"backgammon",label:"Starting board"}).startsWith("<svg"));
|
|
60
|
+
* ```
|
|
30
61
|
*/
|
|
31
62
|
export function drawSugoroku(position, options = {}) {
|
|
32
63
|
const spec = typeof options.variant === "object" ? options.variant : VARIANTS[options.variant ?? "backgammon"];
|
package/dist/element-define.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from "./element.ts";
|
package/dist/element-define.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* <script type="module" src="https://cdn.jsdelivr.net/npm/@johnmorrisdotca/sugoroku@1/dist/element-define.js"></script>
|
|
6
|
-
* <sugoroku-board variant="nackgammon" black="strong"></sugoroku-board>
|
|
7
|
-
* ```
|
|
8
|
-
*
|
|
9
|
-
* A tag already defined is left as it is, and on a server, where there is no page, nothing happens.
|
|
10
|
-
*/
|
|
11
|
-
import { SugorokuBoard } from "./element.js";
|
|
12
|
-
if (typeof customElements !== "undefined" && customElements.get("sugoroku-board") === undefined)
|
|
13
|
-
customElements.define("sugoroku-board", SugorokuBoard);
|
|
1
|
+
import { defineBoard, definePosition } from "./element.js";
|
|
2
|
+
defineBoard();
|
|
3
|
+
definePosition();
|
|
4
|
+
export * from "./element.js";
|