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