@johnmorrisdotca/sugoroku 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +78 -68
- package/dist/board.d.ts +216 -12
- package/dist/board.js +204 -12
- package/dist/colours.d.ts +80 -8
- package/dist/colours.js +51 -6
- package/dist/computer.d.ts +138 -8
- package/dist/computer.js +109 -7
- package/dist/dice.d.ts +53 -4
- package/dist/dice.js +42 -3
- package/dist/draw.d.ts +43 -3
- package/dist/draw.js +33 -2
- package/dist/element-define.d.ts +1 -1
- package/dist/element-define.js +4 -13
- package/dist/element.d.ts +233 -3
- package/dist/element.js +402 -48
- package/dist/evaluate.d.ts +76 -4
- package/dist/evaluate.js +65 -3
- package/dist/game.d.ts +408 -23
- package/dist/game.js +328 -18
- package/dist/geometry.d.ts +50 -3
- package/dist/geometry.js +20 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/match.d.ts +66 -4
- package/dist/match.js +55 -4
- package/dist/mount.d.ts +75 -4
- package/dist/mount.js +25 -3
- package/dist/moves.d.ts +48 -1
- package/dist/moves.js +35 -1
- package/dist/notation.d.ts +92 -4
- package/dist/notation.js +82 -3
- package/dist/playStyle.d.ts +6 -0
- package/dist/playStyle.js +6 -0
- package/dist/plays.d.ts +108 -3
- package/dist/plays.js +96 -3
- package/dist/positionId.d.ts +29 -1
- package/dist/positionId.js +29 -1
- package/dist/random.d.ts +33 -3
- package/dist/random.js +22 -2
- package/dist/record.d.ts +150 -7
- package/dist/record.js +94 -5
- package/dist/rules.d.ts +130 -10
- package/dist/rules.js +99 -9
- package/dist/side.d.ts +38 -3
- package/dist/side.js +30 -3
- package/dist/sound.d.ts +61 -3
- package/dist/sound.js +27 -2
- package/dist/strings.d.ts +42 -2
- package/dist/strings.js +34 -2
- package/dist/style.d.ts +6 -0
- package/dist/style.js +6 -0
- package/dist/variants.d.ts +113 -8
- package/dist/variants.js +62 -5
- package/dist/version.d.ts +9 -2
- package/dist/version.js +9 -2
- package/package.json +25 -18
package/dist/evaluate.js
CHANGED
|
@@ -53,7 +53,14 @@ function rollsThatHit(distances) {
|
|
|
53
53
|
}
|
|
54
54
|
return n;
|
|
55
55
|
}
|
|
56
|
-
/** The weights the computer plays with: set by hand and then held against random changes of themselves, none of which won more often over two thousand games.
|
|
56
|
+
/** The weights the computer plays with: set by hand and then held against random changes of themselves, none of which won more often over two thousand games.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { EVALUATION_WEIGHTS } from "@johnmorrisdotca/sugoroku";
|
|
61
|
+
* console.log(EVALUATION_WEIGHTS);
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
57
64
|
export const EVALUATION_WEIGHTS = {
|
|
58
65
|
onRoll: 4,
|
|
59
66
|
raceChecker: 1.6,
|
|
@@ -87,6 +94,21 @@ function distancesTo(counts, onBarOrReserve, target) {
|
|
|
87
94
|
* The score of a position for `side`, with the other side to move, as a game
|
|
88
95
|
* where bearing off first wins. Large positive or negative numbers are a game
|
|
89
96
|
* won or lost; `evaluateFor` turns it over for a game played to lose.
|
|
97
|
+
*
|
|
98
|
+
* @param spec - The native variant specification.
|
|
99
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
100
|
+
* @param side - The side being examined: white or black.
|
|
101
|
+
* @param weights - Optional weights for the native position evaluation.
|
|
102
|
+
* @returns The score of a position for `side`, with the other side to move, as a game where bearing off first wins. Large positive or negative numbers are a game won or lost; `evaluateFor` turns it over for a game played to lose.
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* import { evaluate, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
106
|
+
*
|
|
107
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
108
|
+
* const spec = settings.variant;
|
|
109
|
+
* const position = startPosition(spec);
|
|
110
|
+
* console.log(evaluate(spec,position,"white"));
|
|
111
|
+
* ```
|
|
90
112
|
*/
|
|
91
113
|
export function evaluate(spec, position, side, weights = EVALUATION_WEIGHTS) {
|
|
92
114
|
const i = sideIndex(side);
|
|
@@ -159,12 +181,38 @@ export function evaluate(spec, position, side, weights = EVALUATION_WEIGHTS) {
|
|
|
159
181
|
score += oppBar * (w.bar + w.barBoard * myHome) * w.barOther;
|
|
160
182
|
return score;
|
|
161
183
|
}
|
|
162
|
-
/** The score as `side` plays to win or to lose: a game played to lose is judged the other way up.
|
|
184
|
+
/** The score as `side` plays to win or to lose: a game played to lose is judged the other way up.
|
|
185
|
+
*
|
|
186
|
+
* @param spec - The native variant specification.
|
|
187
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
188
|
+
* @param side - The side being examined: white or black.
|
|
189
|
+
* @param weights - Optional weights for the native position evaluation.
|
|
190
|
+
* @returns The score as `side` plays to win or to lose: a game played to lose is judged the other way up.
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* import { evaluateFor, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
194
|
+
*
|
|
195
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
196
|
+
* const spec = settings.variant;
|
|
197
|
+
* const position = startPosition(spec);
|
|
198
|
+
* console.log(evaluateFor(spec,position,"white"));
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
163
201
|
export function evaluateFor(spec, position, side, weights = EVALUATION_WEIGHTS) {
|
|
164
202
|
const score = evaluate(spec, position, side, weights);
|
|
165
203
|
return spec.goal === "first-off" ? score : -score;
|
|
166
204
|
}
|
|
167
|
-
/** A chance of winning, 0 to 1, that a score in pips stands for: the logistic curve, which a lead of 25 pips in a position of this kind takes to about three in four.
|
|
205
|
+
/** A chance of winning, 0 to 1, that a score in pips stands for: the logistic curve, which a lead of 25 pips in a position of this kind takes to about three in four.
|
|
206
|
+
*
|
|
207
|
+
* @param score - The evaluation difference to map to a win chance.
|
|
208
|
+
* @returns A chance of winning, 0 to 1, that a score in pips stands for: the logistic curve, which a lead of 25 pips in a position of this kind takes to about three in four.
|
|
209
|
+
* @example
|
|
210
|
+
* ```ts
|
|
211
|
+
* import { winChance } from "@johnmorrisdotca/sugoroku";
|
|
212
|
+
*
|
|
213
|
+
* console.log(winChance(0));
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
168
216
|
export function winChance(score) {
|
|
169
217
|
return 1 / (1 + Math.exp(-score / 22));
|
|
170
218
|
}
|
|
@@ -174,6 +222,20 @@ export function winChance(score) {
|
|
|
174
222
|
* 1-point, 1 for each beyond the first on the 2-point and beyond three on the
|
|
175
223
|
* 3-point, and 1 for each empty point among the 4, 5 and 6, and then the side
|
|
176
224
|
* on roll adds a seventh (rounded down). For races only.
|
|
225
|
+
*
|
|
226
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
227
|
+
* @param side - The side being examined: white or black.
|
|
228
|
+
* @param onRoll - The side to roll, or whether this evaluated side is on roll, specified by the signature.
|
|
229
|
+
* @returns The Keith count of a side, a pip count corrected for how badly the checkers are placed for bearing off: plus 2 for each checker beyond the first on the 1-point, 1 for each beyond the first on the 2-point and beyond three on the 3-point, and 1 for each empty point among the 4, 5 and 6, and then the side on roll adds a seventh (rounded down). For races only.
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* import { keithCount, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
233
|
+
*
|
|
234
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
235
|
+
* const spec = settings.variant;
|
|
236
|
+
* const position = startPosition(spec);
|
|
237
|
+
* console.log(keithCount(position,"white",true));
|
|
238
|
+
* ```
|
|
177
239
|
*/
|
|
178
240
|
export function keithCount(position, side, onRoll) {
|
|
179
241
|
const points = position.points[sideIndex(side)];
|
package/dist/game.d.ts
CHANGED
|
@@ -3,7 +3,19 @@ import { type Move } from "./moves.ts";
|
|
|
3
3
|
import { type Play } from "./plays.ts";
|
|
4
4
|
import { type Settings } from "./rules.ts";
|
|
5
5
|
import { type Side } from "./side.ts";
|
|
6
|
-
/** Where a game has got to.
|
|
6
|
+
/** Where a game has got to.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
11
|
+
* import type { Phase } from "@johnmorrisdotca/sugoroku";
|
|
12
|
+
*
|
|
13
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
14
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
15
|
+
* const value: Phase = game.phase;
|
|
16
|
+
* console.log(value);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
7
19
|
export type Phase =
|
|
8
20
|
/** Nobody has the move yet: the sides are throwing a die each to see who starts. */
|
|
9
21
|
"opening"
|
|
@@ -17,12 +29,30 @@ export type Phase =
|
|
|
17
29
|
| "playing"
|
|
18
30
|
/** The game is decided: see `result`. */
|
|
19
31
|
| "over";
|
|
20
|
-
/** The doubling cube: what it shows, and who may turn it (null while it stands in the middle, for either).
|
|
32
|
+
/** The doubling cube: what it shows, and who may turn it (null while it stands in the middle, for either).
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import type { Cube } from "@johnmorrisdotca/sugoroku";
|
|
37
|
+
*
|
|
38
|
+
* const value: Cube = {value:1,owner:null};
|
|
39
|
+
* console.log(value);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
21
42
|
export type Cube = {
|
|
22
43
|
readonly value: number;
|
|
23
44
|
readonly owner: Side | null;
|
|
24
45
|
};
|
|
25
|
-
/** How a game came to its end.
|
|
46
|
+
/** How a game came to its end.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* import type { HowItEnded } from "@johnmorrisdotca/sugoroku";
|
|
51
|
+
*
|
|
52
|
+
* const value: HowItEnded = "bear-off";
|
|
53
|
+
* console.log(value);
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
26
56
|
export type HowItEnded =
|
|
27
57
|
/** Somebody bore off every checker. */
|
|
28
58
|
"bear-off"
|
|
@@ -32,8 +62,31 @@ export type HowItEnded =
|
|
|
32
62
|
| "concede"
|
|
33
63
|
/** Neither side won. */
|
|
34
64
|
| "draw";
|
|
35
|
-
/** What kind of win it was, which is how many times the cube's value it scores.
|
|
65
|
+
/** What kind of win it was, which is how many times the cube's value it scores.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* import type { WinKind } from "@johnmorrisdotca/sugoroku";
|
|
70
|
+
*
|
|
71
|
+
* const value: WinKind = "gammon";
|
|
72
|
+
* console.log(value);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
36
75
|
export type WinKind = "single" | "gammon" | "backgammon";
|
|
76
|
+
/** The decided winner, ending, win kind, cube multiplier and points.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* import { concede, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
81
|
+
* import type { GameResult } from "@johnmorrisdotca/sugoroku";
|
|
82
|
+
*
|
|
83
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
84
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
85
|
+
* const result = concede(game,"black","single").result!;
|
|
86
|
+
* const value: GameResult = result;
|
|
87
|
+
* console.log(value);
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
37
90
|
export type GameResult = {
|
|
38
91
|
/** The side that won, or null for a draw. */
|
|
39
92
|
readonly winner: Side | null;
|
|
@@ -46,6 +99,19 @@ export type GameResult = {
|
|
|
46
99
|
/** What the winner scores: the multiplier times the cube. 0 for a draw. */
|
|
47
100
|
readonly points: number;
|
|
48
101
|
};
|
|
102
|
+
/** The immutable game phase, board, dice, remaining moves, cube and optional result.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
107
|
+
* import type { GameState } from "@johnmorrisdotca/sugoroku";
|
|
108
|
+
*
|
|
109
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
110
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
111
|
+
* const value: GameState = game;
|
|
112
|
+
* console.log(value);
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
49
115
|
export type GameState = {
|
|
50
116
|
readonly settings: Settings;
|
|
51
117
|
/** Whether this is the Crawford game of a match: no cube. */
|
|
@@ -75,81 +141,400 @@ export type GameState = {
|
|
|
75
141
|
readonly opening: readonly [number, number] | null;
|
|
76
142
|
readonly result: GameResult | null;
|
|
77
143
|
};
|
|
78
|
-
/** Options for a new game.
|
|
144
|
+
/** Options for a new game.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* import type { NewGameOptions } from "@johnmorrisdotca/sugoroku";
|
|
149
|
+
*
|
|
150
|
+
* const value: NewGameOptions = {crawford:false,score:[0,0]};
|
|
151
|
+
* console.log(value);
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
79
154
|
export type NewGameOptions = {
|
|
80
155
|
/** Whether it is the Crawford game of a match: played without the cube. */
|
|
81
156
|
crawford?: boolean;
|
|
82
157
|
/** The score of the match so far, `[white, black]`. Default 0 to 0. */
|
|
83
158
|
score?: readonly [number, number];
|
|
84
159
|
};
|
|
85
|
-
/** A new game, waiting for the opening throw.
|
|
160
|
+
/** A new game, waiting for the opening throw.
|
|
161
|
+
*
|
|
162
|
+
* @param settings - The resolved variant and complete rules.
|
|
163
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
164
|
+
* @returns A new game, waiting for the opening throw.
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* import { newGame, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
168
|
+
*
|
|
169
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
170
|
+
* console.log(newGame(settings).phase);
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
86
173
|
export declare function newGame(settings: Settings, options?: NewGameOptions): GameState;
|
|
87
174
|
/**
|
|
88
175
|
* The opening throw: one die for white and one for black. The higher starts.
|
|
89
176
|
* A tie changes nothing but `opening`, and the sides throw again. In a game
|
|
90
177
|
* opened with a roll-off the starter plays the two dice as the first roll;
|
|
91
178
|
* otherwise the starter then rolls as usual.
|
|
179
|
+
*
|
|
180
|
+
* @param game - The current immutable native game state.
|
|
181
|
+
* @param throws - The opening die for white, then black.
|
|
182
|
+
* @returns The opening throw: one die for white and one for black. The higher starts. A tie changes nothing but `opening`, and the sides throw again. In a game opened with a roll-off the starter plays the two dice as the first roll; otherwise the starter then rolls as usual.
|
|
183
|
+
* @example
|
|
184
|
+
* ```ts
|
|
185
|
+
* import { newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
186
|
+
*
|
|
187
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
188
|
+
* console.log(rollOpening(newGame(settings),[3,1]).turn);
|
|
189
|
+
* ```
|
|
92
190
|
*/
|
|
93
191
|
export declare function rollOpening(game: GameState, throws: readonly [number, number]): GameState;
|
|
94
|
-
/** Roll the dice for the side on turn, which must not be waiting on an answer to a double.
|
|
192
|
+
/** Roll the dice for the side on turn, which must not be waiting on an answer to a double.
|
|
193
|
+
*
|
|
194
|
+
* @param game - The current immutable native game state.
|
|
195
|
+
* @param dice - Dice values as thrown, or a reproducible source specified by the signature.
|
|
196
|
+
* @returns Roll the dice for the side on turn, which must not be waiting on an answer to a double.
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* import { endTurn, legalPlaysOf, newGame, playTurn, rollDice, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
200
|
+
*
|
|
201
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
202
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
203
|
+
* const play = legalPlaysOf(game)[0];
|
|
204
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
205
|
+
* console.log(rollDice(beforeRoll,[6,2]).dice);
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
95
208
|
export declare function rollDice(game: GameState, dice: readonly number[]): GameState;
|
|
96
|
-
/** The moves the side on turn may make next. Empty when the turn is played out, or nothing can be played.
|
|
209
|
+
/** The moves the side on turn may make next. Empty when the turn is played out, or nothing can be played.
|
|
210
|
+
*
|
|
211
|
+
* @param game - The current immutable native game state.
|
|
212
|
+
* @returns The moves the side on turn may make next. Empty when the turn is played out, or nothing can be played.
|
|
213
|
+
* @example
|
|
214
|
+
* ```ts
|
|
215
|
+
* import { legalMoves, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
216
|
+
*
|
|
217
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
218
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
219
|
+
* console.log(legalMoves(game));
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
97
222
|
export declare function legalMoves(game: GameState): Move[];
|
|
98
|
-
/** Every different way the side on turn may play out the rest of its turn: for the dice it has yet to play, from where the position stands now.
|
|
223
|
+
/** Every different way the side on turn may play out the rest of its turn: for the dice it has yet to play, from where the position stands now.
|
|
224
|
+
*
|
|
225
|
+
* @param game - The current immutable native game state.
|
|
226
|
+
* @returns Every different way the side on turn may play out the rest of its turn: for the dice it has yet to play, from where the position stands now.
|
|
227
|
+
* @example
|
|
228
|
+
* ```ts
|
|
229
|
+
* import { legalPlaysOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
230
|
+
*
|
|
231
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
232
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
233
|
+
* console.log(legalPlaysOf(game).length);
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
99
236
|
export declare function legalPlaysOf(game: GameState): Play[];
|
|
100
|
-
/** Whether the turn is played out and the side on turn has only to end it.
|
|
237
|
+
/** Whether the turn is played out and the side on turn has only to end it.
|
|
238
|
+
*
|
|
239
|
+
* @param game - The current immutable native game state.
|
|
240
|
+
* @returns Whether the turn is played out and the side on turn has only to end it.
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* import { legalPlaysOf, newGame, playTurn, rollOpening, settingsFor, turnIsPlayed } from "@johnmorrisdotca/sugoroku";
|
|
244
|
+
*
|
|
245
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
246
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
247
|
+
* const play = legalPlaysOf(game)[0];
|
|
248
|
+
* console.log(turnIsPlayed(playTurn(game,play.moves)));
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
101
251
|
export declare function turnIsPlayed(game: GameState): boolean;
|
|
102
252
|
/**
|
|
103
253
|
* Make one move for the side on turn, given as the checker's own point it
|
|
104
254
|
* leaves and the one it goes to. The move must be one of `legalMoves`. Where
|
|
105
255
|
* a checker could be borne off by either of two dice, the smaller is used.
|
|
256
|
+
*
|
|
257
|
+
* @param game - The current immutable native game state.
|
|
258
|
+
* @param step - A requested source and destination from the current legal moves.
|
|
259
|
+
* @returns Make one move for the side on turn, given as the checker's own point it leaves and the one it goes to. The move must be one of `legalMoves`. Where a checker could be borne off by either of two dice, the smaller is used.
|
|
260
|
+
* @example
|
|
261
|
+
* ```ts
|
|
262
|
+
* import { legalPlaysOf, newGame, playMove, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
263
|
+
*
|
|
264
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
265
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
266
|
+
* const play = legalPlaysOf(game)[0];
|
|
267
|
+
* const move = play.moves[0];
|
|
268
|
+
* console.log(playMove(game,move).moves.length);
|
|
269
|
+
* ```
|
|
106
270
|
*/
|
|
107
271
|
export declare function playMove(game: GameState, step: {
|
|
108
272
|
from: number;
|
|
109
273
|
to: number;
|
|
110
274
|
}): GameState;
|
|
111
|
-
/** Take back the last move made this turn. Does nothing if none has been made.
|
|
275
|
+
/** Take back the last move made this turn. Does nothing if none has been made.
|
|
276
|
+
*
|
|
277
|
+
* @param game - The current immutable native game state.
|
|
278
|
+
* @returns Take back the last move made this turn. Does nothing if none has been made.
|
|
279
|
+
* @example
|
|
280
|
+
* ```ts
|
|
281
|
+
* import { legalPlaysOf, newGame, playMove, rollOpening, settingsFor, undoMove } from "@johnmorrisdotca/sugoroku";
|
|
282
|
+
*
|
|
283
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
284
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
285
|
+
* const play = legalPlaysOf(game)[0];
|
|
286
|
+
* const move = play.moves[0];
|
|
287
|
+
* console.log(undoMove(playMove(game,move)).moves.length);
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
112
290
|
export declare function undoMove(game: GameState): GameState;
|
|
113
|
-
/** Take back every move of the turn.
|
|
291
|
+
/** Take back every move of the turn.
|
|
292
|
+
*
|
|
293
|
+
* @param game - The current immutable native game state.
|
|
294
|
+
* @returns Take back every move of the turn.
|
|
295
|
+
* @example
|
|
296
|
+
* ```ts
|
|
297
|
+
* import { legalPlaysOf, newGame, playMove, restartTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
298
|
+
*
|
|
299
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
300
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
301
|
+
* const play = legalPlaysOf(game)[0];
|
|
302
|
+
* const move = play.moves[0];
|
|
303
|
+
* console.log(restartTurn(playMove(game,move)).moves.length);
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
114
306
|
export declare function restartTurn(game: GameState): GameState;
|
|
115
|
-
/** Make a whole play (the moves of `legalPlays`) for the side on turn and end the turn.
|
|
307
|
+
/** Make a whole play (the moves of `legalPlays`) for the side on turn and end the turn.
|
|
308
|
+
*
|
|
309
|
+
* @param game - The current immutable native game state.
|
|
310
|
+
* @param moves - Legal checker moves in their played order.
|
|
311
|
+
* @returns Make a whole play (the moves of `legalPlays`) for the side on turn and end the turn.
|
|
312
|
+
* @example
|
|
313
|
+
* ```ts
|
|
314
|
+
* import { legalPlaysOf, newGame, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
315
|
+
*
|
|
316
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
317
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
318
|
+
* const play = legalPlaysOf(game)[0];
|
|
319
|
+
* console.log(playTurn(game,play.moves).need);
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
116
322
|
export declare function playTurn(game: GameState, moves: readonly Move[]): GameState;
|
|
117
|
-
/** End the side on turn's turn, once everything it had to play has been played. The other side is to move.
|
|
323
|
+
/** End the side on turn's turn, once everything it had to play has been played. The other side is to move.
|
|
324
|
+
*
|
|
325
|
+
* @param game - The current immutable native game state.
|
|
326
|
+
* @returns End the side on turn's turn, once everything it had to play has been played. The other side is to move.
|
|
327
|
+
* @example
|
|
328
|
+
* ```ts
|
|
329
|
+
* import { endTurn, legalPlaysOf, newGame, playMove, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
330
|
+
*
|
|
331
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
332
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
333
|
+
* const play = legalPlaysOf(game)[0];
|
|
334
|
+
* const beforeRoll = endTurn(play.moves.reduce((state,step)=>playMove(state,step),game));
|
|
335
|
+
* console.log(beforeRoll.phase);
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
118
338
|
export declare function endTurn(game: GameState): GameState;
|
|
119
|
-
/** The kind of win a game that `winner` has won by bearing off is: single, gammon or backgammon, before the rules say what they count for.
|
|
339
|
+
/** The kind of win a game that `winner` has won by bearing off is: single, gammon or backgammon, before the rules say what they count for.
|
|
340
|
+
*
|
|
341
|
+
* @param game - The current immutable native game state.
|
|
342
|
+
* @param winner - The side whose win kind is evaluated.
|
|
343
|
+
* @returns The kind of win a game that `winner` has won by bearing off is: single, gammon or backgammon, before the rules say what they count for.
|
|
344
|
+
* @example
|
|
345
|
+
* ```ts
|
|
346
|
+
* import { newGame, rollOpening, settingsFor, winKind } from "@johnmorrisdotca/sugoroku";
|
|
347
|
+
*
|
|
348
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
349
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
350
|
+
* console.log(winKind(game,"white"));
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
120
353
|
export declare function winKind(game: GameState, winner: Side): WinKind;
|
|
121
|
-
/** What a win of `kind` is worth, after the rules: a single win when gammons do not count, or when the Jacoby rule holds them back because the cube was never turned.
|
|
354
|
+
/** What a win of `kind` is worth, after the rules: a single win when gammons do not count, or when the Jacoby rule holds them back because the cube was never turned.
|
|
355
|
+
*
|
|
356
|
+
* @param game - The current immutable native game state.
|
|
357
|
+
* @param kind - The native single, gammon or backgammon result.
|
|
358
|
+
* @returns What a win of `kind` is worth, after the rules: a single win when gammons do not count, or when the Jacoby rule holds them back because the cube was never turned.
|
|
359
|
+
* @example
|
|
360
|
+
* ```ts
|
|
361
|
+
* import { multiplierOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
362
|
+
*
|
|
363
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
364
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
365
|
+
* console.log(multiplierOf(game,"gammon"));
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
122
368
|
export declare function multiplierOf(game: GameState, kind: WinKind): number;
|
|
123
|
-
/** Whether the side on turn may offer a double now.
|
|
369
|
+
/** Whether the side on turn may offer a double now.
|
|
370
|
+
*
|
|
371
|
+
* @param game - The current immutable native game state.
|
|
372
|
+
* @returns Whether the side on turn may offer a double now.
|
|
373
|
+
* @example
|
|
374
|
+
* ```ts
|
|
375
|
+
* import { canDouble, endTurn, legalPlaysOf, newGame, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
376
|
+
*
|
|
377
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
378
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
379
|
+
* const play = legalPlaysOf(game)[0];
|
|
380
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
381
|
+
* console.log(canDouble(beforeRoll));
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
124
384
|
export declare function canDouble(game: GameState): boolean;
|
|
125
|
-
/** The cube is dead when turning it could not matter: it already shows enough for either side to win the match with a win. Never in money play.
|
|
385
|
+
/** The cube is dead when turning it could not matter: it already shows enough for either side to win the match with a win. Never in money play.
|
|
386
|
+
*
|
|
387
|
+
* @param game - The current immutable native game state.
|
|
388
|
+
* @returns The cube is dead when turning it could not matter: it already shows enough for either side to win the match with a win. Never in money play.
|
|
389
|
+
* @example
|
|
390
|
+
* ```ts
|
|
391
|
+
* import { cubeIsDead, endTurn, legalPlaysOf, newGame, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
392
|
+
*
|
|
393
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
394
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
395
|
+
* const play = legalPlaysOf(game)[0];
|
|
396
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
397
|
+
* console.log(cubeIsDead(beforeRoll));
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
126
400
|
export declare function cubeIsDead(game: GameState): boolean;
|
|
127
|
-
/** Offer a double: the other side must now take it or drop it.
|
|
401
|
+
/** Offer a double: the other side must now take it or drop it.
|
|
402
|
+
*
|
|
403
|
+
* @param game - The current immutable native game state.
|
|
404
|
+
* @returns Offer a double: the other side must now take it or drop it.
|
|
405
|
+
* @example
|
|
406
|
+
* ```ts
|
|
407
|
+
* import { endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
408
|
+
*
|
|
409
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
410
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
411
|
+
* const play = legalPlaysOf(game)[0];
|
|
412
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
413
|
+
* console.log(offerDouble(beforeRoll).phase);
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
128
416
|
export declare function offerDouble(game: GameState): GameState;
|
|
129
417
|
/**
|
|
130
418
|
* Take the double that was offered. The cube turns and goes to the side that
|
|
131
419
|
* took it, and the side on turn rolls. (A beaver taken turns it twice, and
|
|
132
420
|
* leaves it with the side that beavered.)
|
|
421
|
+
*
|
|
422
|
+
* @param game - The current immutable native game state.
|
|
423
|
+
* @returns Take the double that was offered. The cube turns and goes to the side that took it, and the side on turn rolls. (A beaver taken turns it twice, and leaves it with the side that beavered.)
|
|
424
|
+
* @example
|
|
425
|
+
* ```ts
|
|
426
|
+
* import { endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor, takeDouble } from "@johnmorrisdotca/sugoroku";
|
|
427
|
+
*
|
|
428
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
429
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
430
|
+
* const play = legalPlaysOf(game)[0];
|
|
431
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
432
|
+
* const offered = offerDouble(beforeRoll);
|
|
433
|
+
* console.log(takeDouble(offered).cube);
|
|
434
|
+
* ```
|
|
133
435
|
*/
|
|
134
436
|
export declare function takeDouble(game: GameState): GameState;
|
|
135
437
|
/**
|
|
136
438
|
* Drop the double that was offered: the side that drops it loses the game, at
|
|
137
439
|
* the cube's value before the double, or for a beaver at its value once the
|
|
138
440
|
* double was taken.
|
|
441
|
+
*
|
|
442
|
+
* @param game - The current immutable native game state.
|
|
443
|
+
* @returns Drop the double that was offered: the side that drops it loses the game, at the cube's value before the double, or for a beaver at its value once the double was taken.
|
|
444
|
+
* @example
|
|
445
|
+
* ```ts
|
|
446
|
+
* import { concede, dropDouble, endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
447
|
+
*
|
|
448
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
449
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
450
|
+
* const play = legalPlaysOf(game)[0];
|
|
451
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
452
|
+
* const offered = offerDouble(beforeRoll);
|
|
453
|
+
* const result = concede(game,"black","single").result!;
|
|
454
|
+
* console.log(dropDouble(offered).result);
|
|
455
|
+
* ```
|
|
139
456
|
*/
|
|
140
457
|
export declare function dropDouble(game: GameState): GameState;
|
|
141
|
-
/** Whether the side that was doubled may beaver: redouble at once and keep the cube. Only where the rules allow it.
|
|
458
|
+
/** Whether the side that was doubled may beaver: redouble at once and keep the cube. Only where the rules allow it.
|
|
459
|
+
*
|
|
460
|
+
* @param game - The current immutable native game state.
|
|
461
|
+
* @returns Whether the side that was doubled may beaver: redouble at once and keep the cube. Only where the rules allow it.
|
|
462
|
+
* @example
|
|
463
|
+
* ```ts
|
|
464
|
+
* import { canBeaver, endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
465
|
+
*
|
|
466
|
+
* const moneySettings = settingsFor("backgammon",{points:0,cube:true,beaver:true});
|
|
467
|
+
* const moneyGame = rollOpening(newGame(moneySettings),[3,1]);
|
|
468
|
+
* const moneyBefore = playTurn(moneyGame,legalPlaysOf(moneyGame)[0].moves);
|
|
469
|
+
* const moneyOffered = offerDouble(moneyBefore);
|
|
470
|
+
* console.log(canBeaver(moneyOffered));
|
|
471
|
+
* ```
|
|
472
|
+
*/
|
|
142
473
|
export declare function canBeaver(game: GameState): boolean;
|
|
143
|
-
/** Beaver: the side that was doubled redoubles and keeps the cube; the side that doubled must now take it or drop it.
|
|
474
|
+
/** Beaver: the side that was doubled redoubles and keeps the cube; the side that doubled must now take it or drop it.
|
|
475
|
+
*
|
|
476
|
+
* @param game - The current immutable native game state.
|
|
477
|
+
* @returns Beaver: the side that was doubled redoubles and keeps the cube; the side that doubled must now take it or drop it.
|
|
478
|
+
* @example
|
|
479
|
+
* ```ts
|
|
480
|
+
* import { beaverDouble, endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
481
|
+
*
|
|
482
|
+
* const moneySettings = settingsFor("backgammon",{points:0,cube:true,beaver:true});
|
|
483
|
+
* const moneyGame = rollOpening(newGame(moneySettings),[3,1]);
|
|
484
|
+
* const moneyBefore = playTurn(moneyGame,legalPlaysOf(moneyGame)[0].moves);
|
|
485
|
+
* const moneyOffered = offerDouble(moneyBefore);
|
|
486
|
+
* console.log(beaverDouble(moneyOffered).phase);
|
|
487
|
+
* ```
|
|
488
|
+
*/
|
|
144
489
|
export declare function beaverDouble(game: GameState): GameState;
|
|
145
490
|
/**
|
|
146
491
|
* What a side gives up if it gives up the game now, which is the worst the
|
|
147
492
|
* position could come to: a single game once it has borne a checker off; a
|
|
148
493
|
* gammon if nothing of the other side's can still hit it; otherwise a
|
|
149
494
|
* backgammon where it may have a checker caught in the other side's home board.
|
|
495
|
+
*
|
|
496
|
+
* @param game - The current immutable native game state.
|
|
497
|
+
* @param side - The side being examined: white or black.
|
|
498
|
+
* @returns What a side gives up if it gives up the game now, which is the worst the position could come to: a single game once it has borne a checker off; a gammon if nothing of the other side's can still hit it; otherwise a backgammon where it may have a checker caught in the other side's home board.
|
|
499
|
+
* @example
|
|
500
|
+
* ```ts
|
|
501
|
+
* import { concedeKind, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
502
|
+
*
|
|
503
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
504
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
505
|
+
* console.log(concedeKind(game,"black"));
|
|
506
|
+
* ```
|
|
150
507
|
*/
|
|
151
508
|
export declare function concedeKind(game: GameState, side: Side): WinKind;
|
|
152
|
-
/** Give up the game: `side` loses. `kind` is what it is giving up, from `concedeKind`'s worst down to a single game.
|
|
509
|
+
/** Give up the game: `side` loses. `kind` is what it is giving up, from `concedeKind`'s worst down to a single game.
|
|
510
|
+
*
|
|
511
|
+
* @param game - The current immutable native game state.
|
|
512
|
+
* @param side - The side being examined: white or black.
|
|
513
|
+
* @param kind - The native single, gammon or backgammon result.
|
|
514
|
+
* @returns Give up the game: `side` loses. `kind` is what it is giving up, from `concedeKind`'s worst down to a single game.
|
|
515
|
+
* @example
|
|
516
|
+
* ```ts
|
|
517
|
+
* import { concede, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
518
|
+
*
|
|
519
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
520
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
521
|
+
* const result = concede(game,"black","single").result!;
|
|
522
|
+
* console.log(concede(game,"black","single").result);
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
153
525
|
export declare function concede(game: GameState, side: Side, kind?: WinKind): GameState;
|
|
154
|
-
/** The game drawn by agreement.
|
|
526
|
+
/** The game drawn by agreement.
|
|
527
|
+
*
|
|
528
|
+
* @param game - The current immutable native game state.
|
|
529
|
+
* @returns The game drawn by agreement.
|
|
530
|
+
* @example
|
|
531
|
+
* ```ts
|
|
532
|
+
* import { agreeDraw, concede, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
533
|
+
*
|
|
534
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
535
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
536
|
+
* const result = concede(game,"black","single").result!;
|
|
537
|
+
* console.log(agreeDraw(game).result);
|
|
538
|
+
* ```
|
|
539
|
+
*/
|
|
155
540
|
export declare function agreeDraw(game: GameState): GameState;
|