@altnovatrail/widgets 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,17 @@
5
5
  // or an adversary answers a move needs the core of games, whose witness is a strategy.
6
6
  import { counted, extreme, ways } from "./enumerate.js";
7
7
  import { canonicalKey } from "./key.js";
8
+ /**
9
+ * The work of trying one move beyond the length of its state's canonical key, by which a kind's
10
+ * limit of work counts: about what listing, playing and keying a move costs whatever its state.
11
+ */
12
+ export const moveWork = 64;
8
13
  /**
9
14
  * The whole state graph of a scene, read from its JSON by the kind's schema: every state the
10
15
  * scene's moves reach, layer by layer, states that are equal by their canonical key merged. A
11
- * graph with more states than the kind's limit is undecided, wherever its goal lies: the limit
12
- * holds every state a learner can reach, not only the search from the scene to the goal. A
16
+ * graph with more states than the kind's limit, or more work than its limit of work, is
17
+ * undecided, wherever its goal lies: the limits hold every state a learner can reach, not only the
18
+ * search from the scene to the goal. A
13
19
  * measure the goal asks that is no safe integer throws, as a state that is no JSON of integers
14
20
  * does: either is a fault of the kind, not of the scene.
15
21
  */
@@ -24,6 +30,13 @@ function search(kind, scene, seed) {
24
30
  if (!Number.isSafeInteger(kind.limit) || kind.limit < 1) {
25
31
  throw new RangeError(`a kind's limit of states is a whole number from 1: ${String(kind.limit)}`);
26
32
  }
33
+ if (!Number.isSafeInteger(kind.workLimit) || kind.workLimit < 1) {
34
+ throw new RangeError(`a kind's limit of work is a whole number from 1: ${String(kind.workLimit)}`);
35
+ }
36
+ // The work is a sum over the states, whatever order they are found in, so a graph passes the
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.
39
+ let work = 0;
27
40
  const nodes = [];
28
41
  const byKey = new Map();
29
42
  const add = (state, key, layer, parent) => {
@@ -35,6 +48,7 @@ function search(kind, scene, seed) {
35
48
  layer,
36
49
  goal: kind.meets(scene, state),
37
50
  distance: null,
51
+ nearest: null,
38
52
  next: [],
39
53
  parent,
40
54
  });
@@ -51,7 +65,15 @@ function search(kind, scene, seed) {
51
65
  // The version a state is played at is its layer, though a widget may reach the state at a
52
66
  // later version, by an undo or a longer way: a kind on this core reads nothing of `at`.
53
67
  const at = { version: node.layer, seed };
54
- for (const move of kind.moves(scene, node.state)) {
68
+ const moves = kind.moves(scene, node.state);
69
+ if (moves.length > 0) {
70
+ const implied = kind.implied?.(scene, node.state) ?? 0;
71
+ work += moves.length * (node.key.length + implied + moveWork);
72
+ }
73
+ if (work > kind.workLimit) {
74
+ return { ok: false, undecided: "limit", passes: "work" };
75
+ }
76
+ for (const move of moves) {
55
77
  const played = kind.play(scene, node.state, move, at);
56
78
  if (!played.ok) {
57
79
  continue;
@@ -65,7 +87,7 @@ function search(kind, scene, seed) {
65
87
  let to = byKey.get(key);
66
88
  if (to === undefined) {
67
89
  if (nodes.length >= kind.limit) {
68
- return { ok: false, undecided: "limit" };
90
+ return { ok: false, undecided: "limit", passes: "states" };
69
91
  }
70
92
  to = add(played.state, key, layer, { from, move });
71
93
  found.push(to);
@@ -79,13 +101,20 @@ function search(kind, scene, seed) {
79
101
  current = found;
80
102
  }
81
103
  const ask = asked(kind, scene);
82
- const targets = ask.key === "max" || ask.key === "min"
83
- ? (extreme(nodes, ask.key, ask.measure)?.at ?? [])
84
- : nodes.filter((node) => node.goal).map((node) => node.index);
85
- distances(nodes, targets);
104
+ const before = movesInto(nodes);
105
+ const nearest = distances(before, nodes.filter((node) => node.goal).map((node) => node.index));
106
+ const distance = ask.key === "max" || ask.key === "min"
107
+ ? distances(before, extreme(nodes, ask.key, ask.measure)?.at ?? [])
108
+ : nearest;
109
+ for (const node of nodes) {
110
+ node.distance = distance[node.index] ?? null;
111
+ node.nearest = nearest[node.index] ?? null;
112
+ }
86
113
  const graph = {
114
+ asks: ask.key,
87
115
  nodes,
88
116
  layers,
117
+ work,
89
118
  at: (state) => {
90
119
  let key;
91
120
  try {
@@ -105,31 +134,37 @@ function search(kind, scene, seed) {
105
134
  function asked(kind, scene) {
106
135
  return (kind.hasGoal(scene) ? kind.asks?.(scene) : undefined) ?? { key: "fewest" };
107
136
  }
108
- // Each state's distance to the states the key aims at: a search by layers backwards from them.
109
- function distances(nodes, targets) {
137
+ // The states each state is reached from by one move, by its index.
138
+ function movesInto(nodes) {
110
139
  const before = nodes.map(() => []);
111
140
  for (const node of nodes) {
112
141
  for (const edge of node.next) {
113
142
  before[edge.to].push(node.index);
114
143
  }
115
144
  }
145
+ return before;
146
+ }
147
+ // Each state's fewest moves to the targets, by its index, null where none can be reached: a search
148
+ // by layers backwards from them.
149
+ function distances(before, targets) {
150
+ const found = before.map(() => null);
116
151
  let current = [...targets];
117
152
  for (const index of current) {
118
- nodes[index].distance = 0;
153
+ found[index] = 0;
119
154
  }
120
155
  for (let distance = 1; current.length > 0; distance++) {
121
- const found = [];
156
+ const next = [];
122
157
  for (const to of current) {
123
158
  for (const from of before[to]) {
124
- const node = nodes[from];
125
- if (node.distance === null) {
126
- node.distance = distance;
127
- found.push(from);
159
+ if (found[from] === null) {
160
+ found[from] = distance;
161
+ next.push(from);
128
162
  }
129
163
  }
130
164
  }
131
- current = found;
165
+ current = next;
132
166
  }
167
+ return found;
133
168
  }
134
169
  // The moves by which the search first reached a state, from the scene: a path of the fewest moves.
135
170
  function pathTo(graph, end) {
@@ -1 +1 @@
1
- export declare const bishop: import("./operation.js").Operation<number>, bishopOn: import("./operation.js").Goal<number>, bishopReach: import("./operation.js").Goal<import("./piece.js").Reach>;
1
+ export declare const bishop: import("./operation.js").Operation<import("./piece.js").Start>, bishopOn: import("./operation.js").Goal<import("./piece.js").On>, bishopReach: import("./operation.js").Goal<import("./piece.js").Reach>;
@@ -5,4 +5,12 @@
5
5
  * every scene of the kind is proved again.
6
6
  */
7
7
  export declare const limit = 3000;
8
+ /**
9
+ * The most work a scene's whole state graph may take, as the search core counts it, at a build and
10
+ * on a device alike: as much as a phone's stand-in does within a second on the operation whose
11
+ * work is slowest, measured by just graphs. It refuses a scene whose states carry hundreds of
12
+ * marks or places, or have a hundred thousand moves to try, though their count is within the
13
+ * limit of states; a change of it, or of how the search counts work, proves every scene again.
14
+ */
15
+ export declare const workLimit = 6000000;
8
16
  export declare const cells: import("../../cores/kind.js").Kind<import("./kind.js").Scene, import("./sheet.js").State, import("./kind.js").Move>;
@@ -1,5 +1,5 @@
1
1
  // The kind cells as a module's build and the widget take it: the registry's operations and goals,
2
- // within the kind's one limit of states.
2
+ // within the kind's one limit of states and one of work.
3
3
  import { cellsKind } from "./kind.js";
4
4
  import { registry } from "./registry.js";
5
5
  /**
@@ -9,4 +9,12 @@ import { registry } from "./registry.js";
9
9
  * every scene of the kind is proved again.
10
10
  */
11
11
  export const limit = 3_000;
12
- export const cells = cellsKind(registry, limit);
12
+ /**
13
+ * The most work a scene's whole state graph may take, as the search core counts it, at a build and
14
+ * on a device alike: as much as a phone's stand-in does within a second on the operation whose
15
+ * work is slowest, measured by just graphs. It refuses a scene whose states carry hundreds of
16
+ * marks or places, or have a hundred thousand moves to try, though their count is within the
17
+ * limit of states; a change of it, or of how the search counts work, proves every scene again.
18
+ */
19
+ export const workLimit = 6_000_000;
20
+ export const cells = cellsKind(registry, limit, workLimit);
@@ -18,7 +18,8 @@ export type Move = {
18
18
  readonly [operation: string]: Json;
19
19
  };
20
20
  /**
21
- * The kind cells with the operations and goals of the registry and the given limit of states.
21
+ * The kind cells with the operations and goals of the registry and the given limits of states and
22
+ * of work, none of work where none is given.
22
23
  * Throws where the registry repeats a name or a member of the state.
23
24
  */
24
- export declare function cellsKind(registry: Registry, limit: number): Kind<Scene, State, Move>;
25
+ export declare function cellsKind(registry: Registry, limit: number, workLimit?: number): Kind<Scene, State, Move>;
@@ -26,19 +26,25 @@ function check(registry) {
26
26
  if ((state === undefined) !== (op.start === undefined)) {
27
27
  throw new Error(`the operation ${op.name} keeps a member of the state and starts it, or neither`);
28
28
  }
29
- if (state !== undefined && (!name.test(state.member) || members.has(state.member))) {
30
- throw new Error(`a member of the state is new and in snake_case: ${state.member}`);
29
+ if ((state?.beside === undefined) !== (op.startBeside === undefined)) {
30
+ throw new Error(`the operation ${op.name} keeps a second member of the state and starts it, or neither`);
31
31
  }
32
- if (state !== undefined) {
33
- members.add(state.member);
32
+ 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
+ if (member !== undefined) {
37
+ members.add(member);
38
+ }
34
39
  }
35
40
  }
36
41
  }
37
42
  /**
38
- * The kind cells with the operations and goals of the registry and the given limit of states.
43
+ * The kind cells with the operations and goals of the registry and the given limits of states and
44
+ * of work, none of work where none is given.
39
45
  * Throws where the registry repeats a name or a member of the state.
40
46
  */
41
- export function cellsKind(registry, limit) {
47
+ export function cellsKind(registry, limit, workLimit = Number.MAX_SAFE_INTEGER) {
42
48
  check(registry);
43
49
  const operations = new Map(registry.operations.map((op) => [op.name, op]));
44
50
  const goals = new Map(registry.goals.map((goal) => [goal.name, goal]));
@@ -53,6 +59,7 @@ export function cellsKind(registry, limit) {
53
59
  };
54
60
  return {
55
61
  limit,
62
+ workLimit,
56
63
  read(input) {
57
64
  const errors = [];
58
65
  const o = object(input, "", errors, ["sheet", "operations"], ["goal"]);
@@ -126,6 +133,11 @@ export function cellsKind(registry, limit) {
126
133
  if (op.schema.state !== undefined && op.start !== undefined) {
127
134
  state[op.schema.state.member] = op.start(scene.sheet, options);
128
135
  }
136
+ const beside = op.schema.state?.beside;
137
+ const second = beside === undefined ? undefined : op.startBeside?.(scene.sheet, options);
138
+ if (beside !== undefined && second !== undefined) {
139
+ state[beside.member] = second;
140
+ }
129
141
  }
130
142
  return state;
131
143
  },
@@ -145,6 +157,7 @@ export function cellsKind(registry, limit) {
145
157
  moves(scene, state) {
146
158
  return scene.operations.flatMap(({ op, options }) => op.moves(scene.sheet, options, state).map((move) => ({ [op.name]: move })));
147
159
  },
160
+ implied: (scene, state) => scene.operations.reduce((sum, { op, options }) => sum + (op.implied?.(scene.sheet, options, state) ?? 0), 0),
148
161
  hasGoal: (scene) => scene.goal !== null,
149
162
  meets: (scene, state) => scene.goal?.goal.meets(scene.sheet, scene.goal.options, state) ?? false,
150
163
  asks: (scene) => scene.goal?.goal.asks?.(scene.sheet, scene.goal.options) ?? { key: "fewest" },
@@ -1 +1 @@
1
- export declare const knight: import("./operation.js").Operation<number>, knightOn: import("./operation.js").Goal<number>;
1
+ export declare const knight: import("./operation.js").Operation<import("./piece.js").Start>, knightOn: import("./operation.js").Goal<import("./piece.js").On>;
@@ -15,10 +15,17 @@ export interface Operation<Options = unknown> {
15
15
  readonly options: Schema;
16
16
  /** What a move of the operation holds, under the operation's name. */
17
17
  readonly move: Schema;
18
- /** The member of the state the operation keeps besides the cells' marks, if it keeps one. */
18
+ /**
19
+ * The member of the state the operation keeps besides the cells' marks, if it keeps one, and a
20
+ * second member beside it, which a state holds only where the scene's parameters ask for it.
21
+ */
19
22
  readonly state?: {
20
23
  readonly member: string;
21
24
  readonly schema: Schema;
25
+ readonly beside?: {
26
+ readonly member: string;
27
+ readonly schema: Schema;
28
+ };
22
29
  };
23
30
  };
24
31
  /**
@@ -28,10 +35,17 @@ export interface Operation<Options = unknown> {
28
35
  read(value: unknown, sheet: Sheet, path: string, errors: SceneError[]): Options | undefined;
29
36
  /** The value of the operation's own member of the state at the scene's start. */
30
37
  start?(sheet: Sheet, options: Options): Json;
38
+ /** The value of the second member at the scene's start, or undefined where the state holds none. */
39
+ startBeside?(sheet: Sheet, options: Options): Json | undefined;
31
40
  /** A move, given what it holds under the operation's name, or the rules' short reason to refuse it. */
32
41
  play(sheet: Sheet, options: Options, state: State, move: Json): Played<State>;
33
42
  /** What every move of the operation to try from the state holds, in a fixed order. */
34
43
  moves(sheet: Sheet, options: Options, state: State): readonly Json[];
44
+ /**
45
+ * What a move from the state reads and writes of the operation's member that the member leaves
46
+ * out, in characters as the state's canonical key would write it, as the kind's implied has it.
47
+ */
48
+ implied?(sheet: Sheet, options: Options, state: State): number;
35
49
  }
36
50
  export interface Goal<Options = unknown> {
37
51
  /** The goal's name: its member in a scene's "goal". */
@@ -58,15 +58,21 @@ function extentOf(sheet, places) {
58
58
  }
59
59
  /**
60
60
  * The places that a fold along the line, laying the given side over the other, gives the cells;
61
- * or the rules' short reason to refuse it: a line outside the paper, a side that is not the line's,
62
- * a part laid over that is wider than the other.
61
+ * or the rules' short reason to refuse it, read in the order of the move's parts, so that the
62
+ * reason named is one a change of that part alone may cure: the line — outside the paper, or where
63
+ * the scene draws lines, one that no drawn line lies along now —, then the side, not the line's,
64
+ * then the two together, a part laid over that is wider than the other. A side or a width named
65
+ * on a line no side of which folds would send the learner to the other side, refused again.
63
66
  */
64
- function folded(sheet, places, line, lay) {
67
+ function folded(sheet, places, line, lay, drawn) {
65
68
  const e = extentOf(sheet, places);
66
69
  const [first, last] = line.vertical ? [e.c0, e.c1] : [e.r0, e.r1];
67
70
  if (line.k <= first || line.k > last) {
68
71
  return "no-line";
69
72
  }
73
+ if (drawn !== undefined && !drawn.has(line.name)) {
74
+ return "not-given";
75
+ }
70
76
  const [before, after] = line.vertical ? sides.vertical : sides.horizontal;
71
77
  if (lay !== before && lay !== after) {
72
78
  return "no-side";
@@ -272,13 +278,8 @@ export const paper = {
272
278
  const name = string(fold.line, "/fold/line", errors, linePattern, "");
273
279
  const line = options.lines.find((l) => l.name === name);
274
280
  const lay = typeof fold.lay === "string" ? fold.lay : "";
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
281
  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
- }
282
+ const after = line === undefined ? "no-line" : folded(sheet, places, line, lay, drawn);
282
283
  return typeof after === "string"
283
284
  ? refuse(after)
284
285
  : { ok: true, state: { ...state, paper: foldedJson(sheet, after) } };
@@ -299,6 +300,22 @@ export const paper = {
299
300
  const punched = cells.map((cell) => cellName(sheet, cell));
300
301
  return { ok: true, state: { ...state, paper: { punched } } };
301
302
  },
303
+ // A folded state names only the cells off their own places, but every move lays out the whole
304
+ // sheet: each cell left out counts as the pair of names that would write it, "A1":"A1",.
305
+ implied(sheet, _options, state) {
306
+ const paper = state.paper;
307
+ if (!isObject(paper) || !isObject(paper.folded)) {
308
+ return 0;
309
+ }
310
+ let implied = 0;
311
+ for (let cell = 0; cell < sheet.rows * sheet.cols; cell++) {
312
+ const name = cellName(sheet, cell);
313
+ if (!sheet.holes.has(cell) && !Object.hasOwn(paper.folded, name)) {
314
+ implied += 2 * name.length + 6;
315
+ }
316
+ }
317
+ return implied;
318
+ },
302
319
  moves(sheet, options, state) {
303
320
  const places = placesOf(sheet, state);
304
321
  if (places === undefined || places === "opened") {
@@ -307,11 +324,8 @@ export const paper = {
307
324
  const moves = [];
308
325
  const drawn = options.drawn === undefined ? undefined : drawnNow(sheet, places, options.drawn);
309
326
  for (const line of options.lines) {
310
- if (drawn !== undefined && !drawn.has(line.name)) {
311
- continue;
312
- }
313
327
  for (const lay of line.vertical ? sides.vertical : sides.horizontal) {
314
- if (typeof folded(sheet, places, line, lay) !== "string") {
328
+ if (typeof folded(sheet, places, line, lay, drawn) !== "string") {
315
329
  moves.push({ fold: { line: line.name, lay } });
316
330
  }
317
331
  }
@@ -1,7 +1,7 @@
1
1
  import type { Goal, Operation } from "./operation.js";
2
2
  import { type Sheet } from "./sheet.js";
3
3
  export interface Piece {
4
- /** The operation's name, and the prefix of its member of the state and of its goal. */
4
+ /** The operation's name, and the prefix of its members of the state and of its goals. */
5
5
  readonly name: string;
6
6
  /** What the piece is and how it moves, as the schema of its parameters says it. */
7
7
  readonly description: string;
@@ -12,15 +12,25 @@ export interface Piece {
12
12
  */
13
13
  refusal(sheet: Sheet, from: number, to: number): string | undefined;
14
14
  }
15
+ /** Whether a piece has made an even or an odd count of moves. */
16
+ export type Parity = "even" | "odd";
17
+ /** A piece's parameters: the cell it starts on, and whether the state keeps its moves' parity. */
18
+ export interface Start {
19
+ readonly cell: number;
20
+ readonly parity: boolean;
21
+ }
22
+ /** A goal of standing on a cell, by its index, after a count of moves of a parity where it names one. */
23
+ export interface On {
24
+ readonly cell: number;
25
+ readonly after?: Parity;
26
+ }
15
27
  /**
16
28
  * A kind of piece as the registry takes it: its operation, its goal of standing on a cell, and its
17
29
  * goal of the cells it reaches, which a count asks.
18
30
  */
19
31
  export interface PieceParts {
20
- /** Its parameters are the cell the piece starts on, by its index. */
21
- readonly operation: Operation<number>;
22
- /** Its parameters are the cell the piece is to stand on, by its index. */
23
- readonly goal: Goal<number>;
32
+ readonly operation: Operation<Start>;
33
+ readonly goal: Goal<On>;
24
34
  /** Its parameters are read into the cells it reaches within the moves they give. */
25
35
  readonly reach: Goal<Reach>;
26
36
  }
@@ -31,6 +41,7 @@ export interface Reach {
31
41
  /**
32
42
  * The operation and the goal of a kind of piece. A move names the cell the piece goes to, and is
33
43
  * refused where that is no cell of the sheet, "no-cell"; where the state puts the piece on none,
34
- * "no-piece"; a hole, "hole"; the piece's own cell, "not-its-move"; or by the piece's own rules.
44
+ * "no-piece"; where the scene asks for the parity of its moves and the state keeps none,
45
+ * "no-parity"; a hole, "hole"; the piece's own cell, "not-its-move"; or by the piece's own rules.
35
46
  */
36
47
  export declare function piece(kind: Piece): PieceParts;