@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.
- package/README.md +1 -1
- package/kinds/cells/cells.handles.json +3 -3
- package/kinds/cells/cells.schema.json +69 -9
- package/kinds/cells/vectors/knight-after-no-parity.json +31 -0
- package/kinds/cells/vectors/knight-par-56-d2-2.json +61 -6
- package/kinds/cells/vectors/paper-drawn-lines.json +36 -1
- package/kinds/cells/vectors/polyomino-one-pieces.json +28 -0
- package/kinds/line/line.handles.json +195 -0
- package/kinds/line/line.schema.json +321 -0
- package/kinds/line/vectors/cases.json +213 -0
- package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
- package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
- package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
- package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
- package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
- package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
- package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
- package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
- package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
- package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
- package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
- package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
- package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
- package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
- package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
- package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
- package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
- package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
- package/kinds/line/vectors/invalid-line.json +34 -0
- package/kinds/line/vectors/invalid-ring.json +34 -0
- package/kinds/line/vectors/invalid.json +43 -0
- package/kinds/line/vectors/jump-unreachable.json +104 -0
- package/kinds/line/vectors/limit.json +40 -0
- package/kinds/line/vectors/op-not-in-scene.json +47 -0
- package/kinds/line/vectors/ring-cut.json +104 -0
- package/kinds/line/vectors/ring-every.json +107 -0
- package/kinds/line/vectors/ring-jump.json +135 -0
- package/lib/cores/arith.d.ts +62 -0
- package/lib/cores/arith.js +197 -0
- package/lib/cores/enumerate.js +2 -2
- package/lib/cores/kind.d.ts +28 -3
- package/lib/cores/kind.js +2 -2
- package/lib/cores/registry.d.ts +43 -0
- package/lib/cores/registry.js +87 -0
- package/lib/cores/search.d.ts +21 -3
- package/lib/cores/search.js +80 -17
- package/lib/kinds/cells/bishop.d.ts +1 -1
- package/lib/kinds/cells/index.d.ts +8 -0
- package/lib/kinds/cells/index.js +10 -2
- package/lib/kinds/cells/kind.d.ts +3 -2
- package/lib/kinds/cells/kind.js +26 -66
- package/lib/kinds/cells/knight.d.ts +1 -1
- package/lib/kinds/cells/operation.d.ts +21 -1
- package/lib/kinds/cells/paper.js +27 -13
- package/lib/kinds/cells/piece.d.ts +17 -6
- package/lib/kinds/cells/piece.js +119 -38
- package/lib/kinds/cells/polyomino.d.ts +14 -0
- package/lib/kinds/cells/polyomino.js +122 -35
- package/lib/kinds/cells/registry.js +1 -1
- package/lib/kinds/cells/schema.js +4 -1
- package/lib/kinds/cells/step.d.ts +1 -1
- package/lib/kinds/line/cut.d.ts +2 -0
- package/lib/kinds/line/cut.js +63 -0
- package/lib/kinds/line/goals.d.ts +22 -0
- package/lib/kinds/line/goals.js +137 -0
- package/lib/kinds/line/index.d.ts +13 -0
- package/lib/kinds/line/index.js +17 -0
- package/lib/kinds/line/jump.d.ts +10 -0
- package/lib/kinds/line/jump.js +95 -0
- package/lib/kinds/line/kind.d.ts +25 -0
- package/lib/kinds/line/kind.js +125 -0
- package/lib/kinds/line/line.d.ts +63 -0
- package/lib/kinds/line/line.js +157 -0
- package/lib/kinds/line/operation.d.ts +69 -0
- package/lib/kinds/line/operation.js +1 -0
- package/lib/kinds/line/posts.d.ts +11 -0
- package/lib/kinds/line/posts.js +85 -0
- package/lib/kinds/line/registry.d.ts +2 -0
- package/lib/kinds/line/registry.js +11 -0
- package/lib/kinds/line/schema.d.ts +4 -0
- package/lib/kinds/line/schema.js +157 -0
- package/package.json +9 -2
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// A jumper on the line — a frog on stones, a boy on slabs, a girl going up floor by floor — that
|
|
2
|
+
// stands on a place and jumps a step of places at a time: forward, towards greater numbers, and
|
|
3
|
+
// back where the scene allows it, never off an open line; on a ring it goes on round it, past the
|
|
4
|
+
// last place to the first. The state keeps its place under "at".
|
|
5
|
+
import { divideWithRemainder } from "../../cores/arith.js";
|
|
6
|
+
import { integer, object, pointer } from "../../cores/json.js";
|
|
7
|
+
import { lastPlace, maxPlaces, placeOn, readBoolean, readPlace, } from "./line.js";
|
|
8
|
+
const directions = ["forward", "back"];
|
|
9
|
+
function refuse(reason) {
|
|
10
|
+
return { ok: false, reason };
|
|
11
|
+
}
|
|
12
|
+
/** The jumper's place in a state; its start where the state holds no place of the line. */
|
|
13
|
+
function placeOf(line, jumper, state) {
|
|
14
|
+
const at = state.at;
|
|
15
|
+
return Number.isInteger(at) && at >= line.first && at <= lastPlace(line)
|
|
16
|
+
? at
|
|
17
|
+
: jumper.from;
|
|
18
|
+
}
|
|
19
|
+
export const jump = {
|
|
20
|
+
name: "jump",
|
|
21
|
+
schema: {
|
|
22
|
+
options: {
|
|
23
|
+
description: "A jumper that starts on a place and jumps a step of places at a time, forward and, where the scene allows it, back, never off an open line and round a ring.",
|
|
24
|
+
type: "object",
|
|
25
|
+
properties: {
|
|
26
|
+
from: { description: "The place it starts on, by its number.", $ref: "#/$defs/place" },
|
|
27
|
+
step: {
|
|
28
|
+
description: "The places one jump spans, fewer than the line's places.",
|
|
29
|
+
type: "integer",
|
|
30
|
+
minimum: 1,
|
|
31
|
+
maximum: maxPlaces - 1,
|
|
32
|
+
},
|
|
33
|
+
back: {
|
|
34
|
+
description: "Whether it may jump back, towards smaller numbers, too; false where left out.",
|
|
35
|
+
type: "boolean",
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
required: ["from", "step"],
|
|
39
|
+
unevaluatedProperties: false,
|
|
40
|
+
},
|
|
41
|
+
move: {
|
|
42
|
+
description: "A jump: forward, towards greater numbers, or back.",
|
|
43
|
+
enum: [...directions],
|
|
44
|
+
},
|
|
45
|
+
state: {
|
|
46
|
+
member: "at",
|
|
47
|
+
schema: { description: "The place the jumper stands on.", $ref: "#/$defs/place" },
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
read(value, line, path, errors) {
|
|
51
|
+
const before = errors.length;
|
|
52
|
+
const o = object(value, path, errors, ["from", "step"], ["back"]);
|
|
53
|
+
if (o === undefined) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const from = readPlace(line, o.from, pointer(path, "from"), errors);
|
|
57
|
+
const step = integer(o.step, pointer(path, "step"), errors, 1, line.places - 1);
|
|
58
|
+
const back = o.back === undefined ? false : readBoolean(o.back, pointer(path, "back"), errors);
|
|
59
|
+
return from === undefined || step === undefined || back === undefined || errors.length > before
|
|
60
|
+
? undefined
|
|
61
|
+
: { from, step, back };
|
|
62
|
+
},
|
|
63
|
+
start: (_line, jumper) => jumper.from,
|
|
64
|
+
play(line, jumper, state, move) {
|
|
65
|
+
if (move !== "forward" && move !== "back") {
|
|
66
|
+
return refuse("not-a-jump");
|
|
67
|
+
}
|
|
68
|
+
if (move === "back" && !jumper.back) {
|
|
69
|
+
return refuse("no-back");
|
|
70
|
+
}
|
|
71
|
+
const to = placeOn(line, placeOf(line, jumper, state), move === "forward" ? jumper.step : -jumper.step);
|
|
72
|
+
if (to < line.first || to > lastPlace(line)) {
|
|
73
|
+
return refuse("off-line");
|
|
74
|
+
}
|
|
75
|
+
return { ok: true, state: { ...state, at: to } };
|
|
76
|
+
},
|
|
77
|
+
moves: (_line, jumper) => (jumper.back ? directions : ["forward"]),
|
|
78
|
+
counters: {
|
|
79
|
+
at: {
|
|
80
|
+
description: "The number of the place the jumper stands on.",
|
|
81
|
+
value: (line, jumper, state) => placeOf(line, jumper, state),
|
|
82
|
+
},
|
|
83
|
+
distance: {
|
|
84
|
+
description: "The length from the jumper's start to where it stands, their count of places apart times the spacing; on a ring, the shorter way round.",
|
|
85
|
+
value(line, jumper, state) {
|
|
86
|
+
const apart = placeOf(line, jumper, state) - jumper.from;
|
|
87
|
+
if (!line.closed) {
|
|
88
|
+
return Math.abs(apart) * line.spacing;
|
|
89
|
+
}
|
|
90
|
+
const on = divideWithRemainder(apart, line.places).remainder;
|
|
91
|
+
return Math.min(on, line.places - on) * line.spacing;
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Json, Kind } from "../../cores/kind.js";
|
|
2
|
+
import { type Line, type State } from "./line.js";
|
|
3
|
+
import type { Goal, Operation, Registry } from "./operation.js";
|
|
4
|
+
export interface Scene {
|
|
5
|
+
readonly line: Line;
|
|
6
|
+
/** The operations the scene allows, with their parameters, in the registry's order. */
|
|
7
|
+
readonly operations: readonly {
|
|
8
|
+
readonly op: Operation;
|
|
9
|
+
readonly options: unknown;
|
|
10
|
+
}[];
|
|
11
|
+
readonly goal: {
|
|
12
|
+
readonly goal: Goal;
|
|
13
|
+
readonly options: unknown;
|
|
14
|
+
} | null;
|
|
15
|
+
}
|
|
16
|
+
/** A move: one member, named by its operation, holding what the operation's move holds. */
|
|
17
|
+
export type Move = {
|
|
18
|
+
readonly [operation: string]: Json;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The kind line 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.
|
|
23
|
+
* Throws where the registry repeats a name, a member of the state or a counter.
|
|
24
|
+
*/
|
|
25
|
+
export declare function lineKind(registry: Registry, limit: number, workLimit?: number): Kind<Scene, State, Move>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// The kind line, assembled from a registry of operations and goals: a scene is a line, the
|
|
2
|
+
// operations it allows with their parameters, and at most one goal. A move names one operation,
|
|
3
|
+
// which plays it by its own rules; the kind only reads the line and dispatches.
|
|
4
|
+
import { map, object, pointer } from "../../cores/json.js";
|
|
5
|
+
import { checkRegistry, dispatch, inRegistryOrder, readGoal, readPart, takeName, } from "../../cores/registry.js";
|
|
6
|
+
import { readLine } from "./line.js";
|
|
7
|
+
import { lineCounters } from "./posts.js";
|
|
8
|
+
/**
|
|
9
|
+
* Throws where the registry could not make a kind: a name given twice, a member of the state or a
|
|
10
|
+
* counter shared.
|
|
11
|
+
*/
|
|
12
|
+
function check(registry) {
|
|
13
|
+
checkRegistry("line", registry.version, [...registry.operations, ...registry.goals]);
|
|
14
|
+
const members = new Set(["posts"]);
|
|
15
|
+
const counted = new Set(Object.keys(lineCounters));
|
|
16
|
+
for (const op of registry.operations) {
|
|
17
|
+
const member = op.schema.state?.member;
|
|
18
|
+
if ((member === undefined) !== (op.start === undefined)) {
|
|
19
|
+
throw new Error(`the operation ${op.name} keeps a member of the state and starts it, or neither`);
|
|
20
|
+
}
|
|
21
|
+
if (member !== undefined) {
|
|
22
|
+
takeName(members, member, "a member of the state");
|
|
23
|
+
}
|
|
24
|
+
for (const counter of Object.keys(op.counters ?? {})) {
|
|
25
|
+
takeName(counted, counter, "a counter's name");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** The counters of an operation bound to a scene's line and parameters, by their names. */
|
|
30
|
+
function bound(op, line, options) {
|
|
31
|
+
return Object.entries(op.counters ?? {}).map(([named, counter]) => [
|
|
32
|
+
named,
|
|
33
|
+
(state) => counter.value(line, options, state),
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The kind line with the operations and goals of the registry and the given limits of states and
|
|
38
|
+
* of work, none of work where none is given.
|
|
39
|
+
* Throws where the registry repeats a name, a member of the state or a counter.
|
|
40
|
+
*/
|
|
41
|
+
export function lineKind(registry, limit, workLimit = Number.MAX_SAFE_INTEGER) {
|
|
42
|
+
check(registry);
|
|
43
|
+
const operations = new Map(registry.operations.map((op) => [op.name, op]));
|
|
44
|
+
const goals = new Map(registry.goals.map((goal) => [goal.name, goal]));
|
|
45
|
+
return {
|
|
46
|
+
limit,
|
|
47
|
+
workLimit,
|
|
48
|
+
read(input) {
|
|
49
|
+
const errors = [];
|
|
50
|
+
const o = object(input, "", errors, ["line", "operations"], ["goal"]);
|
|
51
|
+
if (o === undefined) {
|
|
52
|
+
return { ok: false, errors };
|
|
53
|
+
}
|
|
54
|
+
const line = readLine(o.line, "/line", errors);
|
|
55
|
+
// The counters the scene has: the line's own, and those of each operation it names that the
|
|
56
|
+
// kind has, a count undefined where the line or the parameters do not read. Where the
|
|
57
|
+
// operations are no map, which ones the scene meant is unknown: the goal reads every
|
|
58
|
+
// operation's counters, and the map's own error stands alone.
|
|
59
|
+
const counters = new Map(Object.entries(lineCounters).map(([named, counter]) => [
|
|
60
|
+
named,
|
|
61
|
+
line === undefined ? undefined : (state) => counter.value(line, undefined, state),
|
|
62
|
+
]));
|
|
63
|
+
const chosen = new Map();
|
|
64
|
+
const allowed = map(o.operations, "/operations", errors);
|
|
65
|
+
if (allowed === undefined) {
|
|
66
|
+
for (const op of registry.operations) {
|
|
67
|
+
for (const named of Object.keys(op.counters ?? {}))
|
|
68
|
+
counters.set(named, undefined);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
for (const [named, value] of Object.entries(allowed ?? {})) {
|
|
72
|
+
const at = pointer("/operations", named);
|
|
73
|
+
const op = operations.get(named);
|
|
74
|
+
if (op === undefined) {
|
|
75
|
+
errors.push({ path: at, message: "no operation of the kind has this name" });
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
for (const counter of Object.keys(op.counters ?? {}))
|
|
79
|
+
counters.set(counter, undefined);
|
|
80
|
+
const read = line === undefined
|
|
81
|
+
? undefined
|
|
82
|
+
: readPart((v, p, e) => op.read(v, line, p, e), value, at, errors);
|
|
83
|
+
if (line !== undefined && read !== undefined) {
|
|
84
|
+
chosen.set(named, read.options);
|
|
85
|
+
for (const [counter, count] of bound(op, line, read.options)) {
|
|
86
|
+
counters.set(counter, count);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const goal = readGoal(o.goal, goals, errors, line === undefined
|
|
91
|
+
? undefined
|
|
92
|
+
: (found, v, p, e) => found.read(v, line, p, e, counters));
|
|
93
|
+
if (errors.length > 0 || line === undefined) {
|
|
94
|
+
return { ok: false, errors };
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
ok: true,
|
|
98
|
+
scene: {
|
|
99
|
+
line,
|
|
100
|
+
operations: inRegistryOrder(registry.operations, chosen),
|
|
101
|
+
goal,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
start(scene) {
|
|
106
|
+
const state = { posts: [...scene.line.posts] };
|
|
107
|
+
for (const { op, options } of scene.operations) {
|
|
108
|
+
if (op.schema.state !== undefined && op.start !== undefined) {
|
|
109
|
+
state[op.schema.state.member] = op.start(scene.line, options);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return state;
|
|
113
|
+
},
|
|
114
|
+
// The kind's moves draw nothing at random, so it reads nothing of the version or the seed.
|
|
115
|
+
play(scene, state, move) {
|
|
116
|
+
return dispatch(scene.operations, operations, move, (op, options, held) => op.play(scene.line, options, state, held));
|
|
117
|
+
},
|
|
118
|
+
moves(scene, state) {
|
|
119
|
+
return scene.operations.flatMap(({ op, options }) => op.moves(scene.line, options, state).map((move) => ({ [op.name]: move })));
|
|
120
|
+
},
|
|
121
|
+
hasGoal: (scene) => scene.goal !== null,
|
|
122
|
+
meets: (scene, state) => scene.goal?.goal.meets(scene.line, scene.goal.options, state) ?? false,
|
|
123
|
+
asks: (scene) => scene.goal?.goal.asks?.(scene.line, scene.goal.options) ?? { key: "fewest" },
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Json, SceneError } from "../../cores/kind.js";
|
|
2
|
+
/** The most places a line holds: the numbers from 0 to 100 among them. */
|
|
3
|
+
export declare const maxPlaces = 101;
|
|
4
|
+
/** The greatest number of a place, the last place's too. */
|
|
5
|
+
export declare const maxNumber = 10000;
|
|
6
|
+
/** The greatest spacing between two neighbouring places. */
|
|
7
|
+
export declare const maxSpacing = 10000;
|
|
8
|
+
export interface Line {
|
|
9
|
+
/** The count of places. */
|
|
10
|
+
readonly places: number;
|
|
11
|
+
/** The number of the first place; each next place has the next number. */
|
|
12
|
+
readonly first: number;
|
|
13
|
+
/** The length from a place to the next, in the statement's unit. */
|
|
14
|
+
readonly spacing: number;
|
|
15
|
+
/** Whether the line is drawn upright, its first place at the bottom: floors, stairs, a well. */
|
|
16
|
+
readonly vertical: boolean;
|
|
17
|
+
/** Whether the line is closed into a ring, its last place next to its first. */
|
|
18
|
+
readonly closed: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The ring's sides, each by its count of gaps, where it is drawn as a figure with corners — a
|
|
21
|
+
* square flowerbed, a garden's fence —, the first place a corner and each next corner a side on;
|
|
22
|
+
* null for a round ring and for an open line. It changes no rule.
|
|
23
|
+
*/
|
|
24
|
+
readonly sides: readonly number[] | null;
|
|
25
|
+
/** The places a post stands on at the scene's start, by their numbers, in ascending order. */
|
|
26
|
+
readonly posts: readonly number[];
|
|
27
|
+
}
|
|
28
|
+
/** A state of the kind: the posts under "posts", and a member for each operation that keeps one. */
|
|
29
|
+
export type State = {
|
|
30
|
+
readonly [member: string]: Json;
|
|
31
|
+
};
|
|
32
|
+
/** The number of the line's last place. */
|
|
33
|
+
export declare function lastPlace(line: Line): number;
|
|
34
|
+
/**
|
|
35
|
+
* The number of the place a count of places on from a place, back where the count is negative: on
|
|
36
|
+
* a ring counted round it, by the core of exact arithmetic; on an open line the plain sum, which
|
|
37
|
+
* may fall off the line.
|
|
38
|
+
*/
|
|
39
|
+
export declare function placeOn(line: Line, place: number, by: number): number;
|
|
40
|
+
/** Whether a value is the number of a place of the line. */
|
|
41
|
+
export declare function isPlace(line: Line, value: unknown): value is number;
|
|
42
|
+
/** A place of the line, by its number, or undefined with its error added. */
|
|
43
|
+
export declare function readPlace(line: Line, value: unknown, path: string, errors: SceneError[]): number | undefined;
|
|
44
|
+
/** A boolean, or undefined with its error added. */
|
|
45
|
+
export declare function readBoolean(value: unknown, path: string, errors: SceneError[]): boolean | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Places of the line by their numbers, none twice, in ascending order, or undefined with their
|
|
48
|
+
* errors added.
|
|
49
|
+
*/
|
|
50
|
+
export declare function readPlaces(line: Line, value: unknown, path: string, errors: SceneError[]): number[] | undefined;
|
|
51
|
+
/** The line of a scene, or undefined with its errors added. */
|
|
52
|
+
export declare function readLine(value: unknown, path: string, errors: SceneError[]): Line | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* The numbers of a state's member, a list of places, each once, in ascending order. A list that is
|
|
55
|
+
* no array, an item that is no place of the line and a place named again count as none, and the
|
|
56
|
+
* order the list gives is not read: a state reported from outside the kind may hold any of them,
|
|
57
|
+
* and no rule counts it.
|
|
58
|
+
*/
|
|
59
|
+
export declare function placesOf(line: Line, state: State, member: string): readonly number[];
|
|
60
|
+
/** The places with one more, in ascending order: a new array, the old one left as it was. */
|
|
61
|
+
export declare function withPlace(places: readonly number[], place: number): number[];
|
|
62
|
+
/** The move's member "at", a place of the line, or undefined where the move holds no such place. */
|
|
63
|
+
export declare function moveAt(line: Line, move: Json): number | undefined;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// The line of the kind line: places in a row, numbered from the scene's first number on, each a
|
|
2
|
+
// spacing from the next, and the posts that stand on places; open, or closed into a ring, whose
|
|
3
|
+
// last place is next to its first, as bushes round a flowerbed or a fence round a garden. A place
|
|
4
|
+
// is named by its number, as a statement names a floor, a page or a post, so a scene, a move and a
|
|
5
|
+
// task's predicate use the numbers the statement uses; the length between two places is their
|
|
6
|
+
// count of gaps times the spacing, in the statement's own unit — metres, steps, minutes.
|
|
7
|
+
import { divideWithRemainder } from "../../cores/arith.js";
|
|
8
|
+
import { array, integer, isObject, object, pointer } from "../../cores/json.js";
|
|
9
|
+
/** The most places a line holds: the numbers from 0 to 100 among them. */
|
|
10
|
+
export const maxPlaces = 101;
|
|
11
|
+
/** The greatest number of a place, the last place's too. */
|
|
12
|
+
export const maxNumber = 10_000;
|
|
13
|
+
/** The greatest spacing between two neighbouring places. */
|
|
14
|
+
export const maxSpacing = 10_000;
|
|
15
|
+
/** The number of the line's last place. */
|
|
16
|
+
export function lastPlace(line) {
|
|
17
|
+
return line.first + line.places - 1;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The number of the place a count of places on from a place, back where the count is negative: on
|
|
21
|
+
* a ring counted round it, by the core of exact arithmetic; on an open line the plain sum, which
|
|
22
|
+
* may fall off the line.
|
|
23
|
+
*/
|
|
24
|
+
export function placeOn(line, place, by) {
|
|
25
|
+
if (!line.closed) {
|
|
26
|
+
return place + by;
|
|
27
|
+
}
|
|
28
|
+
return line.first + divideWithRemainder(place - line.first + by, line.places).remainder;
|
|
29
|
+
}
|
|
30
|
+
/** Whether a value is the number of a place of the line. */
|
|
31
|
+
export function isPlace(line, value) {
|
|
32
|
+
return (Number.isInteger(value) &&
|
|
33
|
+
value >= line.first &&
|
|
34
|
+
value <= lastPlace(line));
|
|
35
|
+
}
|
|
36
|
+
/** A place of the line, by its number, or undefined with its error added. */
|
|
37
|
+
export function readPlace(line, value, path, errors) {
|
|
38
|
+
if (!isPlace(line, value)) {
|
|
39
|
+
errors.push({
|
|
40
|
+
path,
|
|
41
|
+
message: `a place of the line, a whole number from ${line.first} to ${lastPlace(line)}`,
|
|
42
|
+
});
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
/** A boolean, or undefined with its error added. */
|
|
48
|
+
export function readBoolean(value, path, errors) {
|
|
49
|
+
if (typeof value !== "boolean") {
|
|
50
|
+
errors.push({ path, message: "true or false" });
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Places of the line by their numbers, none twice, in ascending order, or undefined with their
|
|
57
|
+
* errors added.
|
|
58
|
+
*/
|
|
59
|
+
export function readPlaces(line, value, path, errors) {
|
|
60
|
+
const before = errors.length;
|
|
61
|
+
const places = [];
|
|
62
|
+
array(value, path, errors)?.forEach((item, i) => {
|
|
63
|
+
const at = pointer(path, i);
|
|
64
|
+
const place = readPlace(line, item, at, errors);
|
|
65
|
+
if (place !== undefined && places.includes(place)) {
|
|
66
|
+
errors.push({ path: at, message: "a place named twice" });
|
|
67
|
+
}
|
|
68
|
+
else if (place !== undefined) {
|
|
69
|
+
places.push(place);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return errors.length > before || !Array.isArray(value) ? undefined : places.sort((a, b) => a - b);
|
|
73
|
+
}
|
|
74
|
+
/** The sides of a ring by their counts of gaps, or undefined with their errors added. */
|
|
75
|
+
function readSides(value, places, path, errors) {
|
|
76
|
+
const before = errors.length;
|
|
77
|
+
const sides = [];
|
|
78
|
+
array(value, path, errors, 3, maxPlaces)?.forEach((item, i) => {
|
|
79
|
+
const side = integer(item, pointer(path, i), errors, 1, maxPlaces - 1);
|
|
80
|
+
if (side !== undefined)
|
|
81
|
+
sides.push(side);
|
|
82
|
+
});
|
|
83
|
+
if (errors.length > before) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
if (sides.reduce((sum, side) => sum + side, 0) !== places) {
|
|
87
|
+
errors.push({ path, message: `the sides' gaps together are the ring's places, ${places}` });
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
return sides;
|
|
91
|
+
}
|
|
92
|
+
/** The line of a scene, or undefined with its errors added. */
|
|
93
|
+
export function readLine(value, path, errors) {
|
|
94
|
+
const before = errors.length;
|
|
95
|
+
const o = object(value, path, errors, ["places"], ["first", "spacing", "vertical", "closed", "sides", "posts"]);
|
|
96
|
+
if (o === undefined) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
const places = integer(o.places, pointer(path, "places"), errors, 2, maxPlaces);
|
|
100
|
+
const first = o.first === undefined ? 0 : integer(o.first, pointer(path, "first"), errors, 0, maxNumber);
|
|
101
|
+
const spacing = o.spacing === undefined
|
|
102
|
+
? 1
|
|
103
|
+
: integer(o.spacing, pointer(path, "spacing"), errors, 1, maxSpacing);
|
|
104
|
+
const vertical = o.vertical === undefined ? false : readBoolean(o.vertical, pointer(path, "vertical"), errors);
|
|
105
|
+
const closed = o.closed === undefined ? false : readBoolean(o.closed, pointer(path, "closed"), errors);
|
|
106
|
+
if (closed === true && vertical === true) {
|
|
107
|
+
errors.push({ path: pointer(path, "vertical"), message: "a ring is never drawn upright" });
|
|
108
|
+
}
|
|
109
|
+
let sides = null;
|
|
110
|
+
if (o.sides !== undefined && closed === false) {
|
|
111
|
+
errors.push({ path: pointer(path, "sides"), message: "sides only of a ring, a closed line" });
|
|
112
|
+
}
|
|
113
|
+
else if (o.sides !== undefined && closed === true && places !== undefined) {
|
|
114
|
+
sides = readSides(o.sides, places, pointer(path, "sides"), errors);
|
|
115
|
+
}
|
|
116
|
+
if (places === undefined ||
|
|
117
|
+
first === undefined ||
|
|
118
|
+
spacing === undefined ||
|
|
119
|
+
vertical === undefined ||
|
|
120
|
+
closed === undefined ||
|
|
121
|
+
sides === undefined) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
if (first + places - 1 > maxNumber) {
|
|
125
|
+
errors.push({ path, message: `the last place's number is at most ${maxNumber}` });
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const line = { places, first, spacing, vertical, closed, sides, posts: [] };
|
|
129
|
+
const posts = o.posts === undefined ? [] : readPlaces(line, o.posts, pointer(path, "posts"), errors);
|
|
130
|
+
return posts === undefined || errors.length > before ? undefined : { ...line, posts };
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The numbers of a state's member, a list of places, each once, in ascending order. A list that is
|
|
134
|
+
* no array, an item that is no place of the line and a place named again count as none, and the
|
|
135
|
+
* order the list gives is not read: a state reported from outside the kind may hold any of them,
|
|
136
|
+
* and no rule counts it.
|
|
137
|
+
*/
|
|
138
|
+
export function placesOf(line, state, member) {
|
|
139
|
+
const value = state[member];
|
|
140
|
+
if (!Array.isArray(value)) {
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
const places = new Set(value.filter((item) => isPlace(line, item)));
|
|
144
|
+
return [...places].sort((a, b) => a - b);
|
|
145
|
+
}
|
|
146
|
+
/** The places with one more, in ascending order: a new array, the old one left as it was. */
|
|
147
|
+
export function withPlace(places, place) {
|
|
148
|
+
return [...places, place].sort((a, b) => a - b);
|
|
149
|
+
}
|
|
150
|
+
/** The move's member "at", a place of the line, or undefined where the move holds no such place. */
|
|
151
|
+
export function moveAt(line, move) {
|
|
152
|
+
if (!isObject(move)) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
const keys = Object.keys(move);
|
|
156
|
+
return keys.length === 1 && keys[0] === "at" && isPlace(line, move.at) ? move.at : undefined;
|
|
157
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Ask, Json, Played, SceneError } from "../../cores/kind.js";
|
|
2
|
+
import type { Line, State } from "./line.js";
|
|
3
|
+
/**
|
|
4
|
+
* A part of the kind's JSON Schema, written by the conventions of the module format's schemas:
|
|
5
|
+
* an object closed by unevaluatedProperties, a description for each member.
|
|
6
|
+
*/
|
|
7
|
+
export type Schema = {
|
|
8
|
+
readonly [keyword: string]: Json;
|
|
9
|
+
};
|
|
10
|
+
/** A number of a state, a safe integer, by the line and an operation's parameters. */
|
|
11
|
+
export interface Counter<Options = unknown> {
|
|
12
|
+
/** What the number counts, for the schema. */
|
|
13
|
+
readonly description: string;
|
|
14
|
+
value(line: Line, options: Options, state: State): number;
|
|
15
|
+
}
|
|
16
|
+
/** A counter bound to a scene: the number of a state. */
|
|
17
|
+
export type Count = (state: State) => number;
|
|
18
|
+
export interface Operation<Options = unknown> {
|
|
19
|
+
/** The operation's name: its member in a scene's "operations" and in a move. */
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly schema: {
|
|
22
|
+
/** The operation's parameters in a scene, under "operations". */
|
|
23
|
+
readonly options: Schema;
|
|
24
|
+
/** What a move of the operation holds, under the operation's name. */
|
|
25
|
+
readonly move: Schema;
|
|
26
|
+
/** The member of the state the operation keeps besides the posts, if it keeps one. */
|
|
27
|
+
readonly state?: {
|
|
28
|
+
readonly member: string;
|
|
29
|
+
readonly schema: Schema;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
/** The parameters, read by the line; undefined where they cannot be read, with their errors added. */
|
|
33
|
+
read(value: unknown, line: Line, path: string, errors: SceneError[]): Options | undefined;
|
|
34
|
+
/** The value of the operation's own member of the state at the scene's start. */
|
|
35
|
+
start?(line: Line, options: Options): Json;
|
|
36
|
+
/** A move, given what it holds under the operation's name, or the rules' short reason to refuse it. */
|
|
37
|
+
play(line: Line, options: Options, state: State, move: Json): Played<State>;
|
|
38
|
+
/** What every move of the operation to try from the state holds, in a fixed order. */
|
|
39
|
+
moves(line: Line, options: Options, state: State): readonly Json[];
|
|
40
|
+
/** The counters the operation adds, by their names. */
|
|
41
|
+
readonly counters?: Readonly<Record<string, Counter<Options>>>;
|
|
42
|
+
}
|
|
43
|
+
export interface Goal<Options = unknown> {
|
|
44
|
+
/** The goal's name: its member in a scene's "goal". */
|
|
45
|
+
readonly name: string;
|
|
46
|
+
/** The goal's parameters in a scene; a counter is named by a $ref to #/$defs/counter. */
|
|
47
|
+
readonly schema: Schema;
|
|
48
|
+
/**
|
|
49
|
+
* The parameters, or undefined with their errors added, by the line and the counters the scene
|
|
50
|
+
* has: the line's own, and each of the scene's operations' that the kind has, by its name, the
|
|
51
|
+
* count undefined where the operation's parameters do not read, or, where the scene's operations
|
|
52
|
+
* are no map, for every operation of the kind, the scene refused either way.
|
|
53
|
+
*/
|
|
54
|
+
read(value: unknown, line: Line, path: string, errors: SceneError[], counters: ReadonlyMap<string, Count | undefined>): Options | undefined;
|
|
55
|
+
meets(line: Line, options: Options, state: State): boolean;
|
|
56
|
+
/** What the goal asks of the scene's state graph where it is not the fewest moves. */
|
|
57
|
+
asks?(line: Line, options: Options): Ask<State>;
|
|
58
|
+
}
|
|
59
|
+
/** The operations and goals of the kind, and the version of its schema they make. */
|
|
60
|
+
export interface Registry {
|
|
61
|
+
/**
|
|
62
|
+
* The version of the kind's schema, "major.minor": a new operation, goal or counter is the next
|
|
63
|
+
* minor version, and a change of one a scene, a move or a state already uses the next major one.
|
|
64
|
+
*/
|
|
65
|
+
readonly version: string;
|
|
66
|
+
/** In the order a state's moves are listed in, and a proof's path with them. */
|
|
67
|
+
readonly operations: readonly Operation[];
|
|
68
|
+
readonly goals: readonly Goal[];
|
|
69
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SceneError } from "../../cores/kind.js";
|
|
2
|
+
import type { Counter, Operation } from "./operation.js";
|
|
3
|
+
/** The line's own counters, which every scene has. */
|
|
4
|
+
export declare const lineCounters: Readonly<Record<string, Counter>>;
|
|
5
|
+
/** The schema of a move that names a place, {"at": <number>}. */
|
|
6
|
+
export declare function placeMove(description: string): Operation["schema"]["move"];
|
|
7
|
+
/** The schema of parameters that are none, {}. */
|
|
8
|
+
export declare function noOptions(description: string): Operation["schema"]["options"];
|
|
9
|
+
/** Parameters that are none, {}: true, or undefined with their errors added. */
|
|
10
|
+
export declare function readNone(value: unknown, path: string, errors: SceneError[]): true | undefined;
|
|
11
|
+
export declare const post: Operation<true>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// The posts of the line — trees in a row, stops of a bus, pages read, flowers, lamps — and the
|
|
2
|
+
// operation that puts one on a free place. The line keeps its posts under "posts" whether or not a
|
|
3
|
+
// scene allows the operation, so a line only to look at has them too, and its counters: the posts,
|
|
4
|
+
// the gaps between neighbouring posts and their lengths together — on a ring, as many gaps as
|
|
5
|
+
// posts, which is what a closed line teaches, and the whole way round. A post is
|
|
6
|
+
// never taken away by a move: the widget's undo is the shell's, and a post that could go would make
|
|
7
|
+
// every graph one of every set of places both ways, with no key shorter.
|
|
8
|
+
import { object } from "../../cores/json.js";
|
|
9
|
+
import { moveAt, placesOf, withPlace } from "./line.js";
|
|
10
|
+
const posts = (line, state) => placesOf(line, state, "posts");
|
|
11
|
+
/** The line's own counters, which every scene has. */
|
|
12
|
+
export const lineCounters = {
|
|
13
|
+
posts: {
|
|
14
|
+
description: "The posts on the line.",
|
|
15
|
+
value: (line, _options, state) => posts(line, state).length,
|
|
16
|
+
},
|
|
17
|
+
gaps: {
|
|
18
|
+
description: "The gaps between neighbouring posts: on an open line one fewer than the posts, on a ring as many as the posts, a lone post's gap the way round from it back to it; none where there is no post.",
|
|
19
|
+
value(line, _options, state) {
|
|
20
|
+
const on = posts(line, state).length;
|
|
21
|
+
return line.closed ? on : Math.max(on - 1, 0);
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
length: {
|
|
25
|
+
description: "The gaps' lengths together, each its count of places times the spacing: on an open line from the first post to the last, 0 where there are fewer than two posts; on a ring the whole way round where there is a post, 0 where there is none.",
|
|
26
|
+
value(line, _options, state) {
|
|
27
|
+
const on = posts(line, state);
|
|
28
|
+
if (line.closed) {
|
|
29
|
+
return on.length === 0 ? 0 : line.places * line.spacing;
|
|
30
|
+
}
|
|
31
|
+
return on.length < 2 ? 0 : (on.at(-1) - on[0]) * line.spacing;
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
function refuse(reason) {
|
|
36
|
+
return { ok: false, reason };
|
|
37
|
+
}
|
|
38
|
+
/** The schema of a move that names a place, {"at": <number>}. */
|
|
39
|
+
export function placeMove(description) {
|
|
40
|
+
return {
|
|
41
|
+
description,
|
|
42
|
+
type: "object",
|
|
43
|
+
properties: { at: { description: "The place, by its number.", $ref: "#/$defs/place" } },
|
|
44
|
+
required: ["at"],
|
|
45
|
+
unevaluatedProperties: false,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** The schema of parameters that are none, {}. */
|
|
49
|
+
export function noOptions(description) {
|
|
50
|
+
return { description, type: "object", unevaluatedProperties: false };
|
|
51
|
+
}
|
|
52
|
+
/** Parameters that are none, {}: true, or undefined with their errors added. */
|
|
53
|
+
export function readNone(value, path, errors) {
|
|
54
|
+
const before = errors.length;
|
|
55
|
+
return object(value, path, errors, []) === undefined || errors.length > before ? undefined : true;
|
|
56
|
+
}
|
|
57
|
+
export const post = {
|
|
58
|
+
name: "post",
|
|
59
|
+
schema: {
|
|
60
|
+
options: noOptions("Put a post on any free place of the line; no parameters."),
|
|
61
|
+
move: placeMove("Put a post on a place."),
|
|
62
|
+
},
|
|
63
|
+
read: (value, _line, path, errors) => readNone(value, path, errors),
|
|
64
|
+
play(line, _options, state, move) {
|
|
65
|
+
const at = moveAt(line, move);
|
|
66
|
+
if (at === undefined) {
|
|
67
|
+
return refuse("no-place");
|
|
68
|
+
}
|
|
69
|
+
const on = posts(line, state);
|
|
70
|
+
if (on.includes(at)) {
|
|
71
|
+
return refuse("taken");
|
|
72
|
+
}
|
|
73
|
+
return { ok: true, state: { ...state, posts: withPlace(on, at) } };
|
|
74
|
+
},
|
|
75
|
+
moves(line, _options, state) {
|
|
76
|
+
const on = new Set(posts(line, state));
|
|
77
|
+
const moves = [];
|
|
78
|
+
for (let place = line.first; place < line.first + line.places; place++) {
|
|
79
|
+
if (!on.has(place)) {
|
|
80
|
+
moves.push({ at: place });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return moves;
|
|
84
|
+
},
|
|
85
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The operations and goals of the kind line. A new operation or goal is a module of its own,
|
|
2
|
+
// listed here, with the next minor version: nothing else of the kind changes for it.
|
|
3
|
+
import { cut } from "./cut.js";
|
|
4
|
+
import { counters, every } from "./goals.js";
|
|
5
|
+
import { jump } from "./jump.js";
|
|
6
|
+
import { post } from "./posts.js";
|
|
7
|
+
export const registry = {
|
|
8
|
+
version: "1.2",
|
|
9
|
+
operations: [post, cut, jump],
|
|
10
|
+
goals: [counters, every],
|
|
11
|
+
};
|