@cogniplay/logic-games 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 (89) hide show
  1. package/README.md +172 -2
  2. package/board.d.ts +102 -0
  3. package/board.d.ts.map +1 -0
  4. package/board.js +201 -0
  5. package/board.js.map +1 -0
  6. package/clues.d.ts +16 -0
  7. package/clues.d.ts.map +1 -0
  8. package/clues.js +64 -0
  9. package/clues.js.map +1 -0
  10. package/digits/generated/digits.d.ts +10 -0
  11. package/digits/generated/digits.d.ts.map +1 -0
  12. package/digits/generated/digits.js +397 -0
  13. package/digits/generated/digits.js.map +1 -0
  14. package/digits.d.ts +7 -0
  15. package/digits.d.ts.map +1 -0
  16. package/digits.js +38 -0
  17. package/digits.js.map +1 -0
  18. package/fair-shares.d.ts +20 -0
  19. package/fair-shares.d.ts.map +1 -0
  20. package/fair-shares.js +164 -0
  21. package/fair-shares.js.map +1 -0
  22. package/fills.d.ts +31 -0
  23. package/fills.d.ts.map +1 -0
  24. package/fills.js +142 -0
  25. package/fills.js.map +1 -0
  26. package/geometry.d.ts +14 -0
  27. package/geometry.d.ts.map +1 -0
  28. package/geometry.js +55 -0
  29. package/geometry.js.map +1 -0
  30. package/index.d.ts +12 -0
  31. package/index.d.ts.map +1 -0
  32. package/index.js +9 -0
  33. package/index.js.map +1 -0
  34. package/line-counts.d.ts +25 -0
  35. package/line-counts.d.ts.map +1 -0
  36. package/line-counts.js +136 -0
  37. package/line-counts.js.map +1 -0
  38. package/marks.d.ts +17 -0
  39. package/marks.d.ts.map +1 -0
  40. package/marks.js +50 -0
  41. package/marks.js.map +1 -0
  42. package/outline.d.ts +31 -0
  43. package/outline.d.ts.map +1 -0
  44. package/outline.js +150 -0
  45. package/outline.js.map +1 -0
  46. package/package.json +21 -4
  47. package/positions.d.ts +7 -0
  48. package/positions.d.ts.map +1 -0
  49. package/positions.js +28 -0
  50. package/positions.js.map +1 -0
  51. package/regions.d.ts +20 -0
  52. package/regions.d.ts.map +1 -0
  53. package/regions.js +84 -0
  54. package/regions.js.map +1 -0
  55. package/solution.d.ts +15 -0
  56. package/solution.d.ts.map +1 -0
  57. package/solution.js +50 -0
  58. package/solution.js.map +1 -0
  59. package/src/board.ts +377 -0
  60. package/src/clues.ts +91 -0
  61. package/src/digits/OFL.txt +93 -0
  62. package/src/digits/README.md +5 -0
  63. package/src/digits/generated/digits.ts +404 -0
  64. package/src/digits.ts +58 -0
  65. package/src/fair-shares.ts +257 -0
  66. package/src/fills.ts +176 -0
  67. package/src/geometry.ts +66 -0
  68. package/src/index.ts +31 -0
  69. package/src/line-counts.ts +211 -0
  70. package/src/marks.ts +68 -0
  71. package/src/outline.ts +196 -0
  72. package/src/positions.ts +35 -0
  73. package/src/regions.ts +108 -0
  74. package/src/solution.ts +75 -0
  75. package/src/svg.ts +46 -0
  76. package/src/tray.ts +114 -0
  77. package/src/types.ts +106 -0
  78. package/svg.d.ts +7 -0
  79. package/svg.d.ts.map +1 -0
  80. package/svg.js +29 -0
  81. package/svg.js.map +1 -0
  82. package/tray.d.ts +32 -0
  83. package/tray.d.ts.map +1 -0
  84. package/tray.js +58 -0
  85. package/tray.js.map +1 -0
  86. package/types.d.ts +95 -0
  87. package/types.d.ts.map +1 -0
  88. package/types.js +2 -0
  89. package/types.js.map +1 -0
package/src/svg.ts ADDED
@@ -0,0 +1,46 @@
1
+ /** A number for an attribute: at most 3 decimals, no trailing zeros. */
2
+ export function fmt(n: number): string {
3
+ return String(Number(n.toFixed(3)));
4
+ }
5
+
6
+ function escapeAttribute(value: string): string {
7
+ return value
8
+ .replaceAll("&", "&")
9
+ .replaceAll('"', """)
10
+ .replaceAll("<", "&lt;");
11
+ }
12
+
13
+ /** `<name a="1" b="x">children</name>`, or self-closing with no children. */
14
+ export function element(
15
+ name: string,
16
+ attributes: Record<string, string | number>,
17
+ children?: string
18
+ ): string {
19
+ const attrs = Object.entries(attributes)
20
+ .map(
21
+ ([key, value]) =>
22
+ ` ${key}="${typeof value === "number" ? fmt(value) : escapeAttribute(value)}"`
23
+ )
24
+ .join("");
25
+ return children === undefined
26
+ ? `<${name}${attrs}/>`
27
+ : `<${name}${attrs}>${children}</${name}>`;
28
+ }
29
+
30
+ /** The root: xmlns, width and height in millimetres, and a viewBox in the same units. */
31
+ export function root(
32
+ widthMm: number,
33
+ heightMm: number,
34
+ children: string
35
+ ): string {
36
+ return element(
37
+ "svg",
38
+ {
39
+ xmlns: "http://www.w3.org/2000/svg",
40
+ width: `${fmt(widthMm)}mm`,
41
+ height: `${fmt(heightMm)}mm`,
42
+ viewBox: `0 0 ${fmt(widthMm)} ${fmt(heightMm)}`,
43
+ },
44
+ children
45
+ );
46
+ }
package/src/tray.ts ADDED
@@ -0,0 +1,114 @@
1
+ import { type RenderOptions, drawTile, lookOf } from "./board.js";
2
+ import { orientations } from "./geometry.js";
3
+ import type { Cell, LoosePiece } from "./types.js";
4
+
5
+ /** What the tray needs of a piece: its id and its shape. */
6
+ export type Shaped = Pick<LoosePiece, "pieceId" | "shape">;
7
+
8
+ /** A piece in the tray: the cells it is drawn as, and its top-left in millimetres from the tray's. */
9
+ export interface TrayPiece<P extends Shaped = Shaped> {
10
+ piece: P;
11
+ cells: Cell[];
12
+ x: number;
13
+ y: number;
14
+ }
15
+
16
+ export interface Tray<P extends Shaped = Shaped> {
17
+ pieces: TrayPiece<P>[];
18
+ width: number;
19
+ height: number;
20
+ }
21
+
22
+ const extent = (cells: readonly Cell[], axis: 0 | 1) =>
23
+ Math.max(...cells.map((cell) => cell[axis])) + 1;
24
+
25
+ /** The piece lying down: the least tall of its orientations, the first of those in the convention's order. */
26
+ function lying(shape: readonly Cell[]): Cell[] {
27
+ return orientations(shape).reduce((best, next) =>
28
+ extent(next, 1) < extent(best, 1) ? next : best
29
+ );
30
+ }
31
+
32
+ /**
33
+ * The pieces to place, laid out in their order: left to right, half a cell
34
+ * apart, starting a new row when the next piece would pass `widthMm`. Each
35
+ * row is centred, and each piece is centred in its row's height. A piece
36
+ * wider than `widthMm` gets a row of its own, and the tray is then that wide.
37
+ */
38
+ export function layTray<P extends Shaped>(
39
+ pieces: readonly P[],
40
+ cellMm: number,
41
+ widthMm: number
42
+ ): Tray<P> {
43
+ const gap = cellMm / 2;
44
+ interface Row {
45
+ items: {
46
+ piece: P;
47
+ cells: Cell[];
48
+ x: number;
49
+ width: number;
50
+ height: number;
51
+ }[];
52
+ width: number;
53
+ height: number;
54
+ }
55
+ const rows: Row[] = [];
56
+ for (const piece of pieces) {
57
+ const cells = lying(piece.shape);
58
+ const width = extent(cells, 0) * cellMm;
59
+ const height = extent(cells, 1) * cellMm;
60
+ let row = rows.at(-1);
61
+ // A hair of slack, so a row that fits exactly isn't broken by rounding.
62
+ if (row === undefined || row.width + gap + width > widthMm + 1e-6) {
63
+ row = { items: [], width: 0, height: 0 };
64
+ rows.push(row);
65
+ }
66
+ const x = row.items.length === 0 ? 0 : row.width + gap;
67
+ row.items.push({ piece, cells, x, width, height });
68
+ row.width = x + width;
69
+ row.height = Math.max(row.height, height);
70
+ }
71
+ const width = Math.max(0, ...rows.map((row) => row.width));
72
+ const out: TrayPiece<P>[] = [];
73
+ let top = 0;
74
+ for (const row of rows) {
75
+ for (const item of row.items) {
76
+ out.push({
77
+ piece: item.piece,
78
+ cells: item.cells,
79
+ x: (width - row.width) / 2 + item.x,
80
+ y: top + (row.height - item.height) / 2,
81
+ });
82
+ }
83
+ top += row.height + gap;
84
+ }
85
+ return { pieces: out, width, height: Math.max(0, top - gap) };
86
+ }
87
+
88
+ /**
89
+ * The tray's pieces as tiles, each with the fill its order in the board's
90
+ * `pieces` gives it. `left` and `top` are the tray's corner in millimetres.
91
+ * Always tiles, whatever the piece style: nothing covers a piece in the tray.
92
+ */
93
+ export function drawTrayPieces<P extends Shaped & { order: number }>(
94
+ pieces: readonly TrayPiece<P>[],
95
+ classes: string,
96
+ left: number,
97
+ top: number,
98
+ cellMm: number,
99
+ options: RenderOptions
100
+ ): string {
101
+ return pieces
102
+ .map(({ piece, cells, x, y }) =>
103
+ drawTile(
104
+ piece.pieceId,
105
+ cells,
106
+ lookOf(piece.pieceId, piece.order, { ...options, pieceStyle: "tile" }),
107
+ classes,
108
+ false,
109
+ ([cx, cy]) => [left + x + cx * cellMm, top + y + cy * cellMm],
110
+ cellMm
111
+ )
112
+ )
113
+ .join("");
114
+ }
package/src/types.ts ADDED
@@ -0,0 +1,106 @@
1
+ /** A cell as [x, y], with y growing down. */
2
+ export type Cell = [x: number, y: number];
3
+
4
+ /** Where a piece sits: its origin cell and which of its orientations it takes. */
5
+ export interface Placement {
6
+ pieceId: string;
7
+ origin: Cell;
8
+ orientation: number;
9
+ }
10
+
11
+ /** A piece as an instance holds it: its shape, and where it starts. */
12
+ export interface Piece extends Placement {
13
+ shape: Cell[];
14
+ }
15
+
16
+ export interface Mark {
17
+ kind: string;
18
+ cell: Cell;
19
+ }
20
+
21
+ /** One board, with everything needed to draw it. Extra fields are ignored. */
22
+ export interface Instance {
23
+ /** Amoeba's data. Data without a type is Amoeba's too. */
24
+ $type?: "amoeba";
25
+ geometry: string;
26
+ board: Cell[];
27
+ blockers: Cell[];
28
+ pieces: Piece[];
29
+ marks: Mark[];
30
+ solution: Placement[];
31
+ }
32
+
33
+ /**
34
+ * A piece to place, or in Fair Shares to share: it has a shape and no
35
+ * placement, and is drawn beside the board. Its origin and orientation, and
36
+ * in Fair Shares its share, are left out, or all null.
37
+ */
38
+ export interface LoosePiece {
39
+ pieceId: string;
40
+ shape: Cell[];
41
+ origin?: null;
42
+ orientation?: null;
43
+ share?: null;
44
+ }
45
+
46
+ /** The number beside a row or a column: how many of its cells the pieces cover, or leave empty. */
47
+ export interface Clue {
48
+ direction: "row" | "column";
49
+ /** Which line: a row's y, a column's x. */
50
+ index: number;
51
+ kind: "covered" | "empty";
52
+ value: number;
53
+ }
54
+
55
+ /** One Line Counts board, with everything needed to draw it. Extra fields are ignored. */
56
+ export interface LineCountsInstance {
57
+ $type: "line-counts";
58
+ geometry: string;
59
+ board: Cell[];
60
+ /** The small boards, each a list of cells; none on an open board. */
61
+ regions: Cell[][];
62
+ clues: Clue[];
63
+ /** A piece with a placement starts on the board; one without is to place. */
64
+ pieces: (Piece | LoosePiece)[];
65
+ /** One placement per piece to place, in any order. An illustration has none. */
66
+ solution?: Placement[];
67
+ }
68
+
69
+ /** Where a piece sits in Fair Shares: on which share's grid, counted from 0, and where on it. */
70
+ export interface SharePlacement extends Placement {
71
+ share: number;
72
+ }
73
+
74
+ /** A piece that starts on a share's grid. */
75
+ export interface SharePiece extends Piece {
76
+ share: number;
77
+ }
78
+
79
+ /** One Fair Shares board, with everything needed to draw it. Extra fields are ignored. */
80
+ export interface FairSharesInstance {
81
+ $type: "fair-shares";
82
+ geometry: string;
83
+ /** The cells of one grid. Every share has a grid of exactly these cells. */
84
+ board: Cell[];
85
+ /** How many shares, and so how many grids. */
86
+ shares: number;
87
+ /** How many pieces stay outside every share. Which ones is the child's choice. */
88
+ leftOut: number;
89
+ /** A piece with a share and a placement starts on that share's grid; one without is to share. */
90
+ pieces: (SharePiece | LoosePiece)[];
91
+ /**
92
+ * One placement per piece to share that isn't left out, in any order. A
93
+ * piece to share without one is left out. An illustration has none.
94
+ */
95
+ solution?: SharePlacement[];
96
+ }
97
+
98
+ /** Any game's data. `$type` tells the games apart. */
99
+ export type GameData = Instance | LineCountsInstance | FairSharesInstance;
100
+
101
+ /** The pieces after some number of moves. */
102
+ export interface Position {
103
+ pieces: Piece[];
104
+ /** The piece the last move placed; absent at the start. */
105
+ moved?: string;
106
+ }
package/svg.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /** A number for an attribute: at most 3 decimals, no trailing zeros. */
2
+ export declare function fmt(n: number): string;
3
+ /** `<name a="1" b="x">children</name>`, or self-closing with no children. */
4
+ export declare function element(name: string, attributes: Record<string, string | number>, children?: string): string;
5
+ /** The root: xmlns, width and height in millimetres, and a viewBox in the same units. */
6
+ export declare function root(widthMm: number, heightMm: number, children: string): string;
7
+ //# sourceMappingURL=svg.d.ts.map
package/svg.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"svg.d.ts","sourceRoot":"","sources":["src/svg.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,wBAAgB,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAErC;AASD,6EAA6E;AAC7E,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,EAC3C,QAAQ,CAAC,EAAE,MAAM,GAChB,MAAM,CAUR;AAED,yFAAyF;AACzF,wBAAgB,IAAI,CAClB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,MAAM,CAWR"}
package/svg.js ADDED
@@ -0,0 +1,29 @@
1
+ /** A number for an attribute: at most 3 decimals, no trailing zeros. */
2
+ export function fmt(n) {
3
+ return String(Number(n.toFixed(3)));
4
+ }
5
+ function escapeAttribute(value) {
6
+ return value
7
+ .replaceAll("&", "&amp;")
8
+ .replaceAll('"', "&quot;")
9
+ .replaceAll("<", "&lt;");
10
+ }
11
+ /** `<name a="1" b="x">children</name>`, or self-closing with no children. */
12
+ export function element(name, attributes, children) {
13
+ const attrs = Object.entries(attributes)
14
+ .map(([key, value]) => ` ${key}="${typeof value === "number" ? fmt(value) : escapeAttribute(value)}"`)
15
+ .join("");
16
+ return children === undefined
17
+ ? `<${name}${attrs}/>`
18
+ : `<${name}${attrs}>${children}</${name}>`;
19
+ }
20
+ /** The root: xmlns, width and height in millimetres, and a viewBox in the same units. */
21
+ export function root(widthMm, heightMm, children) {
22
+ return element("svg", {
23
+ xmlns: "http://www.w3.org/2000/svg",
24
+ width: `${fmt(widthMm)}mm`,
25
+ height: `${fmt(heightMm)}mm`,
26
+ viewBox: `0 0 ${fmt(widthMm)} ${fmt(heightMm)}`,
27
+ }, children);
28
+ }
29
+ //# sourceMapp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,CAAC,QAAQ,CAAC,IAAI;QAC5B,OAAO,EAAE,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,GAAG,CAAC,QAAQ,CAAC,EAAE;KAChD,EACD,QAAQ,CACT,CAAC;AACJ,CAAC"}
package/tray.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { type RenderOptions } from "./board.js";
2
+ import type { Cell, LoosePiece } from "./types.js";
3
+ /** What the tray needs of a piece: its id and its shape. */
4
+ export type Shaped = Pick<LoosePiece, "pieceId" | "shape">;
5
+ /** A piece in the tray: the cells it is drawn as, and its top-left in millimetres from the tray's. */
6
+ export interface TrayPiece<P extends Shaped = Shaped> {
7
+ piece: P;
8
+ cells: Cell[];
9
+ x: number;
10
+ y: number;
11
+ }
12
+ export interface Tray<P extends Shaped = Shaped> {
13
+ pieces: TrayPiece<P>[];
14
+ width: number;
15
+ height: number;
16
+ }
17
+ /**
18
+ * The pieces to place, laid out in their order: left to right, half a cell
19
+ * apart, starting a new row when the next piece would pass `widthMm`. Each
20
+ * row is centred, and each piece is centred in its row's height. A piece
21
+ * wider than `widthMm` gets a row of its own, and the tray is then that wide.
22
+ */
23
+ export declare function layTray<P extends Shaped>(pieces: readonly P[], cellMm: number, widthMm: number): Tray<P>;
24
+ /**
25
+ * The tray's pieces as tiles, each with the fill its order in the board's
26
+ * `pieces` gives it. `left` and `top` are the tray's corner in millimetres.
27
+ * Always tiles, whatever the piece style: nothing covers a piece in the tray.
28
+ */
29
+ export declare function drawTrayPieces<P extends Shaped & {
30
+ order: number;
31
+ }>(pieces: readonly TrayPiece<P>[], classes: string, left: number, top: number, cellMm: number, options: RenderOptions): string;
32
+ //# sourceMappingURL=tray.d.ts.map
package/tray.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tray.d.ts","sourceRoot":"","sources":["src/tray.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAoB,MAAM,YAAY,CAAC;AAElE,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEnD,4DAA4D;AAC5D,MAAM,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,EAAE,SAAS,GAAG,OAAO,CAAC,CAAC;AAE3D,sGAAsG;AACtG,MAAM,WAAW,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;IAClD,KAAK,EAAE,CAAC,CAAC;IACT,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAED,MAAM,WAAW,IAAI,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;IAC7C,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAYD;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,EACtC,MAAM,EAAE,SAAS,CAAC,EAAE,EACpB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GACd,IAAI,CAAC,CAAC,CAAC,CA4CT;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,EACjE,MAAM,EAAE,SAAS,SAAS,CAAC,CAAC,CAAC,EAAE,EAC/B,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,aAAa,GACrB,MAAM,CAcR"}
package/tray.js ADDED
@@ -0,0 +1,58 @@
1
+ import { drawTile, lookOf } from "./board.js";
2
+ import { orientations } from "./geometry.js";
3
+ const extent = (cells, axis) => Math.max(...cells.map((cell) => cell[axis])) + 1;
4
+ /** The piece lying down: the least tall of its orientations, the first of those in the convention's order. */
5
+ function lying(shape) {
6
+ return orientations(shape).reduce((best, next) => extent(next, 1) < extent(best, 1) ? next : best);
7
+ }
8
+ /**
9
+ * The pieces to place, laid out in their order: left to right, half a cell
10
+ * apart, starting a new row when the next piece would pass `widthMm`. Each
11
+ * row is centred, and each piece is centred in its row's height. A piece
12
+ * wider than `widthMm` gets a row of its own, and the tray is then that wide.
13
+ */
14
+ export function layTray(pieces, cellMm, widthMm) {
15
+ const gap = cellMm / 2;
16
+ const rows = [];
17
+ for (const piece of pieces) {
18
+ const cells = lying(piece.shape);
19
+ const width = extent(cells, 0) * cellMm;
20
+ const height = extent(cells, 1) * cellMm;
21
+ let row = rows.at(-1);
22
+ // A hair of slack, so a row that fits exactly isn't broken by rounding.
23
+ if (row === undefined || row.width + gap + width > widthMm + 1e-6) {
24
+ row = { items: [], width: 0, height: 0 };
25
+ rows.push(row);
26
+ }
27
+ const x = row.items.length === 0 ? 0 : row.width + gap;
28
+ row.items.push({ piece, cells, x, width, height });
29
+ row.width = x + width;
30
+ row.height = Math.max(row.height, height);
31
+ }
32
+ const width = Math.max(0, ...rows.map((row) => row.width));
33
+ const out = [];
34
+ let top = 0;
35
+ for (const row of rows) {
36
+ for (const item of row.items) {
37
+ out.push({
38
+ piece: item.piece,
39
+ cells: item.cells,
40
+ x: (width - row.width) / 2 + item.x,
41
+ y: top + (row.height - item.height) / 2,
42
+ });
43
+ }
44
+ top += row.height + gap;
45
+ }
46
+ return { pieces: out, width, height: Math.max(0, top - gap) };
47
+ }
48
+ /**
49
+ * The tray's pieces as tiles, each with the fill its order in the board's
50
+ * `pieces` gives it. `left` and `top` are the tray's corner in millimetres.
51
+ * Always tiles, whatever the piece style: nothing covers a piece in the tray.
52
+ */
53
+ export function drawTrayPieces(pieces, classes, left, top, cellMm, options) {
54
+ return pieces
55
+ .map(({ piece, cells, x, y }) => drawTile(piece.pieceId, cells, lookOf(piece.pieceId, piece.order, { ...options, pieceStyle: "tile" }), classes, false, ([cx, cy]) => [left + x + cx * cellMm, top + y + cy * cellMm], cellMm))
56
+ .join("");
57
+ }
58
+ //# sourceMapp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AAG,IAAI,CAAC,CAAC;gBACnC,CAAC,EAAE,GAAG,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC;aACxC,CAAC,CAAC;QACL,CAAC;QACD,GAAG,IAAI,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;IAC1B,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC,EAAE,CAAC;AAChE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC5B,MAA+B,EAC/B,OAAe,EACf,IAAY,EACZ,GAAW,EACX,MAAc,EACd,OAAsB;IAEtB,OAAO,MAAM;SACV,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAC9B,QAAQ,CACN,KAAK,CAAC,OAAO,EACb,KAAK,EACL,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,EAAE,EAAE,GAAG,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC,EACtE,OAAO,EACP,KAAK,EACL,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,GAAG,EAAE,GAAG,MAAM,EAAE,GAAG,GAAG,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,EAC7D,MAAM,CACP,CACF;SACA,IAAI,CAAC,EAAE,CAAC,CAAC;AACd,CAAC"}
package/types.d.ts ADDED
@@ -0,0 +1,95 @@
1
+ /** A cell as [x, y], with y growing down. */
2
+ export type Cell = [x: number, y: number];
3
+ /** Where a piece sits: its origin cell and which of its orientations it takes. */
4
+ export interface Placement {
5
+ pieceId: string;
6
+ origin: Cell;
7
+ orientation: number;
8
+ }
9
+ /** A piece as an instance holds it: its shape, and where it starts. */
10
+ export interface Piece extends Placement {
11
+ shape: Cell[];
12
+ }
13
+ export interface Mark {
14
+ kind: string;
15
+ cell: Cell;
16
+ }
17
+ /** One board, with everything needed to draw it. Extra fields are ignored. */
18
+ export interface Instance {
19
+ /** Amoeba's data. Data without a type is Amoeba's too. */
20
+ $type?: "amoeba";
21
+ geometry: string;
22
+ board: Cell[];
23
+ blockers: Cell[];
24
+ pieces: Piece[];
25
+ marks: Mark[];
26
+ solution: Placement[];
27
+ }
28
+ /**
29
+ * A piece to place, or in Fair Shares to share: it has a shape and no
30
+ * placement, and is drawn beside the board. Its origin and orientation, and
31
+ * in Fair Shares its share, are left out, or all null.
32
+ */
33
+ export interface LoosePiece {
34
+ pieceId: string;
35
+ shape: Cell[];
36
+ origin?: null;
37
+ orientation?: null;
38
+ share?: null;
39
+ }
40
+ /** The number beside a row or a column: how many of its cells the pieces cover, or leave empty. */
41
+ export interface Clue {
42
+ direction: "row" | "column";
43
+ /** Which line: a row's y, a column's x. */
44
+ index: number;
45
+ kind: "covered" | "empty";
46
+ value: number;
47
+ }
48
+ /** One Line Counts board, with everything needed to draw it. Extra fields are ignored. */
49
+ export interface LineCountsInstance {
50
+ $type: "line-counts";
51
+ geometry: string;
52
+ board: Cell[];
53
+ /** The small boards, each a list of cells; none on an open board. */
54
+ regions: Cell[][];
55
+ clues: Clue[];
56
+ /** A piece with a placement starts on the board; one without is to place. */
57
+ pieces: (Piece | LoosePiece)[];
58
+ /** One placement per piece to place, in any order. An illustration has none. */
59
+ solution?: Placement[];
60
+ }
61
+ /** Where a piece sits in Fair Shares: on which share's grid, counted from 0, and where on it. */
62
+ export interface SharePlacement extends Placement {
63
+ share: number;
64
+ }
65
+ /** A piece that starts on a share's grid. */
66
+ export interface SharePiece extends Piece {
67
+ share: number;
68
+ }
69
+ /** One Fair Shares board, with everything needed to draw it. Extra fields are ignored. */
70
+ export interface FairSharesInstance {
71
+ $type: "fair-shares";
72
+ geometry: string;
73
+ /** The cells of one grid. Every share has a grid of exactly these cells. */
74
+ board: Cell[];
75
+ /** How many shares, and so how many grids. */
76
+ shares: number;
77
+ /** How many pieces stay outside every share. Which ones is the child's choice. */
78
+ leftOut: number;
79
+ /** A piece with a share and a placement starts on that share's grid; one without is to share. */
80
+ pieces: (SharePiece | LoosePiece)[];
81
+ /**
82
+ * One placement per piece to share that isn't left out, in any order. A
83
+ * piece to share without one is left out. An illustration has none.
84
+ */
85
+ solution?: SharePlacement[];
86
+ }
87
+ /** Any game's data. `$type` tells the games apart. */
88
+ export type GameData = Instance | LineCountsInstance | FairSharesInstance;
89
+ /** The pieces after some number of moves. */
90
+ export interface Position {
91
+ pieces: Piece[];
92
+ /** The piece the last move placed; absent at the start. */
93
+ moved?: string;
94
+ }
95
+ //# sourceMappingURL=types.d.ts.map
package/types.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["src/types.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;AAE1C,kFAAkF;AAClF,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,IAAI,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,uEAAuE;AACvE,MAAM,WAAW,KAAM,SAAQ,SAAS;IACtC,KAAK,EAAE,IAAI,EAAE,CAAC;CACf;AAED,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,IAAI,CAAC;CACZ;AAED,8EAA8E;AAC9E,MAAM,WAAW,QAAQ;IACvB,0DAA0D;IAC1D,KAAK,CAAC,EAAE,QAAQ,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,QAAQ,EAAE,IAAI,EAAE,CAAC;IACjB,MAAM,EAAE,KAAK,EAAE,CAAC;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CAAC;IACf,kFAAkF;IAClF,OAAO,EAAE,MAAM,CAAC;IAChB,iGAAiG;IACjG,MAAM,EAAE,CAAC,UAAU,GAAG,UAAU,CAAC,EAAE,CAAC;IACpC;;;OAGG;IACH,QAAQ,CAAC,EAAE,cAAc,EAAE,CAAC;CAC7B;AAED,sDAAsD;AACtD,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,kBAAkB,GAAG,kBAAkB,CAAC;AAE1E,6CAA6C;AAC7C,MAAM,WAAW,QAAQ;IACvB,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,2DAA2D;IAC3D,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB"}
package/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["src/types.ts"],"names":[],"mappings":""}