@johnmorrisdotca/sugoroku 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/README.md +395 -77
- 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 +27 -19
package/dist/match.d.ts
CHANGED
|
@@ -6,6 +6,17 @@ import { type Side } from "./side.ts";
|
|
|
6
6
|
* end in money play. It keeps the score and says which game is the Crawford
|
|
7
7
|
* game. Like everything here it is plain data, and never changed: each
|
|
8
8
|
* function gives back a new match.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { newMatch, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
13
|
+
* import type { Match } from "@johnmorrisdotca/sugoroku";
|
|
14
|
+
*
|
|
15
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
16
|
+
* const match = newMatch(settings);
|
|
17
|
+
* const value: Match = match;
|
|
18
|
+
* console.log(value);
|
|
19
|
+
* ```
|
|
9
20
|
*/
|
|
10
21
|
export type Match = {
|
|
11
22
|
readonly settings: Settings;
|
|
@@ -22,11 +33,62 @@ export type Match = {
|
|
|
22
33
|
/** Who won the match; null while it goes on, or when it was drawn. */
|
|
23
34
|
readonly winner: Side | null;
|
|
24
35
|
};
|
|
25
|
-
/** A match at 0 to 0.
|
|
36
|
+
/** A match at 0 to 0.
|
|
37
|
+
*
|
|
38
|
+
* @param settings - The resolved variant and complete rules.
|
|
39
|
+
* @returns A match at 0 to 0.
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { newMatch, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
43
|
+
*
|
|
44
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
45
|
+
* console.log(newMatch(settings).score);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
26
48
|
export declare function newMatch(settings: Settings): Match;
|
|
27
|
-
/** The first game, or the next game, of a match.
|
|
49
|
+
/** The first game, or the next game, of a match.
|
|
50
|
+
*
|
|
51
|
+
* @param match - The current immutable match and score.
|
|
52
|
+
* @returns The first game, or the next game, of a match.
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { newMatch, settingsFor, startGame } from "@johnmorrisdotca/sugoroku";
|
|
56
|
+
*
|
|
57
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
58
|
+
* const match = newMatch(settings);
|
|
59
|
+
* console.log(startGame(match).phase);
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
28
62
|
export declare function startGame(match: Match): GameState;
|
|
29
|
-
/** How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
63
|
+
/** How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
64
|
+
*
|
|
65
|
+
* @param match - The current immutable match and score.
|
|
66
|
+
* @param side - The side being examined: white or black.
|
|
67
|
+
* @returns How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { newMatch, pointsToGo, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
71
|
+
*
|
|
72
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
73
|
+
* const match = newMatch(settings);
|
|
74
|
+
* console.log(pointsToGo(match,"white"));
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
30
77
|
export declare function pointsToGo(match: Match, side: Side): number;
|
|
31
|
-
/** The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
78
|
+
/** The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
79
|
+
*
|
|
80
|
+
* @param match - The current immutable match and score.
|
|
81
|
+
* @param result - The completed native game result to add to a match.
|
|
82
|
+
* @returns The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { concede, finishGame, newGame, newMatch, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
86
|
+
*
|
|
87
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
88
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
89
|
+
* const match = newMatch(settings);
|
|
90
|
+
* const result = concede(game,"black","single").result!;
|
|
91
|
+
* console.log(finishGame(match,result).score);
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
32
94
|
export declare function finishGame(match: Match, result: GameResult): Match;
|
package/dist/match.js
CHANGED
|
@@ -1,21 +1,72 @@
|
|
|
1
1
|
import { newGame } from "./game.js";
|
|
2
2
|
import { sideIndex } from "./side.js";
|
|
3
|
-
/** A match at 0 to 0.
|
|
3
|
+
/** A match at 0 to 0.
|
|
4
|
+
*
|
|
5
|
+
* @param settings - The resolved variant and complete rules.
|
|
6
|
+
* @returns A match at 0 to 0.
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { newMatch, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
10
|
+
*
|
|
11
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
12
|
+
* console.log(newMatch(settings).score);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
4
15
|
export function newMatch(settings) {
|
|
5
16
|
return { settings, score: [0, 0], results: [], crawfordNext: false, crawfordPlayed: false, over: false, winner: null };
|
|
6
17
|
}
|
|
7
|
-
/** The first game, or the next game, of a match.
|
|
18
|
+
/** The first game, or the next game, of a match.
|
|
19
|
+
*
|
|
20
|
+
* @param match - The current immutable match and score.
|
|
21
|
+
* @returns The first game, or the next game, of a match.
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { newMatch, settingsFor, startGame } from "@johnmorrisdotca/sugoroku";
|
|
25
|
+
*
|
|
26
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
27
|
+
* const match = newMatch(settings);
|
|
28
|
+
* console.log(startGame(match).phase);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
8
31
|
export function startGame(match) {
|
|
9
32
|
if (match.over)
|
|
10
33
|
throw new Error("The match is over.");
|
|
11
34
|
return newGame(match.settings, { crawford: match.crawfordNext, score: match.score });
|
|
12
35
|
}
|
|
13
|
-
/** How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
36
|
+
/** How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
37
|
+
*
|
|
38
|
+
* @param match - The current immutable match and score.
|
|
39
|
+
* @param side - The side being examined: white or black.
|
|
40
|
+
* @returns How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { newMatch, pointsToGo, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
44
|
+
*
|
|
45
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
46
|
+
* const match = newMatch(settings);
|
|
47
|
+
* console.log(pointsToGo(match,"white"));
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
14
50
|
export function pointsToGo(match, side) {
|
|
15
51
|
const { points } = match.settings.rules;
|
|
16
52
|
return points === 0 ? 0 : Math.max(0, points - match.score[sideIndex(side)]);
|
|
17
53
|
}
|
|
18
|
-
/** The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
54
|
+
/** The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
55
|
+
*
|
|
56
|
+
* @param match - The current immutable match and score.
|
|
57
|
+
* @param result - The completed native game result to add to a match.
|
|
58
|
+
* @returns The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* import { concede, finishGame, newGame, newMatch, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
62
|
+
*
|
|
63
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
64
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
65
|
+
* const match = newMatch(settings);
|
|
66
|
+
* const result = concede(game,"black","single").result!;
|
|
67
|
+
* console.log(finishGame(match,result).score);
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
19
70
|
export function finishGame(match, result) {
|
|
20
71
|
const { points, crawford } = match.settings.rules;
|
|
21
72
|
const score = [match.score[0], match.score[1]];
|
package/dist/mount.d.ts
CHANGED
|
@@ -27,7 +27,16 @@ import type { VariantKey } from "./variants.ts";
|
|
|
27
27
|
* Needs a page. The rules it plays by are `game.ts`'s, the drawing is
|
|
28
28
|
* `drawSugoroku`'s and the opponent is `computer.ts`'s: all usable alone.
|
|
29
29
|
*/
|
|
30
|
-
/** How the board looks, apart from the game.
|
|
30
|
+
/** How the board looks, apart from the game.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import type { SugorokuLook } from "@johnmorrisdotca/sugoroku/play";
|
|
35
|
+
*
|
|
36
|
+
* const value: SugorokuLook = {board:"green",checkers:"contrast",numbers:true};
|
|
37
|
+
* console.log(value);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
31
40
|
export type SugorokuLook = {
|
|
32
41
|
board?: SugorokuBoardName;
|
|
33
42
|
checkers?: SugorokuCheckerSetName;
|
|
@@ -42,7 +51,20 @@ export type SugorokuLook = {
|
|
|
42
51
|
/** `auto` (the default) stands the board up in a narrow page, such as a phone's. */
|
|
43
52
|
orientation?: Orientation | "auto";
|
|
44
53
|
};
|
|
45
|
-
/** What every event of the board carries.
|
|
54
|
+
/** What every event of the board carries.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { newGame, newMatch, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
59
|
+
* import type { SugorokuEventDetail } from "@johnmorrisdotca/sugoroku/play";
|
|
60
|
+
*
|
|
61
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
62
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
63
|
+
* const match = newMatch(settings);
|
|
64
|
+
* const value: SugorokuEventDetail = {side:"white",game,match,dice:[3,1]};
|
|
65
|
+
* console.log(value);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
46
68
|
export type SugorokuEventDetail = {
|
|
47
69
|
/** The side it is about; null for the opening throw. */
|
|
48
70
|
side: Side | null;
|
|
@@ -61,6 +83,16 @@ export type SugorokuEventDetail = {
|
|
|
61
83
|
/** The record of the match so far, for `sugoroku-end`. */
|
|
62
84
|
record?: string;
|
|
63
85
|
};
|
|
86
|
+
/** Native variant, rules, seed, computer, display, control, sound and event-callback choices.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* import type { SugorokuMountOptions } from "@johnmorrisdotca/sugoroku/play";
|
|
91
|
+
*
|
|
92
|
+
* const value: SugorokuMountOptions = {variant:"backgammon",seed:42,computer:{black:"greedy"}};
|
|
93
|
+
* console.log(value);
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
64
96
|
export type SugorokuMountOptions = SugorokuLook & {
|
|
65
97
|
/** The variant. Default `backgammon`. */
|
|
66
98
|
variant?: VariantKey;
|
|
@@ -99,6 +131,23 @@ export type SugorokuMountOptions = SugorokuLook & {
|
|
|
99
131
|
onCube?: (detail: SugorokuEventDetail) => void;
|
|
100
132
|
onEnd?: (detail: SugorokuEventDetail) => void;
|
|
101
133
|
};
|
|
134
|
+
/** The mounted native board, current game/match/record, actions and cleanup handle.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts no-run
|
|
138
|
+
* import { newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
139
|
+
* import type { SugorokuMount } from "@johnmorrisdotca/sugoroku/play";
|
|
140
|
+
* import { mountSugoroku } from "@johnmorrisdotca/sugoroku/play";
|
|
141
|
+
*
|
|
142
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
143
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
144
|
+
* const host = document.createElement("div");
|
|
145
|
+
* document.body.append(host);
|
|
146
|
+
* const board: SugorokuMount = mountSugoroku(host,{variant:"backgammon",seed:42,computer:{white:null,black:null}});
|
|
147
|
+
* console.log(board.game().phase);
|
|
148
|
+
* board.destroy();
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
102
151
|
export type SugorokuMount = {
|
|
103
152
|
readonly host: HTMLElement;
|
|
104
153
|
/** The game as it stands. */
|
|
@@ -138,7 +187,29 @@ export type SugorokuMount = {
|
|
|
138
187
|
/** Take the board down: its listeners, its timers and everything it put in the host. */
|
|
139
188
|
destroy: () => void;
|
|
140
189
|
};
|
|
141
|
-
/** Put the style in the page once: in the document's head, or in the shadow root the host is in.
|
|
190
|
+
/** Put the style in the page once: in the document's head, or in the shadow root the host is in.
|
|
191
|
+
*
|
|
192
|
+
* @param host - The DOM element receiving the native board or scoped styles.
|
|
193
|
+
* @returns Nothing; the native scoped board styles are installed for the host.
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts no-run
|
|
196
|
+
* import { ensureSugorokuPlayStyle } from "@johnmorrisdotca/sugoroku/play";
|
|
197
|
+
*
|
|
198
|
+
* const host = document.createElement("div");
|
|
199
|
+
* ensureSugorokuPlayStyle(host);
|
|
200
|
+
* console.log(host.tagName);
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
142
203
|
export declare function ensureSugorokuPlayStyle(host: Element): void;
|
|
143
|
-
/** Mount a board into `host`.
|
|
204
|
+
/** Mount a board into `host`.
|
|
205
|
+
* @param host - The HTMLElement that owns the native interface.
|
|
206
|
+
* @param options - Native settings and callbacks for this mounted interface.
|
|
207
|
+
* @returns The native mounted handle, including controls and destroy() cleanup.
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* import { mountSugoroku } from "@johnmorrisdotca/sugoroku/play";
|
|
211
|
+
* const show = (host: HTMLElement, options: Parameters<typeof mountSugoroku>[1]) => mountSugoroku(host, options);
|
|
212
|
+
* console.log(typeof show); // function
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
144
215
|
export declare function mountSugoroku(host: HTMLElement, options?: SugorokuMountOptions): SugorokuMount;
|
package/dist/mount.js
CHANGED
|
@@ -13,7 +13,19 @@ import { presetByKey, settingsFor } from "./rules.js";
|
|
|
13
13
|
import { otherSide, sideIndex } from "./side.js";
|
|
14
14
|
import { createSounds, packagedSounds } from "./sound.js";
|
|
15
15
|
import { sugorokuLanguageOf, sugorokuSay } from "./strings.js";
|
|
16
|
-
/** Put the style in the page once: in the document's head, or in the shadow root the host is in.
|
|
16
|
+
/** Put the style in the page once: in the document's head, or in the shadow root the host is in.
|
|
17
|
+
*
|
|
18
|
+
* @param host - The DOM element receiving the native board or scoped styles.
|
|
19
|
+
* @returns Nothing; the native scoped board styles are installed for the host.
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts no-run
|
|
22
|
+
* import { ensureSugorokuPlayStyle } from "@johnmorrisdotca/sugoroku/play";
|
|
23
|
+
*
|
|
24
|
+
* const host = document.createElement("div");
|
|
25
|
+
* ensureSugorokuPlayStyle(host);
|
|
26
|
+
* console.log(host.tagName);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
17
29
|
export function ensureSugorokuPlayStyle(host) {
|
|
18
30
|
const root = host.getRootNode();
|
|
19
31
|
const target = typeof ShadowRoot !== "undefined" && root instanceof ShadowRoot ? root : host.ownerDocument.head;
|
|
@@ -31,7 +43,17 @@ function create(document, tag, className, text) {
|
|
|
31
43
|
element.textContent = text;
|
|
32
44
|
return element;
|
|
33
45
|
}
|
|
34
|
-
/** Mount a board into `host`.
|
|
46
|
+
/** Mount a board into `host`.
|
|
47
|
+
* @param host - The HTMLElement that owns the native interface.
|
|
48
|
+
* @param options - Native settings and callbacks for this mounted interface.
|
|
49
|
+
* @returns The native mounted handle, including controls and destroy() cleanup.
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { mountSugoroku } from "@johnmorrisdotca/sugoroku/play";
|
|
53
|
+
* const show = (host: HTMLElement, options: Parameters<typeof mountSugoroku>[1]) => mountSugoroku(host, options);
|
|
54
|
+
* console.log(typeof show); // function
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
35
57
|
export function mountSugoroku(host, options = {}) {
|
|
36
58
|
ensureSugorokuPlayStyle(host);
|
|
37
59
|
const document = host.ownerDocument;
|
|
@@ -104,7 +126,7 @@ export function mountSugoroku(host, options = {}) {
|
|
|
104
126
|
};
|
|
105
127
|
const eventDetail = (side, extra = {}) => ({ side, game, match, ...extra });
|
|
106
128
|
const emit = (name, detail, callback) => {
|
|
107
|
-
host.dispatchEvent(new view.CustomEvent(name, { detail, bubbles: true }));
|
|
129
|
+
host.dispatchEvent(new view.CustomEvent(name, { detail: JSON.parse(JSON.stringify(detail)), bubbles: true, composed: true }));
|
|
108
130
|
callback?.(detail);
|
|
109
131
|
};
|
|
110
132
|
const spec = () => game.settings.variant;
|
package/dist/moves.d.ts
CHANGED
|
@@ -6,6 +6,19 @@ import type { VariantSpec } from "./variants.ts";
|
|
|
6
6
|
* point `from` (or the `BAR`, 25, for a checker entering from the bar or from
|
|
7
7
|
* off the board at the start of a race) to its own point `to` (0 for borne
|
|
8
8
|
* off). A move that lands on a lone checker of the other side hits it.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { legalPlaysOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
13
|
+
* import type { Move } from "@johnmorrisdotca/sugoroku";
|
|
14
|
+
*
|
|
15
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
16
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
17
|
+
* const play = legalPlaysOf(game)[0];
|
|
18
|
+
* const move = play.moves[0];
|
|
19
|
+
* const value: Move = move;
|
|
20
|
+
* console.log(value);
|
|
21
|
+
* ```
|
|
9
22
|
*/
|
|
10
23
|
export type Move = {
|
|
11
24
|
readonly from: number;
|
|
@@ -22,7 +35,41 @@ export type Move = {
|
|
|
22
35
|
* board (in a race) whenever it likes; and a side whose checkers are all
|
|
23
36
|
* home may bear off, by the exact die, or by a larger one from the highest
|
|
24
37
|
* point it has a checker on.
|
|
38
|
+
*
|
|
39
|
+
* @param spec - The native variant specification.
|
|
40
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
41
|
+
* @param side - The side being examined: white or black.
|
|
42
|
+
* @param die - One valid die value, from 1 to 6.
|
|
43
|
+
* @returns Every move of one die that `side` may make from `position`, one for each point a checker can start from. A side with a checker on the bar must enter it first; a side that has not borne off may start a checker off the board (in a race) whenever it likes; and a side whose checkers are all home may bear off, by the exact die, or by a larger one from the highest point it has a checker on.
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { movesOfDie, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
47
|
+
*
|
|
48
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
49
|
+
* const spec = settings.variant;
|
|
50
|
+
* const position = startPosition(spec);
|
|
51
|
+
* console.log(movesOfDie(spec,position,"white",3));
|
|
52
|
+
* ```
|
|
25
53
|
*/
|
|
26
54
|
export declare function movesOfDie(spec: VariantSpec, position: Position, side: Side, die: number): Move[];
|
|
27
|
-
/** The position after a move. Does not check that the move is legal.
|
|
55
|
+
/** The position after a move. Does not check that the move is legal.
|
|
56
|
+
*
|
|
57
|
+
* @param spec - The native variant specification.
|
|
58
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
59
|
+
* @param side - The side being examined: white or black.
|
|
60
|
+
* @param move - A native legal move, including die and hit status.
|
|
61
|
+
* @returns The position after a move. Does not check that the move is legal.
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { applyMove, legalPlaysOf, newGame, rollOpening, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
65
|
+
*
|
|
66
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
67
|
+
* const spec = settings.variant;
|
|
68
|
+
* const position = startPosition(spec);
|
|
69
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
70
|
+
* const play = legalPlaysOf(game)[0];
|
|
71
|
+
* const move = play.moves[0];
|
|
72
|
+
* console.log(applyMove(spec,position,"white",move).bar);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
28
75
|
export declare function applyMove(spec: VariantSpec, position: Position, side: Side, move: Move): Position;
|
package/dist/moves.js
CHANGED
|
@@ -23,6 +23,21 @@ function rearPoint(position, side) {
|
|
|
23
23
|
* board (in a race) whenever it likes; and a side whose checkers are all
|
|
24
24
|
* home may bear off, by the exact die, or by a larger one from the highest
|
|
25
25
|
* point it has a checker on.
|
|
26
|
+
*
|
|
27
|
+
* @param spec - The native variant specification.
|
|
28
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
29
|
+
* @param side - The side being examined: white or black.
|
|
30
|
+
* @param die - One valid die value, from 1 to 6.
|
|
31
|
+
* @returns Every move of one die that `side` may make from `position`, one for each point a checker can start from. A side with a checker on the bar must enter it first; a side that has not borne off may start a checker off the board (in a race) whenever it likes; and a side whose checkers are all home may bear off, by the exact die, or by a larger one from the highest point it has a checker on.
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { movesOfDie, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
35
|
+
*
|
|
36
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
37
|
+
* const spec = settings.variant;
|
|
38
|
+
* const position = startPosition(spec);
|
|
39
|
+
* console.log(movesOfDie(spec,position,"white",3));
|
|
40
|
+
* ```
|
|
26
41
|
*/
|
|
27
42
|
export function movesOfDie(spec, position, side, die) {
|
|
28
43
|
const i = sideIndex(side);
|
|
@@ -50,7 +65,26 @@ export function movesOfDie(spec, position, side, die) {
|
|
|
50
65
|
}
|
|
51
66
|
return moves;
|
|
52
67
|
}
|
|
53
|
-
/** The position after a move. Does not check that the move is legal.
|
|
68
|
+
/** The position after a move. Does not check that the move is legal.
|
|
69
|
+
*
|
|
70
|
+
* @param spec - The native variant specification.
|
|
71
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
72
|
+
* @param side - The side being examined: white or black.
|
|
73
|
+
* @param move - A native legal move, including die and hit status.
|
|
74
|
+
* @returns The position after a move. Does not check that the move is legal.
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { applyMove, legalPlaysOf, newGame, rollOpening, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
78
|
+
*
|
|
79
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
80
|
+
* const spec = settings.variant;
|
|
81
|
+
* const position = startPosition(spec);
|
|
82
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
83
|
+
* const play = legalPlaysOf(game)[0];
|
|
84
|
+
* const move = play.moves[0];
|
|
85
|
+
* console.log(applyMove(spec,position,"white",move).bar);
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
54
88
|
export function applyMove(spec, position, side, move) {
|
|
55
89
|
const i = sideIndex(side);
|
|
56
90
|
const j = sideIndex(otherSide(side));
|
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;
|