@johnmorrisdotca/sugoroku 1.0.2 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/README.md +78 -68
- package/dist/board.d.ts +216 -12
- package/dist/board.js +205 -13
- package/dist/colours.d.ts +80 -8
- package/dist/colours.js +57 -7
- package/dist/computer.d.ts +139 -9
- package/dist/computer.js +118 -11
- package/dist/dice.d.ts +53 -4
- package/dist/dice.js +42 -3
- package/dist/draw-entry.d.ts +3 -3
- package/dist/draw-entry.js +2 -2
- package/dist/draw.d.ts +43 -3
- package/dist/draw.js +63 -13
- package/dist/element-define.d.ts +1 -1
- package/dist/element-define.js +4 -13
- package/dist/element.d.ts +116 -5
- package/dist/element.js +378 -102
- package/dist/elementFrame.d.ts +21 -0
- package/dist/elementFrame.js +110 -0
- package/dist/evaluate.d.ts +76 -4
- package/dist/evaluate.js +70 -5
- package/dist/game.d.ts +408 -23
- package/dist/game.js +344 -20
- package/dist/geometry.d.ts +50 -3
- package/dist/geometry.js +64 -28
- package/dist/index.d.ts +12 -11
- package/dist/index.js +10 -9
- package/dist/match.d.ts +66 -4
- package/dist/match.js +69 -6
- package/dist/mount.d.ts +77 -6
- package/dist/mount.js +105 -357
- package/dist/mountView.d.ts +58 -0
- package/dist/mountView.js +459 -0
- package/dist/moves.d.ts +48 -1
- package/dist/moves.js +35 -1
- package/dist/notation.d.ts +92 -4
- package/dist/notation.js +88 -5
- package/dist/play-entry.d.ts +4 -4
- package/dist/play-entry.js +1 -1
- package/dist/playStyle.d.ts +6 -0
- package/dist/playStyle.js +6 -0
- package/dist/plays.d.ts +108 -3
- package/dist/plays.js +96 -3
- package/dist/positionElement.d.ts +107 -0
- package/dist/positionElement.js +303 -0
- package/dist/positionId.d.ts +29 -1
- package/dist/positionId.js +33 -2
- package/dist/random.d.ts +33 -3
- package/dist/random.js +22 -2
- package/dist/record.d.ts +151 -8
- package/dist/record.js +121 -36
- package/dist/rules.d.ts +130 -10
- package/dist/rules.js +133 -14
- package/dist/side.d.ts +38 -3
- package/dist/side.js +30 -3
- package/dist/sound.d.ts +61 -3
- package/dist/sound.js +27 -2
- package/dist/strings.d.ts +42 -2
- package/dist/strings.js +37 -3
- package/dist/style.d.ts +6 -0
- package/dist/style.js +6 -0
- package/dist/variants.d.ts +113 -8
- package/dist/variants.js +81 -7
- package/dist/version.d.ts +9 -2
- package/dist/version.js +9 -2
- package/package.json +29 -22
package/dist/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")
|
|
@@ -136,7 +281,17 @@ export function endTurn(game) {
|
|
|
136
281
|
fail("The turn is not played out yet.");
|
|
137
282
|
const turns = [game.turns[0], game.turns[1]];
|
|
138
283
|
turns[sideIndex(side)] += 1;
|
|
139
|
-
const next = {
|
|
284
|
+
const next = {
|
|
285
|
+
...game,
|
|
286
|
+
turn: otherSide(side),
|
|
287
|
+
phase: "before-roll",
|
|
288
|
+
dice: null,
|
|
289
|
+
need: [],
|
|
290
|
+
plan: [],
|
|
291
|
+
moves: [],
|
|
292
|
+
turnStart: game.position,
|
|
293
|
+
turns,
|
|
294
|
+
};
|
|
140
295
|
const limit = game.settings.variant.drawAfter;
|
|
141
296
|
if (limit > 0 && turns[0] >= limit && turns[1] >= limit)
|
|
142
297
|
return finish(next, { winner: null, how: "draw", kind: "single", multiplier: 1, cube: game.cube.value, points: 0 });
|
|
@@ -161,14 +316,40 @@ function inTheirHome(game, loser) {
|
|
|
161
316
|
}
|
|
162
317
|
return false;
|
|
163
318
|
}
|
|
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.
|
|
319
|
+
/** 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.
|
|
320
|
+
*
|
|
321
|
+
* @param game - The current immutable native game state.
|
|
322
|
+
* @param winner - The side whose win kind is evaluated.
|
|
323
|
+
* @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.
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* import { newGame, rollOpening, settingsFor, winKind } from "@johnmorrisdotca/sugoroku";
|
|
327
|
+
*
|
|
328
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
329
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
330
|
+
* console.log(winKind(game,"white"));
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
165
333
|
export function winKind(game, winner) {
|
|
166
334
|
const loser = otherSide(winner);
|
|
167
335
|
if (game.position.off[sideIndex(loser)] > 0)
|
|
168
336
|
return "single";
|
|
169
337
|
return inTheirHome(game, loser) ? "backgammon" : "gammon";
|
|
170
338
|
}
|
|
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.
|
|
339
|
+
/** 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.
|
|
340
|
+
*
|
|
341
|
+
* @param game - The current immutable native game state.
|
|
342
|
+
* @param kind - The native single, gammon or backgammon result.
|
|
343
|
+
* @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.
|
|
344
|
+
* @example
|
|
345
|
+
* ```ts
|
|
346
|
+
* import { multiplierOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
347
|
+
*
|
|
348
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
349
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
350
|
+
* console.log(multiplierOf(game,"gammon"));
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
172
353
|
export function multiplierOf(game, kind) {
|
|
173
354
|
const { rules } = game.settings;
|
|
174
355
|
if (!rules.gammons)
|
|
@@ -186,7 +367,21 @@ function bearOffResult(game, finished) {
|
|
|
186
367
|
return resultOf(game, otherSide(finished), "bear-off", "single", game.cube.value);
|
|
187
368
|
return resultOf(game, finished, "bear-off", winKind(game, finished), game.cube.value);
|
|
188
369
|
}
|
|
189
|
-
/** Whether the side on turn may offer a double now.
|
|
370
|
+
/** Whether the side on turn may offer a double now.
|
|
371
|
+
*
|
|
372
|
+
* @param game - The current immutable native game state.
|
|
373
|
+
* @returns Whether the side on turn may offer a double now.
|
|
374
|
+
* @example
|
|
375
|
+
* ```ts
|
|
376
|
+
* import { canDouble, endTurn, legalPlaysOf, newGame, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
377
|
+
*
|
|
378
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
379
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
380
|
+
* const play = legalPlaysOf(game)[0];
|
|
381
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
382
|
+
* console.log(canDouble(beforeRoll));
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
190
385
|
export function canDouble(game) {
|
|
191
386
|
if (game.phase !== "before-roll" || game.turn === null)
|
|
192
387
|
return false;
|
|
@@ -198,14 +393,42 @@ export function canDouble(game) {
|
|
|
198
393
|
return false;
|
|
199
394
|
return !cubeIsDead(game);
|
|
200
395
|
}
|
|
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.
|
|
396
|
+
/** 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.
|
|
397
|
+
*
|
|
398
|
+
* @param game - The current immutable native game state.
|
|
399
|
+
* @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.
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* import { cubeIsDead, endTurn, legalPlaysOf, newGame, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
403
|
+
*
|
|
404
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
405
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
406
|
+
* const play = legalPlaysOf(game)[0];
|
|
407
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
408
|
+
* console.log(cubeIsDead(beforeRoll));
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
202
411
|
export function cubeIsDead(game) {
|
|
203
412
|
const { points } = game.settings.rules;
|
|
204
413
|
if (points === 0)
|
|
205
414
|
return false;
|
|
206
415
|
return game.cube.value >= points - game.score[0] && game.cube.value >= points - game.score[1];
|
|
207
416
|
}
|
|
208
|
-
/** Offer a double: the other side must now take it or drop it.
|
|
417
|
+
/** Offer a double: the other side must now take it or drop it.
|
|
418
|
+
*
|
|
419
|
+
* @param game - The current immutable native game state.
|
|
420
|
+
* @returns Offer a double: the other side must now take it or drop it.
|
|
421
|
+
* @example
|
|
422
|
+
* ```ts
|
|
423
|
+
* import { endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
424
|
+
*
|
|
425
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
426
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
427
|
+
* const play = legalPlaysOf(game)[0];
|
|
428
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
429
|
+
* console.log(offerDouble(beforeRoll).phase);
|
|
430
|
+
* ```
|
|
431
|
+
*/
|
|
209
432
|
export function offerDouble(game) {
|
|
210
433
|
if (!canDouble(game))
|
|
211
434
|
fail("A double cannot be offered now.");
|
|
@@ -218,6 +441,20 @@ function responder(game) {
|
|
|
218
441
|
* Take the double that was offered. The cube turns and goes to the side that
|
|
219
442
|
* took it, and the side on turn rolls. (A beaver taken turns it twice, and
|
|
220
443
|
* leaves it with the side that beavered.)
|
|
444
|
+
*
|
|
445
|
+
* @param game - The current immutable native game state.
|
|
446
|
+
* @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.)
|
|
447
|
+
* @example
|
|
448
|
+
* ```ts
|
|
449
|
+
* import { endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor, takeDouble } from "@johnmorrisdotca/sugoroku";
|
|
450
|
+
*
|
|
451
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
452
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
453
|
+
* const play = legalPlaysOf(game)[0];
|
|
454
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
455
|
+
* const offered = offerDouble(beforeRoll);
|
|
456
|
+
* console.log(takeDouble(offered).cube);
|
|
457
|
+
* ```
|
|
221
458
|
*/
|
|
222
459
|
export function takeDouble(game) {
|
|
223
460
|
const taker = responder(game);
|
|
@@ -232,18 +469,65 @@ export function takeDouble(game) {
|
|
|
232
469
|
* Drop the double that was offered: the side that drops it loses the game, at
|
|
233
470
|
* the cube's value before the double, or for a beaver at its value once the
|
|
234
471
|
* double was taken.
|
|
472
|
+
*
|
|
473
|
+
* @param game - The current immutable native game state.
|
|
474
|
+
* @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.
|
|
475
|
+
* @example
|
|
476
|
+
* ```ts
|
|
477
|
+
* import { concede, dropDouble, endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
478
|
+
*
|
|
479
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
480
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
481
|
+
* const play = legalPlaysOf(game)[0];
|
|
482
|
+
* const beforeRoll = playTurn(game,play.moves);
|
|
483
|
+
* const offered = offerDouble(beforeRoll);
|
|
484
|
+
* const result = concede(game,"black","single").result!;
|
|
485
|
+
* console.log(dropDouble(offered).result);
|
|
486
|
+
* ```
|
|
235
487
|
*/
|
|
236
488
|
export function dropDouble(game) {
|
|
237
489
|
const dropper = responder(game);
|
|
238
490
|
const winner = otherSide(dropper);
|
|
239
|
-
const value = game.phase === "beaver-offered"
|
|
491
|
+
const value = game.phase === "beaver-offered"
|
|
492
|
+
? game.cube.value * 2
|
|
493
|
+
: game.phase === "double-offered"
|
|
494
|
+
? game.cube.value
|
|
495
|
+
: fail("No double has been offered.");
|
|
240
496
|
return finish(game, { winner, how: "drop", kind: "single", multiplier: 1, cube: value, points: value });
|
|
241
497
|
}
|
|
242
|
-
/** Whether the side that was doubled may beaver: redouble at once and keep the cube. Only where the rules allow it.
|
|
498
|
+
/** Whether the side that was doubled may beaver: redouble at once and keep the cube. Only where the rules allow it.
|
|
499
|
+
*
|
|
500
|
+
* @param game - The current immutable native game state.
|
|
501
|
+
* @returns Whether the side that was doubled may beaver: redouble at once and keep the cube. Only where the rules allow it.
|
|
502
|
+
* @example
|
|
503
|
+
* ```ts
|
|
504
|
+
* import { canBeaver, endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
505
|
+
*
|
|
506
|
+
* const moneySettings = settingsFor("backgammon",{points:0,cube:true,beaver:true});
|
|
507
|
+
* const moneyGame = rollOpening(newGame(moneySettings),[3,1]);
|
|
508
|
+
* const moneyBefore = playTurn(moneyGame,legalPlaysOf(moneyGame)[0].moves);
|
|
509
|
+
* const moneyOffered = offerDouble(moneyBefore);
|
|
510
|
+
* console.log(canBeaver(moneyOffered));
|
|
511
|
+
* ```
|
|
512
|
+
*/
|
|
243
513
|
export function canBeaver(game) {
|
|
244
514
|
return game.phase === "double-offered" && game.settings.rules.beaver && game.cube.value * 4 <= CUBE_LIMIT;
|
|
245
515
|
}
|
|
246
|
-
/** Beaver: the side that was doubled redoubles and keeps the cube; the side that doubled must now take it or drop it.
|
|
516
|
+
/** Beaver: the side that was doubled redoubles and keeps the cube; the side that doubled must now take it or drop it.
|
|
517
|
+
*
|
|
518
|
+
* @param game - The current immutable native game state.
|
|
519
|
+
* @returns Beaver: the side that was doubled redoubles and keeps the cube; the side that doubled must now take it or drop it.
|
|
520
|
+
* @example
|
|
521
|
+
* ```ts
|
|
522
|
+
* import { beaverDouble, endTurn, legalPlaysOf, newGame, offerDouble, playTurn, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
523
|
+
*
|
|
524
|
+
* const moneySettings = settingsFor("backgammon",{points:0,cube:true,beaver:true});
|
|
525
|
+
* const moneyGame = rollOpening(newGame(moneySettings),[3,1]);
|
|
526
|
+
* const moneyBefore = playTurn(moneyGame,legalPlaysOf(moneyGame)[0].moves);
|
|
527
|
+
* const moneyOffered = offerDouble(moneyBefore);
|
|
528
|
+
* console.log(beaverDouble(moneyOffered).phase);
|
|
529
|
+
* ```
|
|
530
|
+
*/
|
|
247
531
|
export function beaverDouble(game) {
|
|
248
532
|
if (!canBeaver(game))
|
|
249
533
|
fail("A beaver cannot be played now.");
|
|
@@ -254,6 +538,18 @@ export function beaverDouble(game) {
|
|
|
254
538
|
* position could come to: a single game once it has borne a checker off; a
|
|
255
539
|
* gammon if nothing of the other side's can still hit it; otherwise a
|
|
256
540
|
* backgammon where it may have a checker caught in the other side's home board.
|
|
541
|
+
*
|
|
542
|
+
* @param game - The current immutable native game state.
|
|
543
|
+
* @param side - The side being examined: white or black.
|
|
544
|
+
* @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.
|
|
545
|
+
* @example
|
|
546
|
+
* ```ts
|
|
547
|
+
* import { concedeKind, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
548
|
+
*
|
|
549
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
550
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
551
|
+
* console.log(concedeKind(game,"black"));
|
|
552
|
+
* ```
|
|
257
553
|
*/
|
|
258
554
|
export function concedeKind(game, side) {
|
|
259
555
|
const winner = otherSide(side);
|
|
@@ -286,7 +582,22 @@ function gameCanStillHit(game, by, target) {
|
|
|
286
582
|
return byRear > mirrorPoint(spec, targetRear);
|
|
287
583
|
return true;
|
|
288
584
|
}
|
|
289
|
-
/** Give up the game: `side` loses. `kind` is what it is giving up, from `concedeKind`'s worst down to a single game.
|
|
585
|
+
/** Give up the game: `side` loses. `kind` is what it is giving up, from `concedeKind`'s worst down to a single game.
|
|
586
|
+
*
|
|
587
|
+
* @param game - The current immutable native game state.
|
|
588
|
+
* @param side - The side being examined: white or black.
|
|
589
|
+
* @param kind - The native single, gammon or backgammon result.
|
|
590
|
+
* @returns Give up the game: `side` loses. `kind` is what it is giving up, from `concedeKind`'s worst down to a single game.
|
|
591
|
+
* @example
|
|
592
|
+
* ```ts
|
|
593
|
+
* import { concede, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
594
|
+
*
|
|
595
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
596
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
597
|
+
* const result = concede(game,"black","single").result!;
|
|
598
|
+
* console.log(concede(game,"black","single").result);
|
|
599
|
+
* ```
|
|
600
|
+
*/
|
|
290
601
|
export function concede(game, side, kind = concedeKind(game, side)) {
|
|
291
602
|
if (game.phase === "over")
|
|
292
603
|
fail("The game is over.");
|
|
@@ -296,7 +607,20 @@ export function concede(game, side, kind = concedeKind(game, side)) {
|
|
|
296
607
|
fail(`Giving up a ${kind} is more than this position can lose.`);
|
|
297
608
|
return finish(game, resultOf(game, otherSide(side), "concede", kind, game.cube.value));
|
|
298
609
|
}
|
|
299
|
-
/** The game drawn by agreement.
|
|
610
|
+
/** The game drawn by agreement.
|
|
611
|
+
*
|
|
612
|
+
* @param game - The current immutable native game state.
|
|
613
|
+
* @returns The game drawn by agreement.
|
|
614
|
+
* @example
|
|
615
|
+
* ```ts
|
|
616
|
+
* import { agreeDraw, concede, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
617
|
+
*
|
|
618
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
619
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
620
|
+
* const result = concede(game,"black","single").result!;
|
|
621
|
+
* console.log(agreeDraw(game).result);
|
|
622
|
+
* ```
|
|
623
|
+
*/
|
|
300
624
|
export function agreeDraw(game) {
|
|
301
625
|
if (game.phase === "over")
|
|
302
626
|
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;
|