@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.
- package/LICENSE +201 -0
- package/NOTICE +7 -0
- package/README.md +28 -2
- package/lib/emoji.d.ts +16 -0
- package/lib/emoji.js +64 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +10 -0
- package/lib/kind.d.ts +22 -0
- package/lib/kind.js +2 -0
- package/lib/kinds/balance.d.ts +34 -0
- package/lib/kinds/balance.js +217 -0
- package/lib/kinds/branches.d.ts +33 -0
- package/lib/kinds/branches.js +473 -0
- package/lib/kinds/calendar.d.ts +55 -0
- package/lib/kinds/calendar.js +532 -0
- package/lib/kinds/chart.d.ts +48 -0
- package/lib/kinds/chart.js +645 -0
- package/lib/kinds/choices.d.ts +14 -0
- package/lib/kinds/choices.js +53 -0
- package/lib/kinds/clock.d.ts +36 -0
- package/lib/kinds/clock.js +219 -0
- package/lib/kinds/coins.d.ts +37 -0
- package/lib/kinds/coins.js +550 -0
- package/lib/kinds/column.d.ts +37 -0
- package/lib/kinds/column.js +410 -0
- package/lib/kinds/commands-parts.d.ts +12 -0
- package/lib/kinds/commands-parts.js +24 -0
- package/lib/kinds/commands.d.ts +66 -0
- package/lib/kinds/commands.js +789 -0
- package/lib/kinds/containers.d.ts +52 -0
- package/lib/kinds/containers.js +758 -0
- package/lib/kinds/countfig.d.ts +41 -0
- package/lib/kinds/countfig.js +372 -0
- package/lib/kinds/cubes.d.ts +70 -0
- package/lib/kinds/cubes.js +371 -0
- package/lib/kinds/die.d.ts +32 -0
- package/lib/kinds/die.js +301 -0
- package/lib/kinds/domino.d.ts +37 -0
- package/lib/kinds/domino.js +450 -0
- package/lib/kinds/dotgrid.d.ts +38 -0
- package/lib/kinds/dotgrid.js +411 -0
- package/lib/kinds/figure.d.ts +62 -0
- package/lib/kinds/figure.js +687 -0
- package/lib/kinds/fold-parts.d.ts +73 -0
- package/lib/kinds/fold-parts.js +315 -0
- package/lib/kinds/fold.d.ts +52 -0
- package/lib/kinds/fold.js +650 -0
- package/lib/kinds/fraction.d.ts +50 -0
- package/lib/kinds/fraction.js +568 -0
- package/lib/kinds/gauges.d.ts +53 -0
- package/lib/kinds/gauges.js +880 -0
- package/lib/kinds/gears.d.ts +40 -0
- package/lib/kinds/gears.js +787 -0
- package/lib/kinds/grid.d.ts +45 -0
- package/lib/kinds/grid.js +591 -0
- package/lib/kinds/groups.d.ts +23 -0
- package/lib/kinds/groups.js +335 -0
- package/lib/kinds/index.d.ts +3 -0
- package/lib/kinds/index.js +87 -0
- package/lib/kinds/lanes.d.ts +36 -0
- package/lib/kinds/lanes.js +220 -0
- package/lib/kinds/layers.d.ts +31 -0
- package/lib/kinds/layers.js +419 -0
- package/lib/kinds/line.d.ts +38 -0
- package/lib/kinds/line.js +739 -0
- package/lib/kinds/magic-parts.d.ts +72 -0
- package/lib/kinds/magic-parts.js +186 -0
- package/lib/kinds/magic.d.ts +66 -0
- package/lib/kinds/magic.js +664 -0
- package/lib/kinds/map.d.ts +32 -0
- package/lib/kinds/map.js +370 -0
- package/lib/kinds/matchsticks.d.ts +26 -0
- package/lib/kinds/matchsticks.js +434 -0
- package/lib/kinds/necklace.d.ts +27 -0
- package/lib/kinds/necklace.js +342 -0
- package/lib/kinds/net.d.ts +47 -0
- package/lib/kinds/net.js +241 -0
- package/lib/kinds/overlay.d.ts +50 -0
- package/lib/kinds/overlay.js +519 -0
- package/lib/kinds/pieces.d.ts +41 -0
- package/lib/kinds/pieces.js +420 -0
- package/lib/kinds/pipes.d.ts +46 -0
- package/lib/kinds/pipes.js +657 -0
- package/lib/kinds/plane-parts.d.ts +105 -0
- package/lib/kinds/plane-parts.js +372 -0
- package/lib/kinds/plane.d.ts +35 -0
- package/lib/kinds/plane.js +755 -0
- package/lib/kinds/pyramid.d.ts +26 -0
- package/lib/kinds/pyramid.js +228 -0
- package/lib/kinds/ropes.d.ts +30 -0
- package/lib/kinds/ropes.js +571 -0
- package/lib/kinds/shapes.d.ts +59 -0
- package/lib/kinds/shapes.js +490 -0
- package/lib/kinds/solids.d.ts +78 -0
- package/lib/kinds/solids.js +1058 -0
- package/lib/kinds/tangram.d.ts +52 -0
- package/lib/kinds/tangram.js +713 -0
- package/lib/kinds/target.d.ts +39 -0
- package/lib/kinds/target.js +761 -0
- package/lib/kinds/tiles.d.ts +55 -0
- package/lib/kinds/tiles.js +606 -0
- package/lib/kinds/venn.d.ts +38 -0
- package/lib/kinds/venn.js +710 -0
- package/lib/noto.d.ts +2 -0
- package/lib/noto.js +212 -0
- package/lib/objects.d.ts +7 -0
- package/lib/objects.js +226 -0
- package/lib/pen.d.ts +32 -0
- package/lib/pen.js +60 -0
- package/lib/read.d.ts +9 -0
- package/lib/read.js +49 -0
- package/lib/render.d.ts +24 -0
- package/lib/render.js +68 -0
- package/lib/solve/balance.d.ts +46 -0
- package/lib/solve/balance.js +118 -0
- package/lib/style.d.ts +70 -0
- package/lib/style.js +39 -0
- package/lib/svg.d.ts +22 -0
- package/lib/svg.js +48 -0
- package/lib/things.d.ts +40 -0
- package/lib/things.js +269 -0
- package/package.json +28 -4
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { int, isObj, list, oneOf, only, text } from "../read.js";
|
|
3
|
+
import { colorNames, colors, faint, ground, ink, isColor, outline, } from "../style.js";
|
|
4
|
+
import { poly } from "../svg.js";
|
|
5
|
+
/**
|
|
6
|
+
* Pieces: polyomino pieces cut from squared paper, drawn apart in a row, each
|
|
7
|
+
* a list of its squares, a colour and an optional label under it; and
|
|
8
|
+
* optionally a board, the outline of squares the pieces are to cover, drawn
|
|
9
|
+
* below at the same scale. A square is [row, column], counted from 0 at the
|
|
10
|
+
* top left of its piece, or [row, column, half] for the triangle left when a
|
|
11
|
+
* square is cut along a diagonal: "nw" keeps the half at the top-left corner,
|
|
12
|
+
* "ne", "sw" and "se" the others. Every piece is drawn square by square, its
|
|
13
|
+
* inner lines showing, so the squares can be counted.
|
|
14
|
+
*/
|
|
15
|
+
export const halves = ["nw", "ne", "sw", "se"];
|
|
16
|
+
const opposite = {
|
|
17
|
+
top: "bottom",
|
|
18
|
+
bottom: "top",
|
|
19
|
+
left: "right",
|
|
20
|
+
right: "left",
|
|
21
|
+
};
|
|
22
|
+
const step = {
|
|
23
|
+
top: [-1, 0],
|
|
24
|
+
bottom: [1, 0],
|
|
25
|
+
left: [0, -1],
|
|
26
|
+
right: [0, 1],
|
|
27
|
+
};
|
|
28
|
+
const unitSide = {
|
|
29
|
+
top: [
|
|
30
|
+
{ x: 0, y: 0 },
|
|
31
|
+
{ x: 1, y: 0 },
|
|
32
|
+
],
|
|
33
|
+
right: [
|
|
34
|
+
{ x: 1, y: 0 },
|
|
35
|
+
{ x: 1, y: 1 },
|
|
36
|
+
],
|
|
37
|
+
bottom: [
|
|
38
|
+
{ x: 0, y: 1 },
|
|
39
|
+
{ x: 1, y: 1 },
|
|
40
|
+
],
|
|
41
|
+
left: [
|
|
42
|
+
{ x: 0, y: 0 },
|
|
43
|
+
{ x: 0, y: 1 },
|
|
44
|
+
],
|
|
45
|
+
};
|
|
46
|
+
/** The corners and the whole sides of a square and of each half of one. */
|
|
47
|
+
const forms = {
|
|
48
|
+
full: {
|
|
49
|
+
points: [
|
|
50
|
+
{ x: 0, y: 0 },
|
|
51
|
+
{ x: 1, y: 0 },
|
|
52
|
+
{ x: 1, y: 1 },
|
|
53
|
+
{ x: 0, y: 1 },
|
|
54
|
+
],
|
|
55
|
+
sides: ["top", "right", "bottom", "left"],
|
|
56
|
+
},
|
|
57
|
+
nw: {
|
|
58
|
+
points: [
|
|
59
|
+
{ x: 0, y: 0 },
|
|
60
|
+
{ x: 1, y: 0 },
|
|
61
|
+
{ x: 0, y: 1 },
|
|
62
|
+
],
|
|
63
|
+
sides: ["top", "left"],
|
|
64
|
+
cut: [
|
|
65
|
+
{ x: 1, y: 0 },
|
|
66
|
+
{ x: 0, y: 1 },
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
ne: {
|
|
70
|
+
points: [
|
|
71
|
+
{ x: 0, y: 0 },
|
|
72
|
+
{ x: 1, y: 0 },
|
|
73
|
+
{ x: 1, y: 1 },
|
|
74
|
+
],
|
|
75
|
+
sides: ["top", "right"],
|
|
76
|
+
cut: [
|
|
77
|
+
{ x: 0, y: 0 },
|
|
78
|
+
{ x: 1, y: 1 },
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
sw: {
|
|
82
|
+
points: [
|
|
83
|
+
{ x: 0, y: 0 },
|
|
84
|
+
{ x: 1, y: 1 },
|
|
85
|
+
{ x: 0, y: 1 },
|
|
86
|
+
],
|
|
87
|
+
sides: ["left", "bottom"],
|
|
88
|
+
cut: [
|
|
89
|
+
{ x: 0, y: 0 },
|
|
90
|
+
{ x: 1, y: 1 },
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
se: {
|
|
94
|
+
points: [
|
|
95
|
+
{ x: 1, y: 0 },
|
|
96
|
+
{ x: 1, y: 1 },
|
|
97
|
+
{ x: 0, y: 1 },
|
|
98
|
+
],
|
|
99
|
+
sides: ["right", "bottom"],
|
|
100
|
+
cut: [
|
|
101
|
+
{ x: 1, y: 0 },
|
|
102
|
+
{ x: 0, y: 1 },
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
const size = 36;
|
|
107
|
+
const most = { pieces: 10, squares: 12, extent: 6, boardExtent: 12, boardSquares: 144 };
|
|
108
|
+
const halfWords = {
|
|
109
|
+
nw: "top-left",
|
|
110
|
+
ne: "top-right",
|
|
111
|
+
sw: "bottom-left",
|
|
112
|
+
se: "bottom-right",
|
|
113
|
+
};
|
|
114
|
+
/** squaresOf moves a shape's squares so that its top row and left column are 0. */
|
|
115
|
+
function squaresOf(cells) {
|
|
116
|
+
const r0 = Math.min(...cells.map((s) => s[0]));
|
|
117
|
+
const c0 = Math.min(...cells.map((s) => s[1]));
|
|
118
|
+
return cells.map((s) => ({ r: s[0] - r0, c: s[1] - c0, form: s[2] ?? "full" }));
|
|
119
|
+
}
|
|
120
|
+
function boardSquares(board) {
|
|
121
|
+
if (board.cells)
|
|
122
|
+
return squaresOf(board.cells);
|
|
123
|
+
const out = [];
|
|
124
|
+
for (let r = 0; r < (board.rows ?? 1); r++)
|
|
125
|
+
for (let c = 0; c < (board.cols ?? 1); c++)
|
|
126
|
+
out.push({ r, c, form: "full" });
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
function extent(squares) {
|
|
130
|
+
return {
|
|
131
|
+
rows: Math.max(...squares.map((s) => s.r)) + 1,
|
|
132
|
+
cols: Math.max(...squares.map((s) => s.c)) + 1,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** joined tells whether a square meets the next one along a side, whole side to whole side. */
|
|
136
|
+
function joined(squares, sq, side) {
|
|
137
|
+
if (!forms[sq.form].sides.includes(side))
|
|
138
|
+
return false;
|
|
139
|
+
const [dr, dc] = step[side];
|
|
140
|
+
const other = squares.find((o) => o.r === sq.r + dr && o.c === sq.c + dc);
|
|
141
|
+
return other !== undefined && forms[other.form].sides.includes(opposite[side]);
|
|
142
|
+
}
|
|
143
|
+
function connected(squares) {
|
|
144
|
+
const seen = new Set([squares[0]]);
|
|
145
|
+
const todo = [squares[0]];
|
|
146
|
+
while (todo.length > 0) {
|
|
147
|
+
const sq = todo.pop();
|
|
148
|
+
for (const side of forms[sq.form].sides) {
|
|
149
|
+
if (!joined(squares, sq, side))
|
|
150
|
+
continue;
|
|
151
|
+
const [dr, dc] = step[side];
|
|
152
|
+
const other = squares.find((o) => o.r === sq.r + dr && o.c === sq.c + dc);
|
|
153
|
+
if (!seen.has(other)) {
|
|
154
|
+
seen.add(other);
|
|
155
|
+
todo.push(other);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return seen.size === squares.length;
|
|
160
|
+
}
|
|
161
|
+
function checkSquares(value, path, problems, count, span, wide) {
|
|
162
|
+
const before = problems.length;
|
|
163
|
+
const cells = list(value, path, problems, 1, count);
|
|
164
|
+
const seen = new Set();
|
|
165
|
+
cells.forEach((sq, i) => {
|
|
166
|
+
const at = `${path}[${i}]`;
|
|
167
|
+
if (!Array.isArray(sq) || sq.length < 2 || sq.length > 3) {
|
|
168
|
+
problems.push(`${at}: must be [row, column] or [row, column, half], counted from 0`);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const r = int(sq[0], `${at}[0]`, problems, 0, span - 1);
|
|
172
|
+
const c = int(sq[1], `${at}[1]`, problems, 0, span - 1);
|
|
173
|
+
if (sq.length === 3)
|
|
174
|
+
oneOf(sq[2], halves, `${at}[2]`, problems);
|
|
175
|
+
if (seen.has(`${r},${c}`))
|
|
176
|
+
problems.push(`${at}: the square ${r}, ${c} is named twice`);
|
|
177
|
+
seen.add(`${r},${c}`);
|
|
178
|
+
});
|
|
179
|
+
if (problems.length > before)
|
|
180
|
+
return;
|
|
181
|
+
const squares = squaresOf(cells);
|
|
182
|
+
const { rows, cols } = extent(squares);
|
|
183
|
+
if (rows > wide || cols > wide)
|
|
184
|
+
problems.push(`${path}: a shape spans at most ${wide} rows and ${wide} columns`);
|
|
185
|
+
if (!connected(squares))
|
|
186
|
+
problems.push(`${path}: the squares must join side to side into one piece`);
|
|
187
|
+
}
|
|
188
|
+
/** outlineOf chains the outer sides of a shape's squares into closed loops. */
|
|
189
|
+
function outlineOf(squares) {
|
|
190
|
+
const segments = [];
|
|
191
|
+
for (const sq of squares) {
|
|
192
|
+
const at = (p) => ({ x: sq.c + p.x, y: sq.r + p.y });
|
|
193
|
+
for (const side of forms[sq.form].sides)
|
|
194
|
+
if (!joined(squares, sq, side))
|
|
195
|
+
segments.push([at(unitSide[side][0]), at(unitSide[side][1])]);
|
|
196
|
+
const cut = forms[sq.form].cut;
|
|
197
|
+
if (cut)
|
|
198
|
+
segments.push([at(cut[0]), at(cut[1])]);
|
|
199
|
+
}
|
|
200
|
+
const key = (p) => `${p.x},${p.y}`;
|
|
201
|
+
const used = new Set();
|
|
202
|
+
const loops = [];
|
|
203
|
+
for (let first = 0; first < segments.length; first++) {
|
|
204
|
+
if (used.has(first))
|
|
205
|
+
continue;
|
|
206
|
+
used.add(first);
|
|
207
|
+
const loop = [segments[first][0], segments[first][1]];
|
|
208
|
+
for (;;) {
|
|
209
|
+
const end = key(loop[loop.length - 1]);
|
|
210
|
+
const next = segments.findIndex((seg, i) => !used.has(i) && (key(seg[0]) === end || key(seg[1]) === end));
|
|
211
|
+
if (next < 0)
|
|
212
|
+
break;
|
|
213
|
+
used.add(next);
|
|
214
|
+
const seg = segments[next];
|
|
215
|
+
loop.push(key(seg[0]) === end ? seg[1] : seg[0]);
|
|
216
|
+
}
|
|
217
|
+
loops.push(loop);
|
|
218
|
+
}
|
|
219
|
+
return loops;
|
|
220
|
+
}
|
|
221
|
+
/** drawShape draws a shape's squares at x, y: fills, inner lines, then the outline. */
|
|
222
|
+
function drawShape(pen, squares, x, y, fill, lines, what, dashed = false) {
|
|
223
|
+
for (const sq of squares) {
|
|
224
|
+
pen.el("path", {
|
|
225
|
+
class: sq.form === "full" ? what : `${what}-half`,
|
|
226
|
+
d: poly(forms[sq.form].points.map((p) => ({
|
|
227
|
+
x: x + (sq.c + p.x) * size,
|
|
228
|
+
y: y + (sq.r + p.y) * size,
|
|
229
|
+
}))),
|
|
230
|
+
fill,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
for (const sq of squares)
|
|
234
|
+
for (const side of ["right", "bottom"]) {
|
|
235
|
+
if (!joined(squares, sq, side))
|
|
236
|
+
continue;
|
|
237
|
+
const [a, b] = unitSide[side];
|
|
238
|
+
pen.el("path", {
|
|
239
|
+
d: poly([
|
|
240
|
+
{ x: x + (sq.c + a.x) * size, y: y + (sq.r + a.y) * size },
|
|
241
|
+
{ x: x + (sq.c + b.x) * size, y: y + (sq.r + b.y) * size },
|
|
242
|
+
], false),
|
|
243
|
+
stroke: lines,
|
|
244
|
+
"stroke-width": 1.8,
|
|
245
|
+
"stroke-dasharray": dashed ? "4 4" : undefined,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const loops = outlineOf(squares).map((loop) => {
|
|
249
|
+
const closed = loop.length > 2 &&
|
|
250
|
+
loop[0].x === loop[loop.length - 1].x &&
|
|
251
|
+
loop[0].y === loop[loop.length - 1].y;
|
|
252
|
+
const pts = (closed ? loop.slice(0, -1) : loop).map((p) => ({
|
|
253
|
+
x: x + p.x * size,
|
|
254
|
+
y: y + p.y * size,
|
|
255
|
+
}));
|
|
256
|
+
return poly(pts, closed);
|
|
257
|
+
});
|
|
258
|
+
pen.el("path", {
|
|
259
|
+
d: loops.join(""),
|
|
260
|
+
fill: "none",
|
|
261
|
+
stroke: ink,
|
|
262
|
+
"stroke-width": outline,
|
|
263
|
+
"stroke-linejoin": "round",
|
|
264
|
+
"stroke-linecap": "round",
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
/** sayShape says a shape's squares, row by row, so that it can be drawn again from the words. */
|
|
268
|
+
export function sayShape(squares) {
|
|
269
|
+
const { rows, cols } = extent(squares);
|
|
270
|
+
const whole = squares.filter((s) => s.form === "full").length;
|
|
271
|
+
const cut = squares.length - whole;
|
|
272
|
+
const amount = [
|
|
273
|
+
whole > 0 ? `${whole} square${whole === 1 ? "" : "s"}` : "",
|
|
274
|
+
cut > 0
|
|
275
|
+
? `${cut} half square${cut === 1 ? " (half of a square cut along a diagonal)" : "s (halves of squares cut along a diagonal)"}`
|
|
276
|
+
: "",
|
|
277
|
+
]
|
|
278
|
+
.filter((part) => part !== "")
|
|
279
|
+
.join(" and ");
|
|
280
|
+
const lone = squares[0];
|
|
281
|
+
if (squares.length === 1 && lone.form !== "full")
|
|
282
|
+
return `a triangle, the ${halfWords[lone.form]} half of a square cut along a diagonal`;
|
|
283
|
+
if (cut === 0 && whole === rows * cols) {
|
|
284
|
+
if (whole === 1)
|
|
285
|
+
return "1 square";
|
|
286
|
+
if (rows === 1)
|
|
287
|
+
return `${whole} squares in a row`;
|
|
288
|
+
if (cols === 1)
|
|
289
|
+
return `${whole} squares in a column`;
|
|
290
|
+
return `a ${rows} by ${cols} rectangle of ${whole} squares (${rows} rows of ${cols})`;
|
|
291
|
+
}
|
|
292
|
+
const byRow = [];
|
|
293
|
+
for (let r = 0; r < rows; r++) {
|
|
294
|
+
const parts = squares
|
|
295
|
+
.filter((s) => s.r === r)
|
|
296
|
+
.sort((a, b) => a.c - b.c)
|
|
297
|
+
.map((s) => s.form === "full"
|
|
298
|
+
? `column ${s.c + 1}`
|
|
299
|
+
: `the ${halfWords[s.form]} half of column ${s.c + 1}`);
|
|
300
|
+
byRow.push(`row ${r + 1}: ${parts.join(", ")}`);
|
|
301
|
+
}
|
|
302
|
+
return `${amount}, ${rows} row${rows === 1 ? "" : "s"} by ${cols} column${cols === 1 ? "" : "s"} from the top left (${byRow.join("; ")})`;
|
|
303
|
+
}
|
|
304
|
+
export const pieces = {
|
|
305
|
+
kind: "pieces",
|
|
306
|
+
members: ["kind", "pieces", "board"],
|
|
307
|
+
check(scene, problems) {
|
|
308
|
+
only(scene, this.members, "scene", problems);
|
|
309
|
+
list(scene.pieces, "pieces", problems, 1, most.pieces).forEach((piece, i) => {
|
|
310
|
+
const path = `pieces[${i}]`;
|
|
311
|
+
if (!isObj(piece)) {
|
|
312
|
+
problems.push(`${path}: must be a piece with cells and a color`);
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
only(piece, ["cells", "color", "label"], path, problems);
|
|
316
|
+
checkSquares(piece.cells, `${path}.cells`, problems, most.squares, 10, most.extent);
|
|
317
|
+
if (!isColor(piece.color))
|
|
318
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
319
|
+
if (piece.label !== undefined)
|
|
320
|
+
text(piece.label, `${path}.label`, problems, 4);
|
|
321
|
+
});
|
|
322
|
+
if (scene.board === undefined)
|
|
323
|
+
return;
|
|
324
|
+
const board = scene.board;
|
|
325
|
+
if (!isObj(board)) {
|
|
326
|
+
problems.push("board: must be rows and cols, or cells");
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
only(board, ["rows", "cols", "cells", "color", "label"], "board", problems);
|
|
330
|
+
const sized = board.rows !== undefined || board.cols !== undefined;
|
|
331
|
+
if (sized && board.cells !== undefined)
|
|
332
|
+
problems.push("board: has either rows and cols, or cells, not both");
|
|
333
|
+
else if (board.cells !== undefined)
|
|
334
|
+
checkSquares(board.cells, "board.cells", problems, most.boardSquares, most.boardExtent, most.boardExtent);
|
|
335
|
+
else if (!sized)
|
|
336
|
+
problems.push("board: must have rows and cols, or cells");
|
|
337
|
+
else {
|
|
338
|
+
int(board.rows, "board.rows", problems, 1, most.boardExtent);
|
|
339
|
+
int(board.cols, "board.cols", problems, 1, most.boardExtent);
|
|
340
|
+
}
|
|
341
|
+
if (board.color !== undefined && !isColor(board.color))
|
|
342
|
+
problems.push(`board.color: must be a colour: ${colorNames.join(", ")}`);
|
|
343
|
+
if (board.label !== undefined)
|
|
344
|
+
text(board.label, "board.label", problems, 12);
|
|
345
|
+
},
|
|
346
|
+
draw(scene, pen) {
|
|
347
|
+
const typed = scene;
|
|
348
|
+
const room = 640 - 2 * margin;
|
|
349
|
+
const gap = 30;
|
|
350
|
+
const shapes = typed.pieces.map((p) => {
|
|
351
|
+
const squares = squaresOf(p.cells);
|
|
352
|
+
const { rows, cols } = extent(squares);
|
|
353
|
+
return { piece: p, squares, w: cols * size, h: rows * size };
|
|
354
|
+
});
|
|
355
|
+
const rows = [];
|
|
356
|
+
let width = 0;
|
|
357
|
+
for (const shape of shapes) {
|
|
358
|
+
const row = rows[rows.length - 1];
|
|
359
|
+
const used = row ? row.reduce((sum, s) => sum + s.w + gap, -gap) : 0;
|
|
360
|
+
if (row && used + gap + shape.w <= room)
|
|
361
|
+
row.push(shape);
|
|
362
|
+
else
|
|
363
|
+
rows.push([shape]);
|
|
364
|
+
}
|
|
365
|
+
const rowWidth = (row) => row.reduce((sum, s) => sum + s.w + gap, -gap);
|
|
366
|
+
width = Math.max(...rows.map(rowWidth));
|
|
367
|
+
const board = typed.board ? boardSquares(typed.board) : undefined;
|
|
368
|
+
const boardSize = board ? extent(board) : undefined;
|
|
369
|
+
if (boardSize)
|
|
370
|
+
width = Math.max(width, boardSize.cols * size);
|
|
371
|
+
let y = margin + outline;
|
|
372
|
+
for (const row of rows) {
|
|
373
|
+
const h = Math.max(...row.map((s) => s.h));
|
|
374
|
+
const labelled = row.some((s) => s.piece.label);
|
|
375
|
+
let x = margin + (width - rowWidth(row)) / 2;
|
|
376
|
+
for (const s of row) {
|
|
377
|
+
const c = colors[s.piece.color];
|
|
378
|
+
drawShape(pen, s.squares, x, y + (h - s.h) / 2, c.fill, c.dark, "square");
|
|
379
|
+
if (s.piece.label)
|
|
380
|
+
pen.text(x + s.w / 2, y + h + 20, s.piece.label, { size: 18 });
|
|
381
|
+
x += s.w + gap;
|
|
382
|
+
}
|
|
383
|
+
y += h + (labelled ? 36 : 0) + gap;
|
|
384
|
+
}
|
|
385
|
+
if (board && boardSize && typed.board) {
|
|
386
|
+
y += 6;
|
|
387
|
+
const color = typed.board.color;
|
|
388
|
+
drawShape(pen, board, margin + (width - boardSize.cols * size) / 2, y, color ? colors[color].fill : ground, color ? colors[color].dark : faint, "board", !color);
|
|
389
|
+
y += boardSize.rows * size;
|
|
390
|
+
if (typed.board.label) {
|
|
391
|
+
pen.text(margin + width / 2, y + 20, typed.board.label, { size: 18 });
|
|
392
|
+
y += 36;
|
|
393
|
+
}
|
|
394
|
+
y += gap;
|
|
395
|
+
}
|
|
396
|
+
return { width: width + 2 * margin, height: y - gap + outline + margin };
|
|
397
|
+
},
|
|
398
|
+
describe(scene) {
|
|
399
|
+
const typed = scene;
|
|
400
|
+
const k = typed.pieces.length;
|
|
401
|
+
const out = [
|
|
402
|
+
`${k} piece${k === 1 ? "" : "s"} cut from squared paper, drawn apart in a row${k > 1 ? " from the left" : ""}.`,
|
|
403
|
+
];
|
|
404
|
+
typed.pieces.forEach((p, i) => {
|
|
405
|
+
out.push(`Piece ${p.label ?? i + 1}: ${colors[p.color].word}, ${sayShape(squaresOf(p.cells))}.`);
|
|
406
|
+
});
|
|
407
|
+
if (typed.board) {
|
|
408
|
+
const b = typed.board;
|
|
409
|
+
const squares = boardSquares(b);
|
|
410
|
+
const { rows, cols } = extent(squares);
|
|
411
|
+
const shape = !b.cells && rows === 1
|
|
412
|
+
? `a strip of ${cols} squares in a row`
|
|
413
|
+
: !b.cells && cols === 1
|
|
414
|
+
? `a strip of ${rows} squares in a column`
|
|
415
|
+
: sayShape(squares);
|
|
416
|
+
out.push(`Below them, a ${b.color ? `${colors[b.color].word} ` : ""}board to cover${b.label ? `, labelled "${b.label}"` : ""}: ${shape}.`);
|
|
417
|
+
}
|
|
418
|
+
return out.join(" ");
|
|
419
|
+
},
|
|
420
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Kind } from "../kind.ts";
|
|
2
|
+
import { type ColorName } from "../style.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Pipes: water or balls poured into funnels at the top run down through pipes
|
|
5
|
+
* that fork at junctions into buckets at the bottom. A funnel shows how much
|
|
6
|
+
* is poured in, as words or as the balls themselves; a junction splits what
|
|
7
|
+
* comes in among its pipes, evenly, by the marks written on them, or — for
|
|
8
|
+
* balls — taking turns, a switch pointing the way the next ball goes; a pipe
|
|
9
|
+
* may be closed. The program lays the junctions out in rows from the pipes
|
|
10
|
+
* alone: each stands above the middle of the places its pipes lead to.
|
|
11
|
+
*/
|
|
12
|
+
export declare const flows: readonly ["water", "balls"];
|
|
13
|
+
export type Flow = (typeof flows)[number];
|
|
14
|
+
export declare const rules: readonly ["even", "turns"];
|
|
15
|
+
export type Rule = (typeof rules)[number];
|
|
16
|
+
export type Way = string | {
|
|
17
|
+
to: string;
|
|
18
|
+
label?: string;
|
|
19
|
+
closed?: boolean;
|
|
20
|
+
};
|
|
21
|
+
export type Inlet = {
|
|
22
|
+
id: string;
|
|
23
|
+
to: string;
|
|
24
|
+
amount?: string;
|
|
25
|
+
balls?: number;
|
|
26
|
+
color?: ColorName;
|
|
27
|
+
};
|
|
28
|
+
export type Junction = {
|
|
29
|
+
id: string;
|
|
30
|
+
to: Way[];
|
|
31
|
+
rule?: Rule;
|
|
32
|
+
};
|
|
33
|
+
export type Bucket = {
|
|
34
|
+
id: string;
|
|
35
|
+
label?: string;
|
|
36
|
+
ask?: boolean;
|
|
37
|
+
color?: ColorName;
|
|
38
|
+
};
|
|
39
|
+
export type PipesScene = {
|
|
40
|
+
kind: "pipes";
|
|
41
|
+
flow?: Flow;
|
|
42
|
+
inlets: Inlet[];
|
|
43
|
+
junctions?: Junction[];
|
|
44
|
+
buckets: Bucket[];
|
|
45
|
+
};
|
|
46
|
+
export declare const pipes: Kind;
|