@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
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import { BAR, boardPoint } from "./board.js";
|
|
2
|
+
import { drawSugoroku } from "./draw.js";
|
|
3
|
+
import { canBeaver, canDouble, legalMoves, turnIsPlayed } from "./game.js";
|
|
4
|
+
import { boardSize, CHECKER } from "./geometry.js";
|
|
5
|
+
import { diceToPlay } from "./plays.js";
|
|
6
|
+
import { otherSide, sideIndex } from "./side.js";
|
|
7
|
+
/** Draw the board, keep the words and buttons up to date, and play by pointer. */
|
|
8
|
+
export function attachSugorokuView(input) {
|
|
9
|
+
const host = input.host;
|
|
10
|
+
const document = input.doc;
|
|
11
|
+
const view = input.page;
|
|
12
|
+
const boardBox = input.boardBox;
|
|
13
|
+
const score = input.score;
|
|
14
|
+
const status = input.status;
|
|
15
|
+
const slotA = input.slotA;
|
|
16
|
+
const slotB = input.slotB;
|
|
17
|
+
const slotC = input.slotC;
|
|
18
|
+
const dragAllowed = input.dragAllowed;
|
|
19
|
+
const say = input.say;
|
|
20
|
+
const nameOf = input.nameOf;
|
|
21
|
+
const human = input.human;
|
|
22
|
+
const isComputer = input.isComputer;
|
|
23
|
+
const spec = input.spec;
|
|
24
|
+
const makeMove = input.makeMove;
|
|
25
|
+
let selected = null;
|
|
26
|
+
let message = null;
|
|
27
|
+
let portrait = false;
|
|
28
|
+
function create(owner, tag, className, text) {
|
|
29
|
+
const element = owner.createElement(tag);
|
|
30
|
+
element.className = className;
|
|
31
|
+
if (text !== undefined)
|
|
32
|
+
element.textContent = text;
|
|
33
|
+
return element;
|
|
34
|
+
}
|
|
35
|
+
const hasCube = () => {
|
|
36
|
+
const settings = input.settings();
|
|
37
|
+
return settings.rules.cube && settings.rules.points !== 1;
|
|
38
|
+
};
|
|
39
|
+
/** Whose home board is at the bottom. */
|
|
40
|
+
const viewSide = () => {
|
|
41
|
+
const look = input.look();
|
|
42
|
+
if (look.view !== undefined && look.view !== "auto")
|
|
43
|
+
return look.view;
|
|
44
|
+
if (isComputer("white") && human("black"))
|
|
45
|
+
return "black";
|
|
46
|
+
return "white";
|
|
47
|
+
};
|
|
48
|
+
// Drawing.
|
|
49
|
+
const diceShown = () => {
|
|
50
|
+
const game = input.game();
|
|
51
|
+
const lastOpening = input.lastOpening();
|
|
52
|
+
if (game.phase === "opening") {
|
|
53
|
+
return lastOpening === null
|
|
54
|
+
? null
|
|
55
|
+
: { side: "white", values: [lastOpening[0], lastOpening[1]], spent: [false, false] };
|
|
56
|
+
}
|
|
57
|
+
if (game.dice === null || game.turn === null)
|
|
58
|
+
return null;
|
|
59
|
+
const values = diceToPlay(spec(), game.dice);
|
|
60
|
+
const unused = [...game.need];
|
|
61
|
+
const planLeft = [...game.plan];
|
|
62
|
+
const spent = values.map((value) => {
|
|
63
|
+
const inPlan = planLeft.indexOf(value);
|
|
64
|
+
if (inPlan === -1)
|
|
65
|
+
return true;
|
|
66
|
+
planLeft.splice(inPlan, 1);
|
|
67
|
+
const open = unused.indexOf(value);
|
|
68
|
+
if (open === -1)
|
|
69
|
+
return true;
|
|
70
|
+
unused.splice(open, 1);
|
|
71
|
+
return false;
|
|
72
|
+
});
|
|
73
|
+
return { side: game.turn, values, spent };
|
|
74
|
+
};
|
|
75
|
+
const legalNow = () => {
|
|
76
|
+
const game = input.game();
|
|
77
|
+
return game.phase === "playing" && game.turn !== null && human(game.turn) ? legalMoves(game) : [];
|
|
78
|
+
};
|
|
79
|
+
const draw = () => {
|
|
80
|
+
const game = input.game();
|
|
81
|
+
const look = input.look();
|
|
82
|
+
const moves = legalNow();
|
|
83
|
+
const mover = game.turn;
|
|
84
|
+
const sources = [...new Set(moves.map((move) => move.from))];
|
|
85
|
+
const targets = selected === null ? [] : [...new Set(moves.filter((move) => move.from === selected).map((move) => move.to))];
|
|
86
|
+
return drawSugoroku(game.position, {
|
|
87
|
+
variant: spec(),
|
|
88
|
+
view: viewSide(),
|
|
89
|
+
home: look.home,
|
|
90
|
+
orientation: portrait ? "portrait" : "landscape",
|
|
91
|
+
numbers: look.numbers,
|
|
92
|
+
board: look.board,
|
|
93
|
+
checkers: look.checkers,
|
|
94
|
+
colours: look.colours,
|
|
95
|
+
theme: look.theme,
|
|
96
|
+
dice: diceShown(),
|
|
97
|
+
cube: hasCube() ? { value: game.cube.value, owner: game.cube.owner } : null,
|
|
98
|
+
highlight: mover === null ? null : { side: mover, from: selected, targets, movable: sources },
|
|
99
|
+
label: say("board", { variant: say(`variant_${spec().key}`) }),
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
// The words.
|
|
103
|
+
const pointsText = (count) => say(count === 1 ? "point" : "points", { n: count });
|
|
104
|
+
const diceText = (dice) => dice === null ? "" : diceToPlay(spec(), dice).join(" ");
|
|
105
|
+
const statusText = () => {
|
|
106
|
+
const game = input.game();
|
|
107
|
+
const match = input.match();
|
|
108
|
+
const lastOpening = input.lastOpening();
|
|
109
|
+
const waitingForDice = input.waitingForDice();
|
|
110
|
+
if (message !== null)
|
|
111
|
+
return message;
|
|
112
|
+
const side = game.turn;
|
|
113
|
+
if (game.phase === "opening")
|
|
114
|
+
return waitingForDice
|
|
115
|
+
? say("waitingDice")
|
|
116
|
+
: lastOpening !== null && lastOpening[0] === lastOpening[1]
|
|
117
|
+
? say("openingTie", { die: lastOpening[0] })
|
|
118
|
+
: say("opening");
|
|
119
|
+
if (game.phase === "over") {
|
|
120
|
+
const result = game.result;
|
|
121
|
+
let line;
|
|
122
|
+
if (result.winner === null)
|
|
123
|
+
line = say("drawn");
|
|
124
|
+
else if (result.how === "drop")
|
|
125
|
+
line = say("wonDrop", { side: nameOf(result.winner), points: pointsText(result.points) });
|
|
126
|
+
else if (result.how === "concede")
|
|
127
|
+
line = say("wonConcede", { side: nameOf(result.winner), points: pointsText(result.points) });
|
|
128
|
+
else
|
|
129
|
+
line = say("wonBy", {
|
|
130
|
+
side: nameOf(result.winner),
|
|
131
|
+
kind: say(result.kind === "single" ? "singleGame" : result.kind),
|
|
132
|
+
points: pointsText(result.points),
|
|
133
|
+
});
|
|
134
|
+
if (match.over)
|
|
135
|
+
line += ` ${match.winner === null ? say("matchDrawn") : say("matchWon", { side: nameOf(match.winner), a: match.score[sideIndex(match.winner)], b: match.score[sideIndex(otherSide(match.winner))] })}`;
|
|
136
|
+
return line;
|
|
137
|
+
}
|
|
138
|
+
if (side === null)
|
|
139
|
+
return "";
|
|
140
|
+
if (game.phase === "double-offered" || game.phase === "beaver-offered") {
|
|
141
|
+
const offerer = game.offeredBy;
|
|
142
|
+
const value = game.phase === "double-offered" ? game.cube.value * 2 : game.cube.value * 4;
|
|
143
|
+
const lose = pointsText(game.phase === "double-offered" ? game.cube.value : game.cube.value * 2);
|
|
144
|
+
if (human(otherSide(offerer)))
|
|
145
|
+
return say(game.phase === "double-offered" ? "answerDouble" : "answerBeaver", {
|
|
146
|
+
side: nameOf(offerer),
|
|
147
|
+
value,
|
|
148
|
+
points: lose,
|
|
149
|
+
});
|
|
150
|
+
return say("thinking", { side: nameOf(otherSide(offerer)) });
|
|
151
|
+
}
|
|
152
|
+
if (game.phase === "before-roll") {
|
|
153
|
+
if (isComputer(side))
|
|
154
|
+
return say("thinking", { side: nameOf(side) });
|
|
155
|
+
return isComputer(otherSide(side)) ? say("toRollYou") : say("toRoll", { side: nameOf(side) });
|
|
156
|
+
}
|
|
157
|
+
if (isComputer(side) && game.moves.length === 0 && game.need.length > 0)
|
|
158
|
+
return say("thinking", { side: nameOf(side) });
|
|
159
|
+
if (game.plan.length === 0)
|
|
160
|
+
return say(human(side) && isComputer(otherSide(side)) ? "noMoveYou" : "noMove", { side: nameOf(side) });
|
|
161
|
+
if (turnIsPlayed(game))
|
|
162
|
+
return human(side) ? say("played") : say("toPlay", { side: nameOf(side), dice: diceText(game.dice) });
|
|
163
|
+
return human(side) && isComputer(otherSide(side))
|
|
164
|
+
? say("toPlayYou", { dice: diceText(game.dice) })
|
|
165
|
+
: say("toPlay", { side: nameOf(side), dice: diceText(game.dice) });
|
|
166
|
+
};
|
|
167
|
+
const scoreText = () => {
|
|
168
|
+
const settings = input.settings();
|
|
169
|
+
const match = input.match();
|
|
170
|
+
const game = input.game();
|
|
171
|
+
const { rules } = settings;
|
|
172
|
+
const parts = [
|
|
173
|
+
say("score", { white: nameOf("white"), a: match.score[0], black: nameOf("black"), b: match.score[1] }),
|
|
174
|
+
rules.points === 0 ? say("money") : say("matchTo", { points: rules.points }),
|
|
175
|
+
];
|
|
176
|
+
if (game.crawford && game.phase !== "over")
|
|
177
|
+
parts.push(say("crawford"));
|
|
178
|
+
else if (hasCube())
|
|
179
|
+
parts.push(game.cube.owner === null
|
|
180
|
+
? say("cubeMiddle")
|
|
181
|
+
: say("cubeHeld", { value: game.cube.value, side: nameOf(game.cube.owner) }));
|
|
182
|
+
return parts.join(" · ");
|
|
183
|
+
};
|
|
184
|
+
const slots = () => {
|
|
185
|
+
const game = input.game();
|
|
186
|
+
const match = input.match();
|
|
187
|
+
const waitingForDice = input.waitingForDice();
|
|
188
|
+
const off = { text: "", enabled: false, run: () => undefined };
|
|
189
|
+
const side = game.turn;
|
|
190
|
+
const turnOfHuman = side !== null && human(side);
|
|
191
|
+
if (game.phase === "over") {
|
|
192
|
+
return [
|
|
193
|
+
match.over
|
|
194
|
+
? { text: say("newMatch"), enabled: true, run: () => input.actions().newMatch(), primary: true }
|
|
195
|
+
: { text: say("nextGame"), enabled: true, run: () => input.actions().nextGame(), primary: true },
|
|
196
|
+
match.over ? off : { text: say("newMatch"), enabled: true, run: () => input.actions().newMatch() },
|
|
197
|
+
off,
|
|
198
|
+
];
|
|
199
|
+
}
|
|
200
|
+
if (game.phase === "opening") {
|
|
201
|
+
const nobody = isComputer("white") && isComputer("black");
|
|
202
|
+
return [
|
|
203
|
+
{
|
|
204
|
+
text: say("rollOpening"),
|
|
205
|
+
enabled: !nobody && !waitingForDice,
|
|
206
|
+
run: () => input.actions().roll(),
|
|
207
|
+
primary: true,
|
|
208
|
+
attention: !nobody,
|
|
209
|
+
},
|
|
210
|
+
off,
|
|
211
|
+
off,
|
|
212
|
+
];
|
|
213
|
+
}
|
|
214
|
+
if (game.phase === "double-offered" || game.phase === "beaver-offered") {
|
|
215
|
+
const answerer = otherSide(game.offeredBy);
|
|
216
|
+
const mine = human(answerer);
|
|
217
|
+
return [
|
|
218
|
+
{ text: say("take"), enabled: mine, run: () => input.actions().take(), primary: true, attention: mine },
|
|
219
|
+
{ text: say("drop"), enabled: mine, run: () => input.actions().drop() },
|
|
220
|
+
{ text: say("beaver"), enabled: mine && canBeaver(game), run: () => input.actions().beaver() },
|
|
221
|
+
];
|
|
222
|
+
}
|
|
223
|
+
if (game.phase === "before-roll") {
|
|
224
|
+
return [
|
|
225
|
+
{
|
|
226
|
+
text: say("roll"),
|
|
227
|
+
enabled: turnOfHuman && !waitingForDice,
|
|
228
|
+
run: () => input.actions().roll(),
|
|
229
|
+
primary: true,
|
|
230
|
+
attention: turnOfHuman,
|
|
231
|
+
},
|
|
232
|
+
{ text: say("double"), enabled: turnOfHuman && canDouble(game), run: () => input.actions().double() },
|
|
233
|
+
{ text: say("undo"), enabled: false, run: () => undefined },
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
const played = turnIsPlayed(game);
|
|
237
|
+
return [
|
|
238
|
+
{
|
|
239
|
+
text: say("done"),
|
|
240
|
+
enabled: turnOfHuman && played,
|
|
241
|
+
run: () => input.actions().done(),
|
|
242
|
+
primary: true,
|
|
243
|
+
attention: turnOfHuman && played,
|
|
244
|
+
},
|
|
245
|
+
{ text: say("double"), enabled: false, run: () => undefined },
|
|
246
|
+
{ text: say("undo"), enabled: turnOfHuman && game.moves.length > 0, run: () => input.actions().undo() },
|
|
247
|
+
];
|
|
248
|
+
};
|
|
249
|
+
let handlers = [() => undefined, () => undefined, () => undefined];
|
|
250
|
+
const render = () => {
|
|
251
|
+
const game = input.game();
|
|
252
|
+
const look = input.look();
|
|
253
|
+
if (input.stopped())
|
|
254
|
+
return;
|
|
255
|
+
const width = host.clientWidth || 600;
|
|
256
|
+
portrait = look.orientation === "portrait" || (look.orientation !== "landscape" && width < 560);
|
|
257
|
+
const size = boardSize({ cube: hasCube(), orientation: portrait ? "portrait" : "landscape" });
|
|
258
|
+
boardBox.style.aspectRatio = `${size.width} / ${size.height}`;
|
|
259
|
+
boardBox.innerHTML = draw();
|
|
260
|
+
host.dataset.phase = game.phase;
|
|
261
|
+
host.dataset.turn = game.turn ?? "";
|
|
262
|
+
host.dataset.over = String(game.phase === "over");
|
|
263
|
+
host.dataset.orientation = portrait ? "portrait" : "landscape";
|
|
264
|
+
host.dataset.selected = selected === null ? "" : String(selected);
|
|
265
|
+
host.dataset.cube = String(game.cube.value);
|
|
266
|
+
host.dataset.need = game.need.join("");
|
|
267
|
+
host.dataset.ready = "true";
|
|
268
|
+
score.textContent = scoreText();
|
|
269
|
+
status.textContent = statusText();
|
|
270
|
+
const [a, b, c] = slots();
|
|
271
|
+
const buttons = [slotA, slotB, slotC];
|
|
272
|
+
[a, b, c].forEach((slot, index) => {
|
|
273
|
+
const button = buttons[index];
|
|
274
|
+
button.textContent = slot.text;
|
|
275
|
+
button.disabled = !slot.enabled;
|
|
276
|
+
button.dataset.primary = String(slot.primary === true);
|
|
277
|
+
button.dataset.attention = String(slot.attention === true);
|
|
278
|
+
button.style.visibility = slot.text === "" ? "hidden" : "visible";
|
|
279
|
+
});
|
|
280
|
+
handlers = [a.run, b.run, c.run];
|
|
281
|
+
};
|
|
282
|
+
slotA.addEventListener("click", () => handlers[0]());
|
|
283
|
+
slotB.addEventListener("click", () => handlers[1]());
|
|
284
|
+
slotC.addEventListener("click", () => handlers[2]());
|
|
285
|
+
// Touch and mouse.
|
|
286
|
+
const whereIs = (target) => {
|
|
287
|
+
const game = input.game();
|
|
288
|
+
const mover = game.turn;
|
|
289
|
+
if (mover === null || target === null)
|
|
290
|
+
return null;
|
|
291
|
+
const bar = target.closest("[data-bar]");
|
|
292
|
+
if (bar !== null)
|
|
293
|
+
return bar.dataset.bar === mover ? BAR : null;
|
|
294
|
+
const tray = target.closest("[data-tray]");
|
|
295
|
+
if (tray !== null)
|
|
296
|
+
return tray.dataset.tray === mover ? 0 : null;
|
|
297
|
+
const point = target.closest("[data-board]");
|
|
298
|
+
if (point === null || point.dataset.board === undefined)
|
|
299
|
+
return null;
|
|
300
|
+
return boardPoint(spec(), mover, Number(point.dataset.board));
|
|
301
|
+
};
|
|
302
|
+
const press = (own) => {
|
|
303
|
+
const moves = legalNow();
|
|
304
|
+
if (own === null) {
|
|
305
|
+
selected = null;
|
|
306
|
+
}
|
|
307
|
+
else if (selected !== null && moves.some((move) => move.from === selected && move.to === own)) {
|
|
308
|
+
makeMove({ from: selected, to: own });
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
else if (moves.some((move) => move.from === own)) {
|
|
312
|
+
selected = selected === own ? null : own;
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
selected = null;
|
|
316
|
+
}
|
|
317
|
+
render();
|
|
318
|
+
};
|
|
319
|
+
let drag = null;
|
|
320
|
+
const underPointer = (x, y) => {
|
|
321
|
+
const root = host.getRootNode();
|
|
322
|
+
return root.elementFromPoint?.(x, y) ?? null;
|
|
323
|
+
};
|
|
324
|
+
const sampleColour = (side) => {
|
|
325
|
+
const svg = boardBox.querySelector("svg");
|
|
326
|
+
const face = svg?.querySelector(`.sg-checker[data-side="${side}"] .sg-face`);
|
|
327
|
+
const style = face === null || face === undefined ? null : view.getComputedStyle(face);
|
|
328
|
+
return { fill: style?.fill ?? "#fff", edge: style?.stroke ?? "#000" };
|
|
329
|
+
};
|
|
330
|
+
const onDown = (event) => {
|
|
331
|
+
const game = input.game();
|
|
332
|
+
if (event.button !== 0 && event.pointerType === "mouse")
|
|
333
|
+
return;
|
|
334
|
+
if (game.phase !== "playing" || game.turn === null || !human(game.turn))
|
|
335
|
+
return;
|
|
336
|
+
const own = whereIs(event.target);
|
|
337
|
+
if (own === null)
|
|
338
|
+
return;
|
|
339
|
+
const moves = legalNow();
|
|
340
|
+
if (dragAllowed && moves.some((move) => move.from === own)) {
|
|
341
|
+
drag = { pointer: event.pointerId, from: own, x: event.clientX, y: event.clientY, active: false, ghost: null };
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
const onMove = (event) => {
|
|
345
|
+
const game = input.game();
|
|
346
|
+
if (drag === null || event.pointerId !== drag.pointer)
|
|
347
|
+
return;
|
|
348
|
+
if (!drag.active) {
|
|
349
|
+
if (Math.hypot(event.clientX - drag.x, event.clientY - drag.y) < 8)
|
|
350
|
+
return;
|
|
351
|
+
drag.active = true;
|
|
352
|
+
selected = drag.from;
|
|
353
|
+
render();
|
|
354
|
+
const svg = boardBox.querySelector("svg");
|
|
355
|
+
const rect = svg?.getBoundingClientRect();
|
|
356
|
+
const viewWidth = svg?.viewBox.baseVal.width ?? 1;
|
|
357
|
+
const scale = rect === undefined ? 1 : rect.width / viewWidth;
|
|
358
|
+
const side = game.turn;
|
|
359
|
+
const colours = sampleColour(side);
|
|
360
|
+
const ghost = create(document, "div", "sgp-ghost");
|
|
361
|
+
const diameter = CHECKER * scale;
|
|
362
|
+
ghost.style.width = `${diameter}px`;
|
|
363
|
+
ghost.style.height = `${diameter}px`;
|
|
364
|
+
ghost.style.background = colours.fill;
|
|
365
|
+
ghost.style.borderColor = colours.edge;
|
|
366
|
+
host.append(ghost);
|
|
367
|
+
drag.ghost = ghost;
|
|
368
|
+
try {
|
|
369
|
+
boardBox.setPointerCapture(event.pointerId);
|
|
370
|
+
}
|
|
371
|
+
catch {
|
|
372
|
+
/* Capture is a nicety. */
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (drag.ghost !== null) {
|
|
376
|
+
const w = drag.ghost.offsetWidth;
|
|
377
|
+
drag.ghost.style.transform = `translate(${event.clientX - w / 2}px, ${event.clientY - w / 2}px)`;
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
const onUp = (event) => {
|
|
381
|
+
if (drag === null || event.pointerId !== drag.pointer) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const finished = drag;
|
|
385
|
+
drag = null;
|
|
386
|
+
finished.ghost?.remove();
|
|
387
|
+
try {
|
|
388
|
+
boardBox.releasePointerCapture(event.pointerId);
|
|
389
|
+
}
|
|
390
|
+
catch {
|
|
391
|
+
/* Already released. */
|
|
392
|
+
}
|
|
393
|
+
if (!finished.active)
|
|
394
|
+
return;
|
|
395
|
+
const to = whereIs(underPointer(event.clientX, event.clientY));
|
|
396
|
+
if (to !== null && makeMove({ from: finished.from, to }))
|
|
397
|
+
return;
|
|
398
|
+
selected = null;
|
|
399
|
+
render();
|
|
400
|
+
};
|
|
401
|
+
const onCancel = (event) => {
|
|
402
|
+
if (drag === null || event.pointerId !== drag.pointer)
|
|
403
|
+
return;
|
|
404
|
+
drag.ghost?.remove();
|
|
405
|
+
const wasActive = drag.active;
|
|
406
|
+
drag = null;
|
|
407
|
+
if (wasActive) {
|
|
408
|
+
selected = null;
|
|
409
|
+
render();
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
/** A tap or a click: the drag handlers deal with a drag, and a click that follows one is ignored. */
|
|
413
|
+
let lastDragEnd = 0;
|
|
414
|
+
const onClick = (event) => {
|
|
415
|
+
const game = input.game();
|
|
416
|
+
if (Date.now() - lastDragEnd < 60)
|
|
417
|
+
return;
|
|
418
|
+
if (game.phase !== "playing" || game.turn === null || !human(game.turn))
|
|
419
|
+
return;
|
|
420
|
+
press(whereIs(event.target));
|
|
421
|
+
};
|
|
422
|
+
boardBox.addEventListener("pointerdown", onDown);
|
|
423
|
+
boardBox.addEventListener("pointermove", onMove);
|
|
424
|
+
boardBox.addEventListener("pointerup", (event) => {
|
|
425
|
+
const wasDrag = drag?.active === true;
|
|
426
|
+
onUp(event);
|
|
427
|
+
if (wasDrag)
|
|
428
|
+
lastDragEnd = Date.now();
|
|
429
|
+
});
|
|
430
|
+
boardBox.addEventListener("pointercancel", onCancel);
|
|
431
|
+
boardBox.addEventListener("click", onClick);
|
|
432
|
+
// Following the page: its width, and its language.
|
|
433
|
+
const resize = typeof view.ResizeObserver === "undefined"
|
|
434
|
+
? null
|
|
435
|
+
: new view.ResizeObserver(() => {
|
|
436
|
+
const look = input.look();
|
|
437
|
+
const wide = look.orientation === "portrait" || (look.orientation !== "landscape" && (host.clientWidth || 600) < 560);
|
|
438
|
+
if (wide !== portrait)
|
|
439
|
+
render();
|
|
440
|
+
});
|
|
441
|
+
resize?.observe(host);
|
|
442
|
+
const watch = typeof view.MutationObserver === "undefined"
|
|
443
|
+
? null
|
|
444
|
+
: new view.MutationObserver(() => {
|
|
445
|
+
if (input.followsPageLanguage())
|
|
446
|
+
render();
|
|
447
|
+
});
|
|
448
|
+
watch?.observe(document.documentElement, { attributes: true, attributeFilter: ["lang"] });
|
|
449
|
+
const resetPointer = () => {
|
|
450
|
+
selected = null;
|
|
451
|
+
message = null;
|
|
452
|
+
};
|
|
453
|
+
const disconnect = () => {
|
|
454
|
+
resize?.disconnect();
|
|
455
|
+
watch?.disconnect();
|
|
456
|
+
drag?.ghost?.remove();
|
|
457
|
+
};
|
|
458
|
+
return { render, resetPointer, disconnect };
|
|
459
|
+
}
|
package/dist/moves.d.ts
CHANGED
|
@@ -6,6 +6,19 @@ import type { VariantSpec } from "./variants.ts";
|
|
|
6
6
|
* point `from` (or the `BAR`, 25, for a checker entering from the bar or from
|
|
7
7
|
* off the board at the start of a race) to its own point `to` (0 for borne
|
|
8
8
|
* off). A move that lands on a lone checker of the other side hits it.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { legalPlaysOf, newGame, rollOpening, settingsFor } from "@johnmorrisdotca/sugoroku";
|
|
13
|
+
* import type { Move } from "@johnmorrisdotca/sugoroku";
|
|
14
|
+
*
|
|
15
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
16
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
17
|
+
* const play = legalPlaysOf(game)[0];
|
|
18
|
+
* const move = play.moves[0];
|
|
19
|
+
* const value: Move = move;
|
|
20
|
+
* console.log(value);
|
|
21
|
+
* ```
|
|
9
22
|
*/
|
|
10
23
|
export type Move = {
|
|
11
24
|
readonly from: number;
|
|
@@ -22,7 +35,41 @@ export type Move = {
|
|
|
22
35
|
* board (in a race) whenever it likes; and a side whose checkers are all
|
|
23
36
|
* home may bear off, by the exact die, or by a larger one from the highest
|
|
24
37
|
* point it has a checker on.
|
|
38
|
+
*
|
|
39
|
+
* @param spec - The native variant specification.
|
|
40
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
41
|
+
* @param side - The side being examined: white or black.
|
|
42
|
+
* @param die - One valid die value, from 1 to 6.
|
|
43
|
+
* @returns Every move of one die that `side` may make from `position`, one for each point a checker can start from. A side with a checker on the bar must enter it first; a side that has not borne off may start a checker off the board (in a race) whenever it likes; and a side whose checkers are all home may bear off, by the exact die, or by a larger one from the highest point it has a checker on.
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { movesOfDie, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
47
|
+
*
|
|
48
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
49
|
+
* const spec = settings.variant;
|
|
50
|
+
* const position = startPosition(spec);
|
|
51
|
+
* console.log(movesOfDie(spec,position,"white",3));
|
|
52
|
+
* ```
|
|
25
53
|
*/
|
|
26
54
|
export declare function movesOfDie(spec: VariantSpec, position: Position, side: Side, die: number): Move[];
|
|
27
|
-
/** The position after a move. Does not check that the move is legal.
|
|
55
|
+
/** The position after a move. Does not check that the move is legal.
|
|
56
|
+
*
|
|
57
|
+
* @param spec - The native variant specification.
|
|
58
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
59
|
+
* @param side - The side being examined: white or black.
|
|
60
|
+
* @param move - A native legal move, including die and hit status.
|
|
61
|
+
* @returns The position after a move. Does not check that the move is legal.
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { applyMove, legalPlaysOf, newGame, rollOpening, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
65
|
+
*
|
|
66
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
67
|
+
* const spec = settings.variant;
|
|
68
|
+
* const position = startPosition(spec);
|
|
69
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
70
|
+
* const play = legalPlaysOf(game)[0];
|
|
71
|
+
* const move = play.moves[0];
|
|
72
|
+
* console.log(applyMove(spec,position,"white",move).bar);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
28
75
|
export declare function applyMove(spec: VariantSpec, position: Position, side: Side, move: Move): Position;
|
package/dist/moves.js
CHANGED
|
@@ -23,6 +23,21 @@ function rearPoint(position, side) {
|
|
|
23
23
|
* board (in a race) whenever it likes; and a side whose checkers are all
|
|
24
24
|
* home may bear off, by the exact die, or by a larger one from the highest
|
|
25
25
|
* point it has a checker on.
|
|
26
|
+
*
|
|
27
|
+
* @param spec - The native variant specification.
|
|
28
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
29
|
+
* @param side - The side being examined: white or black.
|
|
30
|
+
* @param die - One valid die value, from 1 to 6.
|
|
31
|
+
* @returns Every move of one die that `side` may make from `position`, one for each point a checker can start from. A side with a checker on the bar must enter it first; a side that has not borne off may start a checker off the board (in a race) whenever it likes; and a side whose checkers are all home may bear off, by the exact die, or by a larger one from the highest point it has a checker on.
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { movesOfDie, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
35
|
+
*
|
|
36
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
37
|
+
* const spec = settings.variant;
|
|
38
|
+
* const position = startPosition(spec);
|
|
39
|
+
* console.log(movesOfDie(spec,position,"white",3));
|
|
40
|
+
* ```
|
|
26
41
|
*/
|
|
27
42
|
export function movesOfDie(spec, position, side, die) {
|
|
28
43
|
const i = sideIndex(side);
|
|
@@ -50,7 +65,26 @@ export function movesOfDie(spec, position, side, die) {
|
|
|
50
65
|
}
|
|
51
66
|
return moves;
|
|
52
67
|
}
|
|
53
|
-
/** The position after a move. Does not check that the move is legal.
|
|
68
|
+
/** The position after a move. Does not check that the move is legal.
|
|
69
|
+
*
|
|
70
|
+
* @param spec - The native variant specification.
|
|
71
|
+
* @param position - The native checker counts, bar, reserve and borne-off counts.
|
|
72
|
+
* @param side - The side being examined: white or black.
|
|
73
|
+
* @param move - A native legal move, including die and hit status.
|
|
74
|
+
* @returns The position after a move. Does not check that the move is legal.
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { applyMove, legalPlaysOf, newGame, rollOpening, settingsFor, startPosition } from "@johnmorrisdotca/sugoroku";
|
|
78
|
+
*
|
|
79
|
+
* const settings = settingsFor("backgammon",{points:5,cube:true});
|
|
80
|
+
* const spec = settings.variant;
|
|
81
|
+
* const position = startPosition(spec);
|
|
82
|
+
* const game = rollOpening(newGame(settings),[3,1]);
|
|
83
|
+
* const play = legalPlaysOf(game)[0];
|
|
84
|
+
* const move = play.moves[0];
|
|
85
|
+
* console.log(applyMove(spec,position,"white",move).bar);
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
54
88
|
export function applyMove(spec, position, side, move) {
|
|
55
89
|
const i = sideIndex(side);
|
|
56
90
|
const j = sideIndex(otherSide(side));
|