@altnovatrail/widgets 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +1 -1
  2. package/cores/vector.schema.json +82 -3
  3. package/kinds/cells/cells.handles.json +148 -3
  4. package/kinds/cells/cells.schema.json +716 -8
  5. package/kinds/cells/vectors/bishop-grid-56-d1-1.json +105 -4
  6. package/kinds/cells/vectors/counter-cases.json +123 -0
  7. package/kinds/cells/vectors/counter-div-56-d1-3.json +57 -0
  8. package/kinds/cells/vectors/counter-div-56-d2-1.json +773 -0
  9. package/kinds/cells/vectors/counter-div-56-d2-3.json +65 -0
  10. package/kinds/cells/vectors/counter-div-56-d4-1.json +57 -0
  11. package/kinds/cells/vectors/counter-div-56-d4-2.json +106 -0
  12. package/kinds/cells/vectors/cut-cases.json +163 -0
  13. package/kinds/cells/vectors/cut-gaps-56-d2-1.json +142 -0
  14. package/kinds/cells/vectors/cut-grid-56-d2-4.json +45 -0
  15. package/kinds/cells/vectors/cut-grid-56-d4-1.json +154 -0
  16. package/kinds/cells/vectors/cut-grid-56-d4-2.json +136 -0
  17. package/kinds/cells/vectors/cut-invalid.json +48 -0
  18. package/kinds/cells/vectors/domino-grid-56-d3-2.json +238 -0
  19. package/kinds/cells/vectors/domino-limit.json +60 -0
  20. package/kinds/cells/vectors/domino-par-56-d3-2.json +194 -0
  21. package/kinds/cells/vectors/domino-par-56-d4-3.json +127 -0
  22. package/kinds/cells/vectors/figure-apart.json +47 -0
  23. package/kinds/cells/vectors/fold-12x12.json +79 -0
  24. package/kinds/cells/vectors/fold-14x14.json +79 -0
  25. package/kinds/cells/vectors/fold-26x26.json +79 -0
  26. package/kinds/cells/vectors/garden-enum-56-d3-4.json +96 -6
  27. package/kinds/cells/vectors/goat-grid-56-d4-4.json +325 -5
  28. package/kinds/cells/vectors/knight-after-no-parity.json +31 -0
  29. package/kinds/cells/vectors/knight-par-56-d2-2.json +61 -6
  30. package/kinds/cells/vectors/mosaic-grid-56-d2-2.json +84 -0
  31. package/kinds/cells/vectors/nodes-gaps-56-d2-2.json +44 -0
  32. package/kinds/cells/vectors/nodes-gaps-56-d4-2.json +70 -0
  33. package/kinds/cells/vectors/number-div-56-d2-2.json +49 -0
  34. package/kinds/cells/vectors/numbers-div-56-d3-1.json +24 -0
  35. package/kinds/cells/vectors/numbers-invalid.json +56 -0
  36. package/kinds/cells/vectors/paper-26x26.json +743 -0
  37. package/kinds/cells/vectors/paper-cube-12-d2-1.json +43 -19
  38. package/kinds/cells/vectors/paper-cube-12-d5-1.json +47 -4
  39. package/kinds/cells/vectors/paper-cube-56-d4-1.json +56 -5
  40. package/kinds/cells/vectors/paper-drawn-lines.json +184 -0
  41. package/kinds/cells/vectors/paper-grid-56-d1-2.json +46 -5
  42. package/kinds/cells/vectors/paper-turn-invalid.json +4 -4
  43. package/kinds/cells/vectors/path-6x6.json +119 -0
  44. package/kinds/cells/vectors/path-beside.json +108 -0
  45. package/kinds/cells/vectors/path-enum-12-d3-5.json +187 -0
  46. package/kinds/cells/vectors/path-enum-34-d4-3.json +412 -0
  47. package/kinds/cells/vectors/path-enum-56-d3-3.json +411 -0
  48. package/kinds/cells/vectors/path-enum-56-d3-5.json +351 -0
  49. package/kinds/cells/vectors/path-invalid.json +37 -0
  50. package/kinds/cells/vectors/path-limit.json +58 -0
  51. package/kinds/cells/vectors/path-maze.json +297 -0
  52. package/kinds/cells/vectors/path-unreachable.json +76 -0
  53. package/kinds/cells/vectors/perimeter-grid-56-d2-1.json +86 -0
  54. package/kinds/cells/vectors/perimeter-grid-56-d3-1.json +102 -0
  55. package/kinds/cells/vectors/perimeter-grid-56-d3-3.json +112 -0
  56. package/kinds/cells/vectors/perimeter-grid-56-d4-3.json +289 -0
  57. package/kinds/cells/vectors/polyomino-cases.json +191 -0
  58. package/kinds/cells/vectors/polyomino-grid-56-d3-4.json +205 -0
  59. package/kinds/cells/vectors/polyomino-grid-56-d4-1.json +137 -0
  60. package/kinds/cells/vectors/polyomino-grid-56-d5-3.json +157 -0
  61. package/kinds/cells/vectors/polyomino-invalid.json +45 -0
  62. package/kinds/cells/vectors/polyomino-one-pieces.json +28 -0
  63. package/kinds/cells/vectors/route-par-56-d3-3.json +245 -0
  64. package/kinds/cells/vectors/sides-gaps-56-d3-2.json +44 -0
  65. package/kinds/cells/vectors/sieve-div-56-d3-2.json +140 -0
  66. package/kinds/cells/vectors/tear-gaps-56-d3-1.json +84 -0
  67. package/kinds/cells/vectors/tear-limit.json +56 -0
  68. package/lib/cores/enumerate.d.ts +33 -0
  69. package/lib/cores/enumerate.js +77 -0
  70. package/lib/cores/kind.d.ts +70 -5
  71. package/lib/cores/kind.js +2 -2
  72. package/lib/cores/search.d.ts +47 -10
  73. package/lib/cores/search.js +129 -37
  74. package/lib/kinds/cells/bishop.d.ts +1 -1
  75. package/lib/kinds/cells/bishop.js +1 -1
  76. package/lib/kinds/cells/counter.d.ts +26 -0
  77. package/lib/kinds/cells/counter.js +249 -0
  78. package/lib/kinds/cells/cut.d.ts +46 -0
  79. package/lib/kinds/cells/cut.js +400 -0
  80. package/lib/kinds/cells/figure.d.ts +13 -0
  81. package/lib/kinds/cells/figure.js +114 -0
  82. package/lib/kinds/cells/holes.d.ts +6 -0
  83. package/lib/kinds/cells/holes.js +57 -0
  84. package/lib/kinds/cells/index.d.ts +8 -0
  85. package/lib/kinds/cells/index.js +10 -2
  86. package/lib/kinds/cells/kind.d.ts +3 -2
  87. package/lib/kinds/cells/kind.js +23 -8
  88. package/lib/kinds/cells/knight.d.ts +1 -1
  89. package/lib/kinds/cells/number.d.ts +6 -0
  90. package/lib/kinds/cells/number.js +32 -0
  91. package/lib/kinds/cells/operation.d.ts +27 -7
  92. package/lib/kinds/cells/paper.d.ts +10 -2
  93. package/lib/kinds/cells/paper.js +109 -13
  94. package/lib/kinds/cells/path.d.ts +38 -0
  95. package/lib/kinds/cells/path.js +288 -0
  96. package/lib/kinds/cells/piece.d.ts +27 -7
  97. package/lib/kinds/cells/piece.js +202 -37
  98. package/lib/kinds/cells/polyomino.d.ts +45 -0
  99. package/lib/kinds/cells/polyomino.js +461 -0
  100. package/lib/kinds/cells/registry.js +35 -4
  101. package/lib/kinds/cells/schema.js +45 -4
  102. package/lib/kinds/cells/sheet.d.ts +20 -1
  103. package/lib/kinds/cells/sheet.js +84 -6
  104. package/lib/kinds/cells/side.d.ts +33 -0
  105. package/lib/kinds/cells/side.js +86 -0
  106. package/lib/kinds/cells/sieve.d.ts +26 -0
  107. package/lib/kinds/cells/sieve.js +208 -0
  108. package/lib/kinds/cells/step.d.ts +1 -1
  109. package/lib/kinds/cells/step.js +1 -1
  110. package/package.json +5 -1
@@ -0,0 +1,288 @@
1
+ // A path drawn on the sheet from cell to cell across their sides, as a robot walks a board: from
2
+ // the cell the scene names, in the directions it allows, never into a hole, through a wall or onto
3
+ // a cell it has visited, but back onto its first cell, which closes it into a route. The state
4
+ // keeps the whole path, so a goal reads which cells it passes, and a count of the states that meet
5
+ // a goal is a count of the paths.
6
+ import { array, object, pointer, string } from "../../cores/json.js";
7
+ import { cellAt, cellName, readCell, wallKey } from "./sheet.js";
8
+ export const directions = ["up", "left", "right", "down"];
9
+ /**
10
+ * The fewest cells a path has drawn before a step back onto its first closes it: a step back onto
11
+ * the cell just left closes nothing.
12
+ */
13
+ const shortest = 3;
14
+ /** The cell a step in the direction leads to, or undefined off the sheet. */
15
+ function stepTo(sheet, from, direction) {
16
+ const [row, col] = [Math.floor(from / sheet.cols), from % sheet.cols];
17
+ switch (direction) {
18
+ case "up":
19
+ return row > 0 ? from - sheet.cols : undefined;
20
+ case "left":
21
+ return col > 0 ? from - 1 : undefined;
22
+ case "right":
23
+ return col < sheet.cols - 1 ? from + 1 : undefined;
24
+ case "down":
25
+ return row < sheet.rows - 1 ? from + sheet.cols : undefined;
26
+ }
27
+ }
28
+ /**
29
+ * The cells of the state's path by index, or undefined where it is no path the rules draw: one
30
+ * that starts elsewhere than the scene's cell, or takes a step they refuse. A state reported from
31
+ * outside the kind may hold one, and then no step is drawn from it and no goal is met.
32
+ */
33
+ export function pathOf(sheet, options, state) {
34
+ const value = state.path;
35
+ if (!Array.isArray(value) || value[0] !== cellName(sheet, options.from)) {
36
+ return undefined;
37
+ }
38
+ const cells = [options.from];
39
+ for (const name of value.slice(1)) {
40
+ const cell = typeof name === "string" ? cellAt(sheet, name) : undefined;
41
+ if (cell === undefined ||
42
+ isClosed(cells) ||
43
+ refusal(sheet, options, cells, cell) !== undefined) {
44
+ return undefined;
45
+ }
46
+ cells.push(cell);
47
+ }
48
+ return cells;
49
+ }
50
+ /** Whether a path is closed: back on its first cell. */
51
+ export function isClosed(path) {
52
+ return path.length > shortest && path[0] === path[path.length - 1];
53
+ }
54
+ /**
55
+ * Why the path cannot step onto the cell, as the rules' short code, or undefined where it can. The
56
+ * path is open, and the cell one of the sheet's.
57
+ */
58
+ function refusal(sheet, options, path, to) {
59
+ const last = path[path.length - 1];
60
+ const direction = directions.find((d) => stepTo(sheet, last, d) === to);
61
+ if (direction === undefined || !options.directions.has(direction)) {
62
+ return "not-a-step";
63
+ }
64
+ if (sheet.holes.has(to)) {
65
+ return "hole";
66
+ }
67
+ if (sheet.walls.has(wallKey(last, to))) {
68
+ return "wall";
69
+ }
70
+ if (path.includes(to) && !(to === path[0] && path.length >= shortest)) {
71
+ return "visited";
72
+ }
73
+ return undefined;
74
+ }
75
+ function refuse(reason) {
76
+ return { ok: false, reason };
77
+ }
78
+ export const path = {
79
+ name: "path",
80
+ schema: {
81
+ options: {
82
+ description: "A path drawn from cell to cell across their sides, from the cell it starts on: never into a hole, through a wall or onto a cell it has visited, but back onto its first cell, which closes it into a route that takes no more steps.",
83
+ type: "object",
84
+ properties: {
85
+ from: { description: "The cell the path starts on, not a hole.", $ref: "#/$defs/cell" },
86
+ directions: {
87
+ description: "The directions a step may go in, as the sheet lies: up, left, right, down; every one where left out.",
88
+ type: "array",
89
+ items: { description: "A direction a step may go in.", enum: [...directions] },
90
+ minItems: 1,
91
+ uniqueItems: true,
92
+ },
93
+ },
94
+ required: ["from"],
95
+ unevaluatedProperties: false,
96
+ },
97
+ move: {
98
+ description: "A step of the path onto a cell beside its last one.",
99
+ type: "object",
100
+ properties: { to: { description: "The cell the path steps onto.", $ref: "#/$defs/cell" } },
101
+ required: ["to"],
102
+ unevaluatedProperties: false,
103
+ },
104
+ state: {
105
+ member: "path",
106
+ schema: {
107
+ description: "The cells the path visits, in order from its first; a route is closed where its last cell is its first.",
108
+ type: "array",
109
+ items: { description: "A cell the path visits.", $ref: "#/$defs/cell" },
110
+ minItems: 1,
111
+ },
112
+ },
113
+ },
114
+ read(value, sheet, path, errors) {
115
+ const before = errors.length;
116
+ const o = object(value, path, errors, ["from"], ["directions"]);
117
+ if (o === undefined) {
118
+ return undefined;
119
+ }
120
+ const at = pointer(path, "from");
121
+ const from = readCell(sheet, o.from, at, errors);
122
+ if (from !== undefined && sheet.holes.has(from)) {
123
+ errors.push({ path: at, message: "a hole: no path starts on it" });
124
+ }
125
+ const allowed = new Set();
126
+ const listed = pointer(path, "directions");
127
+ (o.directions === undefined ? directions : array(o.directions, listed, errors, 1))?.forEach((item, i) => {
128
+ const direction = string(item, pointer(listed, i), errors, directions, "a direction");
129
+ if (direction === undefined)
130
+ return;
131
+ if (allowed.has(direction)) {
132
+ errors.push({ path: pointer(listed, i), message: "a direction named twice" });
133
+ }
134
+ allowed.add(direction);
135
+ });
136
+ return errors.length > before || from === undefined ? undefined : { from, directions: allowed };
137
+ },
138
+ start: (sheet, options) => [cellName(sheet, options.from)],
139
+ play(sheet, options, state, move) {
140
+ const errors = [];
141
+ const o = object(move, "", errors, ["to"]);
142
+ const to = o === undefined ? undefined : readCell(sheet, o.to, "/to", errors);
143
+ if (to === undefined || errors.length > 0) {
144
+ return refuse("no-cell");
145
+ }
146
+ const drawn = pathOf(sheet, options, state);
147
+ if (drawn === undefined) {
148
+ return refuse("no-path");
149
+ }
150
+ if (isClosed(drawn)) {
151
+ return refuse("closed");
152
+ }
153
+ const reason = refusal(sheet, options, drawn, to);
154
+ return reason === undefined
155
+ ? { ok: true, state: { ...state, path: [...state.path, cellName(sheet, to)] } }
156
+ : refuse(reason);
157
+ },
158
+ moves(sheet, options, state) {
159
+ const drawn = pathOf(sheet, options, state);
160
+ if (drawn === undefined || isClosed(drawn)) {
161
+ return [];
162
+ }
163
+ const last = drawn[drawn.length - 1];
164
+ const moves = [];
165
+ // In reading order: up, left, right, down.
166
+ for (const direction of directions) {
167
+ const to = stepTo(sheet, last, direction);
168
+ if (to !== undefined && refusal(sheet, options, drawn, to) === undefined) {
169
+ moves.push({ to: cellName(sheet, to) });
170
+ }
171
+ }
172
+ return moves;
173
+ },
174
+ };
175
+ const endSchema = (description) => ({
176
+ description,
177
+ type: "object",
178
+ properties: {
179
+ cell: { description: "The cell the path ends on, not a hole.", $ref: "#/$defs/cell" },
180
+ through: {
181
+ description: "The cells the path passes on its way, none of them a hole, none named twice.",
182
+ type: "array",
183
+ items: { description: "A cell the path passes.", $ref: "#/$defs/cell" },
184
+ minItems: 1,
185
+ uniqueItems: true,
186
+ },
187
+ },
188
+ required: ["cell"],
189
+ unevaluatedProperties: false,
190
+ });
191
+ /** Adds an error where the scene allows no path; gives the path's start where it reads. */
192
+ function needsPath(operations, path, errors) {
193
+ if (!operations.has("path")) {
194
+ errors.push({ path, message: "the scene allows no operation path, so no path is drawn" });
195
+ }
196
+ return operations.get("path");
197
+ }
198
+ function readEnd(value, sheet, path, errors, operations) {
199
+ const before = errors.length;
200
+ const o = object(value, path, errors, ["cell"], ["through"]);
201
+ const at = pointer(path, "cell");
202
+ const cell = o === undefined ? undefined : readCell(sheet, o.cell, at, errors);
203
+ if (cell !== undefined && sheet.holes.has(cell)) {
204
+ errors.push({ path: at, message: "a hole: no path ends on it" });
205
+ }
206
+ const through = [];
207
+ const listed = pointer(path, "through");
208
+ (o?.through === undefined ? [] : array(o.through, listed, errors, 1))?.forEach((item, i) => {
209
+ const passed = readCell(sheet, item, pointer(listed, i), errors);
210
+ if (passed !== undefined && through.includes(passed)) {
211
+ errors.push({ path: pointer(listed, i), message: "a cell named twice" });
212
+ }
213
+ else if (passed !== undefined && sheet.holes.has(passed)) {
214
+ errors.push({ path: pointer(listed, i), message: "a hole: no path passes it" });
215
+ }
216
+ if (passed !== undefined)
217
+ through.push(passed);
218
+ });
219
+ const drawn = needsPath(operations, path, errors);
220
+ if (drawn !== undefined && cell === drawn.from) {
221
+ errors.push({ path: at, message: "the path starts on it: a path that ends there is no way" });
222
+ }
223
+ if (errors.length > before || cell === undefined) {
224
+ return undefined;
225
+ }
226
+ return drawn === undefined ? { cell, through } : { path: drawn, cell, through };
227
+ }
228
+ /** The state's path as a goal reads it, by the path's parameters where they read. */
229
+ function drawnOf(sheet, options, state) {
230
+ return options === undefined ? undefined : pathOf(sheet, options, state);
231
+ }
232
+ function ends(sheet, goal, state) {
233
+ const drawn = drawnOf(sheet, goal.path, state);
234
+ return (drawn !== undefined &&
235
+ drawn[drawn.length - 1] === goal.cell &&
236
+ goal.through.every((cell) => drawn.includes(cell)));
237
+ }
238
+ /** The path ends on the cell, past the cells named; the key is the fewest steps. */
239
+ export const pathTo = {
240
+ name: "path_to",
241
+ schema: endSchema("The path ends on the cell, never the one it starts on, and passes every cell of through on its way; the key is the fewest steps. The scene allows the operation path."),
242
+ read: readEnd,
243
+ meets: ends,
244
+ };
245
+ /** As path_to, and the key counts the paths: the scene allows the path alone. */
246
+ export const pathCount = {
247
+ name: "path_count",
248
+ schema: endSchema("The path ends on the cell, never the one it starts on, and passes every cell of through on its way; the key counts such paths. The scene allows the operation path alone, so that its states are the paths."),
249
+ read(value, sheet, path, errors, operations) {
250
+ const before = errors.length;
251
+ const end = readEnd(value, sheet, path, errors, operations);
252
+ const others = [...operations]
253
+ .filter(([named, options]) => named !== "path" && options !== undefined)
254
+ .map(([named]) => named);
255
+ if (operations.get("path") !== undefined && others.length > 0) {
256
+ errors.push({
257
+ path,
258
+ message: `the scene allows ${others.join(", ")} beside path: a count of paths counts the path's states alone`,
259
+ });
260
+ }
261
+ return errors.length > before ? undefined : end;
262
+ },
263
+ meets: ends,
264
+ asks: () => ({ key: "count" }),
265
+ };
266
+ /** The path is closed into a route; the key is the most cells a route visits. */
267
+ export const pathClosed = {
268
+ name: "path_closed",
269
+ schema: {
270
+ description: "The path is closed into a route, back on its first cell; the key is the most cells such a route visits. The scene allows the operation path.",
271
+ type: "object",
272
+ unevaluatedProperties: false,
273
+ },
274
+ read(value, _sheet, path, errors, operations) {
275
+ const before = errors.length;
276
+ object(value, path, errors, []);
277
+ const drawn = needsPath(operations, path, errors);
278
+ return errors.length > before ? undefined : drawn === undefined ? {} : { path: drawn };
279
+ },
280
+ meets(sheet, goal, state) {
281
+ const drawn = drawnOf(sheet, goal.path, state);
282
+ return drawn !== undefined && isClosed(drawn);
283
+ },
284
+ asks: (sheet, goal) => ({
285
+ key: "max",
286
+ measure: (state) => (drawnOf(sheet, goal.path, state)?.length ?? 1) - 1,
287
+ }),
288
+ };
@@ -1,7 +1,7 @@
1
1
  import type { Goal, Operation } from "./operation.js";
2
2
  import { type Sheet } from "./sheet.js";
3
3
  export interface Piece {
4
- /** The operation's name, and the prefix of its member of the state and of its goal. */
4
+ /** The operation's name, and the prefix of its members of the state and of its goals. */
5
5
  readonly name: string;
6
6
  /** What the piece is and how it moves, as the schema of its parameters says it. */
7
7
  readonly description: string;
@@ -12,16 +12,36 @@ export interface Piece {
12
12
  */
13
13
  refusal(sheet: Sheet, from: number, to: number): string | undefined;
14
14
  }
15
- /** A kind of piece as the registry takes it: its operation and its goal of standing on a cell. */
15
+ /** Whether a piece has made an even or an odd count of moves. */
16
+ export type Parity = "even" | "odd";
17
+ /** A piece's parameters: the cell it starts on, and whether the state keeps its moves' parity. */
18
+ export interface Start {
19
+ readonly cell: number;
20
+ readonly parity: boolean;
21
+ }
22
+ /** A goal of standing on a cell, by its index, after a count of moves of a parity where it names one. */
23
+ export interface On {
24
+ readonly cell: number;
25
+ readonly after?: Parity;
26
+ }
27
+ /**
28
+ * A kind of piece as the registry takes it: its operation, its goal of standing on a cell, and its
29
+ * goal of the cells it reaches, which a count asks.
30
+ */
16
31
  export interface PieceParts {
17
- /** Its parameters are the cell the piece starts on, by its index. */
18
- readonly operation: Operation<number>;
19
- /** Its parameters are the cell the piece is to stand on, by its index. */
20
- readonly goal: Goal<number>;
32
+ readonly operation: Operation<Start>;
33
+ readonly goal: Goal<On>;
34
+ /** Its parameters are read into the cells it reaches within the moves they give. */
35
+ readonly reach: Goal<Reach>;
36
+ }
37
+ /** The cells a piece reaches from its start within the moves a goal gives, by their indices. */
38
+ export interface Reach {
39
+ readonly cells: ReadonlySet<number>;
21
40
  }
22
41
  /**
23
42
  * The operation and the goal of a kind of piece. A move names the cell the piece goes to, and is
24
43
  * refused where that is no cell of the sheet, "no-cell"; where the state puts the piece on none,
25
- * "no-piece"; a hole, "hole"; the piece's own cell, "not-its-move"; or by the piece's own rules.
44
+ * "no-piece"; where the scene asks for the parity of its moves and the state keeps none,
45
+ * "no-parity"; a hole, "hole"; the piece's own cell, "not-its-move"; or by the piece's own rules.
26
46
  */
27
47
  export declare function piece(kind: Piece): PieceParts;
@@ -3,24 +3,28 @@
3
3
  // with its own member of the state, "<name>_at", and its own goal, "<name>_on", so a new kind of
4
4
  // piece is a new operation and leaves the others as they were. Two pieces of a scene know nothing
5
5
  // of each other: they may stand on one cell, and neither stops the other.
6
- import { object, pointer } from "../../cores/json.js";
7
- import { cellAt, cellName, readCell } from "./sheet.js";
6
+ //
7
+ // Where the scene asks for it, the state keeps beside the cell whether the piece has made an even
8
+ // or an odd count of moves, "<name>_parity", so that a goal may ask for a cell after a count of one
9
+ // parity: on a graph whose states are the cells alone, a cell reached after an odd count is the
10
+ // same state as after an even one, and no key could say the one is never reached.
11
+ import { integer, object, pointer, string } from "../../cores/json.js";
12
+ import { cellAt, cellName, maxSide, readCell } from "./sheet.js";
13
+ const parities = ["even", "odd"];
14
+ /** The most moves a reach names: no piece needs more to reach a cell of the largest sheet. */
15
+ const mostMoves = maxSide * maxSide;
8
16
  function refuse(reason) {
9
17
  return { ok: false, reason };
10
18
  }
11
- /** The parameters {"cell": <cell>}: a cell of the sheet, no hole, or undefined with its errors. */
12
- function readPlace(value, sheet, path, errors) {
13
- const before = errors.length;
14
- const o = object(value, path, errors, ["cell"]);
15
- if (o === undefined) {
16
- return undefined;
17
- }
19
+ /** The member "cell" of parameters: a cell of the sheet, no hole, or undefined with its errors. */
20
+ function readPlace(o, sheet, path, errors) {
18
21
  const at = pointer(path, "cell");
19
22
  const cell = readCell(sheet, o.cell, at, errors);
20
23
  if (cell !== undefined && sheet.holes.has(cell)) {
21
24
  errors.push({ path: at, message: "a hole: no piece stands on it" });
25
+ return undefined;
22
26
  }
23
- return errors.length > before ? undefined : cell;
27
+ return cell;
24
28
  }
25
29
  /**
26
30
  * The cell a state puts the piece on, or undefined where its member is no cell of the sheet, or a
@@ -31,47 +35,74 @@ function where(sheet, state, member) {
31
35
  const cell = typeof value === "string" ? cellAt(sheet, value) : undefined;
32
36
  return cell === undefined || sheet.holes.has(cell) ? undefined : cell;
33
37
  }
34
- const place = (description) => ({
35
- description,
36
- type: "object",
37
- properties: { cell: { description: "The cell, not a hole.", $ref: "#/$defs/cell" } },
38
- required: ["cell"],
39
- unevaluatedProperties: false,
40
- });
38
+ /** The parity a state keeps, or undefined where its member is neither. */
39
+ function parityOf(state, member) {
40
+ const value = state[member];
41
+ return value === "even" || value === "odd" ? value : undefined;
42
+ }
43
+ const cellProperty = (description) => ({ description, $ref: "#/$defs/cell" });
41
44
  /**
42
45
  * The operation and the goal of a kind of piece. A move names the cell the piece goes to, and is
43
46
  * refused where that is no cell of the sheet, "no-cell"; where the state puts the piece on none,
44
- * "no-piece"; a hole, "hole"; the piece's own cell, "not-its-move"; or by the piece's own rules.
47
+ * "no-piece"; where the scene asks for the parity of its moves and the state keeps none,
48
+ * "no-parity"; a hole, "hole"; the piece's own cell, "not-its-move"; or by the piece's own rules.
45
49
  */
46
50
  export function piece(kind) {
47
51
  const member = `${kind.name}_at`;
52
+ const parityMember = `${kind.name}_parity`;
48
53
  const operation = {
49
54
  name: kind.name,
50
55
  schema: {
51
56
  options: {
52
- ...place(kind.description),
57
+ description: kind.description,
58
+ type: "object",
53
59
  properties: {
54
- cell: {
55
- description: "The cell the piece stands on at the scene's start, not a hole.",
56
- $ref: "#/$defs/cell",
60
+ cell: cellProperty("The cell the piece stands on at the scene's start, not a hole."),
61
+ parity: {
62
+ description: `Whether the state keeps, as ${parityMember}, whether the piece has made an even or an odd count of moves; false where left out.`,
63
+ type: "boolean",
57
64
  },
58
65
  },
66
+ required: ["cell"],
67
+ unevaluatedProperties: false,
59
68
  },
60
69
  move: {
61
70
  description: "A move of the piece to another cell.",
62
71
  type: "object",
63
- properties: { to: { description: "The cell the piece goes to.", $ref: "#/$defs/cell" } },
72
+ properties: { to: cellProperty("The cell the piece goes to.") },
64
73
  required: ["to"],
65
74
  unevaluatedProperties: false,
66
75
  },
67
76
  state: {
68
77
  member,
69
- schema: { description: `The cell the piece ${kind.name} stands on.`, $ref: "#/$defs/cell" },
78
+ schema: cellProperty(`The cell the piece ${kind.name} stands on.`),
79
+ beside: {
80
+ member: parityMember,
81
+ schema: {
82
+ description: `Whether the piece ${kind.name} has made an even or an odd count of moves; only where the scene's parameters of the piece ask for it.`,
83
+ type: "string",
84
+ enum: [...parities],
85
+ },
86
+ },
70
87
  },
71
88
  },
72
- read: readPlace,
73
- start: (sheet, cell) => cellName(sheet, cell),
74
- play(sheet, _start, state, move) {
89
+ read(value, sheet, path, errors) {
90
+ const before = errors.length;
91
+ const o = object(value, path, errors, ["cell"], ["parity"]);
92
+ if (o === undefined) {
93
+ return undefined;
94
+ }
95
+ const cell = readPlace(o, sheet, path, errors);
96
+ if (o.parity !== undefined && typeof o.parity !== "boolean") {
97
+ errors.push({ path: pointer(path, "parity"), message: "true or false" });
98
+ }
99
+ return errors.length > before || cell === undefined
100
+ ? undefined
101
+ : { cell, parity: o.parity === true };
102
+ },
103
+ start: (sheet, options) => cellName(sheet, options.cell),
104
+ startBeside: (_sheet, options) => options.parity ? "even" : undefined,
105
+ play(sheet, options, state, move) {
75
106
  const errors = [];
76
107
  const o = object(move, "", errors, ["to"]);
77
108
  const to = o === undefined ? undefined : readCell(sheet, o.to, "/to", errors);
@@ -83,18 +114,32 @@ export function piece(kind) {
83
114
  if (from === undefined) {
84
115
  return refuse("no-piece");
85
116
  }
117
+ const parity = parityOf(state, parityMember);
118
+ if (options.parity && parity === undefined) {
119
+ return refuse("no-parity");
120
+ }
86
121
  if (sheet.holes.has(to)) {
87
122
  return refuse("hole");
88
123
  }
89
124
  const reason = to === from ? "not-its-move" : kind.refusal(sheet, from, to);
90
- return reason === undefined
91
- ? { ok: true, state: { ...state, [member]: cellName(sheet, to) } }
92
- : refuse(reason);
125
+ if (reason !== undefined) {
126
+ return refuse(reason);
127
+ }
128
+ const moved = { ...state, [member]: cellName(sheet, to) };
129
+ return {
130
+ ok: true,
131
+ state: options.parity
132
+ ? { ...moved, [parityMember]: parity === "even" ? "odd" : "even" }
133
+ : moved,
134
+ };
93
135
  },
94
- moves(sheet, _start, state) {
136
+ moves(sheet, options, state) {
95
137
  const from = where(sheet, state, member);
96
138
  const moves = [];
97
- for (let to = 0; from !== undefined && to < sheet.rows * sheet.cols; to++) {
139
+ if (from === undefined || (options.parity && parityOf(state, parityMember) === undefined)) {
140
+ return moves;
141
+ }
142
+ for (let to = 0; to < sheet.rows * sheet.cols; to++) {
98
143
  if (to !== from && !sheet.holes.has(to) && kind.refusal(sheet, from, to) === undefined) {
99
144
  moves.push({ to: cellName(sheet, to) });
100
145
  }
@@ -104,9 +149,27 @@ export function piece(kind) {
104
149
  };
105
150
  const goal = {
106
151
  name: `${kind.name}_on`,
107
- schema: place(`The piece ${kind.name} stands on the cell; the scene allows the operation ${kind.name}, which keeps the piece.`),
152
+ schema: {
153
+ description: `The piece ${kind.name} stands on the cell, after a count of moves of the parity given where one is; the scene allows the operation ${kind.name}, which keeps the piece.`,
154
+ type: "object",
155
+ properties: {
156
+ cell: cellProperty("The cell, not a hole."),
157
+ after: {
158
+ description: `Whether the piece has made an even or an odd count of moves; the operation ${kind.name} of the scene then keeps their parity.`,
159
+ type: "string",
160
+ enum: [...parities],
161
+ },
162
+ },
163
+ required: ["cell"],
164
+ unevaluatedProperties: false,
165
+ },
108
166
  read(value, sheet, path, errors, operations) {
109
- const cell = readPlace(value, sheet, path, errors);
167
+ const before = errors.length;
168
+ const o = object(value, path, errors, ["cell"], ["after"]);
169
+ const cell = o === undefined ? undefined : readPlace(o, sheet, path, errors);
170
+ const after = o?.after === undefined
171
+ ? undefined
172
+ : string(o.after, pointer(path, "after"), errors, parities, "even or odd");
110
173
  if (!operations.has(kind.name)) {
111
174
  errors.push({
112
175
  path,
@@ -114,9 +177,111 @@ export function piece(kind) {
114
177
  });
115
178
  return undefined;
116
179
  }
117
- return cell;
180
+ // Where the piece's own parameters do not read, the scene is refused for them alone.
181
+ const start = operations.get(kind.name);
182
+ // An after that is no parity has its own error, which a parity of the piece would not mend.
183
+ if (after !== undefined && start !== undefined && !start.parity) {
184
+ errors.push({
185
+ path: pointer(path, "after"),
186
+ message: `the operation ${kind.name} keeps no parity of the piece's moves: give it "parity": true`,
187
+ });
188
+ }
189
+ if (errors.length > before || cell === undefined) {
190
+ return undefined;
191
+ }
192
+ return after === undefined ? { cell } : { cell, after };
193
+ },
194
+ meets: (sheet, on, state) => where(sheet, state, member) === on.cell &&
195
+ (on.after === undefined || parityOf(state, parityMember) === on.after),
196
+ };
197
+ // The fewest moves from the start to each cell, by the piece's own rules; -1 where it never goes.
198
+ const fewest = (sheet, start) => {
199
+ const moves = new Array(sheet.rows * sheet.cols).fill(-1);
200
+ moves[start] = 0;
201
+ for (let layer = [start], n = 1; layer.length > 0; n++) {
202
+ const next = [];
203
+ for (const from of layer) {
204
+ for (let to = 0; to < moves.length; to++) {
205
+ if (moves[to] === -1 &&
206
+ !sheet.holes.has(to) &&
207
+ kind.refusal(sheet, from, to) === undefined) {
208
+ moves[to] = n;
209
+ next.push(to);
210
+ }
211
+ }
212
+ }
213
+ layer = next;
214
+ }
215
+ return moves;
216
+ };
217
+ const moveCount = (description) => ({
218
+ description,
219
+ type: "integer",
220
+ minimum: 0,
221
+ maximum: mostMoves,
222
+ });
223
+ const reach = {
224
+ name: `${kind.name}_reach`,
225
+ schema: {
226
+ description: `The piece ${kind.name} stands on a cell whose fewest moves from the cell it starts on are at least least and at most most; the key counts those cells. The scene allows the operation ${kind.name} alone, keeping no parity of its moves, so that its states are the piece's cells.`,
227
+ type: "object",
228
+ properties: {
229
+ least: moveCount("The fewest moves to a cell counted, 0 by default: the cell it starts on."),
230
+ most: moveCount("The most moves to a cell counted; any number by default."),
231
+ },
232
+ unevaluatedProperties: false,
233
+ },
234
+ read(value, sheet, path, errors, operations) {
235
+ const before = errors.length;
236
+ const o = object(value, path, errors, [], ["least", "most"]);
237
+ const least = o?.least === undefined ? 0 : integer(o.least, pointer(path, "least"), errors, 0, mostMoves);
238
+ const most = o?.most === undefined
239
+ ? mostMoves
240
+ : integer(o.most, pointer(path, "most"), errors, 0, mostMoves);
241
+ if (least !== undefined && most !== undefined && least > most) {
242
+ errors.push({ path: pointer(path, "least"), message: "no more than the most moves" });
243
+ }
244
+ if (!operations.has(kind.name)) {
245
+ errors.push({
246
+ path,
247
+ message: `the scene allows no operation ${kind.name}, so no piece reaches any cell`,
248
+ });
249
+ }
250
+ const start = operations.get(kind.name);
251
+ // Where the piece's own parameters, or another operation's, do not read, the scene is refused
252
+ // for them alone.
253
+ const others = [...operations]
254
+ .filter(([named, options]) => named !== kind.name && options !== undefined)
255
+ .map(([named]) => named);
256
+ if (start !== undefined && others.length > 0) {
257
+ errors.push({
258
+ path,
259
+ message: `the scene allows ${others.join(", ")} beside ${kind.name}: a count of the cells reached counts the piece's states alone`,
260
+ });
261
+ }
262
+ // A cell reached after counts of both parities would be two states, and counted twice.
263
+ if (start?.parity === true) {
264
+ errors.push({
265
+ path,
266
+ message: `the operation ${kind.name} keeps the parity of its moves: a count of the cells reached counts the piece's cells alone`,
267
+ });
268
+ }
269
+ if (errors.length > before || least === undefined || most === undefined) {
270
+ return undefined;
271
+ }
272
+ const moves = start === undefined ? [] : fewest(sheet, start.cell);
273
+ const cells = new Set();
274
+ moves.forEach((n, cell) => {
275
+ if (n >= least && n <= most)
276
+ cells.add(cell);
277
+ });
278
+ return { cells };
279
+ },
280
+ meets(sheet, goal, state) {
281
+ const cell = where(sheet, state, member);
282
+ return cell !== undefined && goal.cells.has(cell);
118
283
  },
119
- meets: (sheet, cell, state) => where(sheet, state, member) === cell,
284
+ asks: () => ({ key: "count" }),
120
285
  };
121
- return { operation, goal };
286
+ return { operation, goal, reach };
122
287
  }