@altnovatrail/widgets 0.1.2 → 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.
Files changed (110) hide show
  1. package/README.md +1 -1
  2. package/cores/vector.schema.json +82 -3
  3. package/kinds/cells/cells.handles.json +148 -3
  4. package/kinds/cells/cells.schema.json +716 -8
  5. package/kinds/cells/vectors/bishop-grid-56-d1-1.json +105 -4
  6. package/kinds/cells/vectors/counter-cases.json +123 -0
  7. package/kinds/cells/vectors/counter-div-56-d1-3.json +57 -0
  8. package/kinds/cells/vectors/counter-div-56-d2-1.json +773 -0
  9. package/kinds/cells/vectors/counter-div-56-d2-3.json +65 -0
  10. package/kinds/cells/vectors/counter-div-56-d4-1.json +57 -0
  11. package/kinds/cells/vectors/counter-div-56-d4-2.json +106 -0
  12. package/kinds/cells/vectors/cut-cases.json +163 -0
  13. package/kinds/cells/vectors/cut-gaps-56-d2-1.json +142 -0
  14. package/kinds/cells/vectors/cut-grid-56-d2-4.json +45 -0
  15. package/kinds/cells/vectors/cut-grid-56-d4-1.json +154 -0
  16. package/kinds/cells/vectors/cut-grid-56-d4-2.json +136 -0
  17. package/kinds/cells/vectors/cut-invalid.json +48 -0
  18. package/kinds/cells/vectors/domino-grid-56-d3-2.json +238 -0
  19. package/kinds/cells/vectors/domino-limit.json +60 -0
  20. package/kinds/cells/vectors/domino-par-56-d3-2.json +194 -0
  21. package/kinds/cells/vectors/domino-par-56-d4-3.json +127 -0
  22. package/kinds/cells/vectors/figure-apart.json +47 -0
  23. package/kinds/cells/vectors/fold-12x12.json +79 -0
  24. package/kinds/cells/vectors/fold-14x14.json +79 -0
  25. package/kinds/cells/vectors/fold-26x26.json +79 -0
  26. package/kinds/cells/vectors/garden-enum-56-d3-4.json +96 -6
  27. package/kinds/cells/vectors/goat-grid-56-d4-4.json +325 -5
  28. package/kinds/cells/vectors/knight-after-no-parity.json +31 -0
  29. package/kinds/cells/vectors/knight-par-56-d2-2.json +61 -6
  30. package/kinds/cells/vectors/mosaic-grid-56-d2-2.json +84 -0
  31. package/kinds/cells/vectors/nodes-gaps-56-d2-2.json +44 -0
  32. package/kinds/cells/vectors/nodes-gaps-56-d4-2.json +70 -0
  33. package/kinds/cells/vectors/number-div-56-d2-2.json +49 -0
  34. package/kinds/cells/vectors/numbers-div-56-d3-1.json +24 -0
  35. package/kinds/cells/vectors/numbers-invalid.json +56 -0
  36. package/kinds/cells/vectors/paper-26x26.json +743 -0
  37. package/kinds/cells/vectors/paper-cube-12-d2-1.json +43 -19
  38. package/kinds/cells/vectors/paper-cube-12-d5-1.json +47 -4
  39. package/kinds/cells/vectors/paper-cube-56-d4-1.json +56 -5
  40. package/kinds/cells/vectors/paper-drawn-lines.json +184 -0
  41. package/kinds/cells/vectors/paper-grid-56-d1-2.json +46 -5
  42. package/kinds/cells/vectors/paper-turn-invalid.json +4 -4
  43. package/kinds/cells/vectors/path-6x6.json +119 -0
  44. package/kinds/cells/vectors/path-beside.json +108 -0
  45. package/kinds/cells/vectors/path-enum-12-d3-5.json +187 -0
  46. package/kinds/cells/vectors/path-enum-34-d4-3.json +412 -0
  47. package/kinds/cells/vectors/path-enum-56-d3-3.json +411 -0
  48. package/kinds/cells/vectors/path-enum-56-d3-5.json +351 -0
  49. package/kinds/cells/vectors/path-invalid.json +37 -0
  50. package/kinds/cells/vectors/path-limit.json +58 -0
  51. package/kinds/cells/vectors/path-maze.json +297 -0
  52. package/kinds/cells/vectors/path-unreachable.json +76 -0
  53. package/kinds/cells/vectors/perimeter-grid-56-d2-1.json +86 -0
  54. package/kinds/cells/vectors/perimeter-grid-56-d3-1.json +102 -0
  55. package/kinds/cells/vectors/perimeter-grid-56-d3-3.json +112 -0
  56. package/kinds/cells/vectors/perimeter-grid-56-d4-3.json +289 -0
  57. package/kinds/cells/vectors/polyomino-cases.json +191 -0
  58. package/kinds/cells/vectors/polyomino-grid-56-d3-4.json +205 -0
  59. package/kinds/cells/vectors/polyomino-grid-56-d4-1.json +137 -0
  60. package/kinds/cells/vectors/polyomino-grid-56-d5-3.json +157 -0
  61. package/kinds/cells/vectors/polyomino-invalid.json +45 -0
  62. package/kinds/cells/vectors/polyomino-one-pieces.json +28 -0
  63. package/kinds/cells/vectors/route-par-56-d3-3.json +245 -0
  64. package/kinds/cells/vectors/sides-gaps-56-d3-2.json +44 -0
  65. package/kinds/cells/vectors/sieve-div-56-d3-2.json +140 -0
  66. package/kinds/cells/vectors/tear-gaps-56-d3-1.json +84 -0
  67. package/kinds/cells/vectors/tear-limit.json +56 -0
  68. package/lib/cores/enumerate.d.ts +33 -0
  69. package/lib/cores/enumerate.js +77 -0
  70. package/lib/cores/kind.d.ts +70 -5
  71. package/lib/cores/kind.js +2 -2
  72. package/lib/cores/search.d.ts +47 -10
  73. package/lib/cores/search.js +129 -37
  74. package/lib/kinds/cells/bishop.d.ts +1 -1
  75. package/lib/kinds/cells/bishop.js +1 -1
  76. package/lib/kinds/cells/counter.d.ts +26 -0
  77. package/lib/kinds/cells/counter.js +249 -0
  78. package/lib/kinds/cells/cut.d.ts +46 -0
  79. package/lib/kinds/cells/cut.js +400 -0
  80. package/lib/kinds/cells/figure.d.ts +13 -0
  81. package/lib/kinds/cells/figure.js +114 -0
  82. package/lib/kinds/cells/holes.d.ts +6 -0
  83. package/lib/kinds/cells/holes.js +57 -0
  84. package/lib/kinds/cells/index.d.ts +8 -0
  85. package/lib/kinds/cells/index.js +10 -2
  86. package/lib/kinds/cells/kind.d.ts +3 -2
  87. package/lib/kinds/cells/kind.js +23 -8
  88. package/lib/kinds/cells/knight.d.ts +1 -1
  89. package/lib/kinds/cells/number.d.ts +6 -0
  90. package/lib/kinds/cells/number.js +32 -0
  91. package/lib/kinds/cells/operation.d.ts +27 -7
  92. package/lib/kinds/cells/paper.d.ts +10 -2
  93. package/lib/kinds/cells/paper.js +109 -13
  94. package/lib/kinds/cells/path.d.ts +38 -0
  95. package/lib/kinds/cells/path.js +288 -0
  96. package/lib/kinds/cells/piece.d.ts +27 -7
  97. package/lib/kinds/cells/piece.js +202 -37
  98. package/lib/kinds/cells/polyomino.d.ts +45 -0
  99. package/lib/kinds/cells/polyomino.js +461 -0
  100. package/lib/kinds/cells/registry.js +35 -4
  101. package/lib/kinds/cells/schema.js +45 -4
  102. package/lib/kinds/cells/sheet.d.ts +20 -1
  103. package/lib/kinds/cells/sheet.js +84 -6
  104. package/lib/kinds/cells/side.d.ts +33 -0
  105. package/lib/kinds/cells/side.js +86 -0
  106. package/lib/kinds/cells/sieve.d.ts +26 -0
  107. package/lib/kinds/cells/sieve.js +208 -0
  108. package/lib/kinds/cells/step.d.ts +1 -1
  109. package/lib/kinds/cells/step.js +1 -1
  110. package/package.json +5 -1
@@ -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"]);
@@ -66,7 +73,7 @@ export function cellsKind(registry, limit) {
66
73
  // map, which ones the scene meant is unknown: the goal reads them as all the kind's, and the
67
74
  // map's own error stands alone.
68
75
  const allowed = map(o.operations, "/operations", errors);
69
- const allows = new Set(allowed === undefined ? operations.keys() : []);
76
+ const allows = new Map(allowed === undefined ? [...operations.keys()].map((named) => [named, undefined]) : []);
70
77
  for (const [named, value] of Object.entries(allowed ?? {})) {
71
78
  const at = pointer("/operations", named);
72
79
  const op = operations.get(named);
@@ -74,11 +81,12 @@ export function cellsKind(registry, limit) {
74
81
  errors.push({ path: at, message: "no operation of the kind has this name" });
75
82
  continue;
76
83
  }
77
- allows.add(named);
84
+ allows.set(named, undefined);
78
85
  if (sheet !== undefined) {
79
86
  const read = part(op.read.bind(op), value, sheet, at, errors);
80
87
  if (read !== undefined) {
81
88
  chosen.set(named, read.options);
89
+ allows.set(named, read.options);
82
90
  }
83
91
  }
84
92
  }
@@ -125,6 +133,11 @@ export function cellsKind(registry, limit) {
125
133
  if (op.schema.state !== undefined && op.start !== undefined) {
126
134
  state[op.schema.state.member] = op.start(scene.sheet, options);
127
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
+ }
128
141
  }
129
142
  return state;
130
143
  },
@@ -144,7 +157,9 @@ export function cellsKind(registry, limit) {
144
157
  moves(scene, state) {
145
158
  return scene.operations.flatMap(({ op, options }) => op.moves(scene.sheet, options, state).map((move) => ({ [op.name]: move })));
146
159
  },
160
+ implied: (scene, state) => scene.operations.reduce((sum, { op, options }) => sum + (op.implied?.(scene.sheet, options, state) ?? 0), 0),
147
161
  hasGoal: (scene) => scene.goal !== null,
148
162
  meets: (scene, state) => scene.goal?.goal.meets(scene.sheet, scene.goal.options, state) ?? false,
163
+ asks: (scene) => scene.goal?.goal.asks?.(scene.sheet, scene.goal.options) ?? { key: "fewest" },
149
164
  };
150
165
  }
@@ -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>;
@@ -0,0 +1,6 @@
1
+ import type { Goal } from "./operation.js";
2
+ export interface NumberAtOptions {
3
+ /** The number the cell holds. */
4
+ readonly number: number;
5
+ }
6
+ export declare const numberAt: Goal<NumberAtOptions>;
@@ -0,0 +1,32 @@
1
+ // The goal of a number read off a table of numbers: the cell a statement finds by its row and its
2
+ // column, and the key the number it holds. Every state meets it, so on a table only to look at the
3
+ // key is read off the one state, as a statement's answer is read off the table.
4
+ import { object, pointer } from "../../cores/json.js";
5
+ import { readCell, readTable } from "./sheet.js";
6
+ export const numberAt = {
7
+ name: "number_at",
8
+ schema: {
9
+ description: "The number a cell of a table of numbers holds, found by its row and its column; every state meets it, and the key is the number.",
10
+ type: "object",
11
+ properties: {
12
+ cell: { description: "The cell, not a hole.", $ref: "#/$defs/cell" },
13
+ },
14
+ required: ["cell"],
15
+ unevaluatedProperties: false,
16
+ },
17
+ read(value, sheet, path, errors) {
18
+ const before = errors.length;
19
+ const o = object(value, path, errors, ["cell"]);
20
+ const first = readTable(sheet, path, errors, "no cell holds one to read");
21
+ const at = pointer(path, "cell");
22
+ const cell = o === undefined ? undefined : readCell(sheet, o.cell, at, errors);
23
+ if (cell !== undefined && sheet.holes.has(cell)) {
24
+ errors.push({ path: at, message: "a hole: its number is cut out" });
25
+ }
26
+ return errors.length > before || first === undefined || cell === undefined
27
+ ? undefined
28
+ : { number: first + cell };
29
+ },
30
+ meets: () => true,
31
+ asks: (_sheet, goal) => ({ key: "max", measure: () => goal.number }),
32
+ };
@@ -1,4 +1,4 @@
1
- import type { Json, Played, SceneError } from "../../cores/kind.js";
1
+ import type { Ask, Json, Played, SceneError } from "../../cores/kind.js";
2
2
  import type { Sheet, State } from "./sheet.js";
3
3
  /**
4
4
  * A part of the kind's JSON Schema, written by the conventions of the module format's schemas:
@@ -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". */
@@ -40,13 +54,19 @@ export interface Goal<Options = unknown> {
40
54
  readonly schema: Schema;
41
55
  /**
42
56
  * The parameters, or undefined with their errors added, as an operation's are read, by the sheet
43
- * and by the names of the operations the scene allows that the kind has — each named whether or
44
- * not its own parameters read, and every one the kind has where the scene's operations are no
45
- * map —, so that a goal which reads an operation's member of the state
46
- * refuses a scene without that operation; a goal that reads the marks alone ignores them.
57
+ * and by the operations the scene allows that the kind has, each by its name with its parameters
58
+ * as read: undefined where they do not read, and for every operation the kind has where the
59
+ * scene's operations are no map, the scene refused either way. So a goal which reads an
60
+ * operation's member of the state refuses a scene without that operation, and one that needs
61
+ * the operation's parameters reads them; a goal that reads the marks alone ignores them.
47
62
  */
48
- read(value: unknown, sheet: Sheet, path: string, errors: SceneError[], operations: ReadonlySet<string>): Options | undefined;
63
+ read(value: unknown, sheet: Sheet, path: string, errors: SceneError[], operations: ReadonlyMap<string, unknown>): Options | undefined;
49
64
  meets(sheet: Sheet, options: Options, state: State): boolean;
65
+ /**
66
+ * What the goal asks of the scene's state graph where it is not the fewest moves: a count of the
67
+ * states that meet it, or the greatest or least measure of one.
68
+ */
69
+ asks?(sheet: Sheet, options: Options): Ask<State>;
50
70
  }
51
71
  /** The operations and goals of the kind, and the version of its schema they make. */
52
72
  export interface Registry {
@@ -1,6 +1,7 @@
1
1
  import type { Operation } from "./operation.js";
2
+ import { type Sheet, type State } from "./sheet.js";
2
3
  /** A line of the grid: vertical between the columns k and k + 1, or horizontal between the rows. */
3
- interface PaperLine {
4
+ export interface PaperLine {
4
5
  readonly name: string;
5
6
  readonly vertical: boolean;
6
7
  readonly k: number;
@@ -8,6 +9,13 @@ interface PaperLine {
8
9
  export interface PaperOptions {
9
10
  /** The sheet's lines, by the order of the moves, read once with the scene. */
10
11
  readonly lines: readonly PaperLine[];
12
+ /** The lines drawn on the open sheet, which alone the paper folds along, where the scene gives them. */
13
+ readonly drawn?: readonly PaperLine[];
11
14
  }
15
+ /**
16
+ * The names of the lines the paper may be folded along at the state, as the scene draws them and
17
+ * the folds have laid them; undefined where the scene draws none, and every line of the sheet is
18
+ * one, or where the state's paper is no folded paper.
19
+ */
20
+ export declare function foldLines(sheet: Sheet, options: PaperOptions, state: State): ReadonlySet<string> | undefined;
12
21
  export declare const paper: Operation<PaperOptions>;
13
- export {};
@@ -6,8 +6,12 @@
6
6
  // reads it, and without it two folds that lay the same cells together reach one state. The punch
7
7
  // goes through every layer at a place and opens the sheet, which then holds the cells punched, and
8
8
  // no move of paper is played after it. Paper knows nothing of the scene's other operations, which
9
- // play on the sheet as it lies open, as two pieces know nothing of each other.
10
- import { isObject, object, pointer, string } from "../../cores/json.js";
9
+ // play on the sheet as it lies open, as two pieces know nothing of each other. A scene may give the
10
+ // lines the paper folds along, drawn on the open sheet as a statement draws them, so that the folds
11
+ // it asks are the only ones: a line drawn on a part laid over moves with it, and is folded along
12
+ // where it lies now. A statement's order of the folds is not kept, since the state keeps no count
13
+ // of them.
14
+ import { array, isObject, object, pointer, string } from "../../cores/json.js";
11
15
  import { gridLines, linePattern } from "./line.js";
12
16
  import { cellAt, cellName, readCell } from "./sheet.js";
13
17
  const sides = { vertical: ["left", "right"], horizontal: ["top", "bottom"] };
@@ -54,15 +58,21 @@ function extentOf(sheet, places) {
54
58
  }
55
59
  /**
56
60
  * The places that a fold along the line, laying the given side over the other, gives the cells;
57
- * or the rules' short reason to refuse it: a line outside the paper, a side that is not the line's,
58
- * 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.
59
66
  */
60
- function folded(sheet, places, line, lay) {
67
+ function folded(sheet, places, line, lay, drawn) {
61
68
  const e = extentOf(sheet, places);
62
69
  const [first, last] = line.vertical ? [e.c0, e.c1] : [e.r0, e.r1];
63
70
  if (line.k <= first || line.k > last) {
64
71
  return "no-line";
65
72
  }
73
+ if (drawn !== undefined && !drawn.has(line.name)) {
74
+ return "not-given";
75
+ }
66
76
  const [before, after] = line.vertical ? sides.vertical : sides.horizontal;
67
77
  if (lay !== before && lay !== after) {
68
78
  return "no-side";
@@ -85,6 +95,43 @@ function folded(sheet, places, line, lay) {
85
95
  return line.vertical ? row * sheet.cols + mirrored : mirrored * sheet.cols + col;
86
96
  });
87
97
  }
98
+ /**
99
+ * The names of the lines the drawn lines lie along now, the paper folded as the places say. A fold
100
+ * mirrors the part laid over, so two cells side by side across a drawn line lie side by side still,
101
+ * across the line it lies along now, or on one place once the paper was folded along it, which
102
+ * leaves it on the paper's edge. A pair with a hole tells nothing, and the next pair is read.
103
+ */
104
+ function drawnNow(sheet, places, drawn) {
105
+ const now = new Set();
106
+ for (const line of drawn) {
107
+ for (let i = 0; i < (line.vertical ? sheet.rows : sheet.cols); i++) {
108
+ const [a, b] = line.vertical
109
+ ? [i * sheet.cols + line.k - 1, i * sheet.cols + line.k]
110
+ : [(line.k - 1) * sheet.cols + i, line.k * sheet.cols + i];
111
+ const [pa, pb] = [places[a], places[b]];
112
+ if (pa === -1 || pb === -1) {
113
+ continue;
114
+ }
115
+ const [ra, rb] = [Math.floor(pa / sheet.cols), Math.floor(pb / sheet.cols)];
116
+ if (pa !== pb) {
117
+ now.add(ra === rb ? `v${Math.max(pa % sheet.cols, pb % sheet.cols)}` : `h${Math.max(ra, rb)}`);
118
+ }
119
+ break;
120
+ }
121
+ }
122
+ return now;
123
+ }
124
+ /**
125
+ * The names of the lines the paper may be folded along at the state, as the scene draws them and
126
+ * the folds have laid them; undefined where the scene draws none, and every line of the sheet is
127
+ * one, or where the state's paper is no folded paper.
128
+ */
129
+ export function foldLines(sheet, options, state) {
130
+ const places = placesOf(sheet, state);
131
+ return options.drawn === undefined || !Array.isArray(places)
132
+ ? undefined
133
+ : drawnNow(sheet, places, options.drawn);
134
+ }
88
135
  /** The cells that lie on the place: those a punch there goes through. */
89
136
  const under = (places, place) => places.flatMap((at, cell) => (at === place ? [cell] : []));
90
137
  function foldedJson(sheet, places) {
@@ -107,8 +154,21 @@ export const paper = {
107
154
  name: "paper",
108
155
  schema: {
109
156
  options: {
110
- description: "Fold the sheet over in layers along lines of its grid, each fold laying the part on one side over the other, no wider than it; then punch a place through every layer, which opens the sheet. No parameters.",
157
+ description: "Fold the sheet over in layers along lines of its grid, each fold laying the part on one side over the other, no wider than it; then punch a place through every layer, which opens the sheet.",
111
158
  type: "object",
159
+ properties: {
160
+ lines: {
161
+ description: "The lines the paper may be folded along, as the statement draws them on the open sheet, each moving with a part laid over; every line of the sheet where the scene gives none. Their order is no order of the folds.",
162
+ type: "array",
163
+ items: {
164
+ description: 'A line of the sheet, "v<k>" between the columns k and k + 1, or "h<k>" between the rows k and k + 1, counted from 1.',
165
+ type: "string",
166
+ pattern: linePattern.source,
167
+ },
168
+ minItems: 1,
169
+ uniqueItems: true,
170
+ },
171
+ },
112
172
  unevaluatedProperties: false,
113
173
  },
114
174
  move: {
@@ -168,16 +228,34 @@ export const paper = {
168
228
  },
169
229
  read(value, sheet, path, errors) {
170
230
  const before = errors.length;
171
- object(value, path, errors, []);
172
- if (errors.length > before) {
173
- return undefined;
174
- }
231
+ const o = object(value, path, errors, [], ["lines"]);
175
232
  const lines = gridLines(sheet).map(({ name }) => ({
176
233
  name,
177
234
  vertical: name.startsWith("v"),
178
235
  k: Number(name.slice(1)),
179
236
  }));
180
- return { lines };
237
+ if (o?.lines === undefined) {
238
+ return errors.length > before ? undefined : { lines };
239
+ }
240
+ const linesPath = pointer(path, "lines");
241
+ const given = [];
242
+ array(o.lines, linesPath, errors, 1)?.forEach((item, i) => {
243
+ const at = pointer(linesPath, i);
244
+ const name = string(item, at, errors, linePattern, "a line, v<k> or h<k>");
245
+ if (name === undefined) {
246
+ return;
247
+ }
248
+ if (given.includes(name)) {
249
+ errors.push({ path: at, message: "a line named twice" });
250
+ }
251
+ else if (!lines.some((line) => line.name === name)) {
252
+ errors.push({ path: at, message: "no line of the sheet" });
253
+ }
254
+ given.push(name);
255
+ });
256
+ return errors.length > before
257
+ ? undefined
258
+ : { lines, drawn: lines.filter((line) => given.includes(line.name)) };
181
259
  },
182
260
  start: () => ({ folded: {} }),
183
261
  play(sheet, options, state, move) {
@@ -200,7 +278,8 @@ export const paper = {
200
278
  const name = string(fold.line, "/fold/line", errors, linePattern, "");
201
279
  const line = options.lines.find((l) => l.name === name);
202
280
  const lay = typeof fold.lay === "string" ? fold.lay : "";
203
- const after = line === undefined ? "no-line" : folded(sheet, places, line, lay);
281
+ const drawn = options.drawn === undefined ? undefined : drawnNow(sheet, places, options.drawn);
282
+ const after = line === undefined ? "no-line" : folded(sheet, places, line, lay, drawn);
204
283
  return typeof after === "string"
205
284
  ? refuse(after)
206
285
  : { ok: true, state: { ...state, paper: foldedJson(sheet, after) } };
@@ -221,15 +300,32 @@ export const paper = {
221
300
  const punched = cells.map((cell) => cellName(sheet, cell));
222
301
  return { ok: true, state: { ...state, paper: { punched } } };
223
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
+ },
224
319
  moves(sheet, options, state) {
225
320
  const places = placesOf(sheet, state);
226
321
  if (places === undefined || places === "opened") {
227
322
  return [];
228
323
  }
229
324
  const moves = [];
325
+ const drawn = options.drawn === undefined ? undefined : drawnNow(sheet, places, options.drawn);
230
326
  for (const line of options.lines) {
231
327
  for (const lay of line.vertical ? sides.vertical : sides.horizontal) {
232
- if (typeof folded(sheet, places, line, lay) !== "string") {
328
+ if (typeof folded(sheet, places, line, lay, drawn) !== "string") {
233
329
  moves.push({ fold: { line: line.name, lay } });
234
330
  }
235
331
  }
@@ -0,0 +1,38 @@
1
+ import type { Goal, Operation } from "./operation.js";
2
+ import { type Sheet, type State } from "./sheet.js";
3
+ export declare const directions: readonly ["up", "left", "right", "down"];
4
+ export type Direction = (typeof directions)[number];
5
+ export interface PathOptions {
6
+ /** The cell the path starts on, by its index. */
7
+ readonly from: number;
8
+ /** The directions a step may go in. */
9
+ readonly directions: ReadonlySet<Direction>;
10
+ }
11
+ /**
12
+ * The cells of the state's path by index, or undefined where it is no path the rules draw: one
13
+ * that starts elsewhere than the scene's cell, or takes a step they refuse. A state reported from
14
+ * outside the kind may hold one, and then no step is drawn from it and no goal is met.
15
+ */
16
+ export declare function pathOf(sheet: Sheet, options: PathOptions, state: State): readonly number[] | undefined;
17
+ /** Whether a path is closed: back on its first cell. */
18
+ export declare function isClosed(path: readonly number[]): boolean;
19
+ export declare const path: Operation<PathOptions>;
20
+ export interface PathEnd {
21
+ /**
22
+ * The path's own parameters, by which the goal reads the state's path; undefined only where they
23
+ * fail to read, and the scene is refused for them.
24
+ */
25
+ readonly path?: PathOptions;
26
+ /** The cell the path is to end on, by its index. */
27
+ readonly cell: number;
28
+ /** The cells it is to pass on its way, by their indices. */
29
+ readonly through: readonly number[];
30
+ }
31
+ /** The path ends on the cell, past the cells named; the key is the fewest steps. */
32
+ export declare const pathTo: Goal<PathEnd>;
33
+ /** As path_to, and the key counts the paths: the scene allows the path alone. */
34
+ export declare const pathCount: Goal<PathEnd>;
35
+ /** The path is closed into a route; the key is the most cells a route visits. */
36
+ export declare const pathClosed: Goal<{
37
+ readonly path?: PathOptions;
38
+ }>;