@altnovatrail/widgets 0.2.0 → 0.3.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.
- package/README.md +1 -1
- package/cores/vector.schema.json +22 -1
- package/kinds/cards/cards.handles.json +109 -0
- package/kinds/cards/cards.schema.json +483 -0
- package/kinds/cards/vectors/any.json +255 -0
- package/kinds/cards/vectors/box.json +325 -0
- package/kinds/cards/vectors/fork-taps.json +305 -0
- package/kinds/cards/vectors/fork.json +254 -0
- package/kinds/cards/vectors/invalid-goal.json +70 -0
- package/kinds/cards/vectors/invalid-ids.json +113 -0
- package/kinds/cards/vectors/invalid-schema.json +88 -0
- package/kinds/cards/vectors/invalid-taps.json +214 -0
- package/kinds/cards/vectors/key.json +264 -0
- package/kinds/cards/vectors/limit.json +274 -0
- package/kinds/cards/vectors/no-goal.json +205 -0
- package/kinds/cards/vectors/none.json +239 -0
- package/kinds/cards/vectors/recog.json +113 -0
- package/kinds/cards/vectors/riddle.json +397 -0
- package/kinds/cards/vectors/solo.json +218 -0
- package/kinds/cells/cells.schema.json +17 -2
- package/kinds/line/line.handles.json +195 -0
- package/kinds/line/line.schema.json +336 -0
- package/kinds/line/vectors/cases.json +213 -0
- package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
- package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
- package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
- package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
- package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
- package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
- package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
- package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
- package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
- package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
- package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
- package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
- package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
- package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
- package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
- package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
- package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
- package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
- package/kinds/line/vectors/invalid-line.json +34 -0
- package/kinds/line/vectors/invalid-ring.json +34 -0
- package/kinds/line/vectors/invalid.json +43 -0
- package/kinds/line/vectors/jump-unreachable.json +104 -0
- package/kinds/line/vectors/limit.json +40 -0
- package/kinds/line/vectors/op-not-in-scene.json +47 -0
- package/kinds/line/vectors/ring-cut.json +104 -0
- package/kinds/line/vectors/ring-every.json +107 -0
- package/kinds/line/vectors/ring-jump.json +135 -0
- package/lib/cores/arith.d.ts +62 -0
- package/lib/cores/arith.js +197 -0
- package/lib/cores/kind.d.ts +31 -3
- package/lib/cores/registry.d.ts +43 -0
- package/lib/cores/registry.js +87 -0
- package/lib/cores/search.d.ts +22 -6
- package/lib/cores/search.js +70 -14
- package/lib/kinds/cards/goal.d.ts +25 -0
- package/lib/kinds/cards/goal.js +72 -0
- package/lib/kinds/cards/index.d.ts +14 -0
- package/lib/kinds/cards/index.js +17 -0
- package/lib/kinds/cards/kind.d.ts +23 -0
- package/lib/kinds/cards/kind.js +101 -0
- package/lib/kinds/cards/parts.d.ts +36 -0
- package/lib/kinds/cards/parts.js +34 -0
- package/lib/kinds/cards/scene.d.ts +102 -0
- package/lib/kinds/cards/scene.js +356 -0
- package/lib/kinds/cards/schema.d.ts +10 -0
- package/lib/kinds/cards/schema.js +285 -0
- package/lib/kinds/cells/kind.js +11 -64
- package/lib/kinds/cells/operation.d.ts +6 -0
- package/lib/kinds/cells/polyomino.d.ts +14 -0
- package/lib/kinds/cells/polyomino.js +100 -24
- package/lib/kinds/cells/registry.js +1 -1
- package/lib/kinds/cells/schema.js +3 -1
- package/lib/kinds/line/cut.d.ts +2 -0
- package/lib/kinds/line/cut.js +63 -0
- package/lib/kinds/line/goals.d.ts +22 -0
- package/lib/kinds/line/goals.js +137 -0
- package/lib/kinds/line/index.d.ts +13 -0
- package/lib/kinds/line/index.js +17 -0
- package/lib/kinds/line/jump.d.ts +10 -0
- package/lib/kinds/line/jump.js +95 -0
- package/lib/kinds/line/kind.d.ts +25 -0
- package/lib/kinds/line/kind.js +125 -0
- package/lib/kinds/line/line.d.ts +63 -0
- package/lib/kinds/line/line.js +157 -0
- package/lib/kinds/line/operation.d.ts +69 -0
- package/lib/kinds/line/operation.js +1 -0
- package/lib/kinds/line/posts.d.ts +11 -0
- package/lib/kinds/line/posts.js +85 -0
- package/lib/kinds/line/registry.d.ts +2 -0
- package/lib/kinds/line/registry.js +11 -0
- package/lib/kinds/line/schema.d.ts +4 -0
- package/lib/kinds/line/schema.js +159 -0
- package/package.json +16 -4
- package/cores/handles.schema.json +0 -63
package/lib/kinds/cells/kind.js
CHANGED
|
@@ -1,25 +1,12 @@
|
|
|
1
1
|
// The kind cells, assembled from a registry of operations and goals: a scene is a sheet, the
|
|
2
2
|
// operations it allows with their parameters, and at most one goal. A move names one operation,
|
|
3
3
|
// which plays it by its own rules; the kind only reads the sheet and dispatches.
|
|
4
|
-
import {
|
|
4
|
+
import { map, object, pointer } from "../../cores/json.js";
|
|
5
|
+
import { checkRegistry, dispatch, inRegistryOrder, readGoal, readPart, takeName, } from "../../cores/registry.js";
|
|
5
6
|
import { marksJson, readSheet } from "./sheet.js";
|
|
6
|
-
const version = /^[1-9][0-9]*\.(0|[1-9][0-9]*)$/;
|
|
7
|
-
const name = /^[a-z][a-z0-9_]*$/;
|
|
8
|
-
function refuse(reason) {
|
|
9
|
-
return { ok: false, reason };
|
|
10
|
-
}
|
|
11
7
|
/** Throws where the registry could not make a kind: a name given twice, or a state member shared. */
|
|
12
8
|
function check(registry) {
|
|
13
|
-
|
|
14
|
-
throw new Error(`the version of cells is "major.minor": ${registry.version}`);
|
|
15
|
-
}
|
|
16
|
-
const names = new Set();
|
|
17
|
-
for (const part of [...registry.operations, ...registry.goals]) {
|
|
18
|
-
if (!name.test(part.name) || names.has(part.name)) {
|
|
19
|
-
throw new Error(`an operation's or a goal's name is new and in snake_case: ${part.name}`);
|
|
20
|
-
}
|
|
21
|
-
names.add(part.name);
|
|
22
|
-
}
|
|
9
|
+
checkRegistry("cells", registry.version, [...registry.operations, ...registry.goals]);
|
|
23
10
|
const members = new Set(["marks"]);
|
|
24
11
|
for (const op of registry.operations) {
|
|
25
12
|
const state = op.schema.state;
|
|
@@ -30,11 +17,8 @@ function check(registry) {
|
|
|
30
17
|
throw new Error(`the operation ${op.name} keeps a second member of the state and starts it, or neither`);
|
|
31
18
|
}
|
|
32
19
|
for (const member of [state?.member, state?.beside?.member]) {
|
|
33
|
-
if (member !== undefined && (!name.test(member) || members.has(member))) {
|
|
34
|
-
throw new Error(`a member of the state is new and in snake_case: ${member}`);
|
|
35
|
-
}
|
|
36
20
|
if (member !== undefined) {
|
|
37
|
-
members
|
|
21
|
+
takeName(members, member, "a member of the state");
|
|
38
22
|
}
|
|
39
23
|
}
|
|
40
24
|
}
|
|
@@ -48,15 +32,6 @@ export function cellsKind(registry, limit, workLimit = Number.MAX_SAFE_INTEGER)
|
|
|
48
32
|
check(registry);
|
|
49
33
|
const operations = new Map(registry.operations.map((op) => [op.name, op]));
|
|
50
34
|
const goals = new Map(registry.goals.map((goal) => [goal.name, goal]));
|
|
51
|
-
// Reads one operation's or goal's parameters; the result undefined is an error, named or not.
|
|
52
|
-
const part = (read, value, sheet, path, errors) => {
|
|
53
|
-
const before = errors.length;
|
|
54
|
-
const options = read(value, sheet, path, errors);
|
|
55
|
-
if (options === undefined && errors.length === before) {
|
|
56
|
-
errors.push({ path, message: "parameters the kind cannot read" });
|
|
57
|
-
}
|
|
58
|
-
return options === undefined ? undefined : { options };
|
|
59
|
-
};
|
|
60
35
|
return {
|
|
61
36
|
limit,
|
|
62
37
|
workLimit,
|
|
@@ -83,34 +58,16 @@ export function cellsKind(registry, limit, workLimit = Number.MAX_SAFE_INTEGER)
|
|
|
83
58
|
}
|
|
84
59
|
allows.set(named, undefined);
|
|
85
60
|
if (sheet !== undefined) {
|
|
86
|
-
const read =
|
|
61
|
+
const read = readPart((v, p, e) => op.read(v, sheet, p, e), value, at, errors);
|
|
87
62
|
if (read !== undefined) {
|
|
88
63
|
chosen.set(named, read.options);
|
|
89
64
|
allows.set(named, read.options);
|
|
90
65
|
}
|
|
91
66
|
}
|
|
92
67
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const entries = Object.entries(goalMap);
|
|
97
|
-
const [named, value] = entries[0] ?? ["", undefined];
|
|
98
|
-
const found = goals.get(named);
|
|
99
|
-
if (entries.length !== 1) {
|
|
100
|
-
errors.push({ path: "/goal", message: "one goal, by its name" });
|
|
101
|
-
}
|
|
102
|
-
else if (found === undefined) {
|
|
103
|
-
errors.push({
|
|
104
|
-
path: pointer("/goal", named),
|
|
105
|
-
message: "no goal of the kind has this name",
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
else if (sheet !== undefined) {
|
|
109
|
-
const reading = (v, s, p, e) => found.read(v, s, p, e, allows);
|
|
110
|
-
const read = part(reading, value, sheet, pointer("/goal", named), errors);
|
|
111
|
-
goal = read === undefined ? null : { goal: found, options: read.options };
|
|
112
|
-
}
|
|
113
|
-
}
|
|
68
|
+
const goal = readGoal(o.goal, goals, errors, sheet === undefined
|
|
69
|
+
? undefined
|
|
70
|
+
: (found, v, p, e) => found.read(v, sheet, p, e, allows));
|
|
114
71
|
if (errors.length > 0 || sheet === undefined) {
|
|
115
72
|
return { ok: false, errors };
|
|
116
73
|
}
|
|
@@ -118,9 +75,7 @@ export function cellsKind(registry, limit, workLimit = Number.MAX_SAFE_INTEGER)
|
|
|
118
75
|
ok: true,
|
|
119
76
|
scene: {
|
|
120
77
|
sheet,
|
|
121
|
-
operations: registry.operations
|
|
122
|
-
.filter((op) => chosen.has(op.name))
|
|
123
|
-
.map((op) => ({ op, options: chosen.get(op.name) })),
|
|
78
|
+
operations: inRegistryOrder(registry.operations, chosen),
|
|
124
79
|
goal,
|
|
125
80
|
},
|
|
126
81
|
};
|
|
@@ -143,20 +98,12 @@ export function cellsKind(registry, limit, workLimit = Number.MAX_SAFE_INTEGER)
|
|
|
143
98
|
},
|
|
144
99
|
// The kind's moves draw nothing at random, so it reads nothing of the version or the seed.
|
|
145
100
|
play(scene, state, move) {
|
|
146
|
-
|
|
147
|
-
if (named.length !== 1) {
|
|
148
|
-
return refuse("not-a-move");
|
|
149
|
-
}
|
|
150
|
-
const opName = named[0];
|
|
151
|
-
const chosen = scene.operations.find(({ op }) => op.name === opName);
|
|
152
|
-
if (chosen === undefined) {
|
|
153
|
-
return refuse(operations.has(opName) ? "op-not-in-scene" : "unknown-op");
|
|
154
|
-
}
|
|
155
|
-
return chosen.op.play(scene.sheet, chosen.options, state, move[opName]);
|
|
101
|
+
return dispatch(scene.operations, operations, move, (op, options, held) => op.play(scene.sheet, options, state, held));
|
|
156
102
|
},
|
|
157
103
|
moves(scene, state) {
|
|
158
104
|
return scene.operations.flatMap(({ op, options }) => op.moves(scene.sheet, options, state).map((move) => ({ [op.name]: move })));
|
|
159
105
|
},
|
|
106
|
+
leastMoves: (scene, state) => scene.operations.reduce((sum, { op, options }) => sum + (op.leastMoves?.(scene.sheet, options, state) ?? 0), 0),
|
|
160
107
|
implied: (scene, state) => scene.operations.reduce((sum, { op, options }) => sum + (op.implied?.(scene.sheet, options, state) ?? 0), 0),
|
|
161
108
|
hasGoal: (scene) => scene.goal !== null,
|
|
162
109
|
meets: (scene, state) => scene.goal?.goal.meets(scene.sheet, scene.goal.options, state) ?? false,
|
|
@@ -41,6 +41,12 @@ export interface Operation<Options = unknown> {
|
|
|
41
41
|
play(sheet: Sheet, options: Options, state: State, move: Json): Played<State>;
|
|
42
42
|
/** What every move of the operation to try from the state holds, in a fixed order. */
|
|
43
43
|
moves(sheet: Sheet, options: Options, state: State): readonly Json[];
|
|
44
|
+
/**
|
|
45
|
+
* At least how many moves the operation lists from the state, counted without listing them, as
|
|
46
|
+
* the kind's leastMoves has it: an operation one of whose states may have tens of thousands of
|
|
47
|
+
* moves gives it, never more than moves lists.
|
|
48
|
+
*/
|
|
49
|
+
leastMoves?(sheet: Sheet, options: Options, state: State): number;
|
|
44
50
|
/**
|
|
45
51
|
* What a move from the state reads and writes of the operation's member that the member leaves
|
|
46
52
|
* out, in characters as the state's canonical key would write it, as the kind's implied has it.
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import type { Goal, Operation } from "./operation.js";
|
|
2
2
|
import { type Sheet, type State } from "./sheet.js";
|
|
3
|
+
/** A cell of a shape on its own grid: its row and column, counted from 0. */
|
|
4
|
+
type Square = readonly [number, number];
|
|
3
5
|
/** The most cells of the shapes a count of cells gives: every shape of so many is listed. */
|
|
4
6
|
export declare const mostCells = 6;
|
|
5
7
|
export interface PolyominoOptions {
|
|
6
8
|
/** How many figures may lie on the sheet at once. */
|
|
7
9
|
readonly pieces: number;
|
|
10
|
+
/** Every way a figure may lie, each once, its squares at its grid's corner in reading order. */
|
|
11
|
+
readonly forms: readonly (readonly Square[])[];
|
|
12
|
+
/** How many places on the sheet off its holes the forms lie on: the first state's moves. */
|
|
13
|
+
readonly fits: number;
|
|
14
|
+
}
|
|
15
|
+
/** Every place of the scene's figures, made the first time a state needs them. */
|
|
16
|
+
interface Places {
|
|
8
17
|
/**
|
|
9
18
|
* Every place a figure may lie, each its cells by index in reading order, ordered by those cells;
|
|
10
19
|
* a move lays one of them.
|
|
@@ -13,6 +22,11 @@ export interface PolyominoOptions {
|
|
|
13
22
|
/** The index of each place among placings, by its cells' indices joined by commas. */
|
|
14
23
|
readonly placed: ReadonlyMap<string, number>;
|
|
15
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* The places of the scene's figures, each its cells by index, as a state's figures name them. The
|
|
27
|
+
* sheet is the one the options were read by: the places are kept by the options alone.
|
|
28
|
+
*/
|
|
29
|
+
export declare function placesOf(sheet: Sheet, options: PolyominoOptions): Places;
|
|
16
30
|
/** The figures a state has laid, by their places, and the cells they cover. */
|
|
17
31
|
export interface Laid {
|
|
18
32
|
readonly figures: readonly number[];
|
|
@@ -81,19 +81,48 @@ function everyShape(n) {
|
|
|
81
81
|
}
|
|
82
82
|
return shapes;
|
|
83
83
|
}
|
|
84
|
+
/** The rows and the columns a form spans. */
|
|
85
|
+
function spans(form) {
|
|
86
|
+
return [Math.max(...form.map(([r]) => r)) + 1, Math.max(...form.map(([, c]) => c)) + 1];
|
|
87
|
+
}
|
|
88
|
+
/** Each place on the sheet where a form lies, its cells by index, holes and all. */
|
|
89
|
+
function* placesOn(sheet, form) {
|
|
90
|
+
const [height, width] = spans(form);
|
|
91
|
+
for (let top = 0; top + height <= sheet.rows; top++) {
|
|
92
|
+
for (let left = 0; left + width <= sheet.cols; left++) {
|
|
93
|
+
yield form.map(([r, c]) => (top + r) * sheet.cols + left + c);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* How many places on the sheet off the holes the forms lie on, without making them: two forms,
|
|
99
|
+
* each at its grid's corner, never lie on the same cells.
|
|
100
|
+
*/
|
|
101
|
+
function fitsOf(sheet, forms) {
|
|
102
|
+
let fits = 0;
|
|
103
|
+
for (const form of forms) {
|
|
104
|
+
const [height, width] = spans(form);
|
|
105
|
+
if (sheet.holes.size === 0) {
|
|
106
|
+
fits += Math.max(0, sheet.rows - height + 1) * Math.max(0, sheet.cols - width + 1);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
for (let top = 0; top + height <= sheet.rows; top++) {
|
|
110
|
+
for (let left = 0; left + width <= sheet.cols; left++) {
|
|
111
|
+
if (!form.some(([r, c]) => sheet.holes.has((top + r) * sheet.cols + left + c)))
|
|
112
|
+
fits++;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return fits;
|
|
117
|
+
}
|
|
84
118
|
/** Every place on the sheet off the holes where one of the forms lies, each once, in order. */
|
|
85
119
|
function placingsOf(sheet, all) {
|
|
86
120
|
const found = new Map();
|
|
87
121
|
for (const form of all) {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
}
|
|
122
|
+
for (const cells of placesOn(sheet, form)) {
|
|
123
|
+
if (cells.some((cell) => sheet.holes.has(cell)))
|
|
124
|
+
continue;
|
|
125
|
+
found.set(cells.join(","), cells);
|
|
97
126
|
}
|
|
98
127
|
}
|
|
99
128
|
return [...found.values()].sort((a, b) => {
|
|
@@ -104,6 +133,40 @@ function placingsOf(sheet, all) {
|
|
|
104
133
|
return a.length - b.length;
|
|
105
134
|
});
|
|
106
135
|
}
|
|
136
|
+
// The places of each scene's figures, by its parameters. They are made the first time a state needs
|
|
137
|
+
// them, not as the scene is read: on a large sheet they are a hundred thousand and more, and a scene
|
|
138
|
+
// whose first state's moves pass the limit of work stops on their count alone.
|
|
139
|
+
const placesByOptions = new WeakMap();
|
|
140
|
+
/**
|
|
141
|
+
* The places of the scene's figures, each its cells by index, as a state's figures name them. The
|
|
142
|
+
* sheet is the one the options were read by: the places are kept by the options alone.
|
|
143
|
+
*/
|
|
144
|
+
export function placesOf(sheet, options) {
|
|
145
|
+
let places = placesByOptions.get(options);
|
|
146
|
+
if (places === undefined) {
|
|
147
|
+
const placings = placingsOf(sheet, options.forms);
|
|
148
|
+
places = { placings, placed: new Map(placings.map((placing, i) => [placing.join(","), i])) };
|
|
149
|
+
placesByOptions.set(options, places);
|
|
150
|
+
}
|
|
151
|
+
return places;
|
|
152
|
+
}
|
|
153
|
+
// The places still free in each state, by its figures read, which the count of its moves and their
|
|
154
|
+
// list share.
|
|
155
|
+
const freeByLaid = new WeakMap();
|
|
156
|
+
/** The places of the state's figures that no figure laid covers a cell of, by their indices. */
|
|
157
|
+
function freeOf(sheet, options, laid) {
|
|
158
|
+
let free = freeByLaid.get(laid);
|
|
159
|
+
if (free === undefined) {
|
|
160
|
+
const found = [];
|
|
161
|
+
placesOf(sheet, options).placings.forEach((placing, i) => {
|
|
162
|
+
if (placing.every((cell) => !laid.covered.has(cell)))
|
|
163
|
+
found.push(i);
|
|
164
|
+
});
|
|
165
|
+
free = found;
|
|
166
|
+
freeByLaid.set(laid, free);
|
|
167
|
+
}
|
|
168
|
+
return free;
|
|
169
|
+
}
|
|
107
170
|
// The figures of each state read so far, by the array of its member "polyomino", for each scene's
|
|
108
171
|
// parameters: null for a member the rules never make.
|
|
109
172
|
const laidByOptions = new WeakMap();
|
|
@@ -133,6 +196,9 @@ export function laidOf(sheet, options, state) {
|
|
|
133
196
|
function readLaid(sheet, options, value) {
|
|
134
197
|
if (value.length > options.pieces)
|
|
135
198
|
return null;
|
|
199
|
+
if (value.length === 0)
|
|
200
|
+
return { figures: [], covered: new Set() };
|
|
201
|
+
const { placed } = placesOf(sheet, options);
|
|
136
202
|
const figures = [];
|
|
137
203
|
const covered = new Set();
|
|
138
204
|
let last = -1;
|
|
@@ -140,7 +206,7 @@ function readLaid(sheet, options, value) {
|
|
|
140
206
|
if (!Array.isArray(figure))
|
|
141
207
|
return null;
|
|
142
208
|
const cells = figure.map((name) => typeof name === "string" ? cellAt(sheet, name) : undefined);
|
|
143
|
-
const placing =
|
|
209
|
+
const placing = placed.get(cells.join(","));
|
|
144
210
|
const first = cells[0];
|
|
145
211
|
if (placing === undefined || first === undefined || first <= last)
|
|
146
212
|
return null;
|
|
@@ -156,9 +222,10 @@ function readLaid(sheet, options, value) {
|
|
|
156
222
|
}
|
|
157
223
|
/** The member of the state with the figures given, by their places. */
|
|
158
224
|
function laidJson(sheet, options, figures) {
|
|
225
|
+
const { placings } = placesOf(sheet, options);
|
|
159
226
|
return [...figures]
|
|
160
227
|
.sort((a, b) => a - b)
|
|
161
|
-
.map((placing) =>
|
|
228
|
+
.map((placing) => placings[placing].map((cell) => cellName(sheet, cell)));
|
|
162
229
|
}
|
|
163
230
|
function refuse(reason) {
|
|
164
231
|
return { ok: false, reason };
|
|
@@ -308,16 +375,14 @@ export const polyomino = {
|
|
|
308
375
|
const all = cells === undefined
|
|
309
376
|
? shapes.flatMap((shape) => forms(shape, flags.turn, flags.flip))
|
|
310
377
|
: everyShape(cells);
|
|
311
|
-
|
|
312
|
-
|
|
378
|
+
// Shapes given may turn into one another, so a form is kept once.
|
|
379
|
+
const once = [...new Map(all.map((form) => [formKey(form), form])).values()];
|
|
380
|
+
const fits = fitsOf(sheet, once);
|
|
381
|
+
if (fits === 0) {
|
|
313
382
|
errors.push({ path, message: "no figure fits on the sheet off its holes" });
|
|
314
383
|
return undefined;
|
|
315
384
|
}
|
|
316
|
-
return {
|
|
317
|
-
pieces,
|
|
318
|
-
placings,
|
|
319
|
-
placed: new Map(placings.map((placing, i) => [placing.join(","), i])),
|
|
320
|
-
};
|
|
385
|
+
return { pieces, forms: once, fits };
|
|
321
386
|
},
|
|
322
387
|
start: () => [],
|
|
323
388
|
play(sheet, options, state, move) {
|
|
@@ -333,7 +398,8 @@ export const polyomino = {
|
|
|
333
398
|
const laid = laidOf(sheet, options, state);
|
|
334
399
|
if (laid === undefined)
|
|
335
400
|
return refuse("no-figures");
|
|
336
|
-
const
|
|
401
|
+
const { placings } = placesOf(sheet, options);
|
|
402
|
+
const figures = laid.figures.filter((placing) => !placings[placing].includes(cell));
|
|
337
403
|
if (figures.length === laid.figures.length)
|
|
338
404
|
return refuse("not-laid");
|
|
339
405
|
return { ok: true, state: { ...state, polyomino: laidJson(sheet, options, figures) } };
|
|
@@ -350,7 +416,7 @@ export const polyomino = {
|
|
|
350
416
|
return refuse("all-laid");
|
|
351
417
|
if (cells.some((cell) => sheet.holes.has(cell)))
|
|
352
418
|
return refuse("hole");
|
|
353
|
-
const placing = options.placed.get([...cells].sort((a, b) => a - b).join(","));
|
|
419
|
+
const placing = placesOf(sheet, options).placed.get([...cells].sort((a, b) => a - b).join(","));
|
|
354
420
|
if (placing === undefined || new Set(cells).size !== cells.length)
|
|
355
421
|
return refuse("not-a-shape");
|
|
356
422
|
if (cells.some((cell) => laid.covered.has(cell)))
|
|
@@ -366,9 +432,19 @@ export const polyomino = {
|
|
|
366
432
|
if (laid === undefined || laid.figures.length >= options.pieces) {
|
|
367
433
|
return [];
|
|
368
434
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
435
|
+
const { placings } = placesOf(sheet, options);
|
|
436
|
+
return freeOf(sheet, options, laid).map((i) => ({
|
|
437
|
+
lay: { cells: placings[i].map((cell) => cellName(sheet, cell)) },
|
|
438
|
+
}));
|
|
439
|
+
},
|
|
440
|
+
// The first state's places are counted from the forms, so that a scene whose first state passes
|
|
441
|
+
// the limit of work stops before they are made.
|
|
442
|
+
leastMoves(sheet, options, state) {
|
|
443
|
+
const laid = laidOf(sheet, options, state);
|
|
444
|
+
if (laid === undefined || laid.figures.length >= options.pieces) {
|
|
445
|
+
return 0;
|
|
446
|
+
}
|
|
447
|
+
return laid.figures.length === 0 ? options.fits : freeOf(sheet, options, laid).length;
|
|
372
448
|
},
|
|
373
449
|
};
|
|
374
450
|
const said = {
|
|
@@ -453,7 +529,7 @@ function sameShape(sheet, goal, state) {
|
|
|
453
529
|
if (goal.polyomino === undefined || figure === undefined) {
|
|
454
530
|
return [state];
|
|
455
531
|
}
|
|
456
|
-
const squares = goal.polyomino.placings[figure].map((cell) => [Math.floor(cell / sheet.cols), cell % sheet.cols]);
|
|
532
|
+
const squares = placesOf(sheet, goal.polyomino).placings[figure].map((cell) => [Math.floor(cell / sheet.cols), cell % sheet.cols]);
|
|
457
533
|
return placingsOf(sheet, forms(squares, true, true)).map((cells) => ({
|
|
458
534
|
...state,
|
|
459
535
|
polyomino: [cells.map((cell) => cellName(sheet, cell))],
|
|
@@ -17,7 +17,7 @@ import { sieve, sieveLeast } from "./sieve.js";
|
|
|
17
17
|
import { step, stepOn, stepReach } from "./step.js";
|
|
18
18
|
import { mirrorSymmetric, quarterTurnSymmetric } from "./symmetric.js";
|
|
19
19
|
export const registry = {
|
|
20
|
-
version: "3.
|
|
20
|
+
version: "3.2",
|
|
21
21
|
operations: [fold, paint, step, knight, bishop, paper, counter, sieve, path, cut, polyomino],
|
|
22
22
|
goals: [
|
|
23
23
|
allPainted,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { searchKeySchema } from "../../cores/search.js";
|
|
1
2
|
import { cellPattern, colors, maxFirst, maxSide } from "./sheet.js";
|
|
2
3
|
import { nodePattern, sidePattern } from "./side.js";
|
|
3
4
|
/** The $id of the kind's schema at a version: a name, which need not resolve. */
|
|
@@ -40,7 +41,7 @@ export function cellsSchema(registry) {
|
|
|
40
41
|
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
41
42
|
$id: schemaId(registry.version),
|
|
42
43
|
title: "A scene of the widget kind cells",
|
|
43
|
-
description: "A sheet of cells, the operations a learner may play on it with their parameters, and at most one goal a state may meet. Under $defs, a move and a state of the kind.",
|
|
44
|
+
description: "A sheet of cells, the operations a learner may play on it with their parameters, and at most one goal a state may meet. Under $defs, a move and a state of the kind, and the form of a scene's key.",
|
|
44
45
|
type: "object",
|
|
45
46
|
properties: {
|
|
46
47
|
sheet: {
|
|
@@ -174,6 +175,7 @@ export function cellsSchema(registry) {
|
|
|
174
175
|
properties: state,
|
|
175
176
|
required: ["marks"],
|
|
176
177
|
},
|
|
178
|
+
key: searchKeySchema,
|
|
177
179
|
},
|
|
178
180
|
};
|
|
179
181
|
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { lastPlace, moveAt, placesOf, withPlace } from "./line.js";
|
|
2
|
+
import { noOptions, placeMove, readNone } from "./posts.js";
|
|
3
|
+
const isEnd = (line, place) => !line.closed && (place === line.first || place === lastPlace(line));
|
|
4
|
+
// The cuts at places that may be cut alone: a state reported from outside the kind may name an end.
|
|
5
|
+
const cuts = (line, state) => placesOf(line, state, "cuts").filter((place) => !isEnd(line, place));
|
|
6
|
+
function refuse(reason) {
|
|
7
|
+
return { ok: false, reason };
|
|
8
|
+
}
|
|
9
|
+
export const cut = {
|
|
10
|
+
name: "cut",
|
|
11
|
+
schema: {
|
|
12
|
+
options: noOptions("Cut the line, a log or a ribbon from its first place to its last, at any inner place, or a ring at any place; no parameters."),
|
|
13
|
+
move: placeMove("Cut at a place: never at an end of an open line."),
|
|
14
|
+
state: {
|
|
15
|
+
member: "cuts",
|
|
16
|
+
schema: {
|
|
17
|
+
description: "The places cut, by their numbers, in ascending order.",
|
|
18
|
+
type: "array",
|
|
19
|
+
items: { description: "A place cut.", $ref: "#/$defs/place" },
|
|
20
|
+
uniqueItems: true,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
read: (value, _line, path, errors) => readNone(value, path, errors),
|
|
25
|
+
start: () => [],
|
|
26
|
+
play(line, _options, state, move) {
|
|
27
|
+
const at = moveAt(line, move);
|
|
28
|
+
if (at === undefined) {
|
|
29
|
+
return refuse("no-place");
|
|
30
|
+
}
|
|
31
|
+
if (isEnd(line, at)) {
|
|
32
|
+
return refuse("end");
|
|
33
|
+
}
|
|
34
|
+
const made = cuts(line, state);
|
|
35
|
+
if (made.includes(at)) {
|
|
36
|
+
return refuse("cut");
|
|
37
|
+
}
|
|
38
|
+
return { ok: true, state: { ...state, cuts: withPlace(made, at) } };
|
|
39
|
+
},
|
|
40
|
+
moves(line, _options, state) {
|
|
41
|
+
const made = new Set(cuts(line, state));
|
|
42
|
+
const moves = [];
|
|
43
|
+
for (let place = line.first; place <= lastPlace(line); place++) {
|
|
44
|
+
if (!made.has(place) && !isEnd(line, place)) {
|
|
45
|
+
moves.push({ at: place });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return moves;
|
|
49
|
+
},
|
|
50
|
+
counters: {
|
|
51
|
+
cuts: {
|
|
52
|
+
description: "The cuts made.",
|
|
53
|
+
value: (line, _options, state) => cuts(line, state).length,
|
|
54
|
+
},
|
|
55
|
+
pieces: {
|
|
56
|
+
description: "The pieces the cuts leave: on an open line one more than the cuts; on a ring as many as the cuts, and one where there is none.",
|
|
57
|
+
value(line, _options, state) {
|
|
58
|
+
const made = cuts(line, state).length;
|
|
59
|
+
return line.closed && made > 0 ? made : made + 1;
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Count, Goal } from "./operation.js";
|
|
2
|
+
/** The greatest value a goal asks a counter to have. */
|
|
3
|
+
export declare const maxValue = 1000000000;
|
|
4
|
+
export interface CountersOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The counters a state meets the goal by, each with the value it is to have and its name, which
|
|
7
|
+
* the drawing shows the value beside.
|
|
8
|
+
*/
|
|
9
|
+
readonly is: readonly (readonly [Count, number, string])[];
|
|
10
|
+
/** The counter whose greatest or least value the goal asks, by its name too, where it asks one. */
|
|
11
|
+
readonly asks?: {
|
|
12
|
+
readonly key: "max" | "min";
|
|
13
|
+
readonly count: Count;
|
|
14
|
+
readonly name: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export declare const counters: Goal<CountersOptions>;
|
|
18
|
+
export interface Stretch {
|
|
19
|
+
readonly from: number;
|
|
20
|
+
readonly to: number;
|
|
21
|
+
}
|
|
22
|
+
export declare const every: Goal<Stretch>;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// The goals of the kind line. A statement of counting gaps asks a number of a built line — how many
|
|
2
|
+
// gaps between six trees, how long a fence of eight posts — or how many moves build it — how many
|
|
3
|
+
// cuts make four pieces, how many jumps reach the top —, so one goal names counters: the values a
|
|
4
|
+
// state's counters are to have, and, where it asks a number of the state rather than the fewest
|
|
5
|
+
// moves, the counter whose greatest or least value it asks. The other asks a post on every place of
|
|
6
|
+
// a stretch, as pages read from one to another, both included, or bushes round a flowerbed.
|
|
7
|
+
import { divideWithRemainder } from "../../cores/arith.js";
|
|
8
|
+
import { integer, isObject, map, object, pointer, string } from "../../cores/json.js";
|
|
9
|
+
import { lastPlace, placeOn, placesOf, readPlace } from "./line.js";
|
|
10
|
+
/** The greatest value a goal asks a counter to have. */
|
|
11
|
+
export const maxValue = 1_000_000_000;
|
|
12
|
+
const counterName = (description) => ({ description, $ref: "#/$defs/counter" });
|
|
13
|
+
export const counters = {
|
|
14
|
+
name: "counters",
|
|
15
|
+
schema: {
|
|
16
|
+
description: "A state whose counters have the values given; the key is the fewest moves to one, or, where max or min names a counter, its greatest or least value over such states. At least one of is, max and min; never both max and min.",
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: {
|
|
19
|
+
is: {
|
|
20
|
+
description: "The counters, each by its name, with the value it is to have.",
|
|
21
|
+
type: "object",
|
|
22
|
+
propertyNames: { $ref: "#/$defs/counter" },
|
|
23
|
+
additionalProperties: {
|
|
24
|
+
description: "The value the counter is to have.",
|
|
25
|
+
type: "integer",
|
|
26
|
+
minimum: 0,
|
|
27
|
+
maximum: maxValue,
|
|
28
|
+
},
|
|
29
|
+
minProperties: 1,
|
|
30
|
+
},
|
|
31
|
+
max: counterName("The counter whose greatest value the key is."),
|
|
32
|
+
min: counterName("The counter whose least value the key is."),
|
|
33
|
+
},
|
|
34
|
+
minProperties: 1,
|
|
35
|
+
// Never both max and min. Both are named again here: a validator in strict mode, as Ajv, takes
|
|
36
|
+
// a required member only where the same schema names it.
|
|
37
|
+
not: {
|
|
38
|
+
properties: {
|
|
39
|
+
max: { description: "Max, never beside min." },
|
|
40
|
+
min: { description: "Min, never beside max." },
|
|
41
|
+
},
|
|
42
|
+
required: ["max", "min"],
|
|
43
|
+
},
|
|
44
|
+
unevaluatedProperties: false,
|
|
45
|
+
},
|
|
46
|
+
read(value, _line, path, errors, known) {
|
|
47
|
+
const before = errors.length;
|
|
48
|
+
const o = object(value, path, errors, [], ["is", "max", "min"]);
|
|
49
|
+
if (o === undefined) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
const counter = (name, at) => {
|
|
53
|
+
const named = string(name, at, errors, /^[a-z][a-z0-9_]*$/, "a counter's name");
|
|
54
|
+
if (named !== undefined && !known.has(named)) {
|
|
55
|
+
errors.push({ path: at, message: "no counter of the scene has this name" });
|
|
56
|
+
}
|
|
57
|
+
return named === undefined ? undefined : known.get(named);
|
|
58
|
+
};
|
|
59
|
+
const is = [];
|
|
60
|
+
if (o.is !== undefined) {
|
|
61
|
+
const isPath = pointer(path, "is");
|
|
62
|
+
for (const [name, item] of Object.entries(map(o.is, isPath, errors) ?? {})) {
|
|
63
|
+
const at = pointer(isPath, name);
|
|
64
|
+
const count = counter(name, at);
|
|
65
|
+
const wanted = integer(item, at, errors, 0, maxValue);
|
|
66
|
+
if (count !== undefined && wanted !== undefined) {
|
|
67
|
+
is.push([count, wanted, name]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const max = o.max === undefined ? undefined : counter(o.max, pointer(path, "max"));
|
|
72
|
+
const min = o.min === undefined ? undefined : counter(o.min, pointer(path, "min"));
|
|
73
|
+
if (o.max !== undefined && o.min !== undefined) {
|
|
74
|
+
errors.push({ path, message: "max or min, never both" });
|
|
75
|
+
}
|
|
76
|
+
const key = o.max !== undefined ? "max" : "min";
|
|
77
|
+
const measured = key === "max" ? max : min;
|
|
78
|
+
if (o[key] === undefined && (!isObject(o.is) || Object.keys(o.is).length === 0)) {
|
|
79
|
+
errors.push({ path, message: "a counter's value to have, or a counter to measure" });
|
|
80
|
+
}
|
|
81
|
+
if (errors.length > before) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
return measured === undefined
|
|
85
|
+
? { is }
|
|
86
|
+
: { is, asks: { key, count: measured, name: String(o[key]) } };
|
|
87
|
+
},
|
|
88
|
+
meets: (_line, goal, state) => goal.is.every(([count, wanted]) => count(state) === wanted),
|
|
89
|
+
asks: (_line, goal) => goal.asks === undefined ? { key: "fewest" } : { key: goal.asks.key, measure: goal.asks.count },
|
|
90
|
+
};
|
|
91
|
+
export const every = {
|
|
92
|
+
name: "every",
|
|
93
|
+
schema: {
|
|
94
|
+
description: "A post on every place from one to another, both included; the whole line where neither is given.",
|
|
95
|
+
type: "object",
|
|
96
|
+
properties: {
|
|
97
|
+
from: {
|
|
98
|
+
description: "The first place of the stretch; the line's first where left out.",
|
|
99
|
+
$ref: "#/$defs/place",
|
|
100
|
+
},
|
|
101
|
+
to: {
|
|
102
|
+
description: "The last place of the stretch, never before from on an open line, and on a ring past the last place round to it where before from; the line's last where left out.",
|
|
103
|
+
$ref: "#/$defs/place",
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
unevaluatedProperties: false,
|
|
107
|
+
},
|
|
108
|
+
read(value, line, path, errors) {
|
|
109
|
+
const before = errors.length;
|
|
110
|
+
const o = object(value, path, errors, [], ["from", "to"]);
|
|
111
|
+
if (o === undefined) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
const from = o.from === undefined ? line.first : readPlace(line, o.from, pointer(path, "from"), errors);
|
|
115
|
+
const to = o.to === undefined ? lastPlace(line) : readPlace(line, o.to, pointer(path, "to"), errors);
|
|
116
|
+
if (from !== undefined && to !== undefined && to < from && !line.closed) {
|
|
117
|
+
errors.push({ path: pointer(path, "to"), message: "a place no earlier than from" });
|
|
118
|
+
}
|
|
119
|
+
return from === undefined || to === undefined || errors.length > before
|
|
120
|
+
? undefined
|
|
121
|
+
: { from, to };
|
|
122
|
+
},
|
|
123
|
+
meets(line, stretch, state) {
|
|
124
|
+
const posts = new Set(placesOf(line, state, "posts"));
|
|
125
|
+
// On a ring the stretch goes forward from its first place, past the last to the first where it
|
|
126
|
+
// ends before it starts.
|
|
127
|
+
const count = line.closed
|
|
128
|
+
? divideWithRemainder(stretch.to - stretch.from, line.places).remainder + 1
|
|
129
|
+
: stretch.to - stretch.from + 1;
|
|
130
|
+
for (let i = 0; i < count; i++) {
|
|
131
|
+
if (!posts.has(placeOn(line, stretch.from, i))) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
},
|
|
137
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The most states a scene's whole state graph may hold, at a build and on a device alike: the
|
|
3
|
+
* same as the kind cells', which bounds a hint on a phone; posts on eleven free places, the
|
|
4
|
+
* course's largest scene of posts, take 2,048 states.
|
|
5
|
+
*/
|
|
6
|
+
export declare const limit = 3000;
|
|
7
|
+
/**
|
|
8
|
+
* The most work a scene's whole state graph may take, as the search core counts it, at a build and
|
|
9
|
+
* on a device alike: the same as the kind cells'. A state of the line carries a few numbers, so the
|
|
10
|
+
* course's scenes take a small part of it; a change of it proves every scene again.
|
|
11
|
+
*/
|
|
12
|
+
export declare const workLimit = 6000000;
|
|
13
|
+
export declare const line: import("../../cores/kind.js").Kind<import("./kind.js").Scene, import("./line.js").State, import("./kind.js").Move>;
|