@cogniplay/logic-games 0.0.0-stage → 0.1.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.
- package/README.md +172 -2
- package/board.d.ts +102 -0
- package/board.d.ts.map +1 -0
- package/board.js +201 -0
- package/board.js.map +1 -0
- package/clues.d.ts +16 -0
- package/clues.d.ts.map +1 -0
- package/clues.js +64 -0
- package/clues.js.map +1 -0
- package/digits/generated/digits.d.ts +10 -0
- package/digits/generated/digits.d.ts.map +1 -0
- package/digits/generated/digits.js +397 -0
- package/digits/generated/digits.js.map +1 -0
- package/digits.d.ts +7 -0
- package/digits.d.ts.map +1 -0
- package/digits.js +38 -0
- package/digits.js.map +1 -0
- package/fair-shares.d.ts +20 -0
- package/fair-shares.d.ts.map +1 -0
- package/fair-shares.js +164 -0
- package/fair-shares.js.map +1 -0
- package/fills.d.ts +31 -0
- package/fills.d.ts.map +1 -0
- package/fills.js +142 -0
- package/fills.js.map +1 -0
- package/geometry.d.ts +14 -0
- package/geometry.d.ts.map +1 -0
- package/geometry.js +55 -0
- package/geometry.js.map +1 -0
- package/index.d.ts +12 -0
- package/index.d.ts.map +1 -0
- package/index.js +9 -0
- package/index.js.map +1 -0
- package/line-counts.d.ts +25 -0
- package/line-counts.d.ts.map +1 -0
- package/line-counts.js +136 -0
- package/line-counts.js.map +1 -0
- package/marks.d.ts +17 -0
- package/marks.d.ts.map +1 -0
- package/marks.js +50 -0
- package/marks.js.map +1 -0
- package/outline.d.ts +31 -0
- package/outline.d.ts.map +1 -0
- package/outline.js +150 -0
- package/outline.js.map +1 -0
- package/package.json +21 -4
- package/positions.d.ts +7 -0
- package/positions.d.ts.map +1 -0
- package/positions.js +28 -0
- package/positions.js.map +1 -0
- package/regions.d.ts +20 -0
- package/regions.d.ts.map +1 -0
- package/regions.js +84 -0
- package/regions.js.map +1 -0
- package/solution.d.ts +15 -0
- package/solution.d.ts.map +1 -0
- package/solution.js +50 -0
- package/solution.js.map +1 -0
- package/src/board.ts +377 -0
- package/src/clues.ts +91 -0
- package/src/digits/OFL.txt +93 -0
- package/src/digits/README.md +5 -0
- package/src/digits/generated/digits.ts +404 -0
- package/src/digits.ts +58 -0
- package/src/fair-shares.ts +257 -0
- package/src/fills.ts +176 -0
- package/src/geometry.ts +66 -0
- package/src/index.ts +31 -0
- package/src/line-counts.ts +211 -0
- package/src/marks.ts +68 -0
- package/src/outline.ts +196 -0
- package/src/positions.ts +35 -0
- package/src/regions.ts +108 -0
- package/src/solution.ts +75 -0
- package/src/svg.ts +46 -0
- package/src/tray.ts +114 -0
- package/src/types.ts +106 -0
- package/svg.d.ts +7 -0
- package/svg.d.ts.map +1 -0
- package/svg.js +29 -0
- package/svg.js.map +1 -0
- package/tray.d.ts +32 -0
- package/tray.d.ts.map +1 -0
- package/tray.js +58 -0
- package/tray.js.map +1 -0
- package/types.d.ts +95 -0
- package/types.d.ts.map +1 -0
- package/types.js +2 -0
- package/types.js.map +1 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type RenderOptions,
|
|
3
|
+
defaultCellSize,
|
|
4
|
+
drawCells,
|
|
5
|
+
drawTile,
|
|
6
|
+
lookOf,
|
|
7
|
+
padding,
|
|
8
|
+
requireSquare,
|
|
9
|
+
} from "./board.js";
|
|
10
|
+
import { clueBands, drawClues } from "./clues.js";
|
|
11
|
+
import { isFairShares } from "./fair-shares.js";
|
|
12
|
+
import { place } from "./geometry.js";
|
|
13
|
+
import { drawRegions } from "./regions.js";
|
|
14
|
+
import { element, fmt, root } from "./svg.js";
|
|
15
|
+
import { drawTrayPieces, layTray } from "./tray.js";
|
|
16
|
+
import type {
|
|
17
|
+
Cell,
|
|
18
|
+
GameData,
|
|
19
|
+
LineCountsInstance,
|
|
20
|
+
LoosePiece,
|
|
21
|
+
Piece,
|
|
22
|
+
Placement,
|
|
23
|
+
} from "./types.js";
|
|
24
|
+
|
|
25
|
+
export function isLineCounts(data: GameData): data is LineCountsInstance {
|
|
26
|
+
return data.$type === "line-counts";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether a piece starts on the board, or in Fair Shares on a share's grid. A
|
|
31
|
+
* piece to place has no origin, or a null one: a delivery file may carry
|
|
32
|
+
* either, and the API leaves it out.
|
|
33
|
+
*/
|
|
34
|
+
export function isPlaced<P extends Piece>(piece: P | LoosePiece): piece is P {
|
|
35
|
+
return piece.origin != null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* How many positions a board has, for `renderBoard`'s `position`. Amoeba has
|
|
40
|
+
* the start and one per move. Line Counts and Fair Shares have the start
|
|
41
|
+
* and, when the solution places anything, the solved board.
|
|
42
|
+
*/
|
|
43
|
+
export function positionCount(data: GameData): number {
|
|
44
|
+
const moves = (data.solution ?? []).length;
|
|
45
|
+
if (isLineCounts(data) || isFairShares(data)) return moves > 0 ? 2 : 1;
|
|
46
|
+
return moves + 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A Line Counts board as a complete SVG document: at position 0 the start,
|
|
51
|
+
* with the pieces to place in a tray under the board, or above it when
|
|
52
|
+
* `trayPosition` says so; at position 1 the
|
|
53
|
+
* solved board, where the pieces the solution placed have the heavy outline.
|
|
54
|
+
*/
|
|
55
|
+
export function renderLineCounts(
|
|
56
|
+
data: LineCountsInstance,
|
|
57
|
+
options: RenderOptions & { position?: number } = {}
|
|
58
|
+
): string {
|
|
59
|
+
requireSquare(data);
|
|
60
|
+
if (data.board.length === 0) {
|
|
61
|
+
throw new Error("The board has no cells");
|
|
62
|
+
}
|
|
63
|
+
const index = options.position ?? 0;
|
|
64
|
+
const count = positionCount(data);
|
|
65
|
+
if (!Number.isInteger(index) || index < 0 || index >= count) {
|
|
66
|
+
throw new RangeError(
|
|
67
|
+
`No position ${index}; there are ${count}, numbered 0 to ${count - 1}`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
const size = options.cellSizeMm ?? defaultCellSize;
|
|
71
|
+
const clues = data.clues ?? [];
|
|
72
|
+
|
|
73
|
+
// Solved, each placement of the solution puts its piece on the board.
|
|
74
|
+
const placements = new Map<string, Placement>();
|
|
75
|
+
if (index === 1) {
|
|
76
|
+
(data.solution ?? []).forEach((placement, i) => {
|
|
77
|
+
if (!data.pieces.some((p) => p.pieceId === placement.pieceId)) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
`Placement ${i + 1} names piece "${placement.pieceId}", which the board doesn't have`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
placements.set(placement.pieceId, placement);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
// A piece keeps the fill its place in `pieces` gives it, on the board, in
|
|
86
|
+
// the tray and in the solution.
|
|
87
|
+
const onBoard: {
|
|
88
|
+
cells: Cell[];
|
|
89
|
+
id: string;
|
|
90
|
+
order: number;
|
|
91
|
+
solved: boolean;
|
|
92
|
+
}[] = [];
|
|
93
|
+
const loose: (LoosePiece & { order: number })[] = [];
|
|
94
|
+
// The pieces to place at the start, whatever the position.
|
|
95
|
+
const looseAtStart: typeof loose = [];
|
|
96
|
+
data.pieces.forEach((piece, order) => {
|
|
97
|
+
if (!isPlaced(piece)) {
|
|
98
|
+
looseAtStart.push({ pieceId: piece.pieceId, shape: piece.shape, order });
|
|
99
|
+
}
|
|
100
|
+
const placement = isPlaced(piece) ? piece : placements.get(piece.pieceId);
|
|
101
|
+
if (placement === undefined) {
|
|
102
|
+
loose.push({ pieceId: piece.pieceId, shape: piece.shape, order });
|
|
103
|
+
} else {
|
|
104
|
+
onBoard.push({
|
|
105
|
+
cells: place(piece.shape, { ...placement, pieceId: piece.pieceId }),
|
|
106
|
+
id: piece.pieceId,
|
|
107
|
+
order,
|
|
108
|
+
solved: !isPlaced(piece),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// The bounds hold the board and anything placed off it, as Amoeba's do, and
|
|
114
|
+
// every line a clue names, so a clue is never drawn outside the drawing.
|
|
115
|
+
const boardXs = data.board.map(([x]) => x);
|
|
116
|
+
const boardYs = data.board.map(([, y]) => y);
|
|
117
|
+
const cells: Cell[] = [
|
|
118
|
+
...data.board,
|
|
119
|
+
...onBoard.flatMap((piece) => piece.cells),
|
|
120
|
+
...clues.map(
|
|
121
|
+
(clue): Cell =>
|
|
122
|
+
clue.direction === "row"
|
|
123
|
+
? [boardXs[0]!, clue.index]
|
|
124
|
+
: [clue.index, boardYs[0]!]
|
|
125
|
+
),
|
|
126
|
+
];
|
|
127
|
+
const xs = cells.map(([x]) => x);
|
|
128
|
+
const ys = cells.map(([, y]) => y);
|
|
129
|
+
const minX = Math.min(...xs);
|
|
130
|
+
const minY = Math.min(...ys);
|
|
131
|
+
const columns = Math.max(...xs) - minX + 1;
|
|
132
|
+
const rows = Math.max(...ys) - minY + 1;
|
|
133
|
+
|
|
134
|
+
const bands = clueBands(clues);
|
|
135
|
+
const boardWidth = (bands.left + columns) * size;
|
|
136
|
+
const boardHeight = (bands.top + rows) * size;
|
|
137
|
+
|
|
138
|
+
const trayCell = size * (options.trayScale ?? 1);
|
|
139
|
+
// With `keepTrayRoom` the tray is laid out as at the start, and the pieces
|
|
140
|
+
// since placed leave their room empty.
|
|
141
|
+
const room = layTray(
|
|
142
|
+
options.keepTrayRoom ? looseAtStart : loose,
|
|
143
|
+
trayCell,
|
|
144
|
+
options.trayWidthMm ?? boardWidth
|
|
145
|
+
);
|
|
146
|
+
const stillLoose = new Set(loose.map((piece) => piece.pieceId));
|
|
147
|
+
const tray = {
|
|
148
|
+
...room,
|
|
149
|
+
shown: room.pieces.filter(({ piece }) => stillLoose.has(piece.pieceId)),
|
|
150
|
+
};
|
|
151
|
+
const width = Math.max(boardWidth, tray.width);
|
|
152
|
+
const trayRoom = tray.pieces.length === 0 ? 0 : size / 2 + tray.height;
|
|
153
|
+
const above = options.trayPosition === "above";
|
|
154
|
+
const trayTop = padding + (above ? 0 : boardHeight + size / 2);
|
|
155
|
+
|
|
156
|
+
// The board and the tray are each centred in the drawing's width.
|
|
157
|
+
const left = padding + (width - boardWidth) / 2 + bands.left * size;
|
|
158
|
+
const top = padding + (above ? trayRoom : 0) + bands.top * size;
|
|
159
|
+
const toMm = ([x, y]: Cell): [number, number] => [
|
|
160
|
+
left + (x - minX) * size,
|
|
161
|
+
top + (y - minY) * size,
|
|
162
|
+
];
|
|
163
|
+
|
|
164
|
+
const pieces = element(
|
|
165
|
+
"g",
|
|
166
|
+
{ class: "pieces" },
|
|
167
|
+
onBoard
|
|
168
|
+
.map((piece) =>
|
|
169
|
+
drawTile(
|
|
170
|
+
piece.id,
|
|
171
|
+
piece.cells,
|
|
172
|
+
lookOf(piece.id, piece.order, options),
|
|
173
|
+
piece.solved ? "piece placed" : "piece",
|
|
174
|
+
piece.solved,
|
|
175
|
+
toMm,
|
|
176
|
+
size
|
|
177
|
+
)
|
|
178
|
+
)
|
|
179
|
+
.join("")
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
const trayGroup =
|
|
183
|
+
tray.shown.length === 0
|
|
184
|
+
? ""
|
|
185
|
+
: element(
|
|
186
|
+
"g",
|
|
187
|
+
{ class: "tray" },
|
|
188
|
+
drawTrayPieces(
|
|
189
|
+
tray.shown,
|
|
190
|
+
"piece to-place",
|
|
191
|
+
padding + (width - tray.width) / 2,
|
|
192
|
+
trayTop,
|
|
193
|
+
trayCell,
|
|
194
|
+
options
|
|
195
|
+
)
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
// The thick lines go over the pieces: a small board's border matters more
|
|
199
|
+
// than a tile's edge.
|
|
200
|
+
const body =
|
|
201
|
+
drawCells(data.board, toMm, size) +
|
|
202
|
+
pieces +
|
|
203
|
+
drawRegions(data.regions ?? [], data.board, toMm) +
|
|
204
|
+
drawClues(clues, minX, minY, toMm, size) +
|
|
205
|
+
trayGroup;
|
|
206
|
+
return root(
|
|
207
|
+
Number(fmt(padding * 2 + width)),
|
|
208
|
+
Number(fmt(padding * 2 + boardHeight + trayRoom)),
|
|
209
|
+
body
|
|
210
|
+
);
|
|
211
|
+
}
|
package/src/marks.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { element, fmt } from "./svg.js";
|
|
2
|
+
|
|
3
|
+
/** The cell a mark is drawn in: its top-left in millimetres and its side. */
|
|
4
|
+
export interface MarkCell {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
size: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Draws one mark inside its cell, as an SVG fragment with no ids and no text. */
|
|
11
|
+
export type MarkDrawer = (cell: MarkCell) => string;
|
|
12
|
+
|
|
13
|
+
function centre({ x, y, size }: MarkCell): [number, number] {
|
|
14
|
+
return [x + size / 2, y + size / 2];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Two concentric rings. */
|
|
18
|
+
export const target: MarkDrawer = (cell) => {
|
|
19
|
+
const [cx, cy] = centre(cell);
|
|
20
|
+
const ring = (r: number) =>
|
|
21
|
+
element("circle", {
|
|
22
|
+
cx,
|
|
23
|
+
cy,
|
|
24
|
+
r,
|
|
25
|
+
fill: "none",
|
|
26
|
+
stroke: "#000",
|
|
27
|
+
"stroke-width": cell.size * 0.06,
|
|
28
|
+
});
|
|
29
|
+
return ring(cell.size * 0.35) + ring(cell.size * 0.18);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** A five-point star, filled. */
|
|
33
|
+
export const star: MarkDrawer = (cell) => {
|
|
34
|
+
const [cx, cy] = centre(cell);
|
|
35
|
+
const outer = cell.size * 0.4;
|
|
36
|
+
const inner = outer * 0.4;
|
|
37
|
+
const points: string[] = [];
|
|
38
|
+
for (let i = 0; i < 10; i++) {
|
|
39
|
+
const r = i % 2 === 0 ? outer : inner;
|
|
40
|
+
const angle = -Math.PI / 2 + (i * Math.PI) / 5;
|
|
41
|
+
points.push(
|
|
42
|
+
`${fmt(cx + r * Math.cos(angle))},${fmt(cy + r * Math.sin(angle))}`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return element("polygon", { points: points.join(" "), fill: "#000" });
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** A dashed square, for a kind nobody draws. */
|
|
49
|
+
export const placeholder: MarkDrawer = ({ x, y, size }) => {
|
|
50
|
+
const inset = size * 0.15;
|
|
51
|
+
return element("rect", {
|
|
52
|
+
x: x + inset,
|
|
53
|
+
y: y + inset,
|
|
54
|
+
width: size - 2 * inset,
|
|
55
|
+
height: size - 2 * inset,
|
|
56
|
+
fill: "none",
|
|
57
|
+
stroke: "#000",
|
|
58
|
+
"stroke-width": size * 0.04,
|
|
59
|
+
"stroke-dasharray": `${fmt(size * 0.1)} ${fmt(size * 0.06)}`,
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const standardMarks: Readonly<Record<string, MarkDrawer>> = {
|
|
64
|
+
target,
|
|
65
|
+
star,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const standardMarkKinds: readonly string[] = Object.keys(standardMarks);
|
package/src/outline.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { cellKey } from "./geometry.js";
|
|
2
|
+
import { fmt } from "./svg.js";
|
|
3
|
+
import type { Cell } from "./types.js";
|
|
4
|
+
|
|
5
|
+
export type Point = [x: number, y: number];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The four sides of a cell as the edge's two corners, offsets from the cell's
|
|
9
|
+
* top-left in cells, directed so that the cell is on the right of travel
|
|
10
|
+
* (clockwise, with y growing down).
|
|
11
|
+
*/
|
|
12
|
+
const sides: [dx: number, dy: number, from: Cell, to: Cell][] = [
|
|
13
|
+
[0, -1, [0, 0], [1, 0]], // top, left to right
|
|
14
|
+
[1, 0, [1, 0], [1, 1]], // right, downwards
|
|
15
|
+
[0, 1, [1, 1], [0, 1]], // bottom, right to left
|
|
16
|
+
[-1, 0, [0, 1], [0, 0]], // left, upwards
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const pointKey = ([x, y]: Point) => `${x},${y}`;
|
|
20
|
+
|
|
21
|
+
const direction = (from: Point, to: Point): Point => [
|
|
22
|
+
Math.sign(to[0] - from[0]),
|
|
23
|
+
Math.sign(to[1] - from[1]),
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
/** Positive for a right turn from `d1` to `d2`, with y growing down. */
|
|
27
|
+
const cross = (d1: Point, d2: Point) => d1[0] * d2[1] - d1[1] * d2[0];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The outline of a set of cells as closed loops of corners, in cell units:
|
|
31
|
+
* every cell edge not shared with another cell in the set, chained so that the
|
|
32
|
+
* cells are on the right. A hole is a loop of its own, and so is each part of
|
|
33
|
+
* a set that isn't connected by edges. Corners along a straight run are
|
|
34
|
+
* dropped.
|
|
35
|
+
*/
|
|
36
|
+
export function outlineLoops(cells: readonly Cell[]): Point[][] {
|
|
37
|
+
const set = new Set(cells.map(cellKey));
|
|
38
|
+
// Boundary edges, by the corner they start at.
|
|
39
|
+
const edges = new Map<string, { from: Point; to: Point }[]>();
|
|
40
|
+
for (const [x, y] of cells) {
|
|
41
|
+
for (const [dx, dy, from, to] of sides) {
|
|
42
|
+
if (set.has(cellKey([x + dx, y + dy]))) continue;
|
|
43
|
+
const edge = {
|
|
44
|
+
from: [x + from[0], y + from[1]] as Point,
|
|
45
|
+
to: [x + to[0], y + to[1]] as Point,
|
|
46
|
+
};
|
|
47
|
+
const key = pointKey(edge.from);
|
|
48
|
+
edges.set(key, [...(edges.get(key) ?? []), edge]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const take = (at: Point, arriving: Point | undefined) => {
|
|
53
|
+
const key = pointKey(at);
|
|
54
|
+
const candidates = edges.get(key) ?? [];
|
|
55
|
+
if (candidates.length === 0) return undefined;
|
|
56
|
+
// Where two cells touch only at this corner, both continuations are
|
|
57
|
+
// present; turning right keeps the loop around the cell just left.
|
|
58
|
+
const edge =
|
|
59
|
+
arriving === undefined
|
|
60
|
+
? candidates[0]!
|
|
61
|
+
: (candidates.find(
|
|
62
|
+
(c) => cross(arriving, direction(c.from, c.to)) > 0
|
|
63
|
+
) ?? candidates[0]!);
|
|
64
|
+
const rest = candidates.filter((c) => c !== edge);
|
|
65
|
+
if (rest.length === 0) edges.delete(key);
|
|
66
|
+
else edges.set(key, rest);
|
|
67
|
+
return edge;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const loops: Point[][] = [];
|
|
71
|
+
while (edges.size > 0) {
|
|
72
|
+
const start = edges.values().next().value![0]!.from;
|
|
73
|
+
const loop: Point[] = [];
|
|
74
|
+
let at = start;
|
|
75
|
+
let arriving: Point | undefined;
|
|
76
|
+
for (;;) {
|
|
77
|
+
const edge = take(at, arriving);
|
|
78
|
+
if (edge === undefined) break;
|
|
79
|
+
loop.push(edge.from);
|
|
80
|
+
arriving = direction(edge.from, edge.to);
|
|
81
|
+
at = edge.to;
|
|
82
|
+
if (pointKey(at) === pointKey(start)) break;
|
|
83
|
+
}
|
|
84
|
+
loops.push(dropStraightRuns(loop));
|
|
85
|
+
}
|
|
86
|
+
return loops;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function dropStraightRuns(loop: Point[]): Point[] {
|
|
90
|
+
return loop.filter((point, i) => {
|
|
91
|
+
const before = loop[(i - 1 + loop.length) % loop.length]!;
|
|
92
|
+
const after = loop[(i + 1) % loop.length]!;
|
|
93
|
+
const d1 = direction(before, point);
|
|
94
|
+
const d2 = direction(point, after);
|
|
95
|
+
return d1[0] !== d2[0] || d1[1] !== d2[1];
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The outline loops in millimetres, each corner moved `inset` into the shape.
|
|
101
|
+
* `toMm` gives a cell's top-left, which is also where the corner of that
|
|
102
|
+
* number sits.
|
|
103
|
+
*/
|
|
104
|
+
export function insetLoops(
|
|
105
|
+
cells: readonly Cell[],
|
|
106
|
+
toMm: (cell: Cell) => [x: number, y: number],
|
|
107
|
+
size: number,
|
|
108
|
+
inset: number
|
|
109
|
+
): Point[][] {
|
|
110
|
+
return outlineLoops(cells).map((loop) =>
|
|
111
|
+
loop.map((corner, i) => {
|
|
112
|
+
const [x, y] = toMm(corner);
|
|
113
|
+
const before = loop[(i - 1 + loop.length) % loop.length]!;
|
|
114
|
+
const after = loop[(i + 1) % loop.length]!;
|
|
115
|
+
const d1 = direction(before, corner);
|
|
116
|
+
const d2 = direction(corner, after);
|
|
117
|
+
// The right-hand normals point into the shape; at a right-angled corner
|
|
118
|
+
// their sum is the offset of the inset corner.
|
|
119
|
+
const nx = -d1[1] - d2[1];
|
|
120
|
+
const ny = d1[0] + d2[0];
|
|
121
|
+
return [round(x + inset * nx), round(y + inset * ny)];
|
|
122
|
+
})
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Keeps the arithmetic above from leaving 0.6400000000000001 behind. */
|
|
127
|
+
const round = (n: number) => Number(n.toFixed(6));
|
|
128
|
+
|
|
129
|
+
export interface ShapeStyle {
|
|
130
|
+
/** Millimetres each edge moves into the shape. Default 0. */
|
|
131
|
+
inset?: number;
|
|
132
|
+
/** Millimetres of each corner replaced by a curve. Default 0, square corners. */
|
|
133
|
+
radius?: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A path `d` for the loops: one closed subpath each, each corner rounded by
|
|
138
|
+
* a quadratic curve of the given radius, clamped to half the shorter edge at
|
|
139
|
+
* the corner.
|
|
140
|
+
*/
|
|
141
|
+
export function loopsPath(loops: Point[][], radius = 0): string {
|
|
142
|
+
return loops
|
|
143
|
+
.map((loop) => {
|
|
144
|
+
if (radius <= 0) {
|
|
145
|
+
return (
|
|
146
|
+
loop
|
|
147
|
+
.map(([x, y], i) => `${i === 0 ? "M" : "L"}${fmt(x)} ${fmt(y)}`)
|
|
148
|
+
.join("") + "z"
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
const arms = loop.map((corner, i) => {
|
|
152
|
+
const before = loop[(i - 1 + loop.length) % loop.length]!;
|
|
153
|
+
const after = loop[(i + 1) % loop.length]!;
|
|
154
|
+
const r = Math.min(
|
|
155
|
+
radius,
|
|
156
|
+
distance(before, corner) / 2,
|
|
157
|
+
distance(corner, after) / 2
|
|
158
|
+
);
|
|
159
|
+
const d1 = direction(before, corner);
|
|
160
|
+
const d2 = direction(corner, after);
|
|
161
|
+
return {
|
|
162
|
+
corner,
|
|
163
|
+
in: [corner[0] - d1[0] * r, corner[1] - d1[1] * r] as Point,
|
|
164
|
+
out: [corner[0] + d2[0] * r, corner[1] + d2[1] * r] as Point,
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
const first = arms[0]!;
|
|
168
|
+
return (
|
|
169
|
+
`M${fmt(first.out[0])} ${fmt(first.out[1])}` +
|
|
170
|
+
[...arms.slice(1), first]
|
|
171
|
+
.map(
|
|
172
|
+
(arm) =>
|
|
173
|
+
`L${fmt(arm.in[0])} ${fmt(arm.in[1])}Q${fmt(arm.corner[0])} ${fmt(arm.corner[1])} ${fmt(arm.out[0])} ${fmt(arm.out[1])}`
|
|
174
|
+
)
|
|
175
|
+
.join("") +
|
|
176
|
+
"z"
|
|
177
|
+
);
|
|
178
|
+
})
|
|
179
|
+
.join("");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const distance = (a: Point, b: Point) =>
|
|
183
|
+
Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
|
|
184
|
+
|
|
185
|
+
/** The outline of the cells as a path `d` in millimetres, inset and rounded as styled. */
|
|
186
|
+
export function shapePath(
|
|
187
|
+
cells: readonly Cell[],
|
|
188
|
+
toMm: (cell: Cell) => [x: number, y: number],
|
|
189
|
+
size: number,
|
|
190
|
+
style: ShapeStyle
|
|
191
|
+
): string {
|
|
192
|
+
return loopsPath(
|
|
193
|
+
insetLoops(cells, toMm, size, style.inset ?? 0),
|
|
194
|
+
style.radius ?? 0
|
|
195
|
+
);
|
|
196
|
+
}
|
package/src/positions.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { orientations } from "./geometry.js";
|
|
2
|
+
import type { Instance, Position } from "./types.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The positions a solution passes through: the start, then one per move, each
|
|
6
|
+
* with that move's piece in its new placement and recorded as `moved`.
|
|
7
|
+
*/
|
|
8
|
+
export function positions(instance: Instance): Position[] {
|
|
9
|
+
let pieces = [...instance.pieces];
|
|
10
|
+
const out: Position[] = [{ pieces }];
|
|
11
|
+
(instance.solution ?? []).forEach((move, index) => {
|
|
12
|
+
const piece = pieces.find((p) => p.pieceId === move.pieceId);
|
|
13
|
+
if (piece === undefined) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
`Move ${index + 1} names piece "${move.pieceId}", which the instance doesn't have`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
// Only the square convention is implemented, so only square can be checked.
|
|
19
|
+
if (instance.geometry === "square") {
|
|
20
|
+
const count = orientations(piece.shape).length;
|
|
21
|
+
if (move.orientation < 0 || move.orientation >= count) {
|
|
22
|
+
throw new RangeError(
|
|
23
|
+
`Move ${index + 1}: piece "${move.pieceId}" has no orientation ${move.orientation}; it has ${count}`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
pieces = pieces.map((p) =>
|
|
28
|
+
p === piece
|
|
29
|
+
? { ...p, origin: move.origin, orientation: move.orientation }
|
|
30
|
+
: p
|
|
31
|
+
);
|
|
32
|
+
out.push({ pieces, moved: move.pieceId });
|
|
33
|
+
});
|
|
34
|
+
return out;
|
|
35
|
+
}
|
package/src/regions.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { cellKey } from "./geometry.js";
|
|
2
|
+
import { element } from "./svg.js";
|
|
3
|
+
import type { Cell } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/** A straight line between two cell corners, in cells. */
|
|
6
|
+
export interface RegionLine {
|
|
7
|
+
from: Cell;
|
|
8
|
+
to: Cell;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The lines where two regions meet: every edge shared by two cells of
|
|
13
|
+
* different regions, with the edges of one straight run joined. Upright lines
|
|
14
|
+
* come first, left to right, then level ones, top to bottom.
|
|
15
|
+
*/
|
|
16
|
+
export function regionLines(
|
|
17
|
+
regions: readonly (readonly Cell[])[]
|
|
18
|
+
): RegionLine[] {
|
|
19
|
+
const regionOf = new Map<string, number>();
|
|
20
|
+
regions.forEach((cells, region) =>
|
|
21
|
+
cells.forEach((cell) => regionOf.set(cellKey(cell), region))
|
|
22
|
+
);
|
|
23
|
+
// Unit edges by the line they lie on: an upright edge at x from y to y + 1
|
|
24
|
+
// is kept as y under x, and a level one as x under y.
|
|
25
|
+
const upright = new Map<number, number[]>();
|
|
26
|
+
const level = new Map<number, number[]>();
|
|
27
|
+
const add = (edges: Map<number, number[]>, line: number, at: number) =>
|
|
28
|
+
edges.set(line, [...(edges.get(line) ?? []), at]);
|
|
29
|
+
regions.forEach((cells, region) => {
|
|
30
|
+
for (const [x, y] of cells) {
|
|
31
|
+
const right = regionOf.get(cellKey([x + 1, y]));
|
|
32
|
+
if (right !== undefined && right !== region) add(upright, x + 1, y);
|
|
33
|
+
const below = regionOf.get(cellKey([x, y + 1]));
|
|
34
|
+
if (below !== undefined && below !== region) add(level, y + 1, x);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const runs = (
|
|
38
|
+
edges: Map<number, number[]>,
|
|
39
|
+
line: (at: number, from: number, to: number) => RegionLine
|
|
40
|
+
) =>
|
|
41
|
+
[...edges.entries()]
|
|
42
|
+
.sort(([a], [b]) => a - b)
|
|
43
|
+
.flatMap(([at, starts]) => {
|
|
44
|
+
const sorted = [...new Set(starts)].sort((a, b) => a - b);
|
|
45
|
+
const out: RegionLine[] = [];
|
|
46
|
+
let from = sorted[0]!;
|
|
47
|
+
sorted.forEach((start, i) => {
|
|
48
|
+
if (sorted[i + 1] !== start + 1) {
|
|
49
|
+
out.push(line(at, from, start + 1));
|
|
50
|
+
from = sorted[i + 1]!;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return out;
|
|
54
|
+
});
|
|
55
|
+
return [
|
|
56
|
+
...runs(upright, (x, from, to) => ({ from: [x, from], to: [x, to] })),
|
|
57
|
+
...runs(level, (y, from, to) => ({ from: [from, y], to: [to, y] })),
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const lineWidth = 1.6;
|
|
62
|
+
const lineStroke = { stroke: "#000", "stroke-width": lineWidth };
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The thick lines between regions, as a group; empty when there are none. A
|
|
66
|
+
* line's end inside the board runs half its width on, so lines that meet at
|
|
67
|
+
* a corner close it; an end on the board's edge stops there, flush with the
|
|
68
|
+
* board's outline.
|
|
69
|
+
*/
|
|
70
|
+
export function drawRegions(
|
|
71
|
+
regions: readonly (readonly Cell[])[],
|
|
72
|
+
board: readonly Cell[],
|
|
73
|
+
toMm: (cell: Cell) => [number, number]
|
|
74
|
+
): string {
|
|
75
|
+
const lines = regionLines(regions);
|
|
76
|
+
if (lines.length === 0) return "";
|
|
77
|
+
const cells = new Set(board.map(cellKey));
|
|
78
|
+
// A corner is inside the board when all four cells around it are board cells.
|
|
79
|
+
const inside = ([x, y]: Cell) =>
|
|
80
|
+
[
|
|
81
|
+
[x - 1, y - 1],
|
|
82
|
+
[x, y - 1],
|
|
83
|
+
[x - 1, y],
|
|
84
|
+
[x, y],
|
|
85
|
+
].every((cell) => cells.has(cellKey(cell as Cell)));
|
|
86
|
+
return element(
|
|
87
|
+
"g",
|
|
88
|
+
{ class: "regions" },
|
|
89
|
+
lines
|
|
90
|
+
.map(({ from, to }) => {
|
|
91
|
+
const dx = Math.sign(to[0] - from[0]);
|
|
92
|
+
const dy = Math.sign(to[1] - from[1]);
|
|
93
|
+
const before = inside(from) ? lineWidth / 2 : 0;
|
|
94
|
+
const after = inside(to) ? lineWidth / 2 : 0;
|
|
95
|
+
const [x1, y1] = toMm(from);
|
|
96
|
+
const [x2, y2] = toMm(to);
|
|
97
|
+
return element("line", {
|
|
98
|
+
class: "region-line",
|
|
99
|
+
x1: x1 - dx * before,
|
|
100
|
+
y1: y1 - dy * before,
|
|
101
|
+
x2: x2 + dx * after,
|
|
102
|
+
y2: y2 + dy * after,
|
|
103
|
+
...lineStroke,
|
|
104
|
+
});
|
|
105
|
+
})
|
|
106
|
+
.join("")
|
|
107
|
+
);
|
|
108
|
+
}
|
package/src/solution.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type RenderOptions,
|
|
3
|
+
bounds,
|
|
4
|
+
drawPosition,
|
|
5
|
+
requireSquare,
|
|
6
|
+
} from "./board.js";
|
|
7
|
+
import { isFairShares, renderFairShares } from "./fair-shares.js";
|
|
8
|
+
import {
|
|
9
|
+
isLineCounts,
|
|
10
|
+
positionCount,
|
|
11
|
+
renderLineCounts,
|
|
12
|
+
} from "./line-counts.js";
|
|
13
|
+
import { positions } from "./positions.js";
|
|
14
|
+
import { element, fmt, root } from "./svg.js";
|
|
15
|
+
import type { GameData } from "./types.js";
|
|
16
|
+
|
|
17
|
+
/** Millimetres between positions in a solution drawing. */
|
|
18
|
+
export const gap = 8;
|
|
19
|
+
|
|
20
|
+
const defaultPerRow = 4;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Amoeba's solution: every position in order, left to right, wrapping after
|
|
24
|
+
* `perRow` (default 4), as one SVG document. A Line Counts or a Fair Shares
|
|
25
|
+
* board's solution is the solved board alone. The order is the numbering; the moved piece and the
|
|
26
|
+
* hint of where it came from show each move. Every position is drawn within
|
|
27
|
+
* the same bounds, so the boards line up.
|
|
28
|
+
*/
|
|
29
|
+
export function renderSolution(
|
|
30
|
+
instance: GameData,
|
|
31
|
+
options: RenderOptions & { perRow?: number } = {}
|
|
32
|
+
): string {
|
|
33
|
+
if (isLineCounts(instance)) {
|
|
34
|
+
// The order of a Line Counts solution carries no meaning, so there is no
|
|
35
|
+
// row of positions: the solution is the board with every piece placed.
|
|
36
|
+
return renderLineCounts(instance, {
|
|
37
|
+
...options,
|
|
38
|
+
position: positionCount(instance) - 1,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (isFairShares(instance)) {
|
|
42
|
+
// Likewise: the answer is the split, and one drawing shows it.
|
|
43
|
+
return renderFairShares(instance, {
|
|
44
|
+
...options,
|
|
45
|
+
position: positionCount(instance) - 1,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
requireSquare(instance);
|
|
49
|
+
const all = positions(instance);
|
|
50
|
+
const perRow = Math.max(1, Math.floor(options.perRow ?? defaultPerRow));
|
|
51
|
+
const within = bounds(instance, all);
|
|
52
|
+
const drawings = all.map((position, i) =>
|
|
53
|
+
drawPosition(instance, position, all[i - 1], options, within)
|
|
54
|
+
);
|
|
55
|
+
const width = Math.max(...drawings.map((d) => d.width));
|
|
56
|
+
const height = Math.max(...drawings.map((d) => d.height));
|
|
57
|
+
const columns = Math.min(perRow, drawings.length);
|
|
58
|
+
const rows = Math.ceil(drawings.length / perRow);
|
|
59
|
+
const body = drawings
|
|
60
|
+
.map((drawing, i) => {
|
|
61
|
+
const x = (i % perRow) * (width + gap);
|
|
62
|
+
const y = Math.floor(i / perRow) * (height + gap);
|
|
63
|
+
return element(
|
|
64
|
+
"g",
|
|
65
|
+
{ class: "position", transform: `translate(${fmt(x)} ${fmt(y)})` },
|
|
66
|
+
drawing.body
|
|
67
|
+
);
|
|
68
|
+
})
|
|
69
|
+
.join("");
|
|
70
|
+
return root(
|
|
71
|
+
columns * width + (columns - 1) * gap,
|
|
72
|
+
rows * height + (rows - 1) * gap,
|
|
73
|
+
body
|
|
74
|
+
);
|
|
75
|
+
}
|