@altnovatrail/picture-kit 0.1.0 → 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 (130) hide show
  1. package/README.md +2 -1
  2. package/lib/index.d.ts +45 -1
  3. package/lib/index.js +34 -3
  4. package/lib/kinds/balance.js +2 -3
  5. package/lib/kinds/calendar.d.ts +5 -1
  6. package/lib/kinds/calendar.js +33 -11
  7. package/lib/kinds/chart.js +2 -4
  8. package/lib/kinds/clock.d.ts +5 -0
  9. package/lib/kinds/clock.js +45 -4
  10. package/lib/kinds/coins.js +3 -2
  11. package/lib/kinds/column.js +2 -3
  12. package/lib/kinds/commands.d.ts +8 -6
  13. package/lib/kinds/commands.js +57 -15
  14. package/lib/kinds/containers.js +2 -1
  15. package/lib/kinds/countfig.d.ts +2 -0
  16. package/lib/kinds/countfig.js +6 -1
  17. package/lib/kinds/cubes.d.ts +29 -5
  18. package/lib/kinds/cubes.js +82 -34
  19. package/lib/kinds/die.d.ts +8 -0
  20. package/lib/kinds/die.js +75 -14
  21. package/lib/kinds/domino.js +3 -3
  22. package/lib/kinds/dotgrid.d.ts +3 -0
  23. package/lib/kinds/dotgrid.js +6 -4
  24. package/lib/kinds/figure.js +5 -4
  25. package/lib/kinds/fold-parts.d.ts +0 -2
  26. package/lib/kinds/fold-parts.js +0 -4
  27. package/lib/kinds/fold.d.ts +20 -1
  28. package/lib/kinds/fold.js +15 -6
  29. package/lib/kinds/fraction.js +3 -2
  30. package/lib/kinds/gauges.js +2 -1
  31. package/lib/kinds/gears.d.ts +21 -2
  32. package/lib/kinds/gears.js +113 -62
  33. package/lib/kinds/grid.d.ts +5 -0
  34. package/lib/kinds/grid.js +30 -2
  35. package/lib/kinds/groups.d.ts +3 -1
  36. package/lib/kinds/groups.js +142 -28
  37. package/lib/kinds/lanes.js +3 -2
  38. package/lib/kinds/layers.d.ts +6 -0
  39. package/lib/kinds/layers.js +23 -4
  40. package/lib/kinds/line.d.ts +4 -0
  41. package/lib/kinds/line.js +117 -32
  42. package/lib/kinds/matchsticks.d.ts +4 -0
  43. package/lib/kinds/matchsticks.js +5 -3
  44. package/lib/kinds/net.js +4 -3
  45. package/lib/kinds/overlay.d.ts +23 -1
  46. package/lib/kinds/overlay.js +7 -3
  47. package/lib/kinds/pieces.js +3 -2
  48. package/lib/kinds/plane-parts.d.ts +0 -2
  49. package/lib/kinds/plane-parts.js +0 -6
  50. package/lib/kinds/plane.js +6 -6
  51. package/lib/kinds/ropes.js +1 -3
  52. package/lib/kinds/shapes.js +5 -7
  53. package/lib/kinds/solids.js +3 -2
  54. package/lib/kinds/tangram.js +2 -4
  55. package/lib/kinds/target.js +4 -6
  56. package/lib/kinds/tiles.js +5 -4
  57. package/lib/kinds/venn.js +3 -5
  58. package/lib/noto.js +1 -0
  59. package/lib/objects.js +2 -1
  60. package/lib/read.d.ts +2 -2
  61. package/lib/read.js +3 -3
  62. package/lib/solve/balance.d.ts +11 -0
  63. package/lib/solve/balance.js +116 -1
  64. package/lib/solve/calendar.d.ts +46 -0
  65. package/lib/solve/calendar.js +206 -0
  66. package/lib/solve/choice-scenes.d.ts +20 -0
  67. package/lib/solve/choice-scenes.js +51 -0
  68. package/lib/solve/clock.d.ts +54 -0
  69. package/lib/solve/clock.js +240 -0
  70. package/lib/solve/coins.d.ts +71 -0
  71. package/lib/solve/coins.js +222 -0
  72. package/lib/solve/commands.d.ts +36 -0
  73. package/lib/solve/commands.js +210 -0
  74. package/lib/solve/countfig.d.ts +41 -0
  75. package/lib/solve/countfig.js +199 -0
  76. package/lib/solve/cubes.d.ts +49 -0
  77. package/lib/solve/cubes.js +255 -0
  78. package/lib/solve/die.d.ts +39 -0
  79. package/lib/solve/die.js +93 -0
  80. package/lib/solve/domino.d.ts +57 -0
  81. package/lib/solve/domino.js +263 -0
  82. package/lib/solve/enumerate.d.ts +75 -0
  83. package/lib/solve/enumerate.js +323 -0
  84. package/lib/solve/figure.d.ts +26 -0
  85. package/lib/solve/figure.js +88 -0
  86. package/lib/solve/fold.d.ts +22 -0
  87. package/lib/solve/fold.js +68 -0
  88. package/lib/solve/gaps.d.ts +65 -0
  89. package/lib/solve/gaps.js +292 -0
  90. package/lib/solve/gears.d.ts +33 -0
  91. package/lib/solve/gears.js +130 -0
  92. package/lib/solve/grid.d.ts +35 -0
  93. package/lib/solve/grid.js +194 -0
  94. package/lib/solve/groups.d.ts +93 -0
  95. package/lib/solve/groups.js +346 -0
  96. package/lib/solve/index.d.ts +60 -0
  97. package/lib/solve/index.js +194 -0
  98. package/lib/solve/layers.d.ts +30 -0
  99. package/lib/solve/layers.js +72 -0
  100. package/lib/solve/magic.d.ts +32 -0
  101. package/lib/solve/magic.js +216 -0
  102. package/lib/solve/matchsticks.d.ts +37 -0
  103. package/lib/solve/matchsticks.js +214 -0
  104. package/lib/solve/order.d.ts +94 -0
  105. package/lib/solve/order.js +419 -0
  106. package/lib/solve/overlay.d.ts +22 -0
  107. package/lib/solve/overlay.js +56 -0
  108. package/lib/solve/parity.d.ts +22 -0
  109. package/lib/solve/parity.js +27 -0
  110. package/lib/solve/pattern.d.ts +62 -0
  111. package/lib/solve/pattern.js +533 -0
  112. package/lib/solve/pieces.d.ts +57 -0
  113. package/lib/solve/pieces.js +309 -0
  114. package/lib/solve/pigeonhole.d.ts +38 -0
  115. package/lib/solve/pigeonhole.js +95 -0
  116. package/lib/solve/proof.d.ts +115 -0
  117. package/lib/solve/proof.js +105 -0
  118. package/lib/solve/pyramid.d.ts +28 -0
  119. package/lib/solve/pyramid.js +161 -0
  120. package/lib/solve/question.d.ts +25 -0
  121. package/lib/solve/question.js +75 -0
  122. package/lib/solve/shapes.d.ts +30 -0
  123. package/lib/solve/shapes.js +182 -0
  124. package/lib/solve/target.d.ts +40 -0
  125. package/lib/solve/target.js +190 -0
  126. package/lib/style.d.ts +1 -1
  127. package/lib/style.js +1 -1
  128. package/lib/words.d.ts +15 -0
  129. package/lib/words.js +39 -0
  130. package/package.json +1 -1
@@ -4,6 +4,7 @@ import { int, isObj, list, oneOf, only, text } from "../read.js";
4
4
  import { colors, ground, ink, outline, soft } from "../style.js";
5
5
  import { n, poly } from "../svg.js";
6
6
  import { checkThing, drawThing, sameThing, wordOf } from "../things.js";
7
+ import { article } from "../words.js";
7
8
  import { smallText } from "./commands-parts.js";
8
9
  export const facings = ["up", "right", "down", "left"];
9
10
  /** arrows move one cell the way they point, whatever way the walker faces. */
@@ -190,21 +191,30 @@ function turnGlyph(pen, side, c) {
190
191
  thick(pen, `M${x(-7)} ${n(c.y + 14)}L${x(-7)} ${n(c.y + 1)}Q${x(-7)} ${n(c.y - 9)} ${x(3)} ${n(c.y - 9)}L${x(5)} ${n(c.y - 9)}`, fill);
191
192
  head(pen, { x: c.x + side * 16, y: c.y - 9 }, { x: side, y: 0 }, fill);
192
193
  }
193
- /** jumpGlyph is an arrow that hops over a little bar. */
194
+ /**
195
+ * jumpGlyph is an arrow that hops from a cell over the next, and a bar on it, to the cell after:
196
+ * three cells of a floor under it.
197
+ */
194
198
  function jumpGlyph(pen, c) {
195
199
  const fill = colors.purple.fill;
200
+ pen.el("path", {
201
+ d: `M${n(c.x - 19)} ${n(c.y + 17)}H${n(c.x + 19)}M${n(c.x - 6.5)} ${n(c.y + 13)}V${n(c.y + 17)}M${n(c.x + 6.5)} ${n(c.y + 13)}V${n(c.y + 17)}`,
202
+ stroke: ink,
203
+ "stroke-width": 2,
204
+ "stroke-linecap": "round",
205
+ });
196
206
  pen.el("rect", {
197
- x: c.x - 5,
198
- y: c.y + 4,
199
- width: 8,
200
- height: 11,
207
+ x: c.x - 3.5,
208
+ y: c.y + 5,
209
+ width: 7,
210
+ height: 12,
201
211
  rx: 2,
202
212
  fill: colors.gray.fill,
203
213
  stroke: ink,
204
214
  "stroke-width": 2,
205
215
  });
206
- thick(pen, `M${n(c.x - 14)} ${n(c.y + 14)}C${n(c.x - 14)} ${n(c.y - 17)} ${n(c.x + 13)} ${n(c.y - 17)} ${n(c.x + 13)} ${n(c.y + 3)}`, fill, 5);
207
- head(pen, { x: c.x + 13, y: c.y + 16 }, { x: 0, y: 1 }, fill, 8);
216
+ thick(pen, `M${n(c.x - 13)} ${n(c.y + 12)}C${n(c.x - 13)} ${n(c.y - 17)} ${n(c.x + 13)} ${n(c.y - 17)} ${n(c.x + 13)} ${n(c.y + 1)}`, fill, 5);
217
+ head(pen, { x: c.x + 13, y: c.y + 14 }, { x: 0, y: 1 }, fill, 7.5);
208
218
  }
209
219
  function drawTile(pen, c, x, cy) {
210
220
  const centre = { x: x + tile / 2, y: cy };
@@ -350,6 +360,10 @@ function name(at) {
350
360
  function key(at) {
351
361
  return `${at[0]},${at[1]}`;
352
362
  }
363
+ /** wallKey names the wall between two cells, whichever is named first. */
364
+ function wallKey(a, b) {
365
+ return [key(a), key(b)].sort((a, b) => a.localeCompare(b)).join(" ");
366
+ }
353
367
  function walkerOf(s) {
354
368
  return s.walker ?? "toy.robot";
355
369
  }
@@ -372,6 +386,15 @@ function drawBoard(pen, s, board, x, y) {
372
386
  stroke: ink,
373
387
  "stroke-width": outline + 0.5,
374
388
  });
389
+ for (const [a, b] of board.walls ?? []) {
390
+ // A wall stands on the whole edge two cells share.
391
+ const row = Math.max(a[0], b[0]);
392
+ const col = Math.max(a[1], b[1]);
393
+ const d = a[0] === b[0]
394
+ ? `M${n(x + (col - 1) * c)} ${n(y + (row - 1) * c)}V${n(y + row * c)}`
395
+ : `M${n(x + (col - 1) * c)} ${n(y + (row - 1) * c)}H${n(x + col * c)}`;
396
+ pen.el("path", { d, stroke: ink, "stroke-width": 7, class: "wall" });
397
+ }
375
398
  const centre = (at) => ({ x: x + (at[1] - 0.5) * c, y: y + (at[0] - 0.5) * c });
376
399
  const start = s.start?.at;
377
400
  if (board.path && start) {
@@ -535,7 +558,7 @@ function checkCommand(c, path, problems, depth) {
535
558
  problems.push(`${path}: must be a command, {"ask": true}, a repeat or an if`);
536
559
  }
537
560
  function checkBoard(b, start, problems) {
538
- only(b, ["rows", "cols", "target", "obstacles", "things", "marks", "path"], "board", problems);
561
+ only(b, ["rows", "cols", "target", "obstacles", "things", "marks", "walls", "path"], "board", problems);
539
562
  const before = problems.length;
540
563
  const board = {
541
564
  rows: int(b.rows, "board.rows", problems, 1, 8),
@@ -600,6 +623,24 @@ function checkBoard(b, start, problems) {
600
623
  put(checkAt(o.at, `${path}.at`, problems, board), `${path}.at`, path);
601
624
  });
602
625
  }
626
+ const walls = new Set();
627
+ if (b.walls !== undefined)
628
+ list(b.walls, "board.walls", problems, 1, 24).forEach((w, i) => {
629
+ const path = `board.walls[${i}]`;
630
+ if (!Array.isArray(w) || w.length !== 2) {
631
+ problems.push(`${path}: must be two cells beside each other, [[row, column], [row, column]]`);
632
+ return;
633
+ }
634
+ const a = checkAt(w[0], `${path}[0]`, problems, board);
635
+ const c = checkAt(w[1], `${path}[1]`, problems, board);
636
+ if (!a || !c)
637
+ return;
638
+ if (Math.abs(a[0] - c[0]) + Math.abs(a[1] - c[1]) !== 1)
639
+ problems.push(`${path}: ${name(a)} and ${name(c)} are not beside each other; a wall stands between two cells that share a side`);
640
+ else if (walls.has(wallKey(a, c)))
641
+ problems.push(`${path}: another wall stands between ${name(a)} and ${name(c)}`);
642
+ walls.add(wallKey(a, c));
643
+ });
603
644
  if (b.path === undefined)
604
645
  return;
605
646
  const obstacles = new Set((Array.isArray(b.obstacles) ? b.obstacles : [])
@@ -618,15 +659,14 @@ function checkBoard(b, start, problems) {
618
659
  problems.push(`${path}: ${name(here)} is not beside ${name(prev)}; a path goes from a cell to one beside it`);
619
660
  if (obstacles.has(key(here)))
620
661
  problems.push(`${path}: ${name(here)} holds an obstacle; a path goes round obstacles`);
662
+ if (prev && walls.has(wallKey(prev, here)))
663
+ problems.push(`${path}: a wall stands between ${name(prev)} and ${name(here)}; a path goes round walls`);
621
664
  if (seen.has(key(here)))
622
665
  problems.push(`${path}: the path came to ${name(here)} before; it visits a cell once`);
623
666
  seen.add(key(here));
624
667
  prev = here;
625
668
  });
626
669
  }
627
- function article(words) {
628
- return /^(?:uni|use|eu)/i.test(words) ? "a" : /^[aeiou]/i.test(words) ? "an" : "a";
629
- }
630
670
  function capital(words) {
631
671
  return `${words[0]?.toUpperCase() ?? ""}${words.slice(1)}`;
632
672
  }
@@ -637,7 +677,7 @@ function sayOne(c) {
637
677
  if (c === "step")
638
678
  return "step forward";
639
679
  if (c === "jump")
640
- return "jump";
680
+ return "jump two cells forward, over the cell ahead";
641
681
  if (c === "turn-left")
642
682
  return "turn left";
643
683
  if (c === "turn-right")
@@ -648,7 +688,7 @@ function sayOne(c) {
648
688
  return isAsk(c.repeat)
649
689
  ? `a repeat whose count is a question mark (${sayAll(c.do)})`
650
690
  : `repeat ${c.repeat} times (${sayAll(c.do)})`;
651
- const thing = isAsk(c.if) ? "a question mark" : `${article(wordOf(c.if))} ${wordOf(c.if)}`;
691
+ const thing = isAsk(c.if) ? "a question mark" : article(wordOf(c.if));
652
692
  return `check for ${thing}: if yes (${sayAll(c.yes)}), if no (${c.no ? sayAll(c.no) : "nothing"})`;
653
693
  }
654
694
  function sayAll(cmds) {
@@ -671,7 +711,7 @@ function sayGroups(spots) {
671
711
  .map((g) => {
672
712
  const word = wordOf(g.thing, g.at.length);
673
713
  return g.at.length === 1
674
- ? `${article(word)} ${word} at ${name(g.at[0])}`
714
+ ? `${article(word)} at ${name(g.at[0])}`
675
715
  : `${word} at ${g.at.map(name).join("; ")}`;
676
716
  })
677
717
  .join("; ");
@@ -767,7 +807,7 @@ export const commands = {
767
807
  out.push(`The ${walker} stands at ${name(s.start.at)}${facing ? `, facing ${facing}` : ""}.`);
768
808
  if (b.target) {
769
809
  const word = wordOf(b.target.thing);
770
- out.push(`The target, ${article(word)} ${word}, is at ${name(b.target.at)}.`);
810
+ out.push(`The target, ${article(word)}, is at ${name(b.target.at)}.`);
771
811
  }
772
812
  if (b.obstacles)
773
813
  out.push(`Obstacles: ${sayGroups(b.obstacles.map((o) => ({ at: o.at, thing: o.thing ?? rock })))}.`);
@@ -775,6 +815,8 @@ export const commands = {
775
815
  out.push(`${capital(sayGroups(b.things))}.`);
776
816
  if (b.marks)
777
817
  out.push(`Cells marked with letters: ${b.marks.map((m) => `${m.label} at ${name(m.at)}`).join("; ")}.`);
818
+ if (b.walls)
819
+ out.push(`Walls stand between ${b.walls.map(([a, c]) => `${name(a)} and ${name(c)}`).join("; between ")}.`);
778
820
  if (b.path && s.start?.at) {
779
821
  const steps = b.path;
780
822
  out.push(`A red line with an arrow goes from the ${walker}${steps.length > 1 ? ` through ${steps.slice(0, -1).map(name).join(", then ")}` : ""} to ${name(steps[steps.length - 1])}.`);
@@ -3,7 +3,8 @@ import { textWidth } from "../pen.js";
3
3
  import { int, isObj, list, oneOf, only, text } from "../read.js";
4
4
  import { colorNames, colors, faint, ink, isColor, outline } from "../style.js";
5
5
  import { n, poly } from "../svg.js";
6
- import { article, mix } from "./cubes.js";
6
+ import { article } from "../words.js";
7
+ import { mix } from "./cubes.js";
7
8
  /**
8
9
  * Containers: vessels that hold a liquid — jugs, glasses, bottles, buckets,
9
10
  * tanks — each with its capacity, in rows of steps. The first row is the
@@ -31,6 +31,8 @@ export type CountfigScene = {
31
31
  highlight?: Highlight;
32
32
  facts?: Partial<Record<Countable, number>>;
33
33
  };
34
+ /** lonePoints tells which points of a scene lie on no segment, so that no shape has them. */
35
+ export declare function lonePoints(f: CountfigScene): boolean[];
34
36
  /**
35
37
  * countShapes counts what a task may ask about: the triangles, squares and
36
38
  * rectangles whose sides lie along the lines of the figure, with corners at
@@ -3,7 +3,8 @@ import { textWidth } from "../pen.js";
3
3
  import { int, isObj, list, only } from "../read.js";
4
4
  import { colorNames, colors, ink, isColor, outline } from "../style.js";
5
5
  import { byCodeUnit, poly } from "../svg.js";
6
- import { area, around, boxAt, boxToSegment, covered, crossing, distance, dot, gap, listOf, many, meet, pointOf, say, sub, toBox, toLine, toSegment, widest, } from "./plane-parts.js";
6
+ import { listOf } from "../words.js";
7
+ import { area, around, boxAt, boxToSegment, covered, crossing, distance, dot, gap, many, meet, pointOf, say, sub, toBox, toLine, toSegment, widest, } from "./plane-parts.js";
7
8
  export const countables = ["triangles", "squares", "rectangles", "segments"];
8
9
  /** The figure lies in a square of this many units a side. */
9
10
  const span = 20;
@@ -52,6 +53,10 @@ function directions(fig, k) {
52
53
  function alone(fig) {
53
54
  return fig.at.map((p) => fig.segs.every(([a, b]) => toSegment(p, a, b) > near));
54
55
  }
56
+ /** lonePoints tells which points of a scene lie on no segment, so that no shape has them. */
57
+ export function lonePoints(f) {
58
+ return alone(figureOf(f));
59
+ }
55
60
  /** letterPlaces finds a place for each point's letter, in pixels from the figure's top left. */
56
61
  function letterPlaces(f, fig, unit) {
57
62
  const px = fig.at.map((p) => ({ x: p.x * unit, y: p.y * unit }));
@@ -8,17 +8,21 @@ import { type Thing } from "../things.ts";
8
8
  * to the front, z up from the table. A structure gives its cubes by place, or
9
9
  * as a height map: rows from the back, places from the left, the number of
10
10
  * cubes stacked in each. Each cube is one colour of the palette; its top is
11
- * the colour itself, its front a darker tone, its right side the darkest.
11
+ * the colour itself, its front a darker tone, its right side the darkest. A
12
+ * cube given by place may paint a face of its own, top, front or right, in
13
+ * another colour, on a face in sight.
12
14
  * A face shows exactly where no nearer cube covers it, so the picture is the
13
15
  * structure seen from the front, the right and above.
14
16
  */
15
17
  export type At = [number, number, number];
18
+ export declare const faces: readonly ["top", "front", "right"];
19
+ export type Face = (typeof faces)[number];
20
+ export type Painted = Partial<Record<Face, ColorName>>;
16
21
  export type CubeItem = At | {
17
22
  at: At;
18
23
  color?: ColorName;
24
+ faces?: Painted;
19
25
  };
20
- export declare const faces: readonly ["top", "front", "right"];
21
- export type Face = (typeof faces)[number];
22
26
  export type Mark = {
23
27
  cube: At;
24
28
  face: Face;
@@ -40,6 +44,7 @@ export type Cube = {
40
44
  y: number;
41
45
  z: number;
42
46
  color: ColorName;
47
+ faces?: Painted;
43
48
  };
44
49
  /** The side of the square a sticker on a face is drawn in, before it is laid on the face. */
45
50
  export declare const box = 64;
@@ -63,8 +68,27 @@ export declare function cornersOf(c: {
63
68
  }, face: Face, at: Projector): P[];
64
69
  /** onFace is the transform that lays a box × box square on a face given by its corners. */
65
70
  export declare function onFace(corners: readonly P[]): string;
71
+ /** A place of the building: x along a row, y the row from the back, z the height, from 0. */
72
+ export type Place = {
73
+ x: number;
74
+ y: number;
75
+ z: number;
76
+ };
77
+ /**
78
+ * The picture is a grid of small triangles, and each face of a cube covers
79
+ * two of them. triangles names them; a triangle shows the face of the cube
80
+ * nearest the eye, the one whose x + y + z is greatest.
81
+ */
82
+ export declare function triangles(c: Place, face: Face): string[];
83
+ /**
84
+ * pictureOf is what cubes show: each small triangle in sight, by its name, and the face of the
85
+ * cube it shows, top, front or right, with the cube's index. Two buildings with the same picture,
86
+ * face for face, are drawn alike but for their colours.
87
+ */
88
+ export declare function pictureOf(cubes: readonly Place[]): Map<string, {
89
+ face: Face;
90
+ cube: number;
91
+ }>;
66
92
  /** sight says, for each cube and face, how many of its two triangles are in sight. */
67
93
  export declare function sight(cubes: readonly Cube[]): Record<Face, number>[];
68
- /** article is "a" or "an" before a word. */
69
- export declare function article(word: string): string;
70
94
  export declare const cubes: Kind;
@@ -4,6 +4,7 @@ import { int, isObj, list, oneOf, only, text } from "../read.js";
4
4
  import { colorNames, colors, ink, isColor, outline } from "../style.js";
5
5
  import { poly } from "../svg.js";
6
6
  import { checkThing, drawThing, wordOf } from "../things.js";
7
+ import { article, listOf } from "../words.js";
7
8
  export const faces = ["top", "front", "right"];
8
9
  const most = 30;
9
10
  const reach = 9;
@@ -47,8 +48,12 @@ export function cubesOf(st) {
47
48
  }
48
49
  return (st.cubes ?? []).map((item) => {
49
50
  const [x, y, z] = Array.isArray(item) ? item : item.at;
50
- const color = Array.isArray(item) ? base : (item.color ?? base);
51
- return { x, y, z, color };
51
+ if (Array.isArray(item))
52
+ return { x, y, z, color: base };
53
+ const cube = { x, y, z, color: item.color ?? base };
54
+ if (item.faces)
55
+ cube.faces = item.faces;
56
+ return cube;
52
57
  });
53
58
  }
54
59
  export function projector(s, ox, oy) {
@@ -77,7 +82,7 @@ export function onFace(corners) {
77
82
  * two of them. triangles names them; a triangle shows the face of the cube
78
83
  * nearest the eye, the one whose x + y + z is greatest.
79
84
  */
80
- function triangles(c, face) {
85
+ export function triangles(c, face) {
81
86
  const u = 3 * (c.x - c.y);
82
87
  const v = c.x + c.y - 2 * c.z;
83
88
  if (face === "top")
@@ -86,24 +91,30 @@ function triangles(c, face) {
86
91
  return [`${u - 1},${v + 1}`, `${u - 2},${v}`];
87
92
  return [`${u + 1},${v + 1}`, `${u + 2},${v}`];
88
93
  }
89
- /** sight says, for each cube and face, how many of its two triangles are in sight. */
90
- export function sight(cubes) {
91
- const owner = new Map();
94
+ /**
95
+ * pictureOf is what cubes show: each small triangle in sight, by its name, and the face of the
96
+ * cube it shows, top, front or right, with the cube's index. Two buildings with the same picture,
97
+ * face for face, are drawn alike but for their colours.
98
+ */
99
+ export function pictureOf(cubes) {
100
+ const out = new Map();
92
101
  const depth = (c) => c.x + c.y + c.z;
93
102
  cubes.forEach((c, i) => {
94
103
  for (const face of faces)
95
104
  for (const t of triangles(c, face)) {
96
- const was = owner.get(t);
97
- if (was === undefined || depth(cubes[was]) < depth(c))
98
- owner.set(t, i);
105
+ const was = out.get(t);
106
+ if (was === undefined || depth(cubes[was.cube]) < depth(c))
107
+ out.set(t, { face, cube: i });
99
108
  }
100
109
  });
101
- return cubes.map((c, i) => {
102
- const seen = { top: 0, front: 0, right: 0 };
103
- for (const face of faces)
104
- seen[face] = triangles(c, face).filter((t) => owner.get(t) === i).length;
105
- return seen;
106
- });
110
+ return out;
111
+ }
112
+ /** sight says, for each cube and face, how many of its two triangles are in sight. */
113
+ export function sight(cubes) {
114
+ const seen = cubes.map(() => ({ top: 0, front: 0, right: 0 }));
115
+ for (const { face, cube } of pictureOf(cubes).values())
116
+ seen[cube][face]++;
117
+ return seen;
107
118
  }
108
119
  function boundsOf(cubes) {
109
120
  return {
@@ -149,10 +160,20 @@ function checkStructure(st, path, problems) {
149
160
  list(st.cubes, `${path}.cubes`, problems, 1, most).forEach((item, j) => {
150
161
  const p = `${path}.cubes[${j}]`;
151
162
  if (isObj(item)) {
152
- only(item, ["at", "color"], p, problems);
163
+ only(item, ["at", "color", "faces"], p, problems);
153
164
  checkAt(item.at, `${p}.at`, problems);
154
165
  if (item.color !== undefined && !isColor(item.color))
155
166
  problems.push(`${p}.color: must be a colour: ${colorNames.join(", ")}`);
167
+ if (item.faces !== undefined) {
168
+ if (!isObj(item.faces) || Object.keys(item.faces).length === 0)
169
+ problems.push(`${p}.faces: must give a colour for any of top, front and right`);
170
+ else {
171
+ only(item.faces, faces, `${p}.faces`, problems);
172
+ for (const f of faces)
173
+ if (item.faces[f] !== undefined && !isColor(item.faces[f]))
174
+ problems.push(`${p}.faces.${f}: must be a colour: ${colorNames.join(", ")}`);
175
+ }
176
+ }
156
177
  }
157
178
  else
158
179
  checkAt(item, p, problems);
@@ -204,9 +225,14 @@ function checkStructure(st, path, problems) {
204
225
  const apart = cubes.findIndex((_, j) => !reached.has(j));
205
226
  if (apart >= 0)
206
227
  problems.push(`${named[apart]}: the cubes of a structure must hold together face to face; this one is apart`);
228
+ const seen = sight(cubes);
229
+ cubes.forEach((c, j) => {
230
+ for (const f of faces)
231
+ if (c.faces?.[f] !== undefined && seen[j][f] < 2)
232
+ problems.push(`${named[j]}.faces.${f}: the ${f} face of this cube is not wholly in sight; paint a face no cube covers`);
233
+ });
207
234
  if (st.marks === undefined)
208
235
  return;
209
- const seen = sight(cubes);
210
236
  const taken = new Set();
211
237
  list(st.marks, `${path}.marks`, problems, 1, 6).forEach((m, j) => {
212
238
  const p = `${path}.marks[${j}]`;
@@ -243,14 +269,13 @@ function drawStructure(pen, st, cubes, at) {
243
269
  });
244
270
  for (const i of order) {
245
271
  const c = cubes[i];
246
- const fill = tones(c.color);
247
272
  for (const face of faces) {
248
273
  if (seen[i][face] === 0)
249
274
  continue;
250
275
  const corners = cornersOf(c, face, at);
251
276
  pen.el("path", {
252
277
  d: poly(corners),
253
- fill: fill[face],
278
+ fill: tones(c.faces?.[face] ?? c.color)[face],
254
279
  stroke: ink,
255
280
  "stroke-width": outline,
256
281
  "stroke-linejoin": "round",
@@ -265,28 +290,44 @@ function drawStructure(pen, st, cubes, at) {
265
290
  }
266
291
  }
267
292
  const ordinal = (k) => String(k + 1);
268
- /** article is "a" or "an" before a word. */
269
- export function article(word) {
270
- return /^[aeiou]/i.test(word) ? `an ${word}` : `a ${word}`;
271
- }
293
+ const plural = (k, w) => `${k} ${w}${k === 1 ? "" : "s"}`;
272
294
  function where(c, o) {
273
295
  return `row ${ordinal(c.y - o.y)}, place ${ordinal(c.x - o.x)}, level ${ordinal(c.z)}`;
274
296
  }
297
+ /**
298
+ * said lists the cubes the words give: the cubes in sight, with whether each is, and — when every
299
+ * cube stands on the table or on another — the cubes under one in sight, which hold it up. A cube
300
+ * hidden from sight with none in sight above it is left out, so the words never give its place,
301
+ * its height or its count.
302
+ */
303
+ function said(cubes) {
304
+ const seen = sight(cubes);
305
+ const shows = cubes.map((_, i) => faces.some((f) => seen[i][f] > 0));
306
+ const upright = cubes.every((c) => c.z === 0 || cubes.some((d) => d.x === c.x && d.y === c.y && d.z === c.z - 1));
307
+ return cubes
308
+ .map((cube, i) => ({ cube, shows: shows[i] }))
309
+ .filter(({ cube, shows: own }) => own ||
310
+ (upright &&
311
+ cubes.some((d, j) => shows[j] && d.x === cube.x && d.y === cube.y && d.z > cube.z)));
312
+ }
275
313
  function sayStructure(st) {
276
- const cubes = cubesOf(st);
314
+ const told = said(cubesOf(st));
315
+ const cubes = told.map((t) => t.cube);
316
+ const unseen = new Set(told.filter((t) => !t.shows).map((t) => t.cube));
277
317
  const o = { x: Math.min(...cubes.map((c) => c.x)), y: Math.min(...cubes.map((c) => c.y)) };
278
318
  const rows = Math.max(...cubes.map((c) => c.y)) - o.y + 1;
279
319
  const places = Math.max(...cubes.map((c) => c.x)) - o.x + 1;
280
320
  const stack = (x, y) => cubes.filter((c) => c.x === x + o.x && c.y === y + o.y).sort((a, b) => a.z - b.z);
281
321
  const upright = cubes.every((c) => c.z === 0 || stack(c.x - o.x, c.y - o.y)[c.z - 1]?.z === c.z - 1);
282
- const used = colorNames.filter((name) => cubes.some((c) => c.color === name));
322
+ const inSight = cubes.filter((c) => !unseen.has(c));
323
+ const used = colorNames.filter((name) => inSight.some((c) => c.color === name));
283
324
  const one = used.length === 1;
284
- const plural = (k, w) => `${k} ${w}${k === 1 ? "" : "s"}`;
325
+ const color = (c) => (unseen.has(c) ? "not in sight" : colors[c.color].word);
285
326
  let out;
286
327
  if (upright && rows === 1 && places === 1) {
287
- out = `one stack of ${cubes.length} cubes; from the bottom up: ${cubes
328
+ out = `one stack of ${plural(cubes.length, "cube")}; from the bottom up: ${cubes
288
329
  .sort((a, b) => a.z - b.z)
289
- .map((c) => colors[c.color].word)
330
+ .map(color)
290
331
  .join(", ")}`;
291
332
  }
292
333
  else if (upright) {
@@ -294,9 +335,9 @@ function sayStructure(st) {
294
335
  const s = stack(x, y);
295
336
  return one || s.length === 0
296
337
  ? String(s.length)
297
- : `${s.length} (${s.map((c) => colors[c.color].word).join(", ")})`;
338
+ : `${s.length} (${s.map(color).join(", ")})`;
298
339
  }).join(", "));
299
- out = `stacks of ${one ? `${colors[used[0]].word} ` : ""}cubes on ${plural(rows, "row")} of ${plural(places, "place")}; seen from above, from the back row to the front row and from left to right, the number of cubes in each place${one ? "" : " (their colours from the bottom up)"}: ${lines.join("; ")}`;
340
+ out = `stacks of ${one ? `${colors[used[0]].word} ` : ""}cubes on ${plural(rows, "row")} of ${plural(places, "place")}; seen from above, from the back row to the front row and from left to right, how many cubes high each place shows${one ? "" : " (their colours from the bottom up)"}: ${lines.join("; ")}`;
300
341
  }
301
342
  else {
302
343
  const levels = Math.max(...cubes.map((c) => c.z)) + 1;
@@ -307,15 +348,22 @@ function sayStructure(st) {
307
348
  .sort((a, b) => a.x - b.x)
308
349
  .map((c) => `${ordinal(c.x - o.x)}${one ? "" : ` ${colors[c.color].word}`}`);
309
350
  return here.length
310
- ? `row ${ordinal(y)}: ${plural(here.length, "cube")} at place ${here.join(", ")}`
351
+ ? `row ${ordinal(y)}: ${plural(here.length, "cube")} at place${here.length === 1 ? "" : "s"} ${listOf(here)}`
311
352
  : "";
312
353
  }).filter(Boolean);
313
- return `level ${ordinal(z)} — ${rowsAt.join("; ")}`;
354
+ return `${z === 0 ? "level" : "Level"} ${ordinal(z)} — ${rowsAt.join("; ")}`;
314
355
  });
315
- out = `${one ? `${colors[used[0]].word} ` : ""}cubes on ${plural(levels, "level")} from the table up, rows from the back to the front, places from left to right: ${say.join(". ")}`;
356
+ out = `${one ? `${colors[used[0]].word} ` : ""}cubes in sight on ${plural(levels, "level")} from the table up, rows from the back to the front, places from left to right: ${say.join(". ")}`;
316
357
  }
317
358
  if (!one)
318
- out += `. In all, ${used.map((name) => plural(cubes.filter((c) => c.color === name).length, `${colors[name].word} cube`)).join(" and ")}`;
359
+ out += `. In sight, ${listOf(used.map((name) => plural(inSight.filter((c) => c.color === name).length, `${colors[name].word} cube`)))}`;
360
+ const painted = cubes.filter((c) => c.faces);
361
+ if (painted.length > 0)
362
+ out += `. Painted faces: ${painted
363
+ .map((c) => `the cube at ${where(c, o)} has ${listOf(faces
364
+ .filter((f) => c.faces[f])
365
+ .map((f) => `its ${f} face ${colors[c.faces[f]].word}`))}`)
366
+ .join("; ")}`;
319
367
  for (const m of st.marks ?? [])
320
368
  out += `. On the ${m.face} face of the cube at ${where({ x: m.cube[0], y: m.cube[1], z: m.cube[2] }, o)}: ${typeof m.thing === "object" && "ask" in m.thing ? "a question mark" : article(wordOf(m.thing))}`;
321
369
  return `${out}.`;
@@ -7,17 +7,22 @@ import type { Kind } from "../kind.ts";
7
7
  * die is a standard one, opposite faces adding up to 7, so no two faces that
8
8
  * add up to 7 show together; with same, the views are all of one and the
9
9
  * same die, and the check proves that one die can show them all.
10
+ * The pips of 2, 3 and 6 lie one way or the other: a face turned 90 is the
11
+ * same face a quarter turn clockwise, so a die can be drawn as it lies.
10
12
  */
11
13
  export type Value = number | "?";
14
+ export type Turn = (typeof turnings)[number];
12
15
  export type View = {
13
16
  top: Value;
14
17
  front: Value;
15
18
  right: Value;
16
19
  label?: string;
20
+ turns?: Partial<Record<(typeof shown)[number], Turn>>;
17
21
  };
18
22
  export type Flat = {
19
23
  face: Value;
20
24
  label?: string;
25
+ turn?: Turn;
21
26
  };
22
27
  export type Die = View | Flat;
23
28
  export type DieScene = {
@@ -27,6 +32,9 @@ export type DieScene = {
27
32
  same?: boolean;
28
33
  arrows?: boolean;
29
34
  };
35
+ declare const shown: readonly ["top", "front", "right"];
36
+ export declare const turnings: readonly [0, 90];
30
37
  /** pips are the places of the pips of each value on a box × box face. */
31
38
  export declare const pips: Record<number, [number, number][]>;
32
39
  export declare const die: Kind;
40
+ export {};