@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/game.js
CHANGED
|
@@ -3,7 +3,19 @@ import { applyMove } from "./moves.js";
|
|
|
3
3
|
import { diceMustPlay, diceToPlay, legalPlays, nextMoves, playMoves } from "./plays.js";
|
|
4
4
|
import { CUBE_LIMIT, cubeInPlay } from "./rules.js";
|
|
5
5
|
import { otherSide, sideIndex } from "./side.js";
|
|
6
|
-
/** A new game, waiting for the opening throw.
|
|
6
|
+
/** A new game, waiting for the opening throw.
|
|
7
|
+
*
|
|
8
|
+
* @param settings - The resolved variant and complete rules.
|
|
9
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
10
|
+
* @returns A new game, waiting for the opening throw.
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { newGame, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
14
|
+
*
|
|
15
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
16
|
+
* console.log(newGame(settings).phase);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
7
19
|
export function newGame(settings, options = {}) {
|
|
8
20
|
const position = startPosition(settings.variant);
|
|
9
21
|
return {
|
|
@@ -45,6 +57,17 @@ function startPlaying(game, side, dice) {
|
|
|
45
57
|
* A tie changes nothing but `opening`, and the sides throw again. In a game
|
|
46
58
|
* opened with a roll-off the starter plays the two dice as the first roll;
|
|
47
59
|
* otherwise the starter then rolls as usual.
|
|
60
|
+
*
|
|
61
|
+
* @param game - The current immutable native game state.
|
|
62
|
+
* @param throws - The opening die for white, then black.
|
|
63
|
+
* @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.
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* import { newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
67
|
+
*
|
|
68
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
69
|
+
* console.log(rollOpening(newGame(settings),[3,1]).turn);
|
|
70
|
+
* ```
|
|
48
71
|
*/
|
|
49
72
|
export function rollOpening(game, throws) {
|
|
50
73
|
if (game.phase !== "opening")
|
|
@@ -60,7 +83,22 @@ export function rollOpening(game, throws) {
|
|
|
60
83
|
return startPlaying({ ...game, opening }, starter, [white, black]);
|
|
61
84
|
return { ...game, opening, turn: starter, phase: "before-roll" };
|
|
62
85
|
}
|
|
63
|
-
/** Roll the dice for the side on turn, which must not be waiting on an answer to a double.
|
|
86
|
+
/** Roll the dice for the side on turn, which must not be waiting on an answer to a double.
|
|
87
|
+
*
|
|
88
|
+
* @param game - The current immutable native game state.
|
|
89
|
+
* @param dice - Dice values as thrown, or a reproducible source specified by the signature.
|
|
90
|
+
* @returns Roll the dice for the side on turn, which must not be waiting on an answer to a double.
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { endTurn, legalPlaysOf, newGame, playTurn, rollDice, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
94
|
+
*
|
|
95
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
96
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
97
|
+
* const play = legalPlaysOf(game)[0];
|
|
98
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
99
|
+
* console.log(rollDice(beforeRoll,[6,2]).dice);
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
64
102
|
export function rollDice(game, dice) {
|
|
65
103
|
const side = onTurn(game);
|
|
66
104
|
if (game.phase !== "before-roll")
|
|
@@ -69,19 +107,56 @@ export function rollDice(game, dice) {
|
|
|
69
107
|
fail(`A roll is ${game.settings.variant.dice} dice, each 1 to 6.`);
|
|
70
108
|
return startPlaying(game, side, dice);
|
|
71
109
|
}
|
|
72
|
-
/** The moves the side on turn may make next. Empty when the turn is played out, or nothing can be played.
|
|
110
|
+
/** The moves the side on turn may make next. Empty when the turn is played out, or nothing can be played.
|
|
111
|
+
*
|
|
112
|
+
* @param game - The current immutable native game state.
|
|
113
|
+
* @returns The moves the side on turn may make next. Empty when the turn is played out, or nothing can be played.
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* import { legalMoves, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
117
|
+
*
|
|
118
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
119
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
120
|
+
* console.log(legalMoves(game));
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
73
123
|
export function legalMoves(game) {
|
|
74
124
|
if (game.phase !== "playing" || game.turn === null)
|
|
75
125
|
return [];
|
|
76
126
|
return nextMoves(game.settings.variant, game.position, game.turn, game.need);
|
|
77
127
|
}
|
|
78
|
-
/** 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.
|
|
128
|
+
/** 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.
|
|
129
|
+
*
|
|
130
|
+
* @param game - The current immutable native game state.
|
|
131
|
+
* @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.
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* import { legalPlaysOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
135
|
+
*
|
|
136
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
137
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
138
|
+
* console.log(legalPlaysOf(game).length);
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
79
141
|
export function legalPlaysOf(game) {
|
|
80
142
|
if (game.phase !== "playing" || game.turn === null)
|
|
81
143
|
return [];
|
|
82
144
|
return legalPlays(game.settings.variant, game.position, game.turn, game.need);
|
|
83
145
|
}
|
|
84
|
-
/** Whether the turn is played out and the side on turn has only to end it.
|
|
146
|
+
/** Whether the turn is played out and the side on turn has only to end it.
|
|
147
|
+
*
|
|
148
|
+
* @param game - The current immutable native game state.
|
|
149
|
+
* @returns Whether the turn is played out and the side on turn has only to end it.
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* import { legalPlaysOf, newGame, playTurn, rollOpening, settingsFor, turnIsPlayed } from "@johnmorrisdotca/sugoroku";
|
|
153
|
+
*
|
|
154
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
155
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
156
|
+
* const play = legalPlaysOf(game)[0];
|
|
157
|
+
* console.log(turnIsPlayed(playTurn(game,play.moves)));
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
85
160
|
export function turnIsPlayed(game) {
|
|
86
161
|
return game.phase === "playing" && game.need.length === 0;
|
|
87
162
|
}
|
|
@@ -89,6 +164,20 @@ export function turnIsPlayed(game) {
|
|
|
89
164
|
* Make one move for the side on turn, given as the checker's own point it
|
|
90
165
|
* leaves and the one it goes to. The move must be one of `legalMoves`. Where
|
|
91
166
|
* a checker could be borne off by either of two dice, the smaller is used.
|
|
167
|
+
*
|
|
168
|
+
* @param game - The current immutable native game state.
|
|
169
|
+
* @param step - A requested source and destination from the current legal moves.
|
|
170
|
+
* @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.
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* import { legalPlaysOf, newGame, playMove, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
174
|
+
*
|
|
175
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
176
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
177
|
+
* const play = legalPlaysOf(game)[0];
|
|
178
|
+
* const move = play.moves[0];
|
|
179
|
+
* console.log(playMove(game,move).moves.length);
|
|
180
|
+
* ```
|
|
92
181
|
*/
|
|
93
182
|
export function playMove(game, step) {
|
|
94
183
|
const side = onTurn(game);
|
|
@@ -103,7 +192,21 @@ export function playMove(game, step) {
|
|
|
103
192
|
const winner = finisher(moved, side);
|
|
104
193
|
return winner === null ? moved : finish(moved, bearOffResult(moved, winner));
|
|
105
194
|
}
|
|
106
|
-
/** Take back the last move made this turn. Does nothing if none has been made.
|
|
195
|
+
/** Take back the last move made this turn. Does nothing if none has been made.
|
|
196
|
+
*
|
|
197
|
+
* @param game - The current immutable native game state.
|
|
198
|
+
* @returns Take back the last move made this turn. Does nothing if none has been made.
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* import { legalPlaysOf, newGame, playMove, rollOpening, settingsFor, undoMove } from "@johnmorrisdotca/sugoroku";
|
|
202
|
+
*
|
|
203
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
204
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
205
|
+
* const play = legalPlaysOf(game)[0];
|
|
206
|
+
* const move = play.moves[0];
|
|
207
|
+
* console.log(undoMove(playMove(game,move)).moves.length);
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
107
210
|
export function undoMove(game) {
|
|
108
211
|
if (game.phase !== "playing" || game.moves.length === 0 || game.turn === null)
|
|
109
212
|
return game;
|
|
@@ -114,20 +217,62 @@ export function undoMove(game) {
|
|
|
114
217
|
need.splice(need.indexOf(move.die), 1);
|
|
115
218
|
return { ...game, position: playMoves(game.settings.variant, game.turnStart, side, kept), moves: kept, need };
|
|
116
219
|
}
|
|
117
|
-
/** Take back every move of the turn.
|
|
220
|
+
/** Take back every move of the turn.
|
|
221
|
+
*
|
|
222
|
+
* @param game - The current immutable native game state.
|
|
223
|
+
* @returns Take back every move of the turn.
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* import { legalPlaysOf, newGame, playMove, restartTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
227
|
+
*
|
|
228
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
229
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
230
|
+
* const play = legalPlaysOf(game)[0];
|
|
231
|
+
* const move = play.moves[0];
|
|
232
|
+
* console.log(restartTurn(playMove(game,move)).moves.length);
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
118
235
|
export function restartTurn(game) {
|
|
119
236
|
if (game.phase !== "playing" || game.moves.length === 0)
|
|
120
237
|
return game;
|
|
121
238
|
return { ...game, position: game.turnStart, moves: [], need: game.plan };
|
|
122
239
|
}
|
|
123
|
-
/** Make a whole play (the moves of `legalPlays`) for the side on turn and end the turn.
|
|
240
|
+
/** Make a whole play (the moves of `legalPlays`) for the side on turn and end the turn.
|
|
241
|
+
*
|
|
242
|
+
* @param game - The current immutable native game state.
|
|
243
|
+
* @param moves - Legal checker moves in their played order.
|
|
244
|
+
* @returns Make a whole play (the moves of `legalPlays`) for the side on turn and end the turn.
|
|
245
|
+
* @example
|
|
246
|
+
* ```ts
|
|
247
|
+
* import { legalPlaysOf, newGame, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
248
|
+
*
|
|
249
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
250
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
251
|
+
* const play = legalPlaysOf(game)[0];
|
|
252
|
+
* console.log(playTurn(game,play.moves).need);
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
124
255
|
export function playTurn(game, moves) {
|
|
125
256
|
let at = game;
|
|
126
257
|
for (const move of moves)
|
|
127
258
|
at = playMove(at, move);
|
|
128
259
|
return at.phase === "over" ? at : endTurn(at);
|
|
129
260
|
}
|
|
130
|
-
/** End the side on turn's turn, once everything it had to play has been played. The other side is to move.
|
|
261
|
+
/** End the side on turn's turn, once everything it had to play has been played. The other side is to move.
|
|
262
|
+
*
|
|
263
|
+
* @param game - The current immutable native game state.
|
|
264
|
+
* @returns End the side on turn's turn, once everything it had to play has been played. The other side is to move.
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* import { endTurn, legalPlaysOf, newGame, playMove, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
268
|
+
*
|
|
269
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
270
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
271
|
+
* const play = legalPlaysOf(game)[0];
|
|
272
|
+
* const beforeRoll = endTurn(play.moves.reduce((state,step)=>playMove(state,step),game));
|
|
273
|
+
* console.log(beforeRoll.phase);
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
131
276
|
export function endTurn(game) {
|
|
132
277
|
const side = onTurn(game);
|
|
133
278
|
if (game.phase !== "playing")
|
|
@@ -161,14 +306,40 @@ function inTheirHome(game, loser) {
|
|
|
161
306
|
}
|
|
162
307
|
return false;
|
|
163
308
|
}
|
|
164
|
-
/** 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.
|
|
309
|
+
/** 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.
|
|
310
|
+
*
|
|
311
|
+
* @param game - The current immutable native game state.
|
|
312
|
+
* @param winner - The side whose win kind is evaluated.
|
|
313
|
+
* @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.
|
|
314
|
+
* @example
|
|
315
|
+
* ```ts
|
|
316
|
+
* import { newGame, rollOpening, settingsFor, winKind } from "@johnmorrisdotca/sugoroku";
|
|
317
|
+
*
|
|
318
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
319
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
320
|
+
* console.log(winKind(game,"white"));
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
165
323
|
export function winKind(game, winner) {
|
|
166
324
|
const loser = otherSide(winner);
|
|
167
325
|
if (game.position.off[sideIndex(loser)] > 0)
|
|
168
326
|
return "single";
|
|
169
327
|
return inTheirHome(game, loser) ? "backgammon" : "gammon";
|
|
170
328
|
}
|
|
171
|
-
/** 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.
|
|
329
|
+
/** 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.
|
|
330
|
+
*
|
|
331
|
+
* @param game - The current immutable native game state.
|
|
332
|
+
* @param kind - The native single, gammon or backgammon result.
|
|
333
|
+
* @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.
|
|
334
|
+
* @example
|
|
335
|
+
* ```ts
|
|
336
|
+
* import { multiplierOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
337
|
+
*
|
|
338
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
339
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
340
|
+
* console.log(multiplierOf(game,"gammon"));
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
172
343
|
export function multiplierOf(game, kind) {
|
|
173
344
|
const { rules } = game.settings;
|
|
174
345
|
if (!rules.gammons)
|
|
@@ -186,7 +357,21 @@ function bearOffResult(game, finished) {
|
|
|
186
357
|
return resultOf(game, otherSide(finished), "bear-off", "single", game.cube.value);
|
|
187
358
|
return resultOf(game, finished, "bear-off", winKind(game, finished), game.cube.value);
|
|
188
359
|
}
|
|
189
|
-
/** Whether the side on turn may offer a double now.
|
|
360
|
+
/** Whether the side on turn may offer a double now.
|
|
361
|
+
*
|
|
362
|
+
* @param game - The current immutable native game state.
|
|
363
|
+
* @returns Whether the side on turn may offer a double now.
|
|
364
|
+
* @example
|
|
365
|
+
* ```ts
|
|
366
|
+
* import { canDouble, endTurn, legalPlaysOf, newGame, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
367
|
+
*
|
|
368
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
369
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
370
|
+
* const play = legalPlaysOf(game)[0];
|
|
371
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
372
|
+
* console.log(canDouble(beforeRoll));
|
|
373
|
+
* ```
|
|
374
|
+
*/
|
|
190
375
|
export function canDouble(game) {
|
|
191
376
|
if (game.phase !== "before-roll" || game.turn === null)
|
|
192
377
|
return false;
|
|
@@ -198,14 +383,42 @@ export function canDouble(game) {
|
|
|
198
383
|
return false;
|
|
199
384
|
return !cubeIsDead(game);
|
|
200
385
|
}
|
|
201
|
-
/** 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
|
+
/** 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.
|
|
387
|
+
*
|
|
388
|
+
* @param game - The current immutable native game state.
|
|
389
|
+
* @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.
|
|
390
|
+
* @example
|
|
391
|
+
* ```ts
|
|
392
|
+
* import { cubeIsDead, endTurn, legalPlaysOf, newGame, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
393
|
+
*
|
|
394
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
395
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
396
|
+
* const play = legalPlaysOf(game)[0];
|
|
397
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
398
|
+
* console.log(cubeIsDead(beforeRoll));
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
202
401
|
export function cubeIsDead(game) {
|
|
203
402
|
const { points } = game.settings.rules;
|
|
204
403
|
if (points === 0)
|
|
205
404
|
return false;
|
|
206
405
|
return game.cube.value >= points - game.score[0] && game.cube.value >= points - game.score[1];
|
|
207
406
|
}
|
|
208
|
-
/** Offer a double: the other side must now take it or drop it.
|
|
407
|
+
/** Offer a double: the other side must now take it or drop it.
|
|
408
|
+
*
|
|
409
|
+
* @param game - The current immutable native game state.
|
|
410
|
+
* @returns Offer a double: the other side must now take it or drop it.
|
|
411
|
+
* @example
|
|
412
|
+
* ```ts
|
|
413
|
+
* import { endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
414
|
+
*
|
|
415
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
416
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
417
|
+
* const play = legalPlaysOf(game)[0];
|
|
418
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
419
|
+
* console.log(offerDouble(beforeRoll).phase);
|
|
420
|
+
* ```
|
|
421
|
+
*/
|
|
209
422
|
export function offerDouble(game) {
|
|
210
423
|
if (!canDouble(game))
|
|
211
424
|
fail("A double cannot be offered now.");
|
|
@@ -218,6 +431,20 @@ function responder(game) {
|
|
|
218
431
|
* Take the double that was offered. The cube turns and goes to the side that
|
|
219
432
|
* took it, and the side on turn rolls. (A beaver taken turns it twice, and
|
|
220
433
|
* leaves it with the side that beavered.)
|
|
434
|
+
*
|
|
435
|
+
* @param game - The current immutable native game state.
|
|
436
|
+
* @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.)
|
|
437
|
+
* @example
|
|
438
|
+
* ```ts
|
|
439
|
+
* import { endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor, takeDouble } from "@johnmorrisdotca/sugoroku";
|
|
440
|
+
*
|
|
441
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
442
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
443
|
+
* const play = legalPlaysOf(game)[0];
|
|
444
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
445
|
+
* const offered = offerDouble(beforeRoll);
|
|
446
|
+
* console.log(takeDouble(offered).cube);
|
|
447
|
+
* ```
|
|
221
448
|
*/
|
|
222
449
|
export function takeDouble(game) {
|
|
223
450
|
const taker = responder(game);
|
|
@@ -232,6 +459,21 @@ export function takeDouble(game) {
|
|
|
232
459
|
* Drop the double that was offered: the side that drops it loses the game, at
|
|
233
460
|
* the cube's value before the double, or for a beaver at its value once the
|
|
234
461
|
* double was taken.
|
|
462
|
+
*
|
|
463
|
+
* @param game - The current immutable native game state.
|
|
464
|
+
* @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.
|
|
465
|
+
* @example
|
|
466
|
+
* ```ts
|
|
467
|
+
* import { concede, dropDouble, endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
468
|
+
*
|
|
469
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
470
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
471
|
+
* const play = legalPlaysOf(game)[0];
|
|
472
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
473
|
+
* const offered = offerDouble(beforeRoll);
|
|
474
|
+
* const result = concede(game,"black","single").result!;
|
|
475
|
+
* console.log(dropDouble(offered).result);
|
|
476
|
+
* ```
|
|
235
477
|
*/
|
|
236
478
|
export function dropDouble(game) {
|
|
237
479
|
const dropper = responder(game);
|
|
@@ -239,11 +481,39 @@ export function dropDouble(game) {
|
|
|
239
481
|
const value = game.phase === "beaver-offered" ? game.cube.value * 2 : game.phase === "double-offered" ? game.cube.value : fail("No double has been offered.");
|
|
240
482
|
return finish(game, { winner, how: "drop", kind: "single", multiplier: 1, cube: value, points: value });
|
|
241
483
|
}
|
|
242
|
-
/** Whether the side that was doubled may beaver: redouble at once and keep the cube. Only where the rules allow it.
|
|
484
|
+
/** Whether the side that was doubled may beaver: redouble at once and keep the cube. Only where the rules allow it.
|
|
485
|
+
*
|
|
486
|
+
* @param game - The current immutable native game state.
|
|
487
|
+
* @returns Whether the side that was doubled may beaver: redouble at once and keep the cube. Only where the rules allow it.
|
|
488
|
+
* @example
|
|
489
|
+
* ```ts
|
|
490
|
+
* import { canBeaver, endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
491
|
+
*
|
|
492
|
+
* const moneySettings = settingsFor("backgammon",{points:0,cube:true,beaver:true});
|
|
493
|
+
* const moneyGame = rollOpening(newGame(moneySettings),[3,1]);
|
|
494
|
+
* const moneyBefore = playTurn(moneyGame,legalPlaysOf(moneyGame)[0].moves);
|
|
495
|
+
* const moneyOffered = offerDouble(moneyBefore);
|
|
496
|
+
* console.log(canBeaver(moneyOffered));
|
|
497
|
+
* ```
|
|
498
|
+
*/
|
|
243
499
|
export function canBeaver(game) {
|
|
244
500
|
return game.phase === "double-offered" && game.settings.rules.beaver && game.cube.value * 4 <= CUBE_LIMIT;
|
|
245
501
|
}
|
|
246
|
-
/** Beaver: the side that was doubled redoubles and keeps the cube; the side that doubled must now take it or drop it.
|
|
502
|
+
/** Beaver: the side that was doubled redoubles and keeps the cube; the side that doubled must now take it or drop it.
|
|
503
|
+
*
|
|
504
|
+
* @param game - The current immutable native game state.
|
|
505
|
+
* @returns Beaver: the side that was doubled redoubles and keeps the cube; the side that doubled must now take it or drop it.
|
|
506
|
+
* @example
|
|
507
|
+
* ```ts
|
|
508
|
+
* import { beaverDouble, endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
509
|
+
*
|
|
510
|
+
* const moneySettings = settingsFor("backgammon",{points:0,cube:true,beaver:true});
|
|
511
|
+
* const moneyGame = rollOpening(newGame(moneySettings),[3,1]);
|
|
512
|
+
* const moneyBefore = playTurn(moneyGame,legalPlaysOf(moneyGame)[0].moves);
|
|
513
|
+
* const moneyOffered = offerDouble(moneyBefore);
|
|
514
|
+
* console.log(beaverDouble(moneyOffered).phase);
|
|
515
|
+
* ```
|
|
516
|
+
*/
|
|
247
517
|
export function beaverDouble(game) {
|
|
248
518
|
if (!canBeaver(game))
|
|
249
519
|
fail("A beaver cannot be played now.");
|
|
@@ -254,6 +524,18 @@ export function beaverDouble(game) {
|
|
|
254
524
|
* position could come to: a single game once it has borne a checker off; a
|
|
255
525
|
* gammon if nothing of the other side's can still hit it; otherwise a
|
|
256
526
|
* backgammon where it may have a checker caught in the other side's home board.
|
|
527
|
+
*
|
|
528
|
+
* @param game - The current immutable native game state.
|
|
529
|
+
* @param side - The side being examined: white or black.
|
|
530
|
+
* @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.
|
|
531
|
+
* @example
|
|
532
|
+
* ```ts
|
|
533
|
+
* import { concedeKind, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
534
|
+
*
|
|
535
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
536
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
537
|
+
* console.log(concedeKind(game,"black"));
|
|
538
|
+
* ```
|
|
257
539
|
*/
|
|
258
540
|
export function concedeKind(game, side) {
|
|
259
541
|
const winner = otherSide(side);
|
|
@@ -286,7 +568,22 @@ function gameCanStillHit(game, by, target) {
|
|
|
286
568
|
return byRear > mirrorPoint(spec, targetRear);
|
|
287
569
|
return true;
|
|
288
570
|
}
|
|
289
|
-
/** Give up the game: `side` loses. `kind` is what it is giving up, from `concedeKind`'s worst down to a single game.
|
|
571
|
+
/** Give up the game: `side` loses. `kind` is what it is giving up, from `concedeKind`'s worst down to a single game.
|
|
572
|
+
*
|
|
573
|
+
* @param game - The current immutable native game state.
|
|
574
|
+
* @param side - The side being examined: white or black.
|
|
575
|
+
* @param kind - The native single, gammon or backgammon result.
|
|
576
|
+
* @returns Give up the game: `side` loses. `kind` is what it is giving up, from `concedeKind`'s worst down to a single game.
|
|
577
|
+
* @example
|
|
578
|
+
* ```ts
|
|
579
|
+
* import { concede, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
580
|
+
*
|
|
581
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
582
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
583
|
+
* const result = concede(game,"black","single").result!;
|
|
584
|
+
* console.log(concede(game,"black","single").result);
|
|
585
|
+
* ```
|
|
586
|
+
*/
|
|
290
587
|
export function concede(game, side, kind = concedeKind(game, side)) {
|
|
291
588
|
if (game.phase === "over")
|
|
292
589
|
fail("The game is over.");
|
|
@@ -296,7 +593,20 @@ export function concede(game, side, kind = concedeKind(game, side)) {
|
|
|
296
593
|
fail(`Giving up a ${kind} is more than this position can lose.`);
|
|
297
594
|
return finish(game, resultOf(game, otherSide(side), "concede", kind, game.cube.value));
|
|
298
595
|
}
|
|
299
|
-
/** The game drawn by agreement.
|
|
596
|
+
/** The game drawn by agreement.
|
|
597
|
+
*
|
|
598
|
+
* @param game - The current immutable native game state.
|
|
599
|
+
* @returns The game drawn by agreement.
|
|
600
|
+
* @example
|
|
601
|
+
* ```ts
|
|
602
|
+
* import { agreeDraw, concede, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
603
|
+
*
|
|
604
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
605
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
606
|
+
* const result = concede(game,"black","single").result!;
|
|
607
|
+
* console.log(agreeDraw(game).result);
|
|
608
|
+
* ```
|
|
609
|
+
*/
|
|
300
610
|
export function agreeDraw(game) {
|
|
301
611
|
if (game.phase === "over")
|
|
302
612
|
fail("The game is over.");
|
package/dist/geometry.d.ts
CHANGED
|
@@ -7,7 +7,16 @@ import type { Side } from "./side.ts";
|
|
|
7
7
|
* quarter, which is the better shape for a phone. The box is the same size
|
|
8
8
|
* whatever the position, the dice and the cube: a board never changes shape.
|
|
9
9
|
*/
|
|
10
|
-
/** Whether the board lies across the page or stands up in it.
|
|
10
|
+
/** Whether the board lies across the page or stands up in it.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import type { Orientation } from "@johnmorrisdotca/sugoroku/draw";
|
|
15
|
+
*
|
|
16
|
+
* const value: Orientation = "landscape";
|
|
17
|
+
* console.log(value);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
11
20
|
export type Orientation = "landscape" | "portrait";
|
|
12
21
|
export declare const PAD = 14;
|
|
13
22
|
/** The strip above and below the points that holds their numbers. */
|
|
@@ -25,6 +34,16 @@ export declare const CHECKER = 44;
|
|
|
25
34
|
/** How far apart checkers stack: slightly less than their width, so five fill a point. */
|
|
26
35
|
export declare const STACK_STEP = 40;
|
|
27
36
|
export declare const DIE = 46;
|
|
37
|
+
/** The cube, viewing side, home direction and orientation used to calculate board geometry.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import type { BoardOptions } from "@johnmorrisdotca/sugoroku/draw";
|
|
42
|
+
*
|
|
43
|
+
* const value: BoardOptions = {cube:true,view:"white",home:"right",orientation:"landscape"};
|
|
44
|
+
* console.log(value);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
28
47
|
export type BoardOptions = {
|
|
29
48
|
/** Whether the board has a rail for the doubling cube. */
|
|
30
49
|
cube: boolean;
|
|
@@ -34,7 +53,16 @@ export type BoardOptions = {
|
|
|
34
53
|
home: "left" | "right";
|
|
35
54
|
orientation: Orientation;
|
|
36
55
|
};
|
|
37
|
-
/** A box: where something is, in landscape units before any turning.
|
|
56
|
+
/** A box: where something is, in landscape units before any turning.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import type { Box } from "@johnmorrisdotca/sugoroku/draw";
|
|
61
|
+
*
|
|
62
|
+
* const value: Box = {x:0,y:0,w:100,h:100};
|
|
63
|
+
* console.log(value);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
38
66
|
export type Box = {
|
|
39
67
|
x: number;
|
|
40
68
|
y: number;
|
|
@@ -46,7 +74,17 @@ export declare function landscapeSize(cube: boolean): {
|
|
|
46
74
|
width: number;
|
|
47
75
|
height: number;
|
|
48
76
|
};
|
|
49
|
-
/** The width and height the board is drawn in, which is the other way about when it stands up.
|
|
77
|
+
/** The width and height the board is drawn in, which is the other way about when it stands up.
|
|
78
|
+
*
|
|
79
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
80
|
+
* @returns The width and height the board is drawn in, which is the other way about when it stands up.
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* import { boardSize } from "@johnmorrisdotca/sugoroku/draw";
|
|
84
|
+
*
|
|
85
|
+
* console.log(boardSize({cube:true,orientation:"landscape"}));
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
50
88
|
export declare function boardSize(options: Pick<BoardOptions, "cube" | "orientation">): {
|
|
51
89
|
width: number;
|
|
52
90
|
height: number;
|
|
@@ -54,6 +92,15 @@ export declare function boardSize(options: Pick<BoardOptions, "cube" | "orientat
|
|
|
54
92
|
/**
|
|
55
93
|
* The layout of a board, a function of its options: where each point, the bar,
|
|
56
94
|
* each tray, the rail and the dice sit, all in landscape units.
|
|
95
|
+
*
|
|
96
|
+
* @param options - Cube rail, viewing side, home direction and board orientation.
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* import { BoardLayout } from "@johnmorrisdotca/sugoroku/draw";
|
|
100
|
+
*
|
|
101
|
+
* const layout = new BoardLayout({cube:true,view:"white",home:"right",orientation:"landscape"});
|
|
102
|
+
* console.log(layout);
|
|
103
|
+
* ```
|
|
57
104
|
*/
|
|
58
105
|
export declare class BoardLayout {
|
|
59
106
|
#private;
|
package/dist/geometry.js
CHANGED
|
@@ -31,7 +31,17 @@ export function landscapeSize(cube) {
|
|
|
31
31
|
const rail = cube ? RAIL_WIDTH + 10 : 0;
|
|
32
32
|
return { width: PAD + rail + 2 * HALF_WIDTH + BAR_WIDTH + TRAY_GAP + TRAY_WIDTH + PAD, height: PAD + STRIP + 2 * POINT_LENGTH + MIDDLE + STRIP + PAD };
|
|
33
33
|
}
|
|
34
|
-
/** The width and height the board is drawn in, which is the other way about when it stands up.
|
|
34
|
+
/** The width and height the board is drawn in, which is the other way about when it stands up.
|
|
35
|
+
*
|
|
36
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
37
|
+
* @returns The width and height the board is drawn in, which is the other way about when it stands up.
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { boardSize } from "@johnmorrisdotca/sugoroku/draw";
|
|
41
|
+
*
|
|
42
|
+
* console.log(boardSize({cube:true,orientation:"landscape"}));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
35
45
|
export function boardSize(options) {
|
|
36
46
|
const { width, height } = landscapeSize(options.cube);
|
|
37
47
|
return options.orientation === "portrait" ? { width: height, height: width } : { width, height };
|
|
@@ -39,6 +49,15 @@ export function boardSize(options) {
|
|
|
39
49
|
/**
|
|
40
50
|
* The layout of a board, a function of its options: where each point, the bar,
|
|
41
51
|
* each tray, the rail and the dice sit, all in landscape units.
|
|
52
|
+
*
|
|
53
|
+
* @param options - Cube rail, viewing side, home direction and board orientation.
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { BoardLayout } from "@johnmorrisdotca/sugoroku/draw";
|
|
57
|
+
*
|
|
58
|
+
* const layout = new BoardLayout({cube:true,view:"white",home:"right",orientation:"landscape"});
|
|
59
|
+
* console.log(layout);
|
|
60
|
+
* ```
|
|
42
61
|
*/
|
|
43
62
|
export class BoardLayout {
|
|
44
63
|
constructor(options) {
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED