@altnovatrail/widgets 0.1.3 → 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.
Files changed (82) hide show
  1. package/README.md +1 -1
  2. package/kinds/cells/cells.handles.json +3 -3
  3. package/kinds/cells/cells.schema.json +69 -9
  4. package/kinds/cells/vectors/knight-after-no-parity.json +31 -0
  5. package/kinds/cells/vectors/knight-par-56-d2-2.json +61 -6
  6. package/kinds/cells/vectors/paper-drawn-lines.json +36 -1
  7. package/kinds/cells/vectors/polyomino-one-pieces.json +28 -0
  8. package/kinds/line/line.handles.json +195 -0
  9. package/kinds/line/line.schema.json +321 -0
  10. package/kinds/line/vectors/cases.json +213 -0
  11. package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
  12. package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
  13. package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
  14. package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
  15. package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
  16. package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
  17. package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
  18. package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
  19. package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
  20. package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
  21. package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
  22. package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
  23. package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
  24. package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
  25. package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
  26. package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
  27. package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
  28. package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
  29. package/kinds/line/vectors/invalid-line.json +34 -0
  30. package/kinds/line/vectors/invalid-ring.json +34 -0
  31. package/kinds/line/vectors/invalid.json +43 -0
  32. package/kinds/line/vectors/jump-unreachable.json +104 -0
  33. package/kinds/line/vectors/limit.json +40 -0
  34. package/kinds/line/vectors/op-not-in-scene.json +47 -0
  35. package/kinds/line/vectors/ring-cut.json +104 -0
  36. package/kinds/line/vectors/ring-every.json +107 -0
  37. package/kinds/line/vectors/ring-jump.json +135 -0
  38. package/lib/cores/arith.d.ts +62 -0
  39. package/lib/cores/arith.js +197 -0
  40. package/lib/cores/enumerate.js +2 -2
  41. package/lib/cores/kind.d.ts +28 -3
  42. package/lib/cores/kind.js +2 -2
  43. package/lib/cores/registry.d.ts +43 -0
  44. package/lib/cores/registry.js +87 -0
  45. package/lib/cores/search.d.ts +21 -3
  46. package/lib/cores/search.js +80 -17
  47. package/lib/kinds/cells/bishop.d.ts +1 -1
  48. package/lib/kinds/cells/index.d.ts +8 -0
  49. package/lib/kinds/cells/index.js +10 -2
  50. package/lib/kinds/cells/kind.d.ts +3 -2
  51. package/lib/kinds/cells/kind.js +26 -66
  52. package/lib/kinds/cells/knight.d.ts +1 -1
  53. package/lib/kinds/cells/operation.d.ts +21 -1
  54. package/lib/kinds/cells/paper.js +27 -13
  55. package/lib/kinds/cells/piece.d.ts +17 -6
  56. package/lib/kinds/cells/piece.js +119 -38
  57. package/lib/kinds/cells/polyomino.d.ts +14 -0
  58. package/lib/kinds/cells/polyomino.js +122 -35
  59. package/lib/kinds/cells/registry.js +1 -1
  60. package/lib/kinds/cells/schema.js +4 -1
  61. package/lib/kinds/cells/step.d.ts +1 -1
  62. package/lib/kinds/line/cut.d.ts +2 -0
  63. package/lib/kinds/line/cut.js +63 -0
  64. package/lib/kinds/line/goals.d.ts +22 -0
  65. package/lib/kinds/line/goals.js +137 -0
  66. package/lib/kinds/line/index.d.ts +13 -0
  67. package/lib/kinds/line/index.js +17 -0
  68. package/lib/kinds/line/jump.d.ts +10 -0
  69. package/lib/kinds/line/jump.js +95 -0
  70. package/lib/kinds/line/kind.d.ts +25 -0
  71. package/lib/kinds/line/kind.js +125 -0
  72. package/lib/kinds/line/line.d.ts +63 -0
  73. package/lib/kinds/line/line.js +157 -0
  74. package/lib/kinds/line/operation.d.ts +69 -0
  75. package/lib/kinds/line/operation.js +1 -0
  76. package/lib/kinds/line/posts.d.ts +11 -0
  77. package/lib/kinds/line/posts.js +85 -0
  78. package/lib/kinds/line/registry.d.ts +2 -0
  79. package/lib/kinds/line/registry.js +11 -0
  80. package/lib/kinds/line/schema.d.ts +4 -0
  81. package/lib/kinds/line/schema.js +157 -0
  82. package/package.json +9 -2
@@ -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>;
@@ -1,58 +1,40 @@
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 { isObject, map, object, pointer } from "../../cores/json.js";
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
- if (!version.test(registry.version)) {
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;
26
13
  if ((state === undefined) !== (op.start === undefined)) {
27
14
  throw new Error(`the operation ${op.name} keeps a member of the state and starts it, or neither`);
28
15
  }
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}`);
16
+ if ((state?.beside === undefined) !== (op.startBeside === undefined)) {
17
+ throw new Error(`the operation ${op.name} keeps a second member of the state and starts it, or neither`);
31
18
  }
32
- if (state !== undefined) {
33
- members.add(state.member);
19
+ for (const member of [state?.member, state?.beside?.member]) {
20
+ if (member !== undefined) {
21
+ takeName(members, member, "a member of the state");
22
+ }
34
23
  }
35
24
  }
36
25
  }
37
26
  /**
38
- * The kind cells with the operations and goals of the registry and the given limit of states.
27
+ * The kind cells with the operations and goals of the registry and the given limits of states and
28
+ * of work, none of work where none is given.
39
29
  * Throws where the registry repeats a name or a member of the state.
40
30
  */
41
- export function cellsKind(registry, limit) {
31
+ export function cellsKind(registry, limit, workLimit = Number.MAX_SAFE_INTEGER) {
42
32
  check(registry);
43
33
  const operations = new Map(registry.operations.map((op) => [op.name, op]));
44
34
  const goals = new Map(registry.goals.map((goal) => [goal.name, goal]));
45
- // Reads one operation's or goal's parameters; the result undefined is an error, named or not.
46
- const part = (read, value, sheet, path, errors) => {
47
- const before = errors.length;
48
- const options = read(value, sheet, path, errors);
49
- if (options === undefined && errors.length === before) {
50
- errors.push({ path, message: "parameters the kind cannot read" });
51
- }
52
- return options === undefined ? undefined : { options };
53
- };
54
35
  return {
55
36
  limit,
37
+ workLimit,
56
38
  read(input) {
57
39
  const errors = [];
58
40
  const o = object(input, "", errors, ["sheet", "operations"], ["goal"]);
@@ -76,34 +58,16 @@ export function cellsKind(registry, limit) {
76
58
  }
77
59
  allows.set(named, undefined);
78
60
  if (sheet !== undefined) {
79
- const read = part(op.read.bind(op), value, sheet, at, errors);
61
+ const read = readPart((v, p, e) => op.read(v, sheet, p, e), value, at, errors);
80
62
  if (read !== undefined) {
81
63
  chosen.set(named, read.options);
82
64
  allows.set(named, read.options);
83
65
  }
84
66
  }
85
67
  }
86
- let goal = null;
87
- const goalMap = o.goal === undefined ? undefined : map(o.goal, "/goal", errors);
88
- if (goalMap !== undefined) {
89
- const entries = Object.entries(goalMap);
90
- const [named, value] = entries[0] ?? ["", undefined];
91
- const found = goals.get(named);
92
- if (entries.length !== 1) {
93
- errors.push({ path: "/goal", message: "one goal, by its name" });
94
- }
95
- else if (found === undefined) {
96
- errors.push({
97
- path: pointer("/goal", named),
98
- message: "no goal of the kind has this name",
99
- });
100
- }
101
- else if (sheet !== undefined) {
102
- const reading = (v, s, p, e) => found.read(v, s, p, e, allows);
103
- const read = part(reading, value, sheet, pointer("/goal", named), errors);
104
- goal = read === undefined ? null : { goal: found, options: read.options };
105
- }
106
- }
68
+ const goal = readGoal(o.goal, goals, errors, sheet === undefined
69
+ ? undefined
70
+ : (found, v, p, e) => found.read(v, sheet, p, e, allows));
107
71
  if (errors.length > 0 || sheet === undefined) {
108
72
  return { ok: false, errors };
109
73
  }
@@ -111,9 +75,7 @@ export function cellsKind(registry, limit) {
111
75
  ok: true,
112
76
  scene: {
113
77
  sheet,
114
- operations: registry.operations
115
- .filter((op) => chosen.has(op.name))
116
- .map((op) => ({ op, options: chosen.get(op.name) })),
78
+ operations: inRegistryOrder(registry.operations, chosen),
117
79
  goal,
118
80
  },
119
81
  };
@@ -126,25 +88,23 @@ export function cellsKind(registry, limit) {
126
88
  if (op.schema.state !== undefined && op.start !== undefined) {
127
89
  state[op.schema.state.member] = op.start(scene.sheet, options);
128
90
  }
91
+ const beside = op.schema.state?.beside;
92
+ const second = beside === undefined ? undefined : op.startBeside?.(scene.sheet, options);
93
+ if (beside !== undefined && second !== undefined) {
94
+ state[beside.member] = second;
95
+ }
129
96
  }
130
97
  return state;
131
98
  },
132
99
  // The kind's moves draw nothing at random, so it reads nothing of the version or the seed.
133
100
  play(scene, state, move) {
134
- const named = isObject(move) ? Object.keys(move) : [];
135
- if (named.length !== 1) {
136
- return refuse("not-a-move");
137
- }
138
- const opName = named[0];
139
- const chosen = scene.operations.find(({ op }) => op.name === opName);
140
- if (chosen === undefined) {
141
- return refuse(operations.has(opName) ? "op-not-in-scene" : "unknown-op");
142
- }
143
- 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));
144
102
  },
145
103
  moves(scene, state) {
146
104
  return scene.operations.flatMap(({ op, options }) => op.moves(scene.sheet, options, state).map((move) => ({ [op.name]: move })));
147
105
  },
106
+ leastMoves: (scene, state) => scene.operations.reduce((sum, { op, options }) => sum + (op.leastMoves?.(scene.sheet, options, state) ?? 0), 0),
107
+ implied: (scene, state) => scene.operations.reduce((sum, { op, options }) => sum + (op.implied?.(scene.sheet, options, state) ?? 0), 0),
148
108
  hasGoal: (scene) => scene.goal !== null,
149
109
  meets: (scene, state) => scene.goal?.goal.meets(scene.sheet, scene.goal.options, state) ?? false,
150
110
  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,23 @@ 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
+ * 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;
50
+ /**
51
+ * What a move from the state reads and writes of the operation's member that the member leaves
52
+ * out, in characters as the state's canonical key would write it, as the kind's implied has it.
53
+ */
54
+ implied?(sheet: Sheet, options: Options, state: State): number;
35
55
  }
36
56
  export interface Goal<Options = unknown> {
37
57
  /** 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;
@@ -3,26 +3,28 @@
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 { integer, object, pointer } from "../../cores/json.js";
6
+ //
7
+ // Where the scene asks for it, the state keeps beside the cell whether the piece has made an even
8
+ // or an odd count of moves, "<name>_parity", so that a goal may ask for a cell after a count of one
9
+ // parity: on a graph whose states are the cells alone, a cell reached after an odd count is the
10
+ // same state as after an even one, and no key could say the one is never reached.
11
+ import { integer, object, pointer, string } from "../../cores/json.js";
7
12
  import { cellAt, cellName, maxSide, readCell } from "./sheet.js";
13
+ const parities = ["even", "odd"];
8
14
  /** The most moves a reach names: no piece needs more to reach a cell of the largest sheet. */
9
15
  const mostMoves = maxSide * maxSide;
10
16
  function refuse(reason) {
11
17
  return { ok: false, reason };
12
18
  }
13
- /** The parameters {"cell": <cell>}: a cell of the sheet, no hole, or undefined with its errors. */
14
- function readPlace(value, sheet, path, errors) {
15
- const before = errors.length;
16
- const o = object(value, path, errors, ["cell"]);
17
- if (o === undefined) {
18
- return undefined;
19
- }
19
+ /** The member "cell" of parameters: a cell of the sheet, no hole, or undefined with its errors. */
20
+ function readPlace(o, sheet, path, errors) {
20
21
  const at = pointer(path, "cell");
21
22
  const cell = readCell(sheet, o.cell, at, errors);
22
23
  if (cell !== undefined && sheet.holes.has(cell)) {
23
24
  errors.push({ path: at, message: "a hole: no piece stands on it" });
25
+ return undefined;
24
26
  }
25
- return errors.length > before ? undefined : cell;
27
+ return cell;
26
28
  }
27
29
  /**
28
30
  * The cell a state puts the piece on, or undefined where its member is no cell of the sheet, or a
@@ -33,47 +35,74 @@ function where(sheet, state, member) {
33
35
  const cell = typeof value === "string" ? cellAt(sheet, value) : undefined;
34
36
  return cell === undefined || sheet.holes.has(cell) ? undefined : cell;
35
37
  }
36
- const place = (description) => ({
37
- description,
38
- type: "object",
39
- properties: { cell: { description: "The cell, not a hole.", $ref: "#/$defs/cell" } },
40
- required: ["cell"],
41
- unevaluatedProperties: false,
42
- });
38
+ /** The parity a state keeps, or undefined where its member is neither. */
39
+ function parityOf(state, member) {
40
+ const value = state[member];
41
+ return value === "even" || value === "odd" ? value : undefined;
42
+ }
43
+ const cellProperty = (description) => ({ description, $ref: "#/$defs/cell" });
43
44
  /**
44
45
  * The operation and the goal of a kind of piece. A move names the cell the piece goes to, and is
45
46
  * refused where that is no cell of the sheet, "no-cell"; where the state puts the piece on none,
46
- * "no-piece"; a hole, "hole"; the piece's own cell, "not-its-move"; or by the piece's own rules.
47
+ * "no-piece"; where the scene asks for the parity of its moves and the state keeps none,
48
+ * "no-parity"; a hole, "hole"; the piece's own cell, "not-its-move"; or by the piece's own rules.
47
49
  */
48
50
  export function piece(kind) {
49
51
  const member = `${kind.name}_at`;
52
+ const parityMember = `${kind.name}_parity`;
50
53
  const operation = {
51
54
  name: kind.name,
52
55
  schema: {
53
56
  options: {
54
- ...place(kind.description),
57
+ description: kind.description,
58
+ type: "object",
55
59
  properties: {
56
- cell: {
57
- description: "The cell the piece stands on at the scene's start, not a hole.",
58
- $ref: "#/$defs/cell",
60
+ cell: cellProperty("The cell the piece stands on at the scene's start, not a hole."),
61
+ parity: {
62
+ description: `Whether the state keeps, as ${parityMember}, whether the piece has made an even or an odd count of moves; false where left out.`,
63
+ type: "boolean",
59
64
  },
60
65
  },
66
+ required: ["cell"],
67
+ unevaluatedProperties: false,
61
68
  },
62
69
  move: {
63
70
  description: "A move of the piece to another cell.",
64
71
  type: "object",
65
- properties: { to: { description: "The cell the piece goes to.", $ref: "#/$defs/cell" } },
72
+ properties: { to: cellProperty("The cell the piece goes to.") },
66
73
  required: ["to"],
67
74
  unevaluatedProperties: false,
68
75
  },
69
76
  state: {
70
77
  member,
71
- schema: { description: `The cell the piece ${kind.name} stands on.`, $ref: "#/$defs/cell" },
78
+ schema: cellProperty(`The cell the piece ${kind.name} stands on.`),
79
+ beside: {
80
+ member: parityMember,
81
+ schema: {
82
+ description: `Whether the piece ${kind.name} has made an even or an odd count of moves; only where the scene's parameters of the piece ask for it.`,
83
+ type: "string",
84
+ enum: [...parities],
85
+ },
86
+ },
72
87
  },
73
88
  },
74
- read: readPlace,
75
- start: (sheet, cell) => cellName(sheet, cell),
76
- play(sheet, _start, state, move) {
89
+ read(value, sheet, path, errors) {
90
+ const before = errors.length;
91
+ const o = object(value, path, errors, ["cell"], ["parity"]);
92
+ if (o === undefined) {
93
+ return undefined;
94
+ }
95
+ const cell = readPlace(o, sheet, path, errors);
96
+ if (o.parity !== undefined && typeof o.parity !== "boolean") {
97
+ errors.push({ path: pointer(path, "parity"), message: "true or false" });
98
+ }
99
+ return errors.length > before || cell === undefined
100
+ ? undefined
101
+ : { cell, parity: o.parity === true };
102
+ },
103
+ start: (sheet, options) => cellName(sheet, options.cell),
104
+ startBeside: (_sheet, options) => options.parity ? "even" : undefined,
105
+ play(sheet, options, state, move) {
77
106
  const errors = [];
78
107
  const o = object(move, "", errors, ["to"]);
79
108
  const to = o === undefined ? undefined : readCell(sheet, o.to, "/to", errors);
@@ -85,18 +114,32 @@ export function piece(kind) {
85
114
  if (from === undefined) {
86
115
  return refuse("no-piece");
87
116
  }
117
+ const parity = parityOf(state, parityMember);
118
+ if (options.parity && parity === undefined) {
119
+ return refuse("no-parity");
120
+ }
88
121
  if (sheet.holes.has(to)) {
89
122
  return refuse("hole");
90
123
  }
91
124
  const reason = to === from ? "not-its-move" : kind.refusal(sheet, from, to);
92
- return reason === undefined
93
- ? { ok: true, state: { ...state, [member]: cellName(sheet, to) } }
94
- : refuse(reason);
125
+ if (reason !== undefined) {
126
+ return refuse(reason);
127
+ }
128
+ const moved = { ...state, [member]: cellName(sheet, to) };
129
+ return {
130
+ ok: true,
131
+ state: options.parity
132
+ ? { ...moved, [parityMember]: parity === "even" ? "odd" : "even" }
133
+ : moved,
134
+ };
95
135
  },
96
- moves(sheet, _start, state) {
136
+ moves(sheet, options, state) {
97
137
  const from = where(sheet, state, member);
98
138
  const moves = [];
99
- for (let to = 0; from !== undefined && to < sheet.rows * sheet.cols; to++) {
139
+ if (from === undefined || (options.parity && parityOf(state, parityMember) === undefined)) {
140
+ return moves;
141
+ }
142
+ for (let to = 0; to < sheet.rows * sheet.cols; to++) {
100
143
  if (to !== from && !sheet.holes.has(to) && kind.refusal(sheet, from, to) === undefined) {
101
144
  moves.push({ to: cellName(sheet, to) });
102
145
  }
@@ -106,9 +149,27 @@ export function piece(kind) {
106
149
  };
107
150
  const goal = {
108
151
  name: `${kind.name}_on`,
109
- schema: place(`The piece ${kind.name} stands on the cell; the scene allows the operation ${kind.name}, which keeps the piece.`),
152
+ schema: {
153
+ description: `The piece ${kind.name} stands on the cell, after a count of moves of the parity given where one is; the scene allows the operation ${kind.name}, which keeps the piece.`,
154
+ type: "object",
155
+ properties: {
156
+ cell: cellProperty("The cell, not a hole."),
157
+ after: {
158
+ description: `Whether the piece has made an even or an odd count of moves; the operation ${kind.name} of the scene then keeps their parity.`,
159
+ type: "string",
160
+ enum: [...parities],
161
+ },
162
+ },
163
+ required: ["cell"],
164
+ unevaluatedProperties: false,
165
+ },
110
166
  read(value, sheet, path, errors, operations) {
111
- const cell = readPlace(value, sheet, path, errors);
167
+ const before = errors.length;
168
+ const o = object(value, path, errors, ["cell"], ["after"]);
169
+ const cell = o === undefined ? undefined : readPlace(o, sheet, path, errors);
170
+ const after = o?.after === undefined
171
+ ? undefined
172
+ : string(o.after, pointer(path, "after"), errors, parities, "even or odd");
112
173
  if (!operations.has(kind.name)) {
113
174
  errors.push({
114
175
  path,
@@ -116,9 +177,22 @@ export function piece(kind) {
116
177
  });
117
178
  return undefined;
118
179
  }
119
- return cell;
180
+ // Where the piece's own parameters do not read, the scene is refused for them alone.
181
+ const start = operations.get(kind.name);
182
+ // An after that is no parity has its own error, which a parity of the piece would not mend.
183
+ if (after !== undefined && start !== undefined && !start.parity) {
184
+ errors.push({
185
+ path: pointer(path, "after"),
186
+ message: `the operation ${kind.name} keeps no parity of the piece's moves: give it "parity": true`,
187
+ });
188
+ }
189
+ if (errors.length > before || cell === undefined) {
190
+ return undefined;
191
+ }
192
+ return after === undefined ? { cell } : { cell, after };
120
193
  },
121
- meets: (sheet, cell, state) => where(sheet, state, member) === cell,
194
+ meets: (sheet, on, state) => where(sheet, state, member) === on.cell &&
195
+ (on.after === undefined || parityOf(state, parityMember) === on.after),
122
196
  };
123
197
  // The fewest moves from the start to each cell, by the piece's own rules; -1 where it never goes.
124
198
  const fewest = (sheet, start) => {
@@ -149,7 +223,7 @@ export function piece(kind) {
149
223
  const reach = {
150
224
  name: `${kind.name}_reach`,
151
225
  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.`,
226
+ 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, keeping no parity of its moves, so that its states are the piece's cells.`,
153
227
  type: "object",
154
228
  properties: {
155
229
  least: moveCount("The fewest moves to a cell counted, 0 by default: the cell it starts on."),
@@ -179,16 +253,23 @@ export function piece(kind) {
179
253
  const others = [...operations]
180
254
  .filter(([named, options]) => named !== kind.name && options !== undefined)
181
255
  .map(([named]) => named);
182
- if (typeof start === "number" && others.length > 0) {
256
+ if (start !== undefined && others.length > 0) {
183
257
  errors.push({
184
258
  path,
185
259
  message: `the scene allows ${others.join(", ")} beside ${kind.name}: a count of the cells reached counts the piece's states alone`,
186
260
  });
187
261
  }
262
+ // A cell reached after counts of both parities would be two states, and counted twice.
263
+ if (start?.parity === true) {
264
+ errors.push({
265
+ path,
266
+ message: `the operation ${kind.name} keeps the parity of its moves: a count of the cells reached counts the piece's cells alone`,
267
+ });
268
+ }
188
269
  if (errors.length > before || least === undefined || most === undefined) {
189
270
  return undefined;
190
271
  }
191
- const moves = typeof start === "number" ? fewest(sheet, start) : [];
272
+ const moves = start === undefined ? [] : fewest(sheet, start.cell);
192
273
  const cells = new Set();
193
274
  moves.forEach((n, cell) => {
194
275
  if (n >= least && n <= most)
@@ -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[];