@johnmorrisdotca/sugoroku 1.0.2 → 1.1.1
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 +30 -0
- package/README.md +78 -68
- package/dist/board.d.ts +216 -12
- package/dist/board.js +205 -13
- package/dist/colours.d.ts +80 -8
- package/dist/colours.js +57 -7
- package/dist/computer.d.ts +139 -9
- package/dist/computer.js +118 -11
- package/dist/dice.d.ts +53 -4
- package/dist/dice.js +42 -3
- package/dist/draw-entry.d.ts +3 -3
- package/dist/draw-entry.js +2 -2
- package/dist/draw.d.ts +43 -3
- package/dist/draw.js +63 -13
- package/dist/element-define.d.ts +1 -1
- package/dist/element-define.js +4 -13
- package/dist/element.d.ts +116 -5
- package/dist/element.js +378 -102
- package/dist/elementFrame.d.ts +21 -0
- package/dist/elementFrame.js +110 -0
- package/dist/evaluate.d.ts +76 -4
- package/dist/evaluate.js +70 -5
- package/dist/game.d.ts +408 -23
- package/dist/game.js +344 -20
- package/dist/geometry.d.ts +50 -3
- package/dist/geometry.js +64 -28
- package/dist/index.d.ts +12 -11
- package/dist/index.js +10 -9
- package/dist/match.d.ts +66 -4
- package/dist/match.js +69 -6
- package/dist/mount.d.ts +77 -6
- package/dist/mount.js +105 -357
- package/dist/mountView.d.ts +58 -0
- package/dist/mountView.js +459 -0
- package/dist/moves.d.ts +48 -1
- package/dist/moves.js +35 -1
- package/dist/notation.d.ts +92 -4
- package/dist/notation.js +88 -5
- package/dist/play-entry.d.ts +4 -4
- package/dist/play-entry.js +1 -1
- 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/positionElement.d.ts +107 -0
- package/dist/positionElement.js +303 -0
- package/dist/positionId.d.ts +29 -1
- package/dist/positionId.js +33 -2
- package/dist/random.d.ts +33 -3
- package/dist/random.js +22 -2
- package/dist/record.d.ts +151 -8
- package/dist/record.js +121 -36
- package/dist/rules.d.ts +130 -10
- package/dist/rules.js +133 -14
- 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 +37 -3
- package/dist/style.d.ts +6 -0
- package/dist/style.js +6 -0
- package/dist/variants.d.ts +113 -8
- package/dist/variants.js +81 -7
- package/dist/version.d.ts +9 -2
- package/dist/version.js +9 -2
- package/package.json +29 -22
package/dist/computer.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { inContact } from "./board.js";
|
|
2
2
|
import { rollFrom } from "./dice.js";
|
|
3
|
-
import { canDouble, endTurn, legalPlaysOf, offerDouble, playTurn, rollDice, rollOpening, takeDouble, dropDouble } from "./game.js";
|
|
4
3
|
import { EVALUATION_WEIGHTS, evaluateFor, keithCount, winChance } from "./evaluate.js";
|
|
4
|
+
import { canDouble, dropDouble, endTurn, legalPlaysOf, offerDouble, playTurn, rollDice, rollOpening, takeDouble, } from "./game.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 };
|
|
@@ -27,7 +34,9 @@ const ROLLS_OF_THREE = (() => {
|
|
|
27
34
|
})();
|
|
28
35
|
/** The plays best first by what they leave, each with its score. */
|
|
29
36
|
function ranked(spec, plays, side, weights) {
|
|
30
|
-
return plays
|
|
37
|
+
return plays
|
|
38
|
+
.map((play) => ({ play, score: evaluateFor(spec, play.position, side, weights) }))
|
|
39
|
+
.sort((a, b) => b.score - a.score);
|
|
31
40
|
}
|
|
32
41
|
/** The value, for `side`, of a position that the other side is about to move from: the average over its rolls of what its best reply leaves. */
|
|
33
42
|
function lookedAhead(spec, position, side, weights) {
|
|
@@ -36,7 +45,9 @@ function lookedAhead(spec, position, side, weights) {
|
|
|
36
45
|
let weight = 0;
|
|
37
46
|
const rolls = spec.dice === 2 ? ROLLS : ROLLS_OF_THREE;
|
|
38
47
|
for (const roll of rolls) {
|
|
39
|
-
const dice = spec.dice === 2
|
|
48
|
+
const dice = spec.dice === 2
|
|
49
|
+
? [roll[0], roll[1]]
|
|
50
|
+
: [roll[0], roll[1], roll[2]];
|
|
40
51
|
const chance = roll[spec.dice];
|
|
41
52
|
const replies = legalPlays(spec, position, opp, dice);
|
|
42
53
|
// The other side picks the reply it likes best; we judge what that leaves us.
|
|
@@ -55,7 +66,20 @@ function lookedAhead(spec, position, side, weights) {
|
|
|
55
66
|
return total / weight;
|
|
56
67
|
}
|
|
57
68
|
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.
|
|
69
|
+
/** The play the computer makes for the rest of the turn of the side on turn, from the plays the rules leave it.
|
|
70
|
+
*
|
|
71
|
+
* @param game - The current immutable native game state.
|
|
72
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
73
|
+
* @returns The play the computer makes for the rest of the turn of the side on turn, from the plays the rules leave it.
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { choosePlay, newGame, rollOpening, seededRandom, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
77
|
+
*
|
|
78
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
79
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
80
|
+
* console.log(choosePlay(game,{strength:"greedy",random:seededRandom(42)}).moves);
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
59
83
|
export function choosePlay(game, options = {}) {
|
|
60
84
|
const strength = options.strength ?? "strong";
|
|
61
85
|
const random = options.random ?? Math.random;
|
|
@@ -97,7 +121,20 @@ function pickFromTies(order, random) {
|
|
|
97
121
|
const tied = order.filter((entry) => entry.score >= top - 1e-9);
|
|
98
122
|
return tied[Math.floor(random() * tied.length)].play;
|
|
99
123
|
}
|
|
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).
|
|
124
|
+
/** 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
|
+
*
|
|
126
|
+
* @param game - The current immutable native game state.
|
|
127
|
+
* @param side - The side being examined: white or black.
|
|
128
|
+
* @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).
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { chanceOfWinning, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
132
|
+
*
|
|
133
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
134
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
135
|
+
* console.log(chanceOfWinning(game,"white"));
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
101
138
|
export function chanceOfWinning(game, side) {
|
|
102
139
|
const spec = game.settings.variant;
|
|
103
140
|
return winChance(evaluateFor(spec, game.position, side));
|
|
@@ -109,7 +146,22 @@ function raceCube(game, doubler, redouble) {
|
|
|
109
146
|
const lead = x - y;
|
|
110
147
|
return { double: lead <= (redouble ? 3 : 4), take: lead >= 2 };
|
|
111
148
|
}
|
|
112
|
-
/** Whether the computer, to move before it rolls, should offer a double.
|
|
149
|
+
/** Whether the computer, to move before it rolls, should offer a double.
|
|
150
|
+
*
|
|
151
|
+
* @param game - The current immutable native game state.
|
|
152
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
153
|
+
* @returns Whether the computer, to move before it rolls, should offer a double.
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* import { endTurn, legalPlaysOf, newGame, playTurn, rollOpening, settingsFor, wantsToDouble } from "@johnmorrisdotca/sugoroku";
|
|
157
|
+
*
|
|
158
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
159
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
160
|
+
* const play = legalPlaysOf(game)[0];
|
|
161
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
162
|
+
* console.log(wantsToDouble(beforeRoll,{strength:"greedy"}));
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
113
165
|
export function wantsToDouble(game, options = {}) {
|
|
114
166
|
const strength = options.strength ?? "strong";
|
|
115
167
|
if (strength === "random" || !canDouble(game))
|
|
@@ -122,13 +174,30 @@ export function wantsToDouble(game, options = {}) {
|
|
|
122
174
|
if (!inContact(spec, game.position)) {
|
|
123
175
|
const race = raceCube(game, side, holds);
|
|
124
176
|
// Not when the race is so lopsided that the other side must drop and the gammon is the only prize: keep it simple and double.
|
|
125
|
-
return race.double &&
|
|
177
|
+
return (race.double &&
|
|
178
|
+
keithCount(game.position, side, true) < keithCount(game.position, otherSide(side), false) + (holds ? 4 : 5));
|
|
126
179
|
}
|
|
127
180
|
if (strength === "greedy")
|
|
128
181
|
return false;
|
|
129
182
|
return chanceOfWinning(game, side) >= 0.7;
|
|
130
183
|
}
|
|
131
|
-
/** Whether the computer, doubled, should take.
|
|
184
|
+
/** Whether the computer, doubled, should take.
|
|
185
|
+
*
|
|
186
|
+
* @param game - The current immutable native game state.
|
|
187
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
188
|
+
* @returns Whether the computer, doubled, should take.
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* import { endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor, wantsToTake } from "@johnmorrisdotca/sugoroku";
|
|
192
|
+
*
|
|
193
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
194
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
195
|
+
* const play = legalPlaysOf(game)[0];
|
|
196
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
197
|
+
* const offered = offerDouble(beforeRoll);
|
|
198
|
+
* console.log(wantsToTake(offered,{strength:"greedy"}));
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
132
201
|
export function wantsToTake(game, options = {}) {
|
|
133
202
|
const strength = options.strength ?? "strong";
|
|
134
203
|
if (strength === "random")
|
|
@@ -147,7 +216,20 @@ export function wantsToTake(game, options = {}) {
|
|
|
147
216
|
return true;
|
|
148
217
|
return chanceOfWinning(game, taker) >= 0.25;
|
|
149
218
|
}
|
|
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.
|
|
219
|
+
/** 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.
|
|
220
|
+
*
|
|
221
|
+
* @param game - The current immutable native game state.
|
|
222
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
223
|
+
* @returns The phase-appropriate computer action, or null when the game is over.
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* import { computerAction, newGame, rollOpening, seededRandom, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
227
|
+
*
|
|
228
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
229
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
230
|
+
* console.log(computerAction(game,{strength:"greedy",random:seededRandom(42)}));
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
151
233
|
export function computerAction(game, options = {}) {
|
|
152
234
|
switch (game.phase) {
|
|
153
235
|
case "opening":
|
|
@@ -167,6 +249,19 @@ export function computerAction(game, options = {}) {
|
|
|
167
249
|
* Carry out one step of what the computer would do, with dice from `dice`.
|
|
168
250
|
* Returns the game after it, and what was done, so a caller can write it down
|
|
169
251
|
* or show it. Which side plays is up to the caller: this plays for whichever side is to act.
|
|
252
|
+
*
|
|
253
|
+
* @param game - The current immutable native game state.
|
|
254
|
+
* @param dice - Dice values as thrown, or a reproducible source specified by the signature.
|
|
255
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
256
|
+
* @returns The next state and selected computer action, or null when no action remains.
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* import { newGame, rollOpening, seededDice, seededRandom, settingsFor, stepComputer } from "@johnmorrisdotca/sugoroku";
|
|
260
|
+
*
|
|
261
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
262
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
263
|
+
* console.log(stepComputer(game,seededDice(42),{strength:"greedy",random:seededRandom(42)})?.game.phase);
|
|
264
|
+
* ```
|
|
170
265
|
*/
|
|
171
266
|
export function stepComputer(game, dice, options = {}) {
|
|
172
267
|
const action = computerAction(game, options);
|
|
@@ -191,7 +286,19 @@ export function stepComputer(game, dice, options = {}) {
|
|
|
191
286
|
return { game: endTurn(game), action };
|
|
192
287
|
}
|
|
193
288
|
}
|
|
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.
|
|
289
|
+
/** 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.
|
|
290
|
+
*
|
|
291
|
+
* @param game - The current immutable native game state.
|
|
292
|
+
* @returns The side to make the next decision, or null during opening or after the game ends.
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* import { newGame, rollOpening, settingsFor, sideToAct } from "@johnmorrisdotca/sugoroku";
|
|
296
|
+
*
|
|
297
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
298
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
299
|
+
* console.log(sideToAct(game));
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
195
302
|
export function sideToAct(game) {
|
|
196
303
|
if (game.phase === "over" || game.phase === "opening")
|
|
197
304
|
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-entry.d.ts
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
* (`@johnmorrisdotca/sugoroku/draw`), so a server that only checks a game never
|
|
5
5
|
* loads any of it.
|
|
6
6
|
*/
|
|
7
|
-
export { colourProperty, SUGOROKU_BOARD_NAMES, SUGOROKU_BOARDS, SUGOROKU_CHECKER_SET_NAMES, SUGOROKU_CHECKER_SETS, SUGOROKU_COLOUR_NAMES } from "./colours.ts";
|
|
8
7
|
export type { SugorokuBoardName, SugorokuCheckerSetName, SugorokuColours } from "./colours.ts";
|
|
9
|
-
export {
|
|
8
|
+
export { colourProperty, SUGOROKU_BOARD_NAMES, SUGOROKU_BOARDS, SUGOROKU_CHECKER_SET_NAMES, SUGOROKU_CHECKER_SETS, SUGOROKU_COLOUR_NAMES, } from "./colours.ts";
|
|
10
9
|
export type { SugorokuDrawOptions } from "./draw.ts";
|
|
11
|
-
export {
|
|
10
|
+
export { colourStyle, drawSugoroku, MOST_DRAWN } from "./draw.ts";
|
|
12
11
|
export type { BoardOptions, Box, Orientation } from "./geometry.ts";
|
|
12
|
+
export { BoardLayout, boardSize } from "./geometry.ts";
|
|
13
13
|
export { SUGOROKU_STYLE } from "./style.ts";
|
package/dist/draw-entry.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* (`@johnmorrisdotca/sugoroku/draw`), so a server that only checks a game never
|
|
5
5
|
* loads any of it.
|
|
6
6
|
*/
|
|
7
|
-
export { colourProperty, SUGOROKU_BOARD_NAMES, SUGOROKU_BOARDS, SUGOROKU_CHECKER_SET_NAMES, SUGOROKU_CHECKER_SETS, SUGOROKU_COLOUR_NAMES } from "./colours.js";
|
|
7
|
+
export { colourProperty, SUGOROKU_BOARD_NAMES, SUGOROKU_BOARDS, SUGOROKU_CHECKER_SET_NAMES, SUGOROKU_CHECKER_SETS, SUGOROKU_COLOUR_NAMES, } from "./colours.js";
|
|
8
8
|
export { colourStyle, drawSugoroku, MOST_DRAWN } from "./draw.js";
|
|
9
|
-
export {
|
|
9
|
+
export { BoardLayout, boardSize } from "./geometry.js";
|
|
10
10
|
export { SUGOROKU_STYLE } from "./style.js";
|
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
|
@@ -1,19 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { colourProperty, SUGOROKU_BOARDS, SUGOROKU_CHECKER_SETS, SUGOROKU_COLOUR_NAMES } from "./colours.js";
|
|
3
|
-
import { BAR_WIDTH, BoardLayout, boardSize, CHECKER, DIE, PAD, POINT_WIDTH, STACK_STEP, TRAY_WIDTH } from "./geometry.js";
|
|
4
|
-
import {
|
|
1
|
+
import { BAR, boardPoint, POINTS } from "./board.js";
|
|
2
|
+
import { colourProperty, SUGOROKU_BOARDS, SUGOROKU_CHECKER_SETS, SUGOROKU_COLOUR_NAMES, } from "./colours.js";
|
|
3
|
+
import { BAR_WIDTH, BoardLayout, boardSize, CHECKER, DIE, PAD, POINT_WIDTH, STACK_STEP, TRAY_WIDTH, } from "./geometry.js";
|
|
4
|
+
import { SIDES, sideIndex } from "./side.js";
|
|
5
5
|
import { SUGOROKU_STYLE } from "./style.js";
|
|
6
6
|
import { VARIANTS } from "./variants.js";
|
|
7
|
-
const
|
|
7
|
+
const escapeXml = (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
|
-
const PIPS = [
|
|
13
|
-
|
|
19
|
+
const PIPS = [
|
|
20
|
+
[],
|
|
21
|
+
[4],
|
|
22
|
+
[0, 8],
|
|
23
|
+
[0, 4, 8],
|
|
24
|
+
[0, 2, 6, 8],
|
|
25
|
+
[0, 2, 4, 6, 8],
|
|
26
|
+
[0, 2, 3, 5, 6, 8],
|
|
27
|
+
];
|
|
28
|
+
/** The inline custom properties of a drawing: the named board and checkers, then any colour given.
|
|
29
|
+
*
|
|
30
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
31
|
+
* @returns The inline custom properties of a drawing: the named board and checkers, then any colour given.
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { colourStyle } from "@johnmorrisdotca/sugoroku/draw";
|
|
35
|
+
*
|
|
36
|
+
* console.log(colourStyle({board:"green",checkers:"contrast"}));
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
14
39
|
export function colourStyle(options) {
|
|
15
|
-
const colours = {
|
|
16
|
-
|
|
40
|
+
const colours = {
|
|
41
|
+
...(options.board === undefined ? {} : SUGOROKU_BOARDS[options.board]),
|
|
42
|
+
...(options.checkers === undefined ? {} : SUGOROKU_CHECKER_SETS[options.checkers]),
|
|
43
|
+
...options.colours,
|
|
44
|
+
};
|
|
45
|
+
return SUGOROKU_COLOUR_NAMES.filter((name) => colours[name] !== undefined)
|
|
46
|
+
.map((name) => `${colourProperty(name)}:${escapeXml(String(colours[name]))}`)
|
|
47
|
+
.join(";");
|
|
17
48
|
}
|
|
18
49
|
/** The words a screen reader hears of a drawing when none is given. */
|
|
19
50
|
function describe(position) {
|
|
@@ -27,6 +58,20 @@ function describe(position) {
|
|
|
27
58
|
/**
|
|
28
59
|
* The board as SVG text for `position`. The variant says how the sides' points
|
|
29
60
|
* line up; pass the same one the game is played with.
|
|
61
|
+
*
|
|
62
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
63
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
64
|
+
* @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.
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
68
|
+
* import { drawSugoroku } from "@johnmorrisdotca/sugoroku/draw";
|
|
69
|
+
*
|
|
70
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
71
|
+
* const spec = settings.variant;
|
|
72
|
+
* const position = startPosition(spec);
|
|
73
|
+
* console.log(drawSugoroku(position,{variant:"backgammon",label:"Starting board"}).startsWith("<svg"));
|
|
74
|
+
* ```
|
|
30
75
|
*/
|
|
31
76
|
export function drawSugoroku(position, options = {}) {
|
|
32
77
|
const spec = typeof options.variant === "object" ? options.variant : VARIANTS[options.variant ?? "backgammon"];
|
|
@@ -39,7 +84,7 @@ export function drawSugoroku(position, options = {}) {
|
|
|
39
84
|
const mx = (x, w = 0) => layout.mx(x, w);
|
|
40
85
|
const out = [];
|
|
41
86
|
/** Text drawn upright whichever way the board is turned. */
|
|
42
|
-
const text = (x, y, content, className, extra = "") => `<text class="${className}" x="${n(x)}" y="${n(y)}" text-anchor="middle" dominant-baseline="central"${portrait ? ` transform="rotate(-90 ${n(x)} ${n(y)})"` : ""}${extra}>${
|
|
87
|
+
const text = (x, y, content, className, extra = "") => `<text class="${className}" x="${n(x)}" y="${n(y)}" text-anchor="middle" dominant-baseline="central"${portrait ? ` transform="rotate(-90 ${n(x)} ${n(y)})"` : ""}${extra}>${escapeXml(content)}</text>`;
|
|
43
88
|
// The frame, the field of play, the bar, and the trays' wells.
|
|
44
89
|
const field = { x: layout.fieldLeft, y: layout.fieldTop, w: 2 * POINT_WIDTH * 6 + BAR_WIDTH, h: layout.fieldHeight };
|
|
45
90
|
out.push(`<rect class="sg-frame" x="0" y="0" width="${n(layout.width)}" height="${n(layout.height)}" rx="16"/>`);
|
|
@@ -49,7 +94,12 @@ export function drawSugoroku(position, options = {}) {
|
|
|
49
94
|
out.push(`<rect class="sg-tray-well" x="${n(mx(layout.trayLeft, TRAY_WIDTH))}" y="${n(field.y)}" width="${TRAY_WIDTH}" height="${n(field.h)}" rx="4"/>`);
|
|
50
95
|
// The points, with the highlight on the one picked up and on those it may go to.
|
|
51
96
|
const mover = options.highlight?.side;
|
|
52
|
-
const fromBoard = mover !== undefined &&
|
|
97
|
+
const fromBoard = mover !== undefined &&
|
|
98
|
+
options.highlight?.from != null &&
|
|
99
|
+
options.highlight.from !== BAR &&
|
|
100
|
+
options.highlight.from > 0
|
|
101
|
+
? boardPoint(spec, mover, options.highlight.from)
|
|
102
|
+
: null;
|
|
53
103
|
const targetBoard = new Set();
|
|
54
104
|
let offTarget = false;
|
|
55
105
|
const movableBoard = new Set();
|
|
@@ -169,7 +219,7 @@ export function drawSugoroku(position, options = {}) {
|
|
|
169
219
|
const colours = colourStyle(options);
|
|
170
220
|
const theme = options.theme === undefined || options.theme === "auto" ? "" : ` data-theme="${options.theme}"`;
|
|
171
221
|
const inline = options.style === true ? `<style>${SUGOROKU_STYLE}</style>` : "";
|
|
172
|
-
return (`<svg xmlns="http://www.w3.org/2000/svg" class="sugoroku" viewBox="0 0 ${n(width)} ${n(height)}" role="img" aria-label="${
|
|
222
|
+
return (`<svg xmlns="http://www.w3.org/2000/svg" class="sugoroku" viewBox="0 0 ${n(width)} ${n(height)}" role="img" aria-label="${escapeXml(options.label ?? describe(position))}"` +
|
|
173
223
|
` data-orientation="${orientation}" data-view="${view}" data-home="${options.home ?? "right"}"${theme}${colours === "" ? "" : ` style="${colours}"`}>${inline}${root}</svg>`);
|
|
174
224
|
}
|
|
175
225
|
/** Half the strip above or below the points: where their numbers sit. */
|
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";
|