@altnovatrail/widgets 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/kinds/line/line.handles.json +195 -0
- package/kinds/line/line.schema.json +321 -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 +10 -0
- package/lib/cores/registry.d.ts +43 -0
- package/lib/cores/registry.js +87 -0
- package/lib/cores/search.js +36 -8
- 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/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 +157 -0
- package/package.json +9 -2
package/lib/cores/search.js
CHANGED
|
@@ -35,7 +35,9 @@ function search(kind, scene, seed) {
|
|
|
35
35
|
}
|
|
36
36
|
// The work is a sum over the states, whatever order they are found in, so a graph passes the
|
|
37
37
|
// limit at a build exactly where it does on a device. A state's work is counted before its moves
|
|
38
|
-
// are played, so one with too many moves to try stops the search before they cost anything more
|
|
38
|
+
// are played, so one with too many moves to try stops the search before they cost anything more;
|
|
39
|
+
// where the kind counts them before it lists them, before they are listed too, and since that
|
|
40
|
+
// count is never more than the list, the search stops at the same state either way.
|
|
39
41
|
let work = 0;
|
|
40
42
|
const nodes = [];
|
|
41
43
|
const byKey = new Map();
|
|
@@ -65,15 +67,12 @@ function search(kind, scene, seed) {
|
|
|
65
67
|
// The version a state is played at is its layer, though a widget may reach the state at a
|
|
66
68
|
// later version, by an undo or a longer way: a kind on this core reads nothing of `at`.
|
|
67
69
|
const at = { version: node.layer, seed };
|
|
68
|
-
const
|
|
69
|
-
if (
|
|
70
|
-
const implied = kind.implied?.(scene, node.state) ?? 0;
|
|
71
|
-
work += moves.length * (node.key.length + implied + moveWork);
|
|
72
|
-
}
|
|
73
|
-
if (work > kind.workLimit) {
|
|
70
|
+
const listed = listMoves(kind, scene, node.state, node.key, work);
|
|
71
|
+
if (listed === null) {
|
|
74
72
|
return { ok: false, undecided: "limit", passes: "work" };
|
|
75
73
|
}
|
|
76
|
-
|
|
74
|
+
work = listed.work;
|
|
75
|
+
for (const move of listed.moves) {
|
|
77
76
|
const played = kind.play(scene, node.state, move, at);
|
|
78
77
|
if (!played.ok) {
|
|
79
78
|
continue;
|
|
@@ -130,6 +129,35 @@ function search(kind, scene, seed) {
|
|
|
130
129
|
};
|
|
131
130
|
return { ok: true, graph };
|
|
132
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* The moves the kind lists from a state and the work so far with theirs, or null where the work
|
|
134
|
+
* passes the kind's limit: before the moves are listed, where the kind counts them first. It stays
|
|
135
|
+
* out of the search's loop: a closure over the loop's state there slowed every graph by half on the
|
|
136
|
+
* stand-in for a phone (measurements 2.5).
|
|
137
|
+
*/
|
|
138
|
+
function listMoves(kind, scene, state, key, before) {
|
|
139
|
+
const least = kind.leastMoves?.(scene, state) ?? 0;
|
|
140
|
+
if (!Number.isSafeInteger(least) || least < 0) {
|
|
141
|
+
throw new RangeError(`a kind's count of a state's moves is a whole number: ${least}`);
|
|
142
|
+
}
|
|
143
|
+
let weight = 0;
|
|
144
|
+
if (least > 0) {
|
|
145
|
+
weight = key.length + (kind.implied?.(scene, state) ?? 0) + moveWork;
|
|
146
|
+
if (before + least * weight > kind.workLimit) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const moves = kind.moves(scene, state);
|
|
151
|
+
if (moves.length < least) {
|
|
152
|
+
throw new RangeError(`the kind listed ${moves.length} moves from a state it counted at least ${least} from`);
|
|
153
|
+
}
|
|
154
|
+
let work = before;
|
|
155
|
+
if (moves.length > 0) {
|
|
156
|
+
weight ||= key.length + (kind.implied?.(scene, state) ?? 0) + moveWork;
|
|
157
|
+
work += moves.length * weight;
|
|
158
|
+
}
|
|
159
|
+
return work > kind.workLimit ? null : { moves, work };
|
|
160
|
+
}
|
|
133
161
|
/** What the scene's goal asks: the fewest moves where the kind does not say, or the scene has no goal. */
|
|
134
162
|
function asked(kind, scene) {
|
|
135
163
|
return (kind.hasGoal(scene) ? kind.asks?.(scene) : undefined) ?? { key: "fewest" };
|
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))],
|
|
@@ -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>;
|