@altnovatrail/widgets 0.1.2 → 0.1.3
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/README.md +1 -1
- package/cores/vector.schema.json +82 -3
- package/kinds/cells/cells.handles.json +145 -0
- package/kinds/cells/cells.schema.json +652 -4
- package/kinds/cells/vectors/bishop-grid-56-d1-1.json +105 -4
- package/kinds/cells/vectors/counter-cases.json +123 -0
- package/kinds/cells/vectors/counter-div-56-d1-3.json +57 -0
- package/kinds/cells/vectors/counter-div-56-d2-1.json +773 -0
- package/kinds/cells/vectors/counter-div-56-d2-3.json +65 -0
- package/kinds/cells/vectors/counter-div-56-d4-1.json +57 -0
- package/kinds/cells/vectors/counter-div-56-d4-2.json +106 -0
- package/kinds/cells/vectors/cut-cases.json +163 -0
- package/kinds/cells/vectors/cut-gaps-56-d2-1.json +142 -0
- package/kinds/cells/vectors/cut-grid-56-d2-4.json +45 -0
- package/kinds/cells/vectors/cut-grid-56-d4-1.json +154 -0
- package/kinds/cells/vectors/cut-grid-56-d4-2.json +136 -0
- package/kinds/cells/vectors/cut-invalid.json +48 -0
- package/kinds/cells/vectors/domino-grid-56-d3-2.json +238 -0
- package/kinds/cells/vectors/domino-limit.json +60 -0
- package/kinds/cells/vectors/domino-par-56-d3-2.json +194 -0
- package/kinds/cells/vectors/domino-par-56-d4-3.json +127 -0
- package/kinds/cells/vectors/figure-apart.json +47 -0
- package/kinds/cells/vectors/fold-12x12.json +79 -0
- package/kinds/cells/vectors/fold-14x14.json +79 -0
- package/kinds/cells/vectors/fold-26x26.json +79 -0
- package/kinds/cells/vectors/garden-enum-56-d3-4.json +96 -6
- package/kinds/cells/vectors/goat-grid-56-d4-4.json +325 -5
- package/kinds/cells/vectors/mosaic-grid-56-d2-2.json +84 -0
- package/kinds/cells/vectors/nodes-gaps-56-d2-2.json +44 -0
- package/kinds/cells/vectors/nodes-gaps-56-d4-2.json +70 -0
- package/kinds/cells/vectors/number-div-56-d2-2.json +49 -0
- package/kinds/cells/vectors/numbers-div-56-d3-1.json +24 -0
- package/kinds/cells/vectors/numbers-invalid.json +56 -0
- package/kinds/cells/vectors/paper-26x26.json +743 -0
- package/kinds/cells/vectors/paper-cube-12-d2-1.json +43 -19
- package/kinds/cells/vectors/paper-cube-12-d5-1.json +47 -4
- package/kinds/cells/vectors/paper-cube-56-d4-1.json +56 -5
- package/kinds/cells/vectors/paper-drawn-lines.json +149 -0
- package/kinds/cells/vectors/paper-grid-56-d1-2.json +46 -5
- package/kinds/cells/vectors/paper-turn-invalid.json +4 -4
- package/kinds/cells/vectors/path-6x6.json +119 -0
- package/kinds/cells/vectors/path-beside.json +108 -0
- package/kinds/cells/vectors/path-enum-12-d3-5.json +187 -0
- package/kinds/cells/vectors/path-enum-34-d4-3.json +412 -0
- package/kinds/cells/vectors/path-enum-56-d3-3.json +411 -0
- package/kinds/cells/vectors/path-enum-56-d3-5.json +351 -0
- package/kinds/cells/vectors/path-invalid.json +37 -0
- package/kinds/cells/vectors/path-limit.json +58 -0
- package/kinds/cells/vectors/path-maze.json +297 -0
- package/kinds/cells/vectors/path-unreachable.json +76 -0
- package/kinds/cells/vectors/perimeter-grid-56-d2-1.json +86 -0
- package/kinds/cells/vectors/perimeter-grid-56-d3-1.json +102 -0
- package/kinds/cells/vectors/perimeter-grid-56-d3-3.json +112 -0
- package/kinds/cells/vectors/perimeter-grid-56-d4-3.json +289 -0
- package/kinds/cells/vectors/polyomino-cases.json +191 -0
- package/kinds/cells/vectors/polyomino-grid-56-d3-4.json +205 -0
- package/kinds/cells/vectors/polyomino-grid-56-d4-1.json +137 -0
- package/kinds/cells/vectors/polyomino-grid-56-d5-3.json +157 -0
- package/kinds/cells/vectors/polyomino-invalid.json +45 -0
- package/kinds/cells/vectors/route-par-56-d3-3.json +245 -0
- package/kinds/cells/vectors/sides-gaps-56-d3-2.json +44 -0
- package/kinds/cells/vectors/sieve-div-56-d3-2.json +140 -0
- package/kinds/cells/vectors/tear-gaps-56-d3-1.json +84 -0
- package/kinds/cells/vectors/tear-limit.json +56 -0
- package/lib/cores/enumerate.d.ts +33 -0
- package/lib/cores/enumerate.js +77 -0
- package/lib/cores/kind.d.ts +55 -5
- package/lib/cores/search.d.ts +27 -8
- package/lib/cores/search.js +85 -28
- package/lib/kinds/cells/bishop.d.ts +1 -1
- package/lib/kinds/cells/bishop.js +1 -1
- package/lib/kinds/cells/counter.d.ts +26 -0
- package/lib/kinds/cells/counter.js +249 -0
- package/lib/kinds/cells/cut.d.ts +46 -0
- package/lib/kinds/cells/cut.js +400 -0
- package/lib/kinds/cells/figure.d.ts +13 -0
- package/lib/kinds/cells/figure.js +114 -0
- package/lib/kinds/cells/holes.d.ts +6 -0
- package/lib/kinds/cells/holes.js +57 -0
- package/lib/kinds/cells/kind.js +4 -2
- package/lib/kinds/cells/number.d.ts +6 -0
- package/lib/kinds/cells/number.js +32 -0
- package/lib/kinds/cells/operation.d.ts +12 -6
- package/lib/kinds/cells/paper.d.ts +10 -2
- package/lib/kinds/cells/paper.js +90 -8
- package/lib/kinds/cells/path.d.ts +38 -0
- package/lib/kinds/cells/path.js +288 -0
- package/lib/kinds/cells/piece.d.ts +10 -1
- package/lib/kinds/cells/piece.js +87 -3
- package/lib/kinds/cells/polyomino.d.ts +45 -0
- package/lib/kinds/cells/polyomino.js +450 -0
- package/lib/kinds/cells/registry.js +35 -4
- package/lib/kinds/cells/schema.js +41 -3
- package/lib/kinds/cells/sheet.d.ts +20 -1
- package/lib/kinds/cells/sheet.js +84 -6
- package/lib/kinds/cells/side.d.ts +33 -0
- package/lib/kinds/cells/side.js +86 -0
- package/lib/kinds/cells/sieve.d.ts +26 -0
- package/lib/kinds/cells/sieve.js +208 -0
- package/lib/kinds/cells/step.d.ts +1 -1
- package/lib/kinds/cells/step.js +1 -1
- package/package.json +5 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Json, Played, SceneError } from "../../cores/kind.js";
|
|
1
|
+
import type { Ask, Json, Played, SceneError } from "../../cores/kind.js";
|
|
2
2
|
import type { Sheet, State } from "./sheet.js";
|
|
3
3
|
/**
|
|
4
4
|
* A part of the kind's JSON Schema, written by the conventions of the module format's schemas:
|
|
@@ -40,13 +40,19 @@ export interface Goal<Options = unknown> {
|
|
|
40
40
|
readonly schema: Schema;
|
|
41
41
|
/**
|
|
42
42
|
* The parameters, or undefined with their errors added, as an operation's are read, by the sheet
|
|
43
|
-
* and by the
|
|
44
|
-
*
|
|
45
|
-
* map
|
|
46
|
-
* refuses a scene without that operation
|
|
43
|
+
* and by the operations the scene allows that the kind has, each by its name with its parameters
|
|
44
|
+
* as read: undefined where they do not read, and for every operation the kind has where the
|
|
45
|
+
* scene's operations are no map, the scene refused either way. So a goal which reads an
|
|
46
|
+
* operation's member of the state refuses a scene without that operation, and one that needs
|
|
47
|
+
* the operation's parameters reads them; a goal that reads the marks alone ignores them.
|
|
47
48
|
*/
|
|
48
|
-
read(value: unknown, sheet: Sheet, path: string, errors: SceneError[], operations:
|
|
49
|
+
read(value: unknown, sheet: Sheet, path: string, errors: SceneError[], operations: ReadonlyMap<string, unknown>): Options | undefined;
|
|
49
50
|
meets(sheet: Sheet, options: Options, state: State): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* What the goal asks of the scene's state graph where it is not the fewest moves: a count of the
|
|
53
|
+
* states that meet it, or the greatest or least measure of one.
|
|
54
|
+
*/
|
|
55
|
+
asks?(sheet: Sheet, options: Options): Ask<State>;
|
|
50
56
|
}
|
|
51
57
|
/** The operations and goals of the kind, and the version of its schema they make. */
|
|
52
58
|
export interface Registry {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Operation } from "./operation.js";
|
|
2
|
+
import { type Sheet, type State } from "./sheet.js";
|
|
2
3
|
/** A line of the grid: vertical between the columns k and k + 1, or horizontal between the rows. */
|
|
3
|
-
interface PaperLine {
|
|
4
|
+
export interface PaperLine {
|
|
4
5
|
readonly name: string;
|
|
5
6
|
readonly vertical: boolean;
|
|
6
7
|
readonly k: number;
|
|
@@ -8,6 +9,13 @@ interface PaperLine {
|
|
|
8
9
|
export interface PaperOptions {
|
|
9
10
|
/** The sheet's lines, by the order of the moves, read once with the scene. */
|
|
10
11
|
readonly lines: readonly PaperLine[];
|
|
12
|
+
/** The lines drawn on the open sheet, which alone the paper folds along, where the scene gives them. */
|
|
13
|
+
readonly drawn?: readonly PaperLine[];
|
|
11
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* The names of the lines the paper may be folded along at the state, as the scene draws them and
|
|
17
|
+
* the folds have laid them; undefined where the scene draws none, and every line of the sheet is
|
|
18
|
+
* one, or where the state's paper is no folded paper.
|
|
19
|
+
*/
|
|
20
|
+
export declare function foldLines(sheet: Sheet, options: PaperOptions, state: State): ReadonlySet<string> | undefined;
|
|
12
21
|
export declare const paper: Operation<PaperOptions>;
|
|
13
|
-
export {};
|
package/lib/kinds/cells/paper.js
CHANGED
|
@@ -6,8 +6,12 @@
|
|
|
6
6
|
// reads it, and without it two folds that lay the same cells together reach one state. The punch
|
|
7
7
|
// goes through every layer at a place and opens the sheet, which then holds the cells punched, and
|
|
8
8
|
// no move of paper is played after it. Paper knows nothing of the scene's other operations, which
|
|
9
|
-
// play on the sheet as it lies open, as two pieces know nothing of each other.
|
|
10
|
-
|
|
9
|
+
// play on the sheet as it lies open, as two pieces know nothing of each other. A scene may give the
|
|
10
|
+
// lines the paper folds along, drawn on the open sheet as a statement draws them, so that the folds
|
|
11
|
+
// it asks are the only ones: a line drawn on a part laid over moves with it, and is folded along
|
|
12
|
+
// where it lies now. A statement's order of the folds is not kept, since the state keeps no count
|
|
13
|
+
// of them.
|
|
14
|
+
import { array, isObject, object, pointer, string } from "../../cores/json.js";
|
|
11
15
|
import { gridLines, linePattern } from "./line.js";
|
|
12
16
|
import { cellAt, cellName, readCell } from "./sheet.js";
|
|
13
17
|
const sides = { vertical: ["left", "right"], horizontal: ["top", "bottom"] };
|
|
@@ -85,6 +89,43 @@ function folded(sheet, places, line, lay) {
|
|
|
85
89
|
return line.vertical ? row * sheet.cols + mirrored : mirrored * sheet.cols + col;
|
|
86
90
|
});
|
|
87
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* The names of the lines the drawn lines lie along now, the paper folded as the places say. A fold
|
|
94
|
+
* mirrors the part laid over, so two cells side by side across a drawn line lie side by side still,
|
|
95
|
+
* across the line it lies along now, or on one place once the paper was folded along it, which
|
|
96
|
+
* leaves it on the paper's edge. A pair with a hole tells nothing, and the next pair is read.
|
|
97
|
+
*/
|
|
98
|
+
function drawnNow(sheet, places, drawn) {
|
|
99
|
+
const now = new Set();
|
|
100
|
+
for (const line of drawn) {
|
|
101
|
+
for (let i = 0; i < (line.vertical ? sheet.rows : sheet.cols); i++) {
|
|
102
|
+
const [a, b] = line.vertical
|
|
103
|
+
? [i * sheet.cols + line.k - 1, i * sheet.cols + line.k]
|
|
104
|
+
: [(line.k - 1) * sheet.cols + i, line.k * sheet.cols + i];
|
|
105
|
+
const [pa, pb] = [places[a], places[b]];
|
|
106
|
+
if (pa === -1 || pb === -1) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const [ra, rb] = [Math.floor(pa / sheet.cols), Math.floor(pb / sheet.cols)];
|
|
110
|
+
if (pa !== pb) {
|
|
111
|
+
now.add(ra === rb ? `v${Math.max(pa % sheet.cols, pb % sheet.cols)}` : `h${Math.max(ra, rb)}`);
|
|
112
|
+
}
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return now;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The names of the lines the paper may be folded along at the state, as the scene draws them and
|
|
120
|
+
* the folds have laid them; undefined where the scene draws none, and every line of the sheet is
|
|
121
|
+
* one, or where the state's paper is no folded paper.
|
|
122
|
+
*/
|
|
123
|
+
export function foldLines(sheet, options, state) {
|
|
124
|
+
const places = placesOf(sheet, state);
|
|
125
|
+
return options.drawn === undefined || !Array.isArray(places)
|
|
126
|
+
? undefined
|
|
127
|
+
: drawnNow(sheet, places, options.drawn);
|
|
128
|
+
}
|
|
88
129
|
/** The cells that lie on the place: those a punch there goes through. */
|
|
89
130
|
const under = (places, place) => places.flatMap((at, cell) => (at === place ? [cell] : []));
|
|
90
131
|
function foldedJson(sheet, places) {
|
|
@@ -107,8 +148,21 @@ export const paper = {
|
|
|
107
148
|
name: "paper",
|
|
108
149
|
schema: {
|
|
109
150
|
options: {
|
|
110
|
-
description: "Fold the sheet over in layers along lines of its grid, each fold laying the part on one side over the other, no wider than it; then punch a place through every layer, which opens the sheet.
|
|
151
|
+
description: "Fold the sheet over in layers along lines of its grid, each fold laying the part on one side over the other, no wider than it; then punch a place through every layer, which opens the sheet.",
|
|
111
152
|
type: "object",
|
|
153
|
+
properties: {
|
|
154
|
+
lines: {
|
|
155
|
+
description: "The lines the paper may be folded along, as the statement draws them on the open sheet, each moving with a part laid over; every line of the sheet where the scene gives none. Their order is no order of the folds.",
|
|
156
|
+
type: "array",
|
|
157
|
+
items: {
|
|
158
|
+
description: 'A line of the sheet, "v<k>" between the columns k and k + 1, or "h<k>" between the rows k and k + 1, counted from 1.',
|
|
159
|
+
type: "string",
|
|
160
|
+
pattern: linePattern.source,
|
|
161
|
+
},
|
|
162
|
+
minItems: 1,
|
|
163
|
+
uniqueItems: true,
|
|
164
|
+
},
|
|
165
|
+
},
|
|
112
166
|
unevaluatedProperties: false,
|
|
113
167
|
},
|
|
114
168
|
move: {
|
|
@@ -168,16 +222,34 @@ export const paper = {
|
|
|
168
222
|
},
|
|
169
223
|
read(value, sheet, path, errors) {
|
|
170
224
|
const before = errors.length;
|
|
171
|
-
object(value, path, errors, []);
|
|
172
|
-
if (errors.length > before) {
|
|
173
|
-
return undefined;
|
|
174
|
-
}
|
|
225
|
+
const o = object(value, path, errors, [], ["lines"]);
|
|
175
226
|
const lines = gridLines(sheet).map(({ name }) => ({
|
|
176
227
|
name,
|
|
177
228
|
vertical: name.startsWith("v"),
|
|
178
229
|
k: Number(name.slice(1)),
|
|
179
230
|
}));
|
|
180
|
-
|
|
231
|
+
if (o?.lines === undefined) {
|
|
232
|
+
return errors.length > before ? undefined : { lines };
|
|
233
|
+
}
|
|
234
|
+
const linesPath = pointer(path, "lines");
|
|
235
|
+
const given = [];
|
|
236
|
+
array(o.lines, linesPath, errors, 1)?.forEach((item, i) => {
|
|
237
|
+
const at = pointer(linesPath, i);
|
|
238
|
+
const name = string(item, at, errors, linePattern, "a line, v<k> or h<k>");
|
|
239
|
+
if (name === undefined) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (given.includes(name)) {
|
|
243
|
+
errors.push({ path: at, message: "a line named twice" });
|
|
244
|
+
}
|
|
245
|
+
else if (!lines.some((line) => line.name === name)) {
|
|
246
|
+
errors.push({ path: at, message: "no line of the sheet" });
|
|
247
|
+
}
|
|
248
|
+
given.push(name);
|
|
249
|
+
});
|
|
250
|
+
return errors.length > before
|
|
251
|
+
? undefined
|
|
252
|
+
: { lines, drawn: lines.filter((line) => given.includes(line.name)) };
|
|
181
253
|
},
|
|
182
254
|
start: () => ({ folded: {} }),
|
|
183
255
|
play(sheet, options, state, move) {
|
|
@@ -201,6 +273,12 @@ export const paper = {
|
|
|
201
273
|
const line = options.lines.find((l) => l.name === name);
|
|
202
274
|
const lay = typeof fold.lay === "string" ? fold.lay : "";
|
|
203
275
|
const after = line === undefined ? "no-line" : folded(sheet, places, line, lay);
|
|
276
|
+
// A line off the paper is no line wherever the scene draws; one on it that no drawn line
|
|
277
|
+
// lies along now is not the scene's.
|
|
278
|
+
const drawn = options.drawn === undefined ? undefined : drawnNow(sheet, places, options.drawn);
|
|
279
|
+
if (after !== "no-line" && drawn !== undefined && !drawn.has(name ?? "")) {
|
|
280
|
+
return refuse("not-given");
|
|
281
|
+
}
|
|
204
282
|
return typeof after === "string"
|
|
205
283
|
? refuse(after)
|
|
206
284
|
: { ok: true, state: { ...state, paper: foldedJson(sheet, after) } };
|
|
@@ -227,7 +305,11 @@ export const paper = {
|
|
|
227
305
|
return [];
|
|
228
306
|
}
|
|
229
307
|
const moves = [];
|
|
308
|
+
const drawn = options.drawn === undefined ? undefined : drawnNow(sheet, places, options.drawn);
|
|
230
309
|
for (const line of options.lines) {
|
|
310
|
+
if (drawn !== undefined && !drawn.has(line.name)) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
231
313
|
for (const lay of line.vertical ? sides.vertical : sides.horizontal) {
|
|
232
314
|
if (typeof folded(sheet, places, line, lay) !== "string") {
|
|
233
315
|
moves.push({ fold: { line: line.name, lay } });
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Goal, Operation } from "./operation.js";
|
|
2
|
+
import { type Sheet, type State } from "./sheet.js";
|
|
3
|
+
export declare const directions: readonly ["up", "left", "right", "down"];
|
|
4
|
+
export type Direction = (typeof directions)[number];
|
|
5
|
+
export interface PathOptions {
|
|
6
|
+
/** The cell the path starts on, by its index. */
|
|
7
|
+
readonly from: number;
|
|
8
|
+
/** The directions a step may go in. */
|
|
9
|
+
readonly directions: ReadonlySet<Direction>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The cells of the state's path by index, or undefined where it is no path the rules draw: one
|
|
13
|
+
* that starts elsewhere than the scene's cell, or takes a step they refuse. A state reported from
|
|
14
|
+
* outside the kind may hold one, and then no step is drawn from it and no goal is met.
|
|
15
|
+
*/
|
|
16
|
+
export declare function pathOf(sheet: Sheet, options: PathOptions, state: State): readonly number[] | undefined;
|
|
17
|
+
/** Whether a path is closed: back on its first cell. */
|
|
18
|
+
export declare function isClosed(path: readonly number[]): boolean;
|
|
19
|
+
export declare const path: Operation<PathOptions>;
|
|
20
|
+
export interface PathEnd {
|
|
21
|
+
/**
|
|
22
|
+
* The path's own parameters, by which the goal reads the state's path; undefined only where they
|
|
23
|
+
* fail to read, and the scene is refused for them.
|
|
24
|
+
*/
|
|
25
|
+
readonly path?: PathOptions;
|
|
26
|
+
/** The cell the path is to end on, by its index. */
|
|
27
|
+
readonly cell: number;
|
|
28
|
+
/** The cells it is to pass on its way, by their indices. */
|
|
29
|
+
readonly through: readonly number[];
|
|
30
|
+
}
|
|
31
|
+
/** The path ends on the cell, past the cells named; the key is the fewest steps. */
|
|
32
|
+
export declare const pathTo: Goal<PathEnd>;
|
|
33
|
+
/** As path_to, and the key counts the paths: the scene allows the path alone. */
|
|
34
|
+
export declare const pathCount: Goal<PathEnd>;
|
|
35
|
+
/** The path is closed into a route; the key is the most cells a route visits. */
|
|
36
|
+
export declare const pathClosed: Goal<{
|
|
37
|
+
readonly path?: PathOptions;
|
|
38
|
+
}>;
|
|
@@ -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
|
+
};
|
|
@@ -12,12 +12,21 @@ export interface Piece {
|
|
|
12
12
|
*/
|
|
13
13
|
refusal(sheet: Sheet, from: number, to: number): string | undefined;
|
|
14
14
|
}
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* A kind of piece as the registry takes it: its operation, its goal of standing on a cell, and its
|
|
17
|
+
* goal of the cells it reaches, which a count asks.
|
|
18
|
+
*/
|
|
16
19
|
export interface PieceParts {
|
|
17
20
|
/** Its parameters are the cell the piece starts on, by its index. */
|
|
18
21
|
readonly operation: Operation<number>;
|
|
19
22
|
/** Its parameters are the cell the piece is to stand on, by its index. */
|
|
20
23
|
readonly goal: Goal<number>;
|
|
24
|
+
/** Its parameters are read into the cells it reaches within the moves they give. */
|
|
25
|
+
readonly reach: Goal<Reach>;
|
|
26
|
+
}
|
|
27
|
+
/** The cells a piece reaches from its start within the moves a goal gives, by their indices. */
|
|
28
|
+
export interface Reach {
|
|
29
|
+
readonly cells: ReadonlySet<number>;
|
|
21
30
|
}
|
|
22
31
|
/**
|
|
23
32
|
* The operation and the goal of a kind of piece. A move names the cell the piece goes to, and is
|
package/lib/kinds/cells/piece.js
CHANGED
|
@@ -3,8 +3,10 @@
|
|
|
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
|
+
import { integer, object, pointer } from "../../cores/json.js";
|
|
7
|
+
import { cellAt, cellName, maxSide, readCell } from "./sheet.js";
|
|
8
|
+
/** The most moves a reach names: no piece needs more to reach a cell of the largest sheet. */
|
|
9
|
+
const mostMoves = maxSide * maxSide;
|
|
8
10
|
function refuse(reason) {
|
|
9
11
|
return { ok: false, reason };
|
|
10
12
|
}
|
|
@@ -118,5 +120,87 @@ export function piece(kind) {
|
|
|
118
120
|
},
|
|
119
121
|
meets: (sheet, cell, state) => where(sheet, state, member) === cell,
|
|
120
122
|
};
|
|
121
|
-
|
|
123
|
+
// The fewest moves from the start to each cell, by the piece's own rules; -1 where it never goes.
|
|
124
|
+
const fewest = (sheet, start) => {
|
|
125
|
+
const moves = new Array(sheet.rows * sheet.cols).fill(-1);
|
|
126
|
+
moves[start] = 0;
|
|
127
|
+
for (let layer = [start], n = 1; layer.length > 0; n++) {
|
|
128
|
+
const next = [];
|
|
129
|
+
for (const from of layer) {
|
|
130
|
+
for (let to = 0; to < moves.length; to++) {
|
|
131
|
+
if (moves[to] === -1 &&
|
|
132
|
+
!sheet.holes.has(to) &&
|
|
133
|
+
kind.refusal(sheet, from, to) === undefined) {
|
|
134
|
+
moves[to] = n;
|
|
135
|
+
next.push(to);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
layer = next;
|
|
140
|
+
}
|
|
141
|
+
return moves;
|
|
142
|
+
};
|
|
143
|
+
const moveCount = (description) => ({
|
|
144
|
+
description,
|
|
145
|
+
type: "integer",
|
|
146
|
+
minimum: 0,
|
|
147
|
+
maximum: mostMoves,
|
|
148
|
+
});
|
|
149
|
+
const reach = {
|
|
150
|
+
name: `${kind.name}_reach`,
|
|
151
|
+
schema: {
|
|
152
|
+
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, so that its states are the piece's cells.`,
|
|
153
|
+
type: "object",
|
|
154
|
+
properties: {
|
|
155
|
+
least: moveCount("The fewest moves to a cell counted, 0 by default: the cell it starts on."),
|
|
156
|
+
most: moveCount("The most moves to a cell counted; any number by default."),
|
|
157
|
+
},
|
|
158
|
+
unevaluatedProperties: false,
|
|
159
|
+
},
|
|
160
|
+
read(value, sheet, path, errors, operations) {
|
|
161
|
+
const before = errors.length;
|
|
162
|
+
const o = object(value, path, errors, [], ["least", "most"]);
|
|
163
|
+
const least = o?.least === undefined ? 0 : integer(o.least, pointer(path, "least"), errors, 0, mostMoves);
|
|
164
|
+
const most = o?.most === undefined
|
|
165
|
+
? mostMoves
|
|
166
|
+
: integer(o.most, pointer(path, "most"), errors, 0, mostMoves);
|
|
167
|
+
if (least !== undefined && most !== undefined && least > most) {
|
|
168
|
+
errors.push({ path: pointer(path, "least"), message: "no more than the most moves" });
|
|
169
|
+
}
|
|
170
|
+
if (!operations.has(kind.name)) {
|
|
171
|
+
errors.push({
|
|
172
|
+
path,
|
|
173
|
+
message: `the scene allows no operation ${kind.name}, so no piece reaches any cell`,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
const start = operations.get(kind.name);
|
|
177
|
+
// Where the piece's own parameters, or another operation's, do not read, the scene is refused
|
|
178
|
+
// for them alone.
|
|
179
|
+
const others = [...operations]
|
|
180
|
+
.filter(([named, options]) => named !== kind.name && options !== undefined)
|
|
181
|
+
.map(([named]) => named);
|
|
182
|
+
if (typeof start === "number" && others.length > 0) {
|
|
183
|
+
errors.push({
|
|
184
|
+
path,
|
|
185
|
+
message: `the scene allows ${others.join(", ")} beside ${kind.name}: a count of the cells reached counts the piece's states alone`,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (errors.length > before || least === undefined || most === undefined) {
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
const moves = typeof start === "number" ? fewest(sheet, start) : [];
|
|
192
|
+
const cells = new Set();
|
|
193
|
+
moves.forEach((n, cell) => {
|
|
194
|
+
if (n >= least && n <= most)
|
|
195
|
+
cells.add(cell);
|
|
196
|
+
});
|
|
197
|
+
return { cells };
|
|
198
|
+
},
|
|
199
|
+
meets(sheet, goal, state) {
|
|
200
|
+
const cell = where(sheet, state, member);
|
|
201
|
+
return cell !== undefined && goal.cells.has(cell);
|
|
202
|
+
},
|
|
203
|
+
asks: () => ({ key: "count" }),
|
|
204
|
+
};
|
|
205
|
+
return { operation, goal, reach };
|
|
122
206
|
}
|