@altnovatrail/picture-kit 0.0.0-stage → 0.1.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 (122) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +28 -2
  4. package/lib/emoji.d.ts +16 -0
  5. package/lib/emoji.js +64 -0
  6. package/lib/index.d.ts +9 -0
  7. package/lib/index.js +10 -0
  8. package/lib/kind.d.ts +22 -0
  9. package/lib/kind.js +2 -0
  10. package/lib/kinds/balance.d.ts +34 -0
  11. package/lib/kinds/balance.js +217 -0
  12. package/lib/kinds/branches.d.ts +33 -0
  13. package/lib/kinds/branches.js +473 -0
  14. package/lib/kinds/calendar.d.ts +55 -0
  15. package/lib/kinds/calendar.js +532 -0
  16. package/lib/kinds/chart.d.ts +48 -0
  17. package/lib/kinds/chart.js +645 -0
  18. package/lib/kinds/choices.d.ts +14 -0
  19. package/lib/kinds/choices.js +53 -0
  20. package/lib/kinds/clock.d.ts +36 -0
  21. package/lib/kinds/clock.js +219 -0
  22. package/lib/kinds/coins.d.ts +37 -0
  23. package/lib/kinds/coins.js +550 -0
  24. package/lib/kinds/column.d.ts +37 -0
  25. package/lib/kinds/column.js +410 -0
  26. package/lib/kinds/commands-parts.d.ts +12 -0
  27. package/lib/kinds/commands-parts.js +24 -0
  28. package/lib/kinds/commands.d.ts +66 -0
  29. package/lib/kinds/commands.js +789 -0
  30. package/lib/kinds/containers.d.ts +52 -0
  31. package/lib/kinds/containers.js +758 -0
  32. package/lib/kinds/countfig.d.ts +41 -0
  33. package/lib/kinds/countfig.js +372 -0
  34. package/lib/kinds/cubes.d.ts +70 -0
  35. package/lib/kinds/cubes.js +371 -0
  36. package/lib/kinds/die.d.ts +32 -0
  37. package/lib/kinds/die.js +301 -0
  38. package/lib/kinds/domino.d.ts +37 -0
  39. package/lib/kinds/domino.js +450 -0
  40. package/lib/kinds/dotgrid.d.ts +38 -0
  41. package/lib/kinds/dotgrid.js +411 -0
  42. package/lib/kinds/figure.d.ts +62 -0
  43. package/lib/kinds/figure.js +687 -0
  44. package/lib/kinds/fold-parts.d.ts +73 -0
  45. package/lib/kinds/fold-parts.js +315 -0
  46. package/lib/kinds/fold.d.ts +52 -0
  47. package/lib/kinds/fold.js +650 -0
  48. package/lib/kinds/fraction.d.ts +50 -0
  49. package/lib/kinds/fraction.js +568 -0
  50. package/lib/kinds/gauges.d.ts +53 -0
  51. package/lib/kinds/gauges.js +880 -0
  52. package/lib/kinds/gears.d.ts +40 -0
  53. package/lib/kinds/gears.js +787 -0
  54. package/lib/kinds/grid.d.ts +45 -0
  55. package/lib/kinds/grid.js +591 -0
  56. package/lib/kinds/groups.d.ts +23 -0
  57. package/lib/kinds/groups.js +335 -0
  58. package/lib/kinds/index.d.ts +3 -0
  59. package/lib/kinds/index.js +87 -0
  60. package/lib/kinds/lanes.d.ts +36 -0
  61. package/lib/kinds/lanes.js +220 -0
  62. package/lib/kinds/layers.d.ts +31 -0
  63. package/lib/kinds/layers.js +419 -0
  64. package/lib/kinds/line.d.ts +38 -0
  65. package/lib/kinds/line.js +739 -0
  66. package/lib/kinds/magic-parts.d.ts +72 -0
  67. package/lib/kinds/magic-parts.js +186 -0
  68. package/lib/kinds/magic.d.ts +66 -0
  69. package/lib/kinds/magic.js +664 -0
  70. package/lib/kinds/map.d.ts +32 -0
  71. package/lib/kinds/map.js +370 -0
  72. package/lib/kinds/matchsticks.d.ts +26 -0
  73. package/lib/kinds/matchsticks.js +434 -0
  74. package/lib/kinds/necklace.d.ts +27 -0
  75. package/lib/kinds/necklace.js +342 -0
  76. package/lib/kinds/net.d.ts +47 -0
  77. package/lib/kinds/net.js +241 -0
  78. package/lib/kinds/overlay.d.ts +50 -0
  79. package/lib/kinds/overlay.js +519 -0
  80. package/lib/kinds/pieces.d.ts +41 -0
  81. package/lib/kinds/pieces.js +420 -0
  82. package/lib/kinds/pipes.d.ts +46 -0
  83. package/lib/kinds/pipes.js +657 -0
  84. package/lib/kinds/plane-parts.d.ts +105 -0
  85. package/lib/kinds/plane-parts.js +372 -0
  86. package/lib/kinds/plane.d.ts +35 -0
  87. package/lib/kinds/plane.js +755 -0
  88. package/lib/kinds/pyramid.d.ts +26 -0
  89. package/lib/kinds/pyramid.js +228 -0
  90. package/lib/kinds/ropes.d.ts +30 -0
  91. package/lib/kinds/ropes.js +571 -0
  92. package/lib/kinds/shapes.d.ts +59 -0
  93. package/lib/kinds/shapes.js +490 -0
  94. package/lib/kinds/solids.d.ts +78 -0
  95. package/lib/kinds/solids.js +1058 -0
  96. package/lib/kinds/tangram.d.ts +52 -0
  97. package/lib/kinds/tangram.js +713 -0
  98. package/lib/kinds/target.d.ts +39 -0
  99. package/lib/kinds/target.js +761 -0
  100. package/lib/kinds/tiles.d.ts +55 -0
  101. package/lib/kinds/tiles.js +606 -0
  102. package/lib/kinds/venn.d.ts +38 -0
  103. package/lib/kinds/venn.js +710 -0
  104. package/lib/noto.d.ts +2 -0
  105. package/lib/noto.js +212 -0
  106. package/lib/objects.d.ts +7 -0
  107. package/lib/objects.js +226 -0
  108. package/lib/pen.d.ts +32 -0
  109. package/lib/pen.js +60 -0
  110. package/lib/read.d.ts +9 -0
  111. package/lib/read.js +49 -0
  112. package/lib/render.d.ts +24 -0
  113. package/lib/render.js +68 -0
  114. package/lib/solve/balance.d.ts +46 -0
  115. package/lib/solve/balance.js +118 -0
  116. package/lib/style.d.ts +70 -0
  117. package/lib/style.js +39 -0
  118. package/lib/svg.d.ts +22 -0
  119. package/lib/svg.js +48 -0
  120. package/lib/things.d.ts +40 -0
  121. package/lib/things.js +269 -0
  122. package/package.json +28 -4
@@ -0,0 +1,434 @@
1
+ import { margin } from "../kind.js";
2
+ import { int, isObj, list, oneOf, only, text } from "../read.js";
3
+ import { colors, ink, outline } from "../style.js";
4
+ import { byCodeUnit, n } from "../svg.js";
5
+ const members = ["kind", "text", "marks", "grid", "sticks"];
6
+ const grids = ["square", "triangle"];
7
+ /** The colours a match may take: each stands out from a plain match and from an empty place. */
8
+ const marked = ["red", "orange", "green", "blue", "purple", "pink", "black"];
9
+ const wood = "#f2cf8d";
10
+ /** A match is a step of the grid long, less a little at each end so that matches meeting at a point keep apart. */
11
+ const square = { step: 50, inset: 7, across: 12, down: 8 };
12
+ const triangle = { step: 56, inset: 9, across: 10, down: 7 };
13
+ const body = 7;
14
+ const pad = 8;
15
+ const widest = 640;
16
+ /** The gap between two symbols of a text, in steps. */
17
+ const gap = 0.6;
18
+ /** Where each place of a symbol lies in its cell, one step wide and two tall. */
19
+ const places = {
20
+ top: [0, 0, 1, 0],
21
+ "upper-left": [0, 0, 0, 1],
22
+ "upper-right": [1, 0, 1, 1],
23
+ middle: [0, 1, 1, 1],
24
+ "lower-left": [0, 1, 0, 2],
25
+ "lower-right": [1, 1, 1, 2],
26
+ bottom: [0, 2, 1, 2],
27
+ upright: [0.5, 0.5, 0.5, 1.5],
28
+ upper: [0, 0.7, 1, 0.7],
29
+ lower: [0, 1.3, 1, 1.3],
30
+ };
31
+ const digitPlaces = [
32
+ "top",
33
+ "upper-left",
34
+ "upper-right",
35
+ "middle",
36
+ "lower-left",
37
+ "lower-right",
38
+ "bottom",
39
+ ];
40
+ const shapes = {
41
+ "0": ["top", "upper-left", "upper-right", "lower-left", "lower-right", "bottom"],
42
+ "1": ["upper-right", "lower-right"],
43
+ "2": ["top", "upper-right", "middle", "lower-left", "bottom"],
44
+ "3": ["top", "upper-right", "middle", "lower-right", "bottom"],
45
+ "4": ["upper-left", "upper-right", "middle", "lower-right"],
46
+ "5": ["top", "upper-left", "middle", "lower-right", "bottom"],
47
+ "6": ["top", "upper-left", "middle", "lower-left", "lower-right", "bottom"],
48
+ "7": ["top", "upper-right", "lower-right"],
49
+ "8": digitPlaces,
50
+ "9": ["top", "upper-left", "upper-right", "middle", "lower-right", "bottom"],
51
+ "+": ["middle", "upright"],
52
+ "-": ["middle"],
53
+ "=": ["upper", "lower"],
54
+ };
55
+ /** placesOf is every place a symbol may have a mark at: a digit's seven, a sign's own. */
56
+ function placesOf(c) {
57
+ if (/[0-9]/.test(c))
58
+ return digitPlaces;
59
+ return c === "=" ? ["upper", "lower"] : ["middle", "upright"];
60
+ }
61
+ const symbolOf = (c) => (c === "−" ? "-" : c);
62
+ const words = (place) => place.replace("-", " ");
63
+ const ordinal = (k) => `${k}${k % 10 === 1 && k !== 11 ? "st" : k % 10 === 2 && k !== 12 ? "nd" : k % 10 === 3 && k !== 13 ? "rd" : "th"}`;
64
+ /** layText lays out the symbols of a text from the left, in steps; a 1 stands narrow unless a mark needs its cell. */
65
+ function layText(s) {
66
+ const chars = [...(s.text ?? "")].map(symbolOf);
67
+ const marks = s.marks ?? [];
68
+ const sticks = [];
69
+ let x = 0;
70
+ chars.forEach((c, i) => {
71
+ if (c === " ") {
72
+ x += 0.4 + gap;
73
+ return;
74
+ }
75
+ const own = marks.filter((m) => m.char === i);
76
+ const narrow = c === "1" && own.every((m) => m.place === "upper-right" || m.place === "lower-right");
77
+ const shift = narrow ? -1 : 0;
78
+ const put = (place, mark) => {
79
+ const [x1, y1, x2, y2] = places[place];
80
+ const at = (u, v) => ({
81
+ x: (x + u + shift) * square.step,
82
+ y: v * square.step,
83
+ });
84
+ sticks.push({ a: at(x1, y1), b: at(x2, y2), mark, symbol: i });
85
+ };
86
+ for (const m of own)
87
+ if (m.mark === "ghost")
88
+ put(m.place, "ghost");
89
+ for (const place of shapes[c])
90
+ put(place, own.find((m) => m.place === place)?.mark);
91
+ x += (narrow ? 0 : 1) + gap;
92
+ });
93
+ return frame(sticks, square.inset);
94
+ }
95
+ function pointOf(grid, x, y) {
96
+ return grid === "square"
97
+ ? { x: x * square.step, y: y * square.step }
98
+ : { x: x * triangle.step, y: (y * triangle.step * Math.sqrt(3)) / 2 };
99
+ }
100
+ function stickOf(v) {
101
+ return Array.isArray(v) ? { at: v } : v;
102
+ }
103
+ function layGrid(s) {
104
+ const grid = s.grid ?? "square";
105
+ const sticks = (s.sticks ?? []).map((v) => {
106
+ const { at, mark } = stickOf(v);
107
+ return { a: pointOf(grid, at[0], at[1]), b: pointOf(grid, at[2], at[3]), mark };
108
+ });
109
+ return frame(sticks, grid === "square" ? square.inset : triangle.inset);
110
+ }
111
+ /** frame moves the sticks so that the picture starts at its margin, and measures it. */
112
+ function frame(sticks, inset) {
113
+ const xs = sticks.flatMap((t) => [t.a.x, t.b.x]);
114
+ const ys = sticks.flatMap((t) => [t.a.y, t.b.y]);
115
+ const dx = margin + pad - Math.min(...xs);
116
+ const dy = margin + pad - Math.min(...ys);
117
+ return {
118
+ sticks: sticks.map((t) => ({
119
+ ...t,
120
+ a: { x: t.a.x + dx, y: t.a.y + dy },
121
+ b: { x: t.b.x + dx, y: t.b.y + dy },
122
+ })),
123
+ inset,
124
+ width: Math.max(...xs) - Math.min(...xs) + 2 * (margin + pad),
125
+ height: Math.max(...ys) - Math.min(...ys) + 2 * (margin + pad),
126
+ };
127
+ }
128
+ function checkMark(v, path, problems) {
129
+ if (v === "ghost")
130
+ return "ghost";
131
+ if (typeof v === "string" && marked.includes(v))
132
+ return v;
133
+ problems.push(`${path}: must be "ghost", for an empty place drawn dashed, or a colour that stands out from a plain match: ${marked.join(", ")}`);
134
+ return undefined;
135
+ }
136
+ function checkText(scene, problems) {
137
+ if (scene.grid !== undefined)
138
+ problems.push("grid: only sticks stand on a grid; a text lays out its own matches");
139
+ const t = text(scene.text, "text", problems, 12);
140
+ const chars = [...t].map(symbolOf);
141
+ for (const c of new Set(chars))
142
+ if (!(c in shapes) && c !== " ")
143
+ problems.push(`text: "${c}" cannot be laid in matches here; use digits, + − = and spaces`);
144
+ if (t !== "" && t.trim() !== t)
145
+ problems.push("text: leave out the spaces at its ends");
146
+ if (scene.marks !== undefined) {
147
+ const seen = [];
148
+ list(scene.marks, "marks", problems, 1, 12).forEach((m, i) => {
149
+ const path = `marks[${i}]`;
150
+ if (!isObj(m)) {
151
+ problems.push(`${path}: must be a mark with char, place and mark`);
152
+ return;
153
+ }
154
+ only(m, ["char", "place", "mark"], path, problems);
155
+ const k = int(m.char, `${path}.char`, problems, 0, Math.max(0, chars.length - 1));
156
+ const c = chars[k];
157
+ const mark = checkMark(m.mark, `${path}.mark`, problems);
158
+ if (c === undefined || !(c in shapes)) {
159
+ if (c === " ")
160
+ problems.push(`${path}.char: the ${ordinal(k + 1)} symbol is a space; mark a digit or a sign`);
161
+ return;
162
+ }
163
+ if (typeof m.place !== "string" || !placesOf(c).includes(m.place)) {
164
+ oneOf(m.place, placesOf(c), `${path}.place`, problems);
165
+ return;
166
+ }
167
+ const place = m.place;
168
+ const has = shapes[c].includes(place);
169
+ if (mark === "ghost" && has)
170
+ problems.push(`${path}.place: the ${c} has a match at its ${words(place)}; an empty place goes where there is none`);
171
+ if (mark !== undefined && mark !== "ghost" && !has)
172
+ problems.push(`${path}.place: the ${c} has no match at its ${words(place)}; colour a match that is there`);
173
+ const key = `${k} ${place}`;
174
+ if (seen.includes(key))
175
+ problems.push(`${path}: marks the same place as marks[${seen.indexOf(key)}]`);
176
+ seen.push(key);
177
+ });
178
+ }
179
+ if (problems.length === 0) {
180
+ const { width } = layText(scene);
181
+ if (width > widest)
182
+ problems.push(`text: its ${chars.length} symbols make the picture ${Math.round(width)} px wide, more than ${widest}; use fewer`);
183
+ }
184
+ }
185
+ function checkSticks(scene, problems) {
186
+ if (scene.marks !== undefined)
187
+ problems.push('marks: only a text has marks; mark a stick of the grid as { "at": [...], "mark": ... }');
188
+ const grid = scene.grid === undefined ? "square" : oneOf(scene.grid, grids, "grid", problems);
189
+ const box = grid === "square" ? square : triangle;
190
+ const seen = new Map();
191
+ let parity;
192
+ list(scene.sticks, "sticks", problems, 1, 60).forEach((v, i) => {
193
+ let path = `sticks[${i}]`;
194
+ let at = v;
195
+ if (isObj(v)) {
196
+ only(v, ["at", "mark"], path, problems);
197
+ if (v.mark !== undefined)
198
+ checkMark(v.mark, `${path}.mark`, problems);
199
+ at = v.at;
200
+ path = `${path}.at`;
201
+ }
202
+ if (!Array.isArray(at) || at.length !== 4 || at.some((u) => typeof u !== "number")) {
203
+ problems.push(`${path}: must be [across, down, across, down], the two ends of the match`);
204
+ return;
205
+ }
206
+ const [x1, y1, x2, y2] = at;
207
+ let fine = true;
208
+ for (const [k, u, most, name] of [
209
+ [0, x1, box.across, "across"],
210
+ [1, y1, box.down, "down"],
211
+ [2, x2, box.across, "across"],
212
+ [3, y2, box.down, "down"],
213
+ ]) {
214
+ const steps = grid === "triangle" && name === "across" ? 2 : 1;
215
+ if (!Number.isInteger(u * steps) || u < 0 || u > most) {
216
+ problems.push(`${path}[${k}]: ${name} must be ${steps === 2 ? "a number in steps of 0.5" : "a whole number"} from 0 to ${most}`);
217
+ fine = false;
218
+ }
219
+ }
220
+ if (!fine)
221
+ return;
222
+ const dx = Math.abs(x2 - x1);
223
+ const dy = Math.abs(y2 - y1);
224
+ if (grid === "square" ? dx + dy !== 1 : !((dy === 0 && dx === 1) || (dy === 1 && dx === 0.5))) {
225
+ problems.push(grid === "square"
226
+ ? `${path}: a match joins two neighbouring points, one step apart across or down`
227
+ : `${path}: a match joins two neighbouring points: one step apart in a row, or half a step across and one row down`);
228
+ return;
229
+ }
230
+ if (grid === "triangle") {
231
+ const p = (2 * x1 + y1) % 2;
232
+ if (parity === undefined)
233
+ parity = p;
234
+ else if (p !== parity)
235
+ problems.push(`${path}: its points are off the grid of the first stick; in a row the points stand a whole step apart, and each row is shifted half a step from the next`);
236
+ }
237
+ const key = [`${x1},${y1}`, `${x2},${y2}`].sort(byCodeUnit).join(" ");
238
+ const first = seen.get(key);
239
+ if (first !== undefined)
240
+ problems.push(`${path}: lies where sticks[${first}] lies`);
241
+ else
242
+ seen.set(key, i);
243
+ });
244
+ }
245
+ /**
246
+ * match draws one match from a to b. Its head is at the left end, or the upper one; a match
247
+ * slanting down to the right has it at the lower end, so that at a point of the triangular
248
+ * grid no two heads meet at 60 degrees, where they would touch.
249
+ */
250
+ function match(pen, a, b, inset, mark) {
251
+ const [upper, lower] = a.y < b.y - 0.01 || (Math.abs(a.y - b.y) <= 0.01 && a.x <= b.x) ? [a, b] : [b, a];
252
+ const slantsRight = lower.x > upper.x + 0.01 && lower.y > upper.y + 0.01;
253
+ const [p, q] = slantsRight ? [lower, upper] : [upper, lower];
254
+ const length = Math.hypot(q.x - p.x, q.y - p.y) - 2 * inset;
255
+ const angle = (Math.atan2(q.y - p.y, q.x - p.x) * 180) / Math.PI;
256
+ const cx = (p.x + q.x) / 2;
257
+ const cy = (p.y + q.y) / 2;
258
+ const transform = Math.abs(angle) < 0.01 ? undefined : `rotate(${n(angle)} ${n(cx)} ${n(cy)})`;
259
+ if (mark === "ghost") {
260
+ pen.el("rect", {
261
+ x: cx - length / 2,
262
+ y: cy - body / 2,
263
+ width: length,
264
+ height: body,
265
+ rx: body / 2,
266
+ fill: "none",
267
+ stroke: colors.gray.dark,
268
+ "stroke-width": 2,
269
+ "stroke-dasharray": "5 3.5",
270
+ transform,
271
+ class: "ghost",
272
+ });
273
+ return;
274
+ }
275
+ pen.el("rect", {
276
+ x: cx - length / 2,
277
+ y: cy - body / 2,
278
+ width: length,
279
+ height: body,
280
+ rx: body / 2,
281
+ fill: mark ? colors[mark].fill : wood,
282
+ stroke: ink,
283
+ "stroke-width": outline,
284
+ transform,
285
+ class: "match",
286
+ });
287
+ pen.el("ellipse", {
288
+ cx: cx - length / 2 + 5,
289
+ cy,
290
+ rx: 6.5,
291
+ ry: 5,
292
+ fill: mark === "red" ? colors.red.dark : colors.red.fill,
293
+ stroke: ink,
294
+ "stroke-width": outline,
295
+ transform,
296
+ class: "head",
297
+ });
298
+ }
299
+ /** runs joins the sticks of one direction that follow on from each other along a line. */
300
+ function runs(sticks, line, along) {
301
+ const sorted = [...sticks].sort((s, t) => line(s) - line(t) || along(s) - along(t));
302
+ const out = [];
303
+ for (const s of sorted) {
304
+ const last = out.at(-1);
305
+ if (last && last.to[0] === s[0] && last.to[1] === s[1]) {
306
+ last.to = [s[2], s[3]];
307
+ last.k += 1;
308
+ }
309
+ else
310
+ out.push({ from: [s[0], s[1]], to: [s[2], s[3]], k: 1 });
311
+ }
312
+ return out;
313
+ }
314
+ function describeGrid(s) {
315
+ const grid = s.grid ?? "square";
316
+ const say = (p) => `(${p[0]}, ${p[1]})`;
317
+ // Each stick from its upper end, or its left one, so that runs follow on.
318
+ const ordered = (s.sticks ?? []).map((v) => {
319
+ const { at, mark } = stickOf(v);
320
+ const [x1, y1, x2, y2] = at;
321
+ const forward = y1 < y2 || (y1 === y2 && x1 < x2);
322
+ return { at: (forward ? [x1, y1, x2, y2] : [x2, y2, x1, y1]), mark };
323
+ });
324
+ const plain = ordered.filter((t) => t.mark === undefined).map((t) => t.at);
325
+ const out = [
326
+ grid === "square"
327
+ ? "Matches on a square grid of points, each match one step long between two neighbouring points. Places are (across, down), in steps from the top left point."
328
+ : "Matches on a triangular grid of points: the points stand in rows, each row shifted half a step from the next, and each match joins two neighbouring points, in a row or slanting to the next row. Places are (across, row), across in steps from the left and rows from the top.",
329
+ ];
330
+ const groups = grid === "square"
331
+ ? [
332
+ ["Lying across", (t) => t[1] === t[3], (t) => t[1], (t) => t[0]],
333
+ ["Standing upright", (t) => t[0] === t[2], (t) => t[0], (t) => t[1]],
334
+ ]
335
+ : [
336
+ ["Lying across", (t) => t[1] === t[3], (t) => t[1], (t) => t[0]],
337
+ [
338
+ "Slanting down to the right",
339
+ (t) => t[2] > t[0] && t[3] > t[1],
340
+ (t) => t[0] - t[1] / 2,
341
+ (t) => t[1],
342
+ ],
343
+ [
344
+ "Slanting down to the left",
345
+ (t) => t[2] < t[0] && t[3] > t[1],
346
+ (t) => t[0] + t[1] / 2,
347
+ (t) => t[1],
348
+ ],
349
+ ];
350
+ for (const [name, is, line, along] of groups) {
351
+ const found = runs(plain.filter(is), line, along);
352
+ if (found.length > 0)
353
+ out.push(`${name}: ${found.map((r) => `${r.k === 1 ? "one match" : `${r.k} matches end to end`} from ${say(r.from)} to ${say(r.to)}`).join("; ")}.`);
354
+ }
355
+ const coloured = ordered.filter((t) => t.mark !== undefined && t.mark !== "ghost");
356
+ if (coloured.length > 0)
357
+ out.push(`Coloured matches: ${coloured.map((t) => `a ${colors[t.mark].word} one from ${say([t.at[0], t.at[1]])} to ${say([t.at[2], t.at[3]])}`).join("; ")}.`);
358
+ const ghosts = ordered.filter((t) => t.mark === "ghost");
359
+ if (ghosts.length > 0)
360
+ out.push(`Empty places drawn dashed, with no match in them: ${ghosts.map((t) => `from ${say([t.at[0], t.at[1]])} to ${say([t.at[2], t.at[3]])}`).join("; ")}.`);
361
+ return out.join(" ");
362
+ }
363
+ function describeText(s) {
364
+ const chars = [...(s.text ?? "")].map(symbolOf);
365
+ const reading = chars
366
+ .join("")
367
+ .replace(/([+\-=])/g, " $1 ")
368
+ .replace(/-/g, "−")
369
+ .replace(/ +/g, " ")
370
+ .trim();
371
+ const name = (c) => (c === "-" ? "−" : c);
372
+ const said = chars
373
+ .filter((c) => c !== " ")
374
+ .map((c) => {
375
+ if (c === "+")
376
+ return "+: one match across and one upright through it";
377
+ if (c === "-")
378
+ return "−: one match across";
379
+ if (c === "=")
380
+ return "=: two matches across, one above the other";
381
+ return `${c}: ${shapes[c].map(words).join(", ")}`;
382
+ });
383
+ const out = [
384
+ `Matches laid out as ${reading}, each digit made of matches as on a digital clock. The matches of each symbol, from the left — ${said.join("; ")}.`,
385
+ ];
386
+ const at = (m) => `the ${words(m.place)} of the ${name(chars[m.char])}, the ${ordinal(m.char + 1)} symbol`;
387
+ const marks = s.marks ?? [];
388
+ const coloured = marks.filter((m) => m.mark !== "ghost");
389
+ if (coloured.length > 0)
390
+ out.push(`Coloured matches: ${coloured.map((m) => `${colors[m.mark].word} at ${at(m)}`).join("; ")}.`);
391
+ const ghosts = marks.filter((m) => m.mark === "ghost");
392
+ if (ghosts.length > 0)
393
+ out.push(`Empty places drawn dashed, with no match in them: ${ghosts.map(at).join("; ")}.`);
394
+ return out.join(" ");
395
+ }
396
+ export const matchsticks = {
397
+ kind: "matchsticks",
398
+ members,
399
+ check(scene, problems) {
400
+ only(scene, members, "scene", problems);
401
+ const hasText = scene.text !== undefined;
402
+ const hasSticks = scene.sticks !== undefined;
403
+ if (hasText && hasSticks)
404
+ problems.push("scene: give text or sticks, not both");
405
+ else if (hasText)
406
+ checkText(scene, problems);
407
+ else if (hasSticks)
408
+ checkSticks(scene, problems);
409
+ else
410
+ problems.push("scene: give text, for digits and signs, or sticks, for a grid");
411
+ },
412
+ draw(scene, pen) {
413
+ const s = scene;
414
+ const laid = s.text !== undefined ? layText(s) : layGrid(s);
415
+ let symbol;
416
+ for (const t of laid.sticks) {
417
+ if (t.symbol !== symbol) {
418
+ if (symbol !== undefined)
419
+ pen.add("</g>");
420
+ if (t.symbol !== undefined)
421
+ pen.add('<g class="symbol">');
422
+ symbol = t.symbol;
423
+ }
424
+ match(pen, t.a, t.b, laid.inset, t.mark);
425
+ }
426
+ if (symbol !== undefined)
427
+ pen.add("</g>");
428
+ return { width: laid.width, height: laid.height };
429
+ },
430
+ describe(scene) {
431
+ const s = scene;
432
+ return s.text !== undefined ? describeText(s) : describeGrid(s);
433
+ },
434
+ };
@@ -0,0 +1,27 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type Thing } from "../things.ts";
3
+ /**
4
+ * Necklace: beads on a string — closed, a necklace or a bracelet, its first
5
+ * bead at the top and the rest going round clockwise; or open, laid out flat
6
+ * from the left. A bead is any thing: a ball of a colour, a shape, an emoji,
7
+ * a card of one or two characters, "?" where the question asks, or an empty
8
+ * place; a bead is too small for a label or a weight. A closed necklace
9
+ * may have a clasp at the top, between its last bead and its first, and an
10
+ * open one the two halves of a clasp at its ends; scissors may mark a cut
11
+ * after a bead. Several necklaces stand in a row, each with its label; one
12
+ * marked newRow starts a row of its own, such as the options under the
13
+ * necklace a question shows.
14
+ */
15
+ export type Necklace = {
16
+ beads: Thing[];
17
+ newRow?: boolean;
18
+ open?: boolean;
19
+ clasp?: boolean;
20
+ cut?: number;
21
+ label?: string;
22
+ };
23
+ export type NecklaceScene = {
24
+ kind: "necklace";
25
+ necklaces: Necklace[];
26
+ };
27
+ export declare const necklace: Kind;