@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,45 @@
1
+ import type { Goal, Operation } from "./operation.js";
2
+ import { type Sheet, type State } from "./sheet.js";
3
+ /** The most cells of the shapes a count of cells gives: every shape of so many is listed. */
4
+ export declare const mostCells = 6;
5
+ export interface PolyominoOptions {
6
+ /** How many figures may lie on the sheet at once. */
7
+ readonly pieces: number;
8
+ /**
9
+ * Every place a figure may lie, each its cells by index in reading order, ordered by those cells;
10
+ * a move lays one of them.
11
+ */
12
+ readonly placings: readonly (readonly number[])[];
13
+ /** The index of each place among placings, by its cells' indices joined by commas. */
14
+ readonly placed: ReadonlyMap<string, number>;
15
+ }
16
+ /** The figures a state has laid, by their places, and the cells they cover. */
17
+ export interface Laid {
18
+ readonly figures: readonly number[];
19
+ readonly covered: ReadonlySet<number>;
20
+ }
21
+ /**
22
+ * The figures of the state, or undefined where its member "polyomino" is no list of places the
23
+ * scene's figures may lie on, apart, each its cells in reading order, ordered by their first cells,
24
+ * no more than the pieces — a state reported from outside the kind may hold one, and then no figure
25
+ * is laid or lifted from it and no goal is met.
26
+ */
27
+ export declare function laidOf(sheet: Sheet, options: PolyominoOptions, state: State): Laid | undefined;
28
+ export declare const polyomino: Operation<PolyominoOptions>;
29
+ /** What a goal of the figures reads of the scene: the figures' own parameters, where they read. */
30
+ interface Figures {
31
+ readonly polyomino?: PolyominoOptions;
32
+ }
33
+ /** The figures cover every cell but the holes; the key is the fewest figures. */
34
+ export declare const polyominoCover: Goal<Figures>;
35
+ /** As polyomino_cover, and the key counts the ways. */
36
+ export declare const polyominoTilings: Goal<Figures>;
37
+ /** Any figures; the key is the most figures that lie on the sheet at once. */
38
+ export declare const polyominoMost: Goal<Figures>;
39
+ /** Any figures; the key is the fewest cells left empty, no figure on them, the holes aside. */
40
+ export declare const polyominoEmpty: Goal<Figures>;
41
+ /** One figure on the sheet; the key counts its places. */
42
+ export declare const polyominoPlacings: Goal<Figures>;
43
+ /** One figure on the sheet; the key counts its shapes up to a move, a turn and a turn over. */
44
+ export declare const polyominoShapes: Goal<Figures>;
45
+ export {};
@@ -0,0 +1,461 @@
1
+ // Figures of cells joined by their sides, polyominoes, laid on the sheet and lifted off it: a
2
+ // domino, a tromino, a tile of four cells. The scene gives the shapes, or a count of cells for every
3
+ // shape of so many, and whether a figure may be turned and turned over; a figure covers whole cells,
4
+ // never a hole or another figure, and lies over a wall and over paint, which it knows nothing of.
5
+ // The state keeps the figures laid, so a goal reads which cells they cover.
6
+ import { array, integer, isObject, object, pointer, string } from "../../cores/json.js";
7
+ import { cellAt, cellName, cellPattern, readCell } from "./sheet.js";
8
+ /** The most cells of the shapes a count of cells gives: every shape of so many is listed. */
9
+ export const mostCells = 6;
10
+ const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
11
+ /** A cell's name read as a square of a shape's own grid. */
12
+ function square(name) {
13
+ return [letters.indexOf(name[0]), Number(name.slice(1)) - 1];
14
+ }
15
+ /** The squares moved to the grid's corner and in reading order, so that one shape has one form. */
16
+ function normal(squares) {
17
+ const top = Math.min(...squares.map(([r]) => r));
18
+ const left = Math.min(...squares.map(([, c]) => c));
19
+ return squares
20
+ .map(([r, c]) => [r - top, c - left])
21
+ .sort((a, b) => a[0] - b[0] || a[1] - b[1]);
22
+ }
23
+ const formKey = (squares) => squares.map((s) => s.join(":")).join(",");
24
+ /** Whether the squares are joined by their sides into one figure. */
25
+ function joined(squares) {
26
+ const keys = new Set(squares.map((s) => s.join(":")));
27
+ const seen = new Set([formKey(squares.slice(0, 1))]);
28
+ for (const queue = squares.slice(0, 1); queue.length > 0;) {
29
+ const [r, c] = queue.pop();
30
+ for (const next of [
31
+ [r - 1, c],
32
+ [r + 1, c],
33
+ [r, c - 1],
34
+ [r, c + 1],
35
+ ]) {
36
+ const key = next.join(":");
37
+ if (keys.has(key) && !seen.has(key)) {
38
+ seen.add(key);
39
+ queue.push(next);
40
+ }
41
+ }
42
+ }
43
+ return seen.size === keys.size;
44
+ }
45
+ /**
46
+ * The forms of a shape as it may lie: as given; turned by each quarter where it may turn; turned
47
+ * over, its left and right changed, where it may be turned over, and turned so too where both.
48
+ */
49
+ function forms(shape, turn, flip) {
50
+ const turns = (s) => {
51
+ const all = [normal(s)];
52
+ for (let i = 1; turn && i < 4; i++) {
53
+ all.push(normal(all[i - 1].map(([r, c]) => [c, -r])));
54
+ }
55
+ return all;
56
+ };
57
+ const all = [...turns(shape), ...(flip ? turns(shape.map(([r, c]) => [r, -c])) : [])];
58
+ return [...new Map(all.map((form) => [formKey(form), form])).values()];
59
+ }
60
+ /** Every shape of n squares joined by their sides, in each of the ways it may lie, each once. */
61
+ function everyShape(n) {
62
+ let shapes = [[[0, 0]]];
63
+ for (let size = 1; size < n; size++) {
64
+ const grown = new Map();
65
+ for (const shape of shapes) {
66
+ for (const [r, c] of shape) {
67
+ for (const next of [
68
+ [r - 1, c],
69
+ [r + 1, c],
70
+ [r, c - 1],
71
+ [r, c + 1],
72
+ ]) {
73
+ if (shape.some(([a, b]) => a === next[0] && b === next[1]))
74
+ continue;
75
+ const form = normal([...shape, next]);
76
+ grown.set(formKey(form), form);
77
+ }
78
+ }
79
+ }
80
+ shapes = [...grown.values()];
81
+ }
82
+ return shapes;
83
+ }
84
+ /** Every place on the sheet off the holes where one of the forms lies, each once, in order. */
85
+ function placingsOf(sheet, all) {
86
+ const found = new Map();
87
+ for (const form of all) {
88
+ const height = Math.max(...form.map(([r]) => r)) + 1;
89
+ const width = Math.max(...form.map(([, c]) => c)) + 1;
90
+ for (let top = 0; top + height <= sheet.rows; top++) {
91
+ for (let left = 0; left + width <= sheet.cols; left++) {
92
+ const cells = form.map(([r, c]) => (top + r) * sheet.cols + left + c);
93
+ if (cells.some((cell) => sheet.holes.has(cell)))
94
+ continue;
95
+ found.set(cells.join(","), cells);
96
+ }
97
+ }
98
+ }
99
+ return [...found.values()].sort((a, b) => {
100
+ for (let i = 0; i < Math.min(a.length, b.length); i++) {
101
+ if (a[i] !== b[i])
102
+ return a[i] - b[i];
103
+ }
104
+ return a.length - b.length;
105
+ });
106
+ }
107
+ // The figures of each state read so far, by the array of its member "polyomino", for each scene's
108
+ // parameters: null for a member the rules never make.
109
+ const laidByOptions = new WeakMap();
110
+ /**
111
+ * The figures of the state, or undefined where its member "polyomino" is no list of places the
112
+ * scene's figures may lie on, apart, each its cells in reading order, ordered by their first cells,
113
+ * no more than the pieces — a state reported from outside the kind may hold one, and then no figure
114
+ * is laid or lifted from it and no goal is met.
115
+ */
116
+ export function laidOf(sheet, options, state) {
117
+ const value = state.polyomino;
118
+ if (!Array.isArray(value)) {
119
+ return undefined;
120
+ }
121
+ let byOptions = laidByOptions.get(options);
122
+ if (byOptions === undefined) {
123
+ byOptions = new WeakMap();
124
+ laidByOptions.set(options, byOptions);
125
+ }
126
+ let laid = byOptions.get(value);
127
+ if (laid === undefined) {
128
+ laid = readLaid(sheet, options, value);
129
+ byOptions.set(value, laid);
130
+ }
131
+ return laid ?? undefined;
132
+ }
133
+ function readLaid(sheet, options, value) {
134
+ if (value.length > options.pieces)
135
+ return null;
136
+ const figures = [];
137
+ const covered = new Set();
138
+ let last = -1;
139
+ for (const figure of value) {
140
+ if (!Array.isArray(figure))
141
+ return null;
142
+ const cells = figure.map((name) => typeof name === "string" ? cellAt(sheet, name) : undefined);
143
+ const placing = options.placed.get(cells.join(","));
144
+ const first = cells[0];
145
+ if (placing === undefined || first === undefined || first <= last)
146
+ return null;
147
+ for (const cell of cells) {
148
+ if (covered.has(cell))
149
+ return null;
150
+ covered.add(cell);
151
+ }
152
+ figures.push(placing);
153
+ last = first;
154
+ }
155
+ return { figures, covered };
156
+ }
157
+ /** The member of the state with the figures given, by their places. */
158
+ function laidJson(sheet, options, figures) {
159
+ return [...figures]
160
+ .sort((a, b) => a - b)
161
+ .map((placing) => options.placings[placing].map((cell) => cellName(sheet, cell)));
162
+ }
163
+ function refuse(reason) {
164
+ return { ok: false, reason };
165
+ }
166
+ /** The cells a move names, by index, or undefined where a name is no cell of the sheet. */
167
+ function namedCells(sheet, value) {
168
+ if (!Array.isArray(value) || value.length === 0)
169
+ return undefined;
170
+ const cells = value.map((name) => (typeof name === "string" ? cellAt(sheet, name) : undefined));
171
+ return cells.every((cell) => cell !== undefined) ? cells : undefined;
172
+ }
173
+ const cellList = (description, item, least) => ({
174
+ description,
175
+ type: "array",
176
+ items: { description: item, $ref: "#/$defs/cell" },
177
+ minItems: least,
178
+ uniqueItems: true,
179
+ });
180
+ export const polyomino = {
181
+ name: "polyomino",
182
+ schema: {
183
+ options: {
184
+ description: "Figures of cells joined by their sides laid on the sheet and lifted off it: the shapes given, or every shape of a count of cells, one of them, the count with no turn and no flip. A figure covers whole cells, never a hole or another figure; it lies over a wall and over paint.",
185
+ type: "object",
186
+ properties: {
187
+ shapes: {
188
+ description: "The shapes of the figures, a domino or any other.",
189
+ type: "array",
190
+ items: cellList("A shape, by the cells it covers on a grid of its own, as A1, A2 and B2 for an L of three cells; where on that grid does not matter. Its cells are joined by their sides.", "A cell of the shape.", 2),
191
+ minItems: 1,
192
+ },
193
+ cells: {
194
+ description: `Every shape of so many cells joined by their sides, from 2 to ${mostCells}, in every way it may lie.`,
195
+ type: "integer",
196
+ minimum: 2,
197
+ maximum: mostCells,
198
+ },
199
+ turn: {
200
+ description: "Whether a figure of the shapes given may be turned by a quarter, a half or three quarters; true where left out.",
201
+ type: "boolean",
202
+ },
203
+ flip: {
204
+ description: "Whether a figure of the shapes given may be turned over, its left and right changed; true where left out.",
205
+ type: "boolean",
206
+ },
207
+ pieces: {
208
+ description: "How many figures may lie on the sheet at once; as many as fit where left out.",
209
+ type: "integer",
210
+ minimum: 1,
211
+ maximum: 676,
212
+ },
213
+ },
214
+ unevaluatedProperties: false,
215
+ },
216
+ move: {
217
+ description: "A figure laid, or a figure lifted.",
218
+ type: "object",
219
+ properties: {
220
+ lay: {
221
+ description: "A figure laid on the cells it covers.",
222
+ type: "object",
223
+ properties: {
224
+ cells: cellList("The cells the figure covers, in any order.", "A cell the figure covers.", 1),
225
+ },
226
+ required: ["cells"],
227
+ unevaluatedProperties: false,
228
+ },
229
+ lift: {
230
+ description: "The figure lifted off the cell it covers.",
231
+ type: "object",
232
+ properties: {
233
+ cell: { description: "A cell the figure covers.", $ref: "#/$defs/cell" },
234
+ },
235
+ required: ["cell"],
236
+ unevaluatedProperties: false,
237
+ },
238
+ },
239
+ minProperties: 1,
240
+ maxProperties: 1,
241
+ unevaluatedProperties: false,
242
+ },
243
+ state: {
244
+ member: "polyomino",
245
+ schema: {
246
+ description: "The figures laid, each by the cells it covers in reading order, ordered by their first cells.",
247
+ type: "array",
248
+ items: cellList("A figure laid.", "A cell the figure covers.", 1),
249
+ },
250
+ },
251
+ },
252
+ read(value, sheet, path, errors) {
253
+ const before = errors.length;
254
+ const o = object(value, path, errors, [], ["shapes", "cells", "turn", "flip", "pieces"]);
255
+ if (o === undefined) {
256
+ return undefined;
257
+ }
258
+ const shapes = [];
259
+ if (o.shapes !== undefined) {
260
+ const at = pointer(path, "shapes");
261
+ array(o.shapes, at, errors, 1)?.forEach((item, i) => {
262
+ const here = pointer(at, i);
263
+ const names = [];
264
+ array(item, here, errors, 2)?.forEach((name, j) => {
265
+ const read = string(name, pointer(here, j), errors, cellPattern, "a cell's name, as A1");
266
+ if (read !== undefined && names.includes(read)) {
267
+ errors.push({ path: pointer(here, j), message: "a cell named twice" });
268
+ }
269
+ else if (read !== undefined)
270
+ names.push(read);
271
+ });
272
+ if (names.length < 2 || names.length !== item.length)
273
+ return;
274
+ const squares = names.map(square);
275
+ if (!joined(squares)) {
276
+ errors.push({ path: here, message: "cells not joined by their sides into one figure" });
277
+ }
278
+ else
279
+ shapes.push(squares);
280
+ });
281
+ }
282
+ const cells = o.cells === undefined
283
+ ? undefined
284
+ : integer(o.cells, pointer(path, "cells"), errors, 2, mostCells);
285
+ if ((o.shapes === undefined) === (o.cells === undefined)) {
286
+ errors.push({ path, message: "the shapes or a count of cells, one of them" });
287
+ }
288
+ const flags = { turn: true, flip: true };
289
+ for (const flag of ["turn", "flip"]) {
290
+ if (o[flag] === undefined)
291
+ continue;
292
+ if (typeof o[flag] !== "boolean") {
293
+ errors.push({ path: pointer(path, flag), message: "true or false" });
294
+ }
295
+ else if (o.cells !== undefined) {
296
+ errors.push({
297
+ path: pointer(path, flag),
298
+ message: "every shape of a count of cells lies every way already",
299
+ });
300
+ }
301
+ else
302
+ flags[flag] = o[flag];
303
+ }
304
+ const pieces = o.pieces === undefined ? 676 : integer(o.pieces, pointer(path, "pieces"), errors, 1, 676);
305
+ if (errors.length > before || pieces === undefined) {
306
+ return undefined;
307
+ }
308
+ const all = cells === undefined
309
+ ? shapes.flatMap((shape) => forms(shape, flags.turn, flags.flip))
310
+ : everyShape(cells);
311
+ const placings = placingsOf(sheet, all);
312
+ if (placings.length === 0) {
313
+ errors.push({ path, message: "no figure fits on the sheet off its holes" });
314
+ return undefined;
315
+ }
316
+ return {
317
+ pieces,
318
+ placings,
319
+ placed: new Map(placings.map((placing, i) => [placing.join(","), i])),
320
+ };
321
+ },
322
+ start: () => [],
323
+ play(sheet, options, state, move) {
324
+ const o = isObject(move) ? move : {};
325
+ const parts = Object.keys(o);
326
+ const part = parts.length === 1 ? parts[0] : "";
327
+ const inner = isObject(o[part]) ? o[part] : {};
328
+ const fields = Object.keys(inner);
329
+ if (part === "lift" && fields.length === 1 && fields[0] === "cell") {
330
+ const cell = readCell(sheet, inner.cell, "", []);
331
+ if (cell === undefined)
332
+ return refuse("no-cell");
333
+ const laid = laidOf(sheet, options, state);
334
+ if (laid === undefined)
335
+ return refuse("no-figures");
336
+ const figures = laid.figures.filter((placing) => !options.placings[placing].includes(cell));
337
+ if (figures.length === laid.figures.length)
338
+ return refuse("not-laid");
339
+ return { ok: true, state: { ...state, polyomino: laidJson(sheet, options, figures) } };
340
+ }
341
+ const cells = part === "lay" && fields.length === 1 && fields[0] === "cells"
342
+ ? namedCells(sheet, inner.cells)
343
+ : undefined;
344
+ if (cells === undefined)
345
+ return refuse("no-cell");
346
+ const laid = laidOf(sheet, options, state);
347
+ if (laid === undefined)
348
+ return refuse("no-figures");
349
+ if (laid.figures.length >= options.pieces)
350
+ return refuse("all-laid");
351
+ if (cells.some((cell) => sheet.holes.has(cell)))
352
+ return refuse("hole");
353
+ const placing = options.placed.get([...cells].sort((a, b) => a - b).join(","));
354
+ if (placing === undefined || new Set(cells).size !== cells.length)
355
+ return refuse("not-a-shape");
356
+ if (cells.some((cell) => laid.covered.has(cell)))
357
+ return refuse("overlap");
358
+ return {
359
+ ok: true,
360
+ state: { ...state, polyomino: laidJson(sheet, options, [...laid.figures, placing]) },
361
+ };
362
+ },
363
+ // Only figures laid: a figure lifted leaves a state the figures left reach as well.
364
+ moves(sheet, options, state) {
365
+ const laid = laidOf(sheet, options, state);
366
+ if (laid === undefined || laid.figures.length >= options.pieces) {
367
+ return [];
368
+ }
369
+ return options.placings
370
+ .filter((placing) => placing.every((cell) => !laid.covered.has(cell)))
371
+ .map((placing) => ({ lay: { cells: placing.map((cell) => cellName(sheet, cell)) } }));
372
+ },
373
+ };
374
+ const said = {
375
+ beside: "",
376
+ alone: " alone, so that its states are the figures",
377
+ one: " alone with pieces 1, so that its states are the one figure's places",
378
+ };
379
+ /** The goal's figures, the polyomino's parameters given, or an error where the scene lays none. */
380
+ function readFigures(value, path, errors, operations, takes) {
381
+ const before = errors.length;
382
+ object(value, path, errors, []);
383
+ if (!operations.has("polyomino")) {
384
+ errors.push({ path, message: "the scene allows no operation polyomino, so no figure is laid" });
385
+ }
386
+ const others = [...operations]
387
+ .filter(([named, options]) => named !== "polyomino" && options !== undefined)
388
+ .map(([named]) => named);
389
+ if (takes !== "beside" && operations.get("polyomino") !== undefined && others.length > 0) {
390
+ errors.push({
391
+ path,
392
+ message: `the scene allows ${others.join(", ")} beside polyomino: a count of figures counts the figures' states alone`,
393
+ });
394
+ }
395
+ const options = operations.get("polyomino");
396
+ if (takes === "one" && options !== undefined && options.pieces !== 1) {
397
+ errors.push({
398
+ path,
399
+ message: "a count of one figure takes pieces 1 of the operation polyomino, so that no second figure lies at once",
400
+ });
401
+ }
402
+ return errors.length > before ? undefined : options === undefined ? {} : { polyomino: options };
403
+ }
404
+ /** The figures of the state as a goal reads them, by the figures' parameters where they read. */
405
+ function figuresOf(sheet, goal, state) {
406
+ return goal.polyomino === undefined ? undefined : laidOf(sheet, goal.polyomino, state);
407
+ }
408
+ /** The cells of the sheet that are no hole. */
409
+ const free = (sheet) => sheet.rows * sheet.cols - sheet.holes.size;
410
+ function goalOf(name, description, takes, meets, asks) {
411
+ return {
412
+ name,
413
+ schema: {
414
+ description: `${description} The scene allows the operation polyomino${said[takes]}.`,
415
+ type: "object",
416
+ unevaluatedProperties: false,
417
+ },
418
+ read: (value, _sheet, path, errors, operations) => readFigures(value, path, errors, operations, takes),
419
+ meets(sheet, goal, state) {
420
+ const laid = figuresOf(sheet, goal, state);
421
+ return laid !== undefined && meets(sheet, laid);
422
+ },
423
+ ...(asks === undefined ? {} : { asks }),
424
+ };
425
+ }
426
+ const covers = (sheet, laid) => laid.covered.size === free(sheet);
427
+ const one = (_sheet, laid) => laid.figures.length === 1;
428
+ /** The figures cover every cell but the holes; the key is the fewest figures. */
429
+ export const polyominoCover = goalOf("polyomino_cover", "The figures cover every cell that is no hole; the key is the fewest figures laid.", "beside", covers);
430
+ /** As polyomino_cover, and the key counts the ways. */
431
+ export const polyominoTilings = goalOf("polyomino_tilings", "The figures cover every cell that is no hole; the key counts the ways, two ways one where their figures cover the same cells.", "alone", covers, () => ({ key: "count" }));
432
+ /** Any figures; the key is the most figures that lie on the sheet at once. */
433
+ export const polyominoMost = goalOf("polyomino_most", "Met by every state; the key is the most figures that lie on the sheet at once.", "beside", () => true, (sheet, goal) => ({
434
+ key: "max",
435
+ measure: (state) => figuresOf(sheet, goal, state)?.figures.length ?? 0,
436
+ }));
437
+ /** Any figures; the key is the fewest cells left empty, no figure on them, the holes aside. */
438
+ export const polyominoEmpty = goalOf("polyomino_empty", "Met by every state; the key is the fewest cells that are no hole and no figure covers.", "beside", () => true, (sheet, goal) => ({
439
+ key: "min",
440
+ measure: (state) => free(sheet) - (figuresOf(sheet, goal, state)?.covered.size ?? 0),
441
+ }));
442
+ /** One figure on the sheet; the key counts its places. */
443
+ export const polyominoPlacings = goalOf("polyomino_placings", "One figure lies on the sheet; the key counts the places it may lie on, two places one where they cover the same cells.", "one", one, () => ({ key: "count" }));
444
+ /** One figure on the sheet; the key counts its shapes up to a move, a turn and a turn over. */
445
+ export const polyominoShapes = goalOf("polyomino_shapes", "One figure lies on the sheet; the key counts its shapes, two figures one shape where one moved, turned or turned over lies on the other.", "one", one, (sheet, goal) => ({ key: "count", images: (state) => sameShape(sheet, goal, state) }));
446
+ /**
447
+ * Every state with one figure of the state's own figure's shape, wherever on the sheet off its
448
+ * holes it lies, moved, turned or turned over: the class a count of shapes counts once.
449
+ */
450
+ function sameShape(sheet, goal, state) {
451
+ const laid = figuresOf(sheet, goal, state);
452
+ const [figure] = laid?.figures ?? [];
453
+ if (goal.polyomino === undefined || figure === undefined) {
454
+ return [state];
455
+ }
456
+ const squares = goal.polyomino.placings[figure].map((cell) => [Math.floor(cell / sheet.cols), cell % sheet.cols]);
457
+ return placingsOf(sheet, forms(squares, true, true)).map((cells) => ({
458
+ ...state,
459
+ polyomino: [cells.map((cell) => cellName(sheet, cell))],
460
+ }));
461
+ }
@@ -1,16 +1,24 @@
1
1
  // The operations and goals of the kind cells. A new operation or goal is a module of its own,
2
2
  // listed here, with the next minor version: nothing else of the kind changes for it.
3
- import { bishop, bishopOn } from "./bishop.js";
3
+ import { bishop, bishopOn, bishopReach } from "./bishop.js";
4
+ import { counter, counterIn, counterMissed } from "./counter.js";
5
+ import { cellsApart, cut, cutWays, nodes, parts, sides } from "./cut.js";
6
+ import { area, perimeter } from "./figure.js";
4
7
  import { fold } from "./fold.js";
8
+ import { paperHoles } from "./holes.js";
5
9
  import { knight, knightOn } from "./knight.js";
10
+ import { numberAt } from "./number.js";
6
11
  import { paint } from "./paint.js";
7
12
  import { allPainted, cellsPainted } from "./painted.js";
8
13
  import { paper } from "./paper.js";
9
- import { step, stepOn } from "./step.js";
14
+ import { path, pathClosed, pathCount, pathTo } from "./path.js";
15
+ import { polyomino, polyominoCover, polyominoEmpty, polyominoMost, polyominoPlacings, polyominoShapes, polyominoTilings, } from "./polyomino.js";
16
+ import { sieve, sieveLeast } from "./sieve.js";
17
+ import { step, stepOn, stepReach } from "./step.js";
10
18
  import { mirrorSymmetric, quarterTurnSymmetric } from "./symmetric.js";
11
19
  export const registry = {
12
- version: "2.0",
13
- operations: [fold, paint, step, knight, bishop, paper],
20
+ version: "3.1",
21
+ operations: [fold, paint, step, knight, bishop, paper, counter, sieve, path, cut, polyomino],
14
22
  goals: [
15
23
  allPainted,
16
24
  cellsPainted,
@@ -19,5 +27,28 @@ export const registry = {
19
27
  bishopOn,
20
28
  quarterTurnSymmetric,
21
29
  mirrorSymmetric,
30
+ stepReach,
31
+ bishopReach,
32
+ paperHoles,
33
+ perimeter,
34
+ area,
35
+ numberAt,
36
+ counterIn,
37
+ counterMissed,
38
+ sieveLeast,
39
+ pathTo,
40
+ pathCount,
41
+ pathClosed,
42
+ parts,
43
+ cutWays,
44
+ cellsApart,
45
+ nodes,
46
+ sides,
47
+ polyominoCover,
48
+ polyominoTilings,
49
+ polyominoMost,
50
+ polyominoEmpty,
51
+ polyominoPlacings,
52
+ polyominoShapes,
22
53
  ],
23
54
  };
@@ -1,9 +1,21 @@
1
- import { cellPattern, colors, maxSide } from "./sheet.js";
1
+ import { cellPattern, colors, maxFirst, maxSide } from "./sheet.js";
2
+ import { nodePattern, sidePattern } from "./side.js";
2
3
  /** The $id of the kind's schema at a version: a name, which need not resolve. */
3
4
  export function schemaId(version) {
4
5
  return `https://altnovatrail.github.io/widgets/kinds/cells/${version}/cells.schema.json`;
5
6
  }
6
7
  const cell = (description) => ({ description, $ref: "#/$defs/cell" });
8
+ /** The marks of the places a name of the pattern def names, as the sheet gives them. */
9
+ const placeMarks = (description, def) => ({
10
+ description,
11
+ type: "object",
12
+ propertyNames: { $ref: `#/$defs/${def}` },
13
+ additionalProperties: {
14
+ description: "The mark of the place.",
15
+ $ref: "#/$defs/mark",
16
+ unevaluatedProperties: false,
17
+ },
18
+ });
7
19
  export function cellsSchema(registry) {
8
20
  const options = {};
9
21
  const moves = {};
@@ -16,6 +28,9 @@ export function cellsSchema(registry) {
16
28
  if (op.schema.state !== undefined) {
17
29
  state[op.schema.state.member] = op.schema.state.schema;
18
30
  }
31
+ if (op.schema.state?.beside !== undefined) {
32
+ state[op.schema.state.beside.member] = op.schema.state.beside.schema;
33
+ }
19
34
  }
20
35
  const goals = {};
21
36
  for (const goal of registry.goals) {
@@ -56,8 +71,18 @@ export function cellsSchema(registry) {
56
71
  type: "string",
57
72
  pattern: cellPattern.source,
58
73
  },
74
+ side: {
75
+ description: "A side of a cell by its line and the row or column it lies beside: v0 to v26 the vertical lines from the sheet's left edge, a side on one named by its row's letter, as v2:B between B2 and B3; h0 to h26 the horizontal lines from its top edge, a side on one named by its column's number, as h1:3 between A3 and B3.",
76
+ type: "string",
77
+ pattern: sidePattern.source,
78
+ },
79
+ node: {
80
+ description: "A node of the grid, where a vertical line crosses a horizontal one, by the two lines, as v2:h1, the corner A2, A3, B2 and B3 share; v0 is the sheet's left edge, h0 its top edge.",
81
+ type: "string",
82
+ pattern: nodePattern.source,
83
+ },
59
84
  mark: {
60
- description: "What a cell carries: a colour, a whole number written in it, or both.",
85
+ description: "What a cell, a node or a side carries: a colour, a whole number written in it, or both.",
61
86
  type: "object",
62
87
  properties: {
63
88
  color: { description: "A colour, by its name.", enum: [...colors] },
@@ -81,7 +106,7 @@ export function cellsSchema(registry) {
81
106
  },
82
107
  },
83
108
  sheet: {
84
- description: "Rows and columns of cells, the cells cut out, the walls between cells, and the marks the cells carry.",
109
+ description: "Rows and columns of cells, the cells cut out, the walls between cells, and the marks the cells, the nodes of the grid and the sides of cells carry.",
85
110
  type: "object",
86
111
  properties: {
87
112
  rows: {
@@ -117,6 +142,22 @@ export function cellsSchema(registry) {
117
142
  description: "The marks the cells carry at the scene's start, none on a hole.",
118
143
  $ref: "#/$defs/marks",
119
144
  },
145
+ node_marks: placeMarks("The marks the scene gives nodes of the grid, each by the node's name, none at a node with only holes beside it; no move changes them.", "node"),
146
+ side_marks: placeMarks("The marks the scene gives sides of cells, each by the side's name, none on a side with only holes beside it; no move changes them.", "side"),
147
+ numbers: {
148
+ description: "The sheet as a table of numbers: its cells numbered in reading order, A1 the first number, each next cell the next number, a hole too, though nothing lies on it; a row of the table is a row of the sheet.",
149
+ type: "object",
150
+ properties: {
151
+ first: {
152
+ description: "The number A1 holds.",
153
+ type: "integer",
154
+ minimum: 0,
155
+ maximum: maxFirst,
156
+ },
157
+ },
158
+ required: ["first"],
159
+ unevaluatedProperties: false,
160
+ },
120
161
  },
121
162
  required: ["rows", "cols"],
122
163
  },
@@ -128,7 +169,7 @@ export function cellsSchema(registry) {
128
169
  maxProperties: 1,
129
170
  },
130
171
  state: {
131
- description: "A state: the cells' marks, and a member for each operation of the scene that keeps one.",
172
+ description: "A state: the cells' marks, and a member for each operation of the scene that keeps one, with a second beside it where the scene's parameters of the operation ask for it.",
132
173
  type: "object",
133
174
  properties: state,
134
175
  required: ["marks"],