@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/geometry.js
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _BoardLayout_fieldLeft;
|
|
13
1
|
export const PAD = 14;
|
|
14
2
|
/** The strip above and below the points that holds their numbers. */
|
|
15
3
|
export const STRIP = 18;
|
|
@@ -29,9 +17,22 @@ export const DIE = 46;
|
|
|
29
17
|
/** The width and height of the board drawn lying down. */
|
|
30
18
|
export function landscapeSize(cube) {
|
|
31
19
|
const rail = cube ? RAIL_WIDTH + 10 : 0;
|
|
32
|
-
return {
|
|
20
|
+
return {
|
|
21
|
+
width: PAD + rail + 2 * HALF_WIDTH + BAR_WIDTH + TRAY_GAP + TRAY_WIDTH + PAD,
|
|
22
|
+
height: PAD + STRIP + 2 * POINT_LENGTH + MIDDLE + STRIP + PAD,
|
|
23
|
+
};
|
|
33
24
|
}
|
|
34
|
-
/** The width and height the board is drawn in, which is the other way about when it stands up.
|
|
25
|
+
/** The width and height the board is drawn in, which is the other way about when it stands up.
|
|
26
|
+
*
|
|
27
|
+
* @param options - Native drawing, geometry, mounting or computer choices specified by the signature.
|
|
28
|
+
* @returns The width and height the board is drawn in, which is the other way about when it stands up.
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { boardSize } from "@johnmorrisdotca/sugoroku/draw";
|
|
32
|
+
*
|
|
33
|
+
* console.log(boardSize({cube:true,orientation:"landscape"}));
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
35
36
|
export function boardSize(options) {
|
|
36
37
|
const { width, height } = landscapeSize(options.cube);
|
|
37
38
|
return options.orientation === "portrait" ? { width: height, height: width } : { width, height };
|
|
@@ -39,16 +40,28 @@ export function boardSize(options) {
|
|
|
39
40
|
/**
|
|
40
41
|
* The layout of a board, a function of its options: where each point, the bar,
|
|
41
42
|
* each tray, the rail and the dice sit, all in landscape units.
|
|
43
|
+
*
|
|
44
|
+
* @param options - Cube rail, viewing side, home direction and board orientation.
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { BoardLayout } from "@johnmorrisdotca/sugoroku/draw";
|
|
48
|
+
*
|
|
49
|
+
* const layout = new BoardLayout({cube:true,view:"white",home:"right",orientation:"landscape"});
|
|
50
|
+
* console.log(layout);
|
|
51
|
+
* ```
|
|
42
52
|
*/
|
|
43
53
|
export class BoardLayout {
|
|
54
|
+
width;
|
|
55
|
+
height;
|
|
56
|
+
fieldTop = PAD + STRIP;
|
|
57
|
+
options;
|
|
58
|
+
#fieldLeft;
|
|
44
59
|
constructor(options) {
|
|
45
|
-
this.fieldTop = PAD + STRIP;
|
|
46
|
-
_BoardLayout_fieldLeft.set(this, void 0);
|
|
47
60
|
this.options = options;
|
|
48
61
|
const size = landscapeSize(options.cube);
|
|
49
62
|
this.width = size.width;
|
|
50
63
|
this.height = size.height;
|
|
51
|
-
|
|
64
|
+
this.#fieldLeft = PAD + (options.cube ? RAIL_WIDTH + 10 : 0);
|
|
52
65
|
}
|
|
53
66
|
/** An x, for something `w` wide, after the board is mirrored for trays on the left. */
|
|
54
67
|
mx(x, w = 0) {
|
|
@@ -56,16 +69,16 @@ export class BoardLayout {
|
|
|
56
69
|
}
|
|
57
70
|
/** The x the field of points starts at, before mirroring. */
|
|
58
71
|
get fieldLeft() {
|
|
59
|
-
return
|
|
72
|
+
return this.#fieldLeft;
|
|
60
73
|
}
|
|
61
74
|
get fieldHeight() {
|
|
62
75
|
return 2 * POINT_LENGTH + MIDDLE;
|
|
63
76
|
}
|
|
64
77
|
get barLeft() {
|
|
65
|
-
return
|
|
78
|
+
return this.#fieldLeft + HALF_WIDTH;
|
|
66
79
|
}
|
|
67
80
|
get trayLeft() {
|
|
68
|
-
return
|
|
81
|
+
return this.#fieldLeft + 2 * HALF_WIDTH + BAR_WIDTH + TRAY_GAP;
|
|
69
82
|
}
|
|
70
83
|
/**
|
|
71
84
|
* Which slot of the drawing a point of the board is in: its row (the top
|
|
@@ -81,37 +94,60 @@ export class BoardLayout {
|
|
|
81
94
|
}
|
|
82
95
|
/** The x of the left edge of column `col`, before mirroring. */
|
|
83
96
|
columnLeft(col) {
|
|
84
|
-
return col < 6
|
|
97
|
+
return col < 6
|
|
98
|
+
? this.#fieldLeft + col * POINT_WIDTH
|
|
99
|
+
: this.#fieldLeft + HALF_WIDTH + BAR_WIDTH + (col - 6) * POINT_WIDTH;
|
|
85
100
|
}
|
|
86
101
|
/** The box of a point, as a base on its edge of the field and its length. */
|
|
87
102
|
pointBox(point) {
|
|
88
103
|
const { top, col } = this.slotOf(point);
|
|
89
104
|
const x = this.columnLeft(col);
|
|
90
|
-
return {
|
|
105
|
+
return {
|
|
106
|
+
x,
|
|
107
|
+
y: top ? this.fieldTop : this.fieldTop + this.fieldHeight - POINT_LENGTH,
|
|
108
|
+
w: POINT_WIDTH,
|
|
109
|
+
h: POINT_LENGTH,
|
|
110
|
+
top,
|
|
111
|
+
};
|
|
91
112
|
}
|
|
92
113
|
/** The tray of a side: the bottom one is white's when seen from white's side. */
|
|
93
114
|
trayBox(side) {
|
|
94
115
|
const top = (side === "black") === (this.options.view === "white");
|
|
95
|
-
return {
|
|
116
|
+
return {
|
|
117
|
+
x: this.trayLeft,
|
|
118
|
+
y: top ? this.fieldTop : this.fieldTop + this.fieldHeight - POINT_LENGTH,
|
|
119
|
+
w: TRAY_WIDTH,
|
|
120
|
+
h: POINT_LENGTH,
|
|
121
|
+
top,
|
|
122
|
+
};
|
|
96
123
|
}
|
|
97
124
|
/** The half of the bar a side's checkers wait in: the nearer half to the side's own tray. */
|
|
98
125
|
barBox(side) {
|
|
99
126
|
const top = this.trayBox(side).top;
|
|
100
|
-
return {
|
|
127
|
+
return {
|
|
128
|
+
x: this.barLeft,
|
|
129
|
+
y: top ? this.fieldTop : this.fieldTop + this.fieldHeight / 2,
|
|
130
|
+
w: BAR_WIDTH,
|
|
131
|
+
h: this.fieldHeight / 2,
|
|
132
|
+
top,
|
|
133
|
+
};
|
|
101
134
|
}
|
|
102
135
|
/** Where the cube rests: the middle of the rail, or the end of it nearer its owner. */
|
|
103
136
|
cubeBox(owner) {
|
|
104
137
|
const size = 44;
|
|
105
138
|
const x = PAD + (RAIL_WIDTH - size) / 2;
|
|
106
139
|
const top = owner === null ? null : (owner === "black") === (this.options.view === "white");
|
|
107
|
-
const y = top === null
|
|
140
|
+
const y = top === null
|
|
141
|
+
? this.fieldTop + this.fieldHeight / 2 - size / 2
|
|
142
|
+
: top
|
|
143
|
+
? this.fieldTop + 4
|
|
144
|
+
: this.fieldTop + this.fieldHeight - size - 4;
|
|
108
145
|
return { x, y, w: size, h: size };
|
|
109
146
|
}
|
|
110
147
|
/** The middle of the row of dice, on the half of the board a side plays from: its own home half for white, the other for black. */
|
|
111
148
|
diceCentre(side) {
|
|
112
|
-
const rightHalf =
|
|
113
|
-
const leftHalf =
|
|
149
|
+
const rightHalf = this.#fieldLeft + HALF_WIDTH + BAR_WIDTH + HALF_WIDTH / 2;
|
|
150
|
+
const leftHalf = this.#fieldLeft + HALF_WIDTH / 2;
|
|
114
151
|
return { x: side === "white" ? rightHalf : leftHalf, y: this.fieldTop + this.fieldHeight / 2 };
|
|
115
152
|
}
|
|
116
153
|
}
|
|
117
|
-
_BoardLayout_fieldLeft = new WeakMap();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
export * from "./variants.ts";
|
|
3
|
-
export * from "./rules.ts";
|
|
1
|
+
/** Pure backgammon and variant rules, legal turns, cube and match scoring, dice, records and computer choices. */
|
|
4
2
|
export * from "./board.ts";
|
|
5
|
-
export * from "./
|
|
6
|
-
export * from "./
|
|
3
|
+
export * from "./computer.ts";
|
|
4
|
+
export * from "./dice.ts";
|
|
5
|
+
export type { EvaluationWeights } from "./evaluate.ts";
|
|
6
|
+
export { EVALUATION_WEIGHTS, evaluate, evaluateFor, keithCount, winChance } from "./evaluate.ts";
|
|
7
7
|
export * from "./game.ts";
|
|
8
8
|
export * from "./match.ts";
|
|
9
|
-
export * from "./
|
|
10
|
-
export { hashSeed, seededRandom } from "./random.ts";
|
|
11
|
-
export type { Random } from "./random.ts";
|
|
9
|
+
export * from "./moves.ts";
|
|
12
10
|
export * from "./notation.ts";
|
|
11
|
+
export * from "./plays.ts";
|
|
13
12
|
export * from "./positionId.ts";
|
|
13
|
+
export type { Random } from "./random.ts";
|
|
14
|
+
export { hashSeed, seededRandom } from "./random.ts";
|
|
14
15
|
export * from "./record.ts";
|
|
15
|
-
export * from "./
|
|
16
|
-
export
|
|
17
|
-
export
|
|
16
|
+
export * from "./rules.ts";
|
|
17
|
+
export * from "./side.ts";
|
|
18
|
+
export * from "./variants.ts";
|
|
18
19
|
export { VERSION } from "./version.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
export * from "./variants.js";
|
|
3
|
-
export * from "./rules.js";
|
|
1
|
+
/** Pure backgammon and variant rules, legal turns, cube and match scoring, dice, records and computer choices. */
|
|
4
2
|
export * from "./board.js";
|
|
5
|
-
export * from "./
|
|
6
|
-
export * from "./
|
|
3
|
+
export * from "./computer.js";
|
|
4
|
+
export * from "./dice.js";
|
|
5
|
+
export { EVALUATION_WEIGHTS, evaluate, evaluateFor, keithCount, winChance } from "./evaluate.js";
|
|
7
6
|
export * from "./game.js";
|
|
8
7
|
export * from "./match.js";
|
|
9
|
-
export * from "./
|
|
10
|
-
export { hashSeed, seededRandom } from "./random.js";
|
|
8
|
+
export * from "./moves.js";
|
|
11
9
|
export * from "./notation.js";
|
|
10
|
+
export * from "./plays.js";
|
|
12
11
|
export * from "./positionId.js";
|
|
12
|
+
export { hashSeed, seededRandom } from "./random.js";
|
|
13
13
|
export * from "./record.js";
|
|
14
|
-
export * from "./
|
|
15
|
-
export
|
|
14
|
+
export * from "./rules.js";
|
|
15
|
+
export * from "./side.js";
|
|
16
|
+
export * from "./variants.js";
|
|
16
17
|
export { VERSION } from "./version.js";
|
package/dist/match.d.ts
CHANGED
|
@@ -6,6 +6,17 @@ import { type Side } from "./side.ts";
|
|
|
6
6
|
* end in money play. It keeps the score and says which game is the Crawford
|
|
7
7
|
* game. Like everything here it is plain data, and never changed: each
|
|
8
8
|
* function gives back a new match.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { newMatch, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
13
|
+
* import type { Match } from "@johnmorrisdotca/sugoroku";
|
|
14
|
+
*
|
|
15
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
16
|
+
* const match = newMatch(settings);
|
|
17
|
+
* const value: Match = match;
|
|
18
|
+
* console.log(value);
|
|
19
|
+
* ```
|
|
9
20
|
*/
|
|
10
21
|
export type Match = {
|
|
11
22
|
readonly settings: Settings;
|
|
@@ -22,11 +33,62 @@ export type Match = {
|
|
|
22
33
|
/** Who won the match; null while it goes on, or when it was drawn. */
|
|
23
34
|
readonly winner: Side | null;
|
|
24
35
|
};
|
|
25
|
-
/** A match at 0 to 0.
|
|
36
|
+
/** A match at 0 to 0.
|
|
37
|
+
*
|
|
38
|
+
* @param settings - The resolved variant and complete rules.
|
|
39
|
+
* @returns A match at 0 to 0.
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { newMatch, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
43
|
+
*
|
|
44
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
45
|
+
* console.log(newMatch(settings).score);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
26
48
|
export declare function newMatch(settings: Settings): Match;
|
|
27
|
-
/** The first game, or the next game, of a match.
|
|
49
|
+
/** The first game, or the next game, of a match.
|
|
50
|
+
*
|
|
51
|
+
* @param match - The current immutable match and score.
|
|
52
|
+
* @returns The first game, or the next game, of a match.
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { newMatch, settingsFor, startGame } from "@johnmorrisdotca/sugoroku";
|
|
56
|
+
*
|
|
57
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
58
|
+
* const match = newMatch(settings);
|
|
59
|
+
* console.log(startGame(match).phase);
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
28
62
|
export declare function startGame(match: Match): GameState;
|
|
29
|
-
/** How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
63
|
+
/** How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
64
|
+
*
|
|
65
|
+
* @param match - The current immutable match and score.
|
|
66
|
+
* @param side - The side being examined: white or black.
|
|
67
|
+
* @returns How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { newMatch, pointsToGo, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
71
|
+
*
|
|
72
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
73
|
+
* const match = newMatch(settings);
|
|
74
|
+
* console.log(pointsToGo(match,"white"));
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
30
77
|
export declare function pointsToGo(match: Match, side: Side): number;
|
|
31
|
-
/** The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
78
|
+
/** The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
79
|
+
*
|
|
80
|
+
* @param match - The current immutable match and score.
|
|
81
|
+
* @param result - The completed native game result to add to a match.
|
|
82
|
+
* @returns The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { concede, finishGame, newGame, newMatch, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
86
|
+
*
|
|
87
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
88
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
89
|
+
* const match = newMatch(settings);
|
|
90
|
+
* const result = concede(game,"black","single").result!;
|
|
91
|
+
* console.log(finishGame(match,result).score);
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
32
94
|
export declare function finishGame(match: Match, result: GameResult): Match;
|
package/dist/match.js
CHANGED
|
@@ -1,21 +1,80 @@
|
|
|
1
1
|
import { newGame } from "./game.js";
|
|
2
2
|
import { sideIndex } from "./side.js";
|
|
3
|
-
/** A match at 0 to 0.
|
|
3
|
+
/** A match at 0 to 0.
|
|
4
|
+
*
|
|
5
|
+
* @param settings - The resolved variant and complete rules.
|
|
6
|
+
* @returns A match at 0 to 0.
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { newMatch, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
10
|
+
*
|
|
11
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
12
|
+
* console.log(newMatch(settings).score);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
4
15
|
export function newMatch(settings) {
|
|
5
|
-
return {
|
|
16
|
+
return {
|
|
17
|
+
settings,
|
|
18
|
+
score: [0, 0],
|
|
19
|
+
results: [],
|
|
20
|
+
crawfordNext: false,
|
|
21
|
+
crawfordPlayed: false,
|
|
22
|
+
over: false,
|
|
23
|
+
winner: null,
|
|
24
|
+
};
|
|
6
25
|
}
|
|
7
|
-
/** The first game, or the next game, of a match.
|
|
26
|
+
/** The first game, or the next game, of a match.
|
|
27
|
+
*
|
|
28
|
+
* @param match - The current immutable match and score.
|
|
29
|
+
* @returns The first game, or the next game, of a match.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { newMatch, settingsFor, startGame } from "@johnmorrisdotca/sugoroku";
|
|
33
|
+
*
|
|
34
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
35
|
+
* const match = newMatch(settings);
|
|
36
|
+
* console.log(startGame(match).phase);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
8
39
|
export function startGame(match) {
|
|
9
40
|
if (match.over)
|
|
10
41
|
throw new Error("The match is over.");
|
|
11
42
|
return newGame(match.settings, { crawford: match.crawfordNext, score: match.score });
|
|
12
43
|
}
|
|
13
|
-
/** How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
44
|
+
/** How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
45
|
+
*
|
|
46
|
+
* @param match - The current immutable match and score.
|
|
47
|
+
* @param side - The side being examined: white or black.
|
|
48
|
+
* @returns How many points a side still needs to win the match: 0 in money play, where there is no such thing.
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { newMatch, pointsToGo, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
52
|
+
*
|
|
53
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
54
|
+
* const match = newMatch(settings);
|
|
55
|
+
* console.log(pointsToGo(match,"white"));
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
14
58
|
export function pointsToGo(match, side) {
|
|
15
59
|
const { points } = match.settings.rules;
|
|
16
60
|
return points === 0 ? 0 : Math.max(0, points - match.score[sideIndex(side)]);
|
|
17
61
|
}
|
|
18
|
-
/** The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
62
|
+
/** The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
63
|
+
*
|
|
64
|
+
* @param match - The current immutable match and score.
|
|
65
|
+
* @param result - The completed native game result to add to a match.
|
|
66
|
+
* @returns The match after a game's result: the score added up, the Crawford game accounted for, and the match over if it is decided.
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { concede, finishGame, newGame, newMatch, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
70
|
+
*
|
|
71
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
72
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
73
|
+
* const match = newMatch(settings);
|
|
74
|
+
* const result = concede(game,"black","single").result!;
|
|
75
|
+
* console.log(finishGame(match,result).score);
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
19
78
|
export function finishGame(match, result) {
|
|
20
79
|
const { points, crawford } = match.settings.rules;
|
|
21
80
|
const score = [match.score[0], match.score[1]];
|
|
@@ -28,6 +87,10 @@ export function finishGame(match, result) {
|
|
|
28
87
|
return { ...match, score, results, crawfordNext: false, over: true, winner: result.winner };
|
|
29
88
|
// The Crawford game is the one after a side first comes within a point of winning, and there is only one.
|
|
30
89
|
const crawfordPlayed = match.crawfordPlayed || match.crawfordNext;
|
|
31
|
-
const reachedNow = points > 1 &&
|
|
90
|
+
const reachedNow = points > 1 &&
|
|
91
|
+
crawford &&
|
|
92
|
+
!crawfordPlayed &&
|
|
93
|
+
Math.max(score[0], score[1]) === points - 1 &&
|
|
94
|
+
Math.max(match.score[0], match.score[1]) < points - 1;
|
|
32
95
|
return { ...match, score, results, crawfordNext: reachedNow, crawfordPlayed, over: false, winner: null };
|
|
33
96
|
}
|
package/dist/mount.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { SugorokuBoardName, SugorokuCheckerSetName, SugorokuColours } from "./colours.ts";
|
|
2
2
|
import { type Strength } from "./computer.ts";
|
|
3
|
-
import { type Orientation } from "./geometry.ts";
|
|
4
3
|
import { type GameResult, type GameState } from "./game.ts";
|
|
4
|
+
import type { Orientation } from "./geometry.ts";
|
|
5
5
|
import { type Match } from "./match.ts";
|
|
6
6
|
import type { Move } from "./moves.ts";
|
|
7
7
|
import { type RecordSeed } from "./record.ts";
|
|
8
8
|
import { type Rules } from "./rules.ts";
|
|
9
|
-
import {
|
|
9
|
+
import type { Side } from "./side.ts";
|
|
10
10
|
import { type SoundUrls } from "./sound.ts";
|
|
11
11
|
import { type SugorokuLanguage } from "./strings.ts";
|
|
12
12
|
import type { VariantKey } from "./variants.ts";
|
|
@@ -27,7 +27,16 @@ import type { VariantKey } from "./variants.ts";
|
|
|
27
27
|
* Needs a page. The rules it plays by are `game.ts`'s, the drawing is
|
|
28
28
|
* `drawSugoroku`'s and the opponent is `computer.ts`'s: all usable alone.
|
|
29
29
|
*/
|
|
30
|
-
/** How the board looks, apart from the game.
|
|
30
|
+
/** How the board looks, apart from the game.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import type { SugorokuLook } from "@johnmorrisdotca/sugoroku/play";
|
|
35
|
+
*
|
|
36
|
+
* const value: SugorokuLook = {board:"green",checkers:"contrast",numbers:true};
|
|
37
|
+
* console.log(value);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
31
40
|
export type SugorokuLook = {
|
|
32
41
|
board?: SugorokuBoardName;
|
|
33
42
|
checkers?: SugorokuCheckerSetName;
|
|
@@ -42,7 +51,20 @@ export type SugorokuLook = {
|
|
|
42
51
|
/** `auto` (the default) stands the board up in a narrow page, such as a phone's. */
|
|
43
52
|
orientation?: Orientation | "auto";
|
|
44
53
|
};
|
|
45
|
-
/** What every event of the board carries.
|
|
54
|
+
/** What every event of the board carries.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { newGame, newMatch, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
59
|
+
* import type { SugorokuEventDetail } from "@johnmorrisdotca/sugoroku/play";
|
|
60
|
+
*
|
|
61
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
62
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
63
|
+
* const match = newMatch(settings);
|
|
64
|
+
* const value: SugorokuEventDetail = {side:"white",game,match,dice:[3,1]};
|
|
65
|
+
* console.log(value);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
46
68
|
export type SugorokuEventDetail = {
|
|
47
69
|
/** The side it is about; null for the opening throw. */
|
|
48
70
|
side: Side | null;
|
|
@@ -61,6 +83,16 @@ export type SugorokuEventDetail = {
|
|
|
61
83
|
/** The record of the match so far, for `sugoroku-end`. */
|
|
62
84
|
record?: string;
|
|
63
85
|
};
|
|
86
|
+
/** Native variant, rules, seed, computer, display, control, sound and event-callback choices.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* import type { SugorokuMountOptions } from "@johnmorrisdotca/sugoroku/play";
|
|
91
|
+
*
|
|
92
|
+
* const value: SugorokuMountOptions = {variant:"backgammon",seed:42,computer:{black:"greedy"}};
|
|
93
|
+
* console.log(value);
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
64
96
|
export type SugorokuMountOptions = SugorokuLook & {
|
|
65
97
|
/** The variant. Default `backgammon`. */
|
|
66
98
|
variant?: VariantKey;
|
|
@@ -99,6 +131,23 @@ export type SugorokuMountOptions = SugorokuLook & {
|
|
|
99
131
|
onCube?: (detail: SugorokuEventDetail) => void;
|
|
100
132
|
onEnd?: (detail: SugorokuEventDetail) => void;
|
|
101
133
|
};
|
|
134
|
+
/** The mounted native board, current game/match/record, actions and cleanup handle.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts no-run
|
|
138
|
+
* import { newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
139
|
+
* import type { SugorokuMount } from "@johnmorrisdotca/sugoroku/play";
|
|
140
|
+
* import { mountSugoroku } from "@johnmorrisdotca/sugoroku/play";
|
|
141
|
+
*
|
|
142
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
143
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
144
|
+
* const host = document.createElement("div");
|
|
145
|
+
* document.body.append(host);
|
|
146
|
+
* const board: SugorokuMount = mountSugoroku(host,{variant:"backgammon",seed:42,computer:{white:null,black:null}});
|
|
147
|
+
* console.log(board.game().phase);
|
|
148
|
+
* board.destroy();
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
102
151
|
export type SugorokuMount = {
|
|
103
152
|
readonly host: HTMLElement;
|
|
104
153
|
/** The game as it stands. */
|
|
@@ -138,7 +187,29 @@ export type SugorokuMount = {
|
|
|
138
187
|
/** Take the board down: its listeners, its timers and everything it put in the host. */
|
|
139
188
|
destroy: () => void;
|
|
140
189
|
};
|
|
141
|
-
/** Put the style in the page once: in the document's head, or in the shadow root the host is in.
|
|
190
|
+
/** Put the style in the page once: in the document's head, or in the shadow root the host is in.
|
|
191
|
+
*
|
|
192
|
+
* @param host - The DOM element receiving the native board or scoped styles.
|
|
193
|
+
* @returns Nothing; the native scoped board styles are installed for the host.
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts no-run
|
|
196
|
+
* import { ensureSugorokuPlayStyle } from "@johnmorrisdotca/sugoroku/play";
|
|
197
|
+
*
|
|
198
|
+
* const host = document.createElement("div");
|
|
199
|
+
* ensureSugorokuPlayStyle(host);
|
|
200
|
+
* console.log(host.tagName);
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
142
203
|
export declare function ensureSugorokuPlayStyle(host: Element): void;
|
|
143
|
-
/** Mount a board into `host`.
|
|
204
|
+
/** Mount a board into `host`.
|
|
205
|
+
* @param host - The HTMLElement that owns the native interface.
|
|
206
|
+
* @param options - Native settings and callbacks for this mounted interface.
|
|
207
|
+
* @returns The native mounted handle, including controls and destroy() cleanup.
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* import { mountSugoroku } from "@johnmorrisdotca/sugoroku/play";
|
|
211
|
+
* const show = (host: HTMLElement, options: Parameters<typeof mountSugoroku>[1]) => mountSugoroku(host, options);
|
|
212
|
+
* console.log(typeof show); // function
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
144
215
|
export declare function mountSugoroku(host: HTMLElement, options?: SugorokuMountOptions): SugorokuMount;
|