@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.
@@ -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)
@@ -371,8 +371,13 @@ export const polyomino = {
371
371
  .map((placing) => ({ lay: { cells: placing.map((cell) => cellName(sheet, cell)) } }));
372
372
  },
373
373
  };
374
+ const said = {
375
+ beside: "",
376
+ alone: " alone, so that its states are the figures",
377
+ one: " alone with pieces 1, so that its states are the one figure's places",
378
+ };
374
379
  /** The goal's figures, the polyomino's parameters given, or an error where the scene lays none. */
375
- function readFigures(value, path, errors, operations, alone) {
380
+ function readFigures(value, path, errors, operations, takes) {
376
381
  const before = errors.length;
377
382
  object(value, path, errors, []);
378
383
  if (!operations.has("polyomino")) {
@@ -381,13 +386,19 @@ function readFigures(value, path, errors, operations, alone) {
381
386
  const others = [...operations]
382
387
  .filter(([named, options]) => named !== "polyomino" && options !== undefined)
383
388
  .map(([named]) => named);
384
- if (alone && operations.get("polyomino") !== undefined && others.length > 0) {
389
+ if (takes !== "beside" && operations.get("polyomino") !== undefined && others.length > 0) {
385
390
  errors.push({
386
391
  path,
387
392
  message: `the scene allows ${others.join(", ")} beside polyomino: a count of figures counts the figures' states alone`,
388
393
  });
389
394
  }
390
395
  const options = operations.get("polyomino");
396
+ if (takes === "one" && options !== undefined && options.pieces !== 1) {
397
+ errors.push({
398
+ path,
399
+ message: "a count of one figure takes pieces 1 of the operation polyomino, so that no second figure lies at once",
400
+ });
401
+ }
391
402
  return errors.length > before ? undefined : options === undefined ? {} : { polyomino: options };
392
403
  }
393
404
  /** The figures of the state as a goal reads them, by the figures' parameters where they read. */
@@ -396,15 +407,15 @@ function figuresOf(sheet, goal, state) {
396
407
  }
397
408
  /** The cells of the sheet that are no hole. */
398
409
  const free = (sheet) => sheet.rows * sheet.cols - sheet.holes.size;
399
- function goalOf(name, description, alone, meets, asks) {
410
+ function goalOf(name, description, takes, meets, asks) {
400
411
  return {
401
412
  name,
402
413
  schema: {
403
- description: `${description} The scene allows the operation polyomino${alone ? " alone, so that its states are the figures" : ""}.`,
414
+ description: `${description} The scene allows the operation polyomino${said[takes]}.`,
404
415
  type: "object",
405
416
  unevaluatedProperties: false,
406
417
  },
407
- read: (value, _sheet, path, errors, operations) => readFigures(value, path, errors, operations, alone),
418
+ read: (value, _sheet, path, errors, operations) => readFigures(value, path, errors, operations, takes),
408
419
  meets(sheet, goal, state) {
409
420
  const laid = figuresOf(sheet, goal, state);
410
421
  return laid !== undefined && meets(sheet, laid);
@@ -415,23 +426,23 @@ function goalOf(name, description, alone, meets, asks) {
415
426
  const covers = (sheet, laid) => laid.covered.size === free(sheet);
416
427
  const one = (_sheet, laid) => laid.figures.length === 1;
417
428
  /** The figures cover every cell but the holes; the key is the fewest figures. */
418
- export const polyominoCover = goalOf("polyomino_cover", "The figures cover every cell that is no hole; the key is the fewest figures laid.", false, covers);
429
+ export const polyominoCover = goalOf("polyomino_cover", "The figures cover every cell that is no hole; the key is the fewest figures laid.", "beside", covers);
419
430
  /** As polyomino_cover, and the key counts the ways. */
420
- export const polyominoTilings = goalOf("polyomino_tilings", "The figures cover every cell that is no hole; the key counts the ways, two ways one where their figures cover the same cells.", true, covers, () => ({ key: "count" }));
431
+ export const polyominoTilings = goalOf("polyomino_tilings", "The figures cover every cell that is no hole; the key counts the ways, two ways one where their figures cover the same cells.", "alone", covers, () => ({ key: "count" }));
421
432
  /** Any figures; the key is the most figures that lie on the sheet at once. */
422
- export const polyominoMost = goalOf("polyomino_most", "Met by every state; the key is the most figures that lie on the sheet at once.", false, () => true, (sheet, goal) => ({
433
+ export const polyominoMost = goalOf("polyomino_most", "Met by every state; the key is the most figures that lie on the sheet at once.", "beside", () => true, (sheet, goal) => ({
423
434
  key: "max",
424
435
  measure: (state) => figuresOf(sheet, goal, state)?.figures.length ?? 0,
425
436
  }));
426
437
  /** Any figures; the key is the fewest cells left empty, no figure on them, the holes aside. */
427
- export const polyominoEmpty = goalOf("polyomino_empty", "Met by every state; the key is the fewest cells that are no hole and no figure covers.", false, () => true, (sheet, goal) => ({
438
+ export const polyominoEmpty = goalOf("polyomino_empty", "Met by every state; the key is the fewest cells that are no hole and no figure covers.", "beside", () => true, (sheet, goal) => ({
428
439
  key: "min",
429
440
  measure: (state) => free(sheet) - (figuresOf(sheet, goal, state)?.covered.size ?? 0),
430
441
  }));
431
442
  /** One figure on the sheet; the key counts its places. */
432
- export const polyominoPlacings = goalOf("polyomino_placings", "One figure lies on the sheet; the key counts the places it may lie on, two places one where they cover the same cells.", true, one, () => ({ key: "count" }));
443
+ export const polyominoPlacings = goalOf("polyomino_placings", "One figure lies on the sheet; the key counts the places it may lie on, two places one where they cover the same cells.", "one", one, () => ({ key: "count" }));
433
444
  /** One figure on the sheet; the key counts its shapes up to a move, a turn and a turn over. */
434
- export const polyominoShapes = goalOf("polyomino_shapes", "One figure lies on the sheet; the key counts its shapes, two figures one shape where one moved, turned or turned over lies on the other.", true, one, (sheet, goal) => ({ key: "count", images: (state) => sameShape(sheet, goal, state) }));
445
+ export const polyominoShapes = goalOf("polyomino_shapes", "One figure lies on the sheet; the key counts its shapes, two figures one shape where one moved, turned or turned over lies on the other.", "one", one, (sheet, goal) => ({ key: "count", images: (state) => sameShape(sheet, goal, state) }));
435
446
  /**
436
447
  * Every state with one figure of the state's own figure's shape, wherever on the sheet off its
437
448
  * holes it lies, moved, turned or turned over: the class a count of shapes counts once.
@@ -17,7 +17,7 @@ import { sieve, sieveLeast } from "./sieve.js";
17
17
  import { step, stepOn, stepReach } from "./step.js";
18
18
  import { mirrorSymmetric, quarterTurnSymmetric } from "./symmetric.js";
19
19
  export const registry = {
20
- version: "2.5",
20
+ version: "3.1",
21
21
  operations: [fold, paint, step, knight, bishop, paper, counter, sieve, path, cut, polyomino],
22
22
  goals: [
23
23
  allPainted,
@@ -28,6 +28,9 @@ export function cellsSchema(registry) {
28
28
  if (op.schema.state !== undefined) {
29
29
  state[op.schema.state.member] = op.schema.state.schema;
30
30
  }
31
+ if (op.schema.state?.beside !== undefined) {
32
+ state[op.schema.state.beside.member] = op.schema.state.beside.schema;
33
+ }
31
34
  }
32
35
  const goals = {};
33
36
  for (const goal of registry.goals) {
@@ -166,7 +169,7 @@ export function cellsSchema(registry) {
166
169
  maxProperties: 1,
167
170
  },
168
171
  state: {
169
- description: "A state: the cells' marks, and a member for each operation of the scene that keeps one.",
172
+ description: "A state: the cells' marks, and a member for each operation of the scene that keeps one, with a second beside it where the scene's parameters of the operation ask for it.",
170
173
  type: "object",
171
174
  properties: state,
172
175
  required: ["marks"],
@@ -1 +1 @@
1
- export declare const step: import("./operation.js").Operation<number>, stepOn: import("./operation.js").Goal<number>, stepReach: import("./operation.js").Goal<import("./piece.js").Reach>;
1
+ export declare const step: import("./operation.js").Operation<import("./piece.js").Start>, stepOn: import("./operation.js").Goal<import("./piece.js").On>, stepReach: import("./operation.js").Goal<import("./piece.js").Reach>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altnovatrail/widgets",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "NovaTrail's widget kinds: each kind's rules and solver over its states, and the shell they share.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {