@johnmorrisdotca/kazu 1.2.0 → 1.4.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/CHANGELOG.md +34 -0
- package/README.md +69 -27
- package/dist/akari-entry.d.ts +1 -0
- package/dist/akari-entry.js +1 -0
- package/dist/akari.constants.d.ts +12 -0
- package/dist/akari.constants.js +12 -0
- package/dist/akari.types.d.ts +19 -0
- package/dist/akariGenerate.d.ts +10 -5
- package/dist/akariGenerate.js +101 -65
- package/dist/akariLogic.d.ts +10 -0
- package/dist/akariLogic.js +84 -0
- package/dist/akariRate.d.ts +6 -0
- package/dist/akariRate.js +34 -0
- package/dist/akariSolve.d.ts +1 -1
- package/dist/akariSolve.js +8 -96
- package/dist/akariTemplate.d.ts +7 -0
- package/dist/akariTemplate.js +78 -0
- package/dist/cells.d.ts +4 -4
- package/dist/cells.js +6 -6
- package/dist/csp.d.ts +63 -0
- package/dist/csp.js +162 -0
- package/dist/fillomino-entry.d.ts +1 -0
- package/dist/fillomino-entry.js +1 -0
- package/dist/fillomino.constants.d.ts +6 -2
- package/dist/fillomino.constants.js +6 -2
- package/dist/fillomino.types.d.ts +18 -1
- package/dist/fillominoBuild.d.ts +10 -0
- package/dist/fillominoBuild.js +78 -0
- package/dist/fillominoGenerate.d.ts +7 -1
- package/dist/fillominoGenerate.js +75 -107
- package/dist/fillominoLogic.d.ts +14 -0
- package/dist/fillominoLogic.js +177 -0
- package/dist/fillominoMount.js +4 -1
- package/dist/fillominoRate.d.ts +3 -0
- package/dist/fillominoRate.js +57 -0
- package/dist/fillominoSolve.js +15 -88
- package/dist/groupSolve.d.ts +9 -0
- package/dist/groupSolve.js +66 -0
- package/dist/hitori-entry.d.ts +1 -0
- package/dist/hitori-entry.js +1 -0
- package/dist/hitori.constants.d.ts +7 -1
- package/dist/hitori.constants.js +7 -1
- package/dist/hitori.types.d.ts +18 -1
- package/dist/hitoriBoard.js +2 -1
- package/dist/hitoriBuild.d.ts +12 -0
- package/dist/hitoriBuild.js +117 -0
- package/dist/hitoriGenerate.d.ts +10 -3
- package/dist/hitoriGenerate.js +59 -157
- package/dist/hitoriLogic.d.ts +12 -0
- package/dist/hitoriLogic.js +189 -0
- package/dist/hitoriRate.d.ts +3 -0
- package/dist/hitoriRate.js +32 -0
- package/dist/hitoriSolve.d.ts +4 -1
- package/dist/hitoriSolve.js +11 -62
- package/dist/kakuro-entry.d.ts +1 -0
- package/dist/kakuro-entry.js +1 -0
- package/dist/kakuro.constants.d.ts +6 -1
- package/dist/kakuro.constants.js +6 -1
- package/dist/kakuro.types.d.ts +19 -0
- package/dist/kakuroBuild.d.ts +30 -0
- package/dist/kakuroBuild.js +337 -0
- package/dist/kakuroGenerate.d.ts +10 -3
- package/dist/kakuroGenerate.js +54 -122
- package/dist/kakuroLogic.d.ts +13 -0
- package/dist/kakuroLogic.js +153 -0
- package/dist/kakuroRate.d.ts +3 -0
- package/dist/kakuroRate.js +40 -0
- package/dist/kakuroSolve.js +17 -69
- package/dist/kakuroTemplate.d.ts +3 -0
- package/dist/kakuroTemplate.js +130 -0
- package/dist/kinds.js +1 -1
- package/dist/layout.js +1 -0
- package/dist/mount.d.ts +0 -18
- package/dist/mount.js +24 -3
- package/dist/names.js +3 -3
- package/dist/numberPlace.d.ts +1 -1
- package/dist/numberPlace.js +21 -8
- package/dist/shikaku-entry.d.ts +1 -0
- package/dist/shikaku-entry.js +1 -0
- package/dist/shikaku.constants.d.ts +5 -2
- package/dist/shikaku.constants.js +5 -2
- package/dist/shikaku.types.d.ts +18 -1
- package/dist/shikakuBuild.d.ts +21 -0
- package/dist/shikakuBuild.js +135 -0
- package/dist/shikakuGenerate.d.ts +6 -1
- package/dist/shikakuGenerate.js +50 -35
- package/dist/shikakuLogic.d.ts +13 -0
- package/dist/shikakuLogic.js +84 -0
- package/dist/shikakuRate.d.ts +3 -0
- package/dist/shikakuRate.js +27 -0
- package/dist/shikakuSolve.d.ts +1 -1
- package/dist/shikakuSolve.js +23 -44
- package/dist/shikakuTemplate.d.ts +3 -0
- package/dist/shikakuTemplate.js +43 -0
- package/dist/slitherlink-entry.d.ts +1 -0
- package/dist/slitherlink-entry.js +1 -0
- package/dist/slitherlink.constants.d.ts +5 -0
- package/dist/slitherlink.constants.js +5 -0
- package/dist/slitherlink.types.d.ts +18 -0
- package/dist/slitherlinkGenerate.d.ts +10 -3
- package/dist/slitherlinkGenerate.js +161 -80
- package/dist/slitherlinkLogic.d.ts +10 -0
- package/dist/slitherlinkLogic.js +267 -0
- package/dist/slitherlinkRate.d.ts +3 -0
- package/dist/slitherlinkRate.js +26 -0
- package/dist/slitherlinkSolve.d.ts +1 -1
- package/dist/slitherlinkSolve.js +9 -117
- package/dist/slitherlinkTemplate.d.ts +3 -0
- package/dist/slitherlinkTemplate.js +101 -0
- package/dist/strings.js +2 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
package/dist/csp.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ONE SMALL ENGINE for the puzzles that are solved by narrowing down what each square, edge or rectangle can
|
|
3
|
+
* still be. A puzzle supplies its variables and one function that prunes; this file does the two things
|
|
4
|
+
* every one of them needs: counting answers (a search that stops at a limit) and rating how hard a puzzle
|
|
5
|
+
* is (how deep the reasoning must go before the grid is decided).
|
|
6
|
+
*
|
|
7
|
+
* A variable has a few slots (its possible values). `alive[slot]` is 1 while that value is still possible.
|
|
8
|
+
* A variable is decided when exactly one of its slots is alive and impossible when none is.
|
|
9
|
+
*/
|
|
10
|
+
/** How many values a variable still has. */
|
|
11
|
+
export function slotsLeft(csp, alive, variable) {
|
|
12
|
+
let left = 0;
|
|
13
|
+
for (let slot = csp.starts[variable]; slot < csp.starts[variable + 1]; slot += 1)
|
|
14
|
+
left += alive[slot];
|
|
15
|
+
return left;
|
|
16
|
+
}
|
|
17
|
+
/** Every value of every variable still possible. */
|
|
18
|
+
export function openSlots(csp) {
|
|
19
|
+
return new Uint8Array(csp.starts[csp.starts.length - 1]).fill(1);
|
|
20
|
+
}
|
|
21
|
+
/** Leaves `variable` with only `slot` (a slot number, not an offset). */
|
|
22
|
+
export function decide(csp, alive, variable, slot) {
|
|
23
|
+
for (let at = csp.starts[variable]; at < csp.starts[variable + 1]; at += 1)
|
|
24
|
+
alive[at] = at === slot ? 1 : 0;
|
|
25
|
+
}
|
|
26
|
+
/** The slot a decided variable has left, or -1 when it has more than one or none. */
|
|
27
|
+
export function decidedSlot(csp, alive, variable) {
|
|
28
|
+
let found = -1;
|
|
29
|
+
for (let slot = csp.starts[variable]; slot < csp.starts[variable + 1]; slot += 1) {
|
|
30
|
+
if (!alive[slot])
|
|
31
|
+
continue;
|
|
32
|
+
if (found >= 0)
|
|
33
|
+
return -1;
|
|
34
|
+
found = slot;
|
|
35
|
+
}
|
|
36
|
+
return found;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Counts the ways to decide every variable, up to `limit`. `exhausted` says the node budget ran out;
|
|
40
|
+
* `stopped` says the limit was reached. Either way the count must not be read as proof of anything
|
|
41
|
+
* beyond what was seen.
|
|
42
|
+
*/
|
|
43
|
+
export function countCsp(csp, start, limit, budget) {
|
|
44
|
+
const variables = csp.starts.length - 1;
|
|
45
|
+
let count = 0, nodes = 0, exhausted = false, stopped = false;
|
|
46
|
+
let solution = null;
|
|
47
|
+
const solutions = [];
|
|
48
|
+
const visit = (alive, hint) => {
|
|
49
|
+
if (exhausted || stopped)
|
|
50
|
+
return;
|
|
51
|
+
if (++nodes > budget) {
|
|
52
|
+
exhausted = true;
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (!csp.propagate(alive, hint))
|
|
56
|
+
return;
|
|
57
|
+
let pick = csp.choose ? csp.choose(alive) : -1, best = 99999;
|
|
58
|
+
if (!csp.choose) {
|
|
59
|
+
for (let v = 0; v < variables; v += 1) {
|
|
60
|
+
const left = slotsLeft(csp, alive, v);
|
|
61
|
+
if (left > 1 && left < best) {
|
|
62
|
+
best = left;
|
|
63
|
+
pick = v;
|
|
64
|
+
if (left === 2)
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (pick < 0) {
|
|
70
|
+
count += 1;
|
|
71
|
+
solution ?? (solution = alive.slice());
|
|
72
|
+
solutions.push(alive.slice());
|
|
73
|
+
if (count >= limit)
|
|
74
|
+
stopped = true;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
for (let slot = csp.starts[pick]; slot < csp.starts[pick + 1]; slot += 1) {
|
|
78
|
+
if (!alive[slot])
|
|
79
|
+
continue;
|
|
80
|
+
const next = alive.slice();
|
|
81
|
+
decide(csp, next, pick, slot);
|
|
82
|
+
visit(next, pick);
|
|
83
|
+
if (exhausted || stopped)
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
visit(start.slice());
|
|
88
|
+
return { count, solution, solutions, stopped, exhausted, nodes: Math.min(nodes, budget) };
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Solves by reasoning alone, never by guessing a value and keeping it. `depth` 0 is the rules' own
|
|
92
|
+
* pruning. Depth 1 adds probing: suppose one value, prune, and when that breaks a rule the value is
|
|
93
|
+
* ruled out. Depth 2 lets a probe probe. `probes` counts the values ruled out by supposing, which is how
|
|
94
|
+
* much supposing the puzzle asks of a person.
|
|
95
|
+
*/
|
|
96
|
+
export function logicCsp(csp, start, depth) {
|
|
97
|
+
const variables = csp.starts.length - 1;
|
|
98
|
+
let probes = 0;
|
|
99
|
+
const decidedAll = (alive) => {
|
|
100
|
+
for (let v = 0; v < variables; v += 1)
|
|
101
|
+
if (slotsLeft(csp, alive, v) > 1)
|
|
102
|
+
return false;
|
|
103
|
+
return true;
|
|
104
|
+
};
|
|
105
|
+
const run = (alive, level, hint) => {
|
|
106
|
+
let first = hint;
|
|
107
|
+
for (;;) {
|
|
108
|
+
if (!csp.propagate(alive, first))
|
|
109
|
+
return false;
|
|
110
|
+
first = undefined;
|
|
111
|
+
let open = false;
|
|
112
|
+
for (let v = 0; v < variables && !open; v += 1)
|
|
113
|
+
if (slotsLeft(csp, alive, v) > 1)
|
|
114
|
+
open = true;
|
|
115
|
+
if (!open)
|
|
116
|
+
return "solved";
|
|
117
|
+
if (level === 0)
|
|
118
|
+
return "stuck";
|
|
119
|
+
let moved = false;
|
|
120
|
+
for (let v = 0; v < variables; v += 1) {
|
|
121
|
+
if (slotsLeft(csp, alive, v) < 2)
|
|
122
|
+
continue;
|
|
123
|
+
for (let slot = csp.starts[v]; slot < csp.starts[v + 1]; slot += 1) {
|
|
124
|
+
if (!alive[slot] || slotsLeft(csp, alive, v) < 2)
|
|
125
|
+
continue;
|
|
126
|
+
const trial = alive.slice();
|
|
127
|
+
decide(csp, trial, v, slot);
|
|
128
|
+
const result = run(trial, level - 1, v);
|
|
129
|
+
if (result === false) {
|
|
130
|
+
alive[slot] = 0;
|
|
131
|
+
probes += 1;
|
|
132
|
+
moved = true;
|
|
133
|
+
if (!csp.propagate(alive))
|
|
134
|
+
return false;
|
|
135
|
+
if (decidedAll(alive))
|
|
136
|
+
return "solved";
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (!moved)
|
|
141
|
+
return "stuck";
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
const alive = start.slice();
|
|
145
|
+
const result = run(alive, depth);
|
|
146
|
+
return { solved: result === "solved", contradiction: result === false, probes, alive };
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Counts answers like `countCsp`, but first tries to settle the puzzle by reasoning (rules, then one level of
|
|
150
|
+
* supposing): reasoning that leaves every variable decided proves exactly one answer, and reasoning that breaks a
|
|
151
|
+
* rule proves none, in far fewer steps than a search. Only a puzzle that reasoning leaves open is searched, from the
|
|
152
|
+
* state reasoning reached.
|
|
153
|
+
*/
|
|
154
|
+
export function solveCsp(csp, start, limit, budget, depth = 1) {
|
|
155
|
+
const plain = logicCsp(csp, start, 0);
|
|
156
|
+
const reasoned = plain.solved || plain.contradiction || depth < 1 ? plain : logicCsp(csp, start, 1);
|
|
157
|
+
if (reasoned.contradiction)
|
|
158
|
+
return { count: 0, solution: null, solutions: [], stopped: false, exhausted: false, nodes: 0 };
|
|
159
|
+
if (reasoned.solved)
|
|
160
|
+
return { count: 1, solution: reasoned.alive, solutions: [reasoned.alive], stopped: limit <= 1, exhausted: false, nodes: 0 };
|
|
161
|
+
return countCsp(csp, reasoned.alive, limit, budget);
|
|
162
|
+
}
|
|
@@ -3,6 +3,7 @@ export * from "./fillomino.constants.ts";
|
|
|
3
3
|
export * from "./fillominoBoard.ts";
|
|
4
4
|
export * from "./fillominoSolve.ts";
|
|
5
5
|
export * from "./fillominoGenerate.ts";
|
|
6
|
+
export * from "./fillominoRate.ts";
|
|
6
7
|
export * from "./fillominoGame.ts";
|
|
7
8
|
export { drawFillomino } from "./fillominoDraw.ts";
|
|
8
9
|
export type { FillominoDrawOptions } from "./fillominoPlay.types.ts";
|
package/dist/fillomino-entry.js
CHANGED
|
@@ -3,5 +3,6 @@ export * from "./fillomino.constants.js";
|
|
|
3
3
|
export * from "./fillominoBoard.js";
|
|
4
4
|
export * from "./fillominoSolve.js";
|
|
5
5
|
export * from "./fillominoGenerate.js";
|
|
6
|
+
export * from "./fillominoRate.js";
|
|
6
7
|
export * from "./fillominoGame.js";
|
|
7
8
|
export { drawFillomino } from "./fillominoDraw.js";
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export declare const FILLOMINO_MOST_SIDE = 12;
|
|
2
|
-
export declare const FILLOMINO_GENERATOR_MOST_CELLS =
|
|
2
|
+
export declare const FILLOMINO_GENERATOR_MOST_CELLS = 144;
|
|
3
3
|
export declare const FILLOMINO_MOST_NODES = 80000;
|
|
4
4
|
export declare const FILLOMINO_MOST_ATTEMPTS = 120;
|
|
5
|
-
|
|
5
|
+
/** How hard a puzzle is made, by what a person must do to solve it; see `rateFillomino`. */
|
|
6
|
+
export declare const FILLOMINO_LEVELS: readonly ["easy", "medium", "hard", "extra-hard"];
|
|
7
|
+
/** The sides on offer for a square board; any side from 4 to `FILLOMINO_MOST_SIDE` can be made. */
|
|
8
|
+
export declare const FILLOMINO_SIZES: readonly [6, 8, 10, 12];
|
|
9
|
+
export declare const FILLOMINO_LEAST_GENERATED_SIDE = 4;
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export const FILLOMINO_MOST_SIDE = 12;
|
|
2
|
-
export const FILLOMINO_GENERATOR_MOST_CELLS =
|
|
2
|
+
export const FILLOMINO_GENERATOR_MOST_CELLS = 144;
|
|
3
3
|
export const FILLOMINO_MOST_NODES = 80000;
|
|
4
4
|
export const FILLOMINO_MOST_ATTEMPTS = 120;
|
|
5
|
-
|
|
5
|
+
/** How hard a puzzle is made, by what a person must do to solve it; see `rateFillomino`. */
|
|
6
|
+
export const FILLOMINO_LEVELS = ["easy", "medium", "hard", "extra-hard"];
|
|
7
|
+
/** The sides on offer for a square board; any side from 4 to `FILLOMINO_MOST_SIDE` can be made. */
|
|
8
|
+
export const FILLOMINO_SIZES = [6, 8, 10, 12];
|
|
9
|
+
export const FILLOMINO_LEAST_GENERATED_SIDE = 4;
|
|
@@ -8,7 +8,7 @@ export type FillominoPuzzle = FillominoBoard & {
|
|
|
8
8
|
level: FillominoLevel;
|
|
9
9
|
solution: readonly number[];
|
|
10
10
|
};
|
|
11
|
-
export type FillominoLevel = "easy" | "medium" | "hard";
|
|
11
|
+
export type FillominoLevel = "easy" | "medium" | "hard" | "extra-hard";
|
|
12
12
|
export type FillominoCheck = {
|
|
13
13
|
ok: boolean;
|
|
14
14
|
complete: boolean;
|
|
@@ -28,3 +28,20 @@ export type FillominoGame = {
|
|
|
28
28
|
history: readonly (readonly number[])[];
|
|
29
29
|
helped: boolean;
|
|
30
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* How hard a board is, measured by solving it. `depth` 0 means the rules solve it, 1 that somebody has to suppose a
|
|
33
|
+
* number in a square and watch it break, 2 that more than that is needed; `probes` is how many suppositions depth 1
|
|
34
|
+
* needed. The rest describes the board.
|
|
35
|
+
*/
|
|
36
|
+
export type FillominoRating = {
|
|
37
|
+
depth: 0 | 1 | 2;
|
|
38
|
+
probes: number;
|
|
39
|
+
givens: number;
|
|
40
|
+
/** Squares that are given, as a share of the board. */
|
|
41
|
+
givenShare: number;
|
|
42
|
+
regions: number;
|
|
43
|
+
/** Regions with no given in them: they are found only by what is round them. */
|
|
44
|
+
unnamed: number;
|
|
45
|
+
largest: number;
|
|
46
|
+
meanRegion: number;
|
|
47
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FillominoLevel } from "./fillomino.types.ts";
|
|
2
|
+
import type { Random } from "./random.ts";
|
|
3
|
+
/** How likely each region size is, by level: a small board of small regions is easy, long ones with few givens are not. */
|
|
4
|
+
export declare const FILLOMINO_SIZES_BY_LEVEL: Record<FillominoLevel, readonly number[]>;
|
|
5
|
+
/**
|
|
6
|
+
* A full Fillomino answer: the board cut into connected regions, every region holding its own size, and no two
|
|
7
|
+
* regions of the same size touching. Regions are grown one at a time from the square with the fewest free neighbours;
|
|
8
|
+
* null when a corner of the board cannot be finished, so the caller tries again.
|
|
9
|
+
*/
|
|
10
|
+
export declare function partitionFillomino(width: number, height: number, level: FillominoLevel, random: Random): number[] | null;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { fillominoNeighbours } from "./fillominoBoard.js";
|
|
2
|
+
import { shuffled } from "./random.js";
|
|
3
|
+
/** How likely each region size is, by level: a small board of small regions is easy, long ones with few givens are not. */
|
|
4
|
+
export const FILLOMINO_SIZES_BY_LEVEL = {
|
|
5
|
+
easy: [1, 2, 2, 2, 3, 3, 3, 4, 4],
|
|
6
|
+
medium: [1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6],
|
|
7
|
+
hard: [2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 8],
|
|
8
|
+
"extra-hard": [2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9],
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A full Fillomino answer: the board cut into connected regions, every region holding its own size, and no two
|
|
12
|
+
* regions of the same size touching. Regions are grown one at a time from the square with the fewest free neighbours;
|
|
13
|
+
* null when a corner of the board cannot be finished, so the caller tries again.
|
|
14
|
+
*/
|
|
15
|
+
export function partitionFillomino(width, height, level, random) {
|
|
16
|
+
const board = { width, height, givens: Array(width * height).fill(0) };
|
|
17
|
+
const entries = Array(width * height).fill(0);
|
|
18
|
+
const sizes = FILLOMINO_SIZES_BY_LEVEL[level];
|
|
19
|
+
for (let guard = 0; guard < width * height; guard += 1) {
|
|
20
|
+
let anchor = -1, fewest = 9, ties = 0;
|
|
21
|
+
for (let cell = 0; cell < entries.length; cell += 1) {
|
|
22
|
+
if (entries[cell])
|
|
23
|
+
continue;
|
|
24
|
+
const free = fillominoNeighbours(board, cell).filter(next => !entries[next]).length;
|
|
25
|
+
if (free < fewest) {
|
|
26
|
+
fewest = free;
|
|
27
|
+
anchor = cell;
|
|
28
|
+
ties = 1;
|
|
29
|
+
}
|
|
30
|
+
else if (free === fewest && random() * ++ties < 1)
|
|
31
|
+
anchor = cell;
|
|
32
|
+
}
|
|
33
|
+
if (anchor < 0)
|
|
34
|
+
return entries;
|
|
35
|
+
const left = entries.filter(value => !value).length;
|
|
36
|
+
let done = false;
|
|
37
|
+
const tried = new Set();
|
|
38
|
+
// Sizes come in the order of a shuffle of the weighted list, so common sizes are tried first more often.
|
|
39
|
+
for (const area of shuffled(sizes, random)) {
|
|
40
|
+
if (tried.has(area) || area > left)
|
|
41
|
+
continue;
|
|
42
|
+
tried.add(area);
|
|
43
|
+
if (fillominoNeighbours(board, anchor).some(next => entries[next] === area))
|
|
44
|
+
continue;
|
|
45
|
+
for (let proposal = 0; proposal < 6 && !done; proposal += 1) {
|
|
46
|
+
const cells = [anchor], members = new Set(cells);
|
|
47
|
+
while (cells.length < area) {
|
|
48
|
+
const frontier = new Set();
|
|
49
|
+
for (const cell of cells)
|
|
50
|
+
for (const next of fillominoNeighbours(board, cell)) {
|
|
51
|
+
if (entries[next] || members.has(next))
|
|
52
|
+
continue;
|
|
53
|
+
if (fillominoNeighbours(board, next).some(other => entries[other] === area))
|
|
54
|
+
continue;
|
|
55
|
+
frontier.add(next);
|
|
56
|
+
}
|
|
57
|
+
if (!frontier.size)
|
|
58
|
+
break;
|
|
59
|
+
// Prefer squares that are hard to reach later: those with few free neighbours.
|
|
60
|
+
const options = [...frontier].sort((a, b) => fillominoNeighbours(board, a).filter(n => !entries[n] && !members.has(n)).length - fillominoNeighbours(board, b).filter(n => !entries[n] && !members.has(n)).length);
|
|
61
|
+
const pick = random() < .55 ? options[0] : options[Math.floor(random() * options.length)];
|
|
62
|
+
cells.push(pick);
|
|
63
|
+
members.add(pick);
|
|
64
|
+
}
|
|
65
|
+
if (cells.length !== area)
|
|
66
|
+
continue;
|
|
67
|
+
for (const cell of cells)
|
|
68
|
+
entries[cell] = area;
|
|
69
|
+
done = true;
|
|
70
|
+
}
|
|
71
|
+
if (done)
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
if (!done)
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
import type { FillominoLevel, FillominoPuzzle } from "./fillomino.types.ts";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Makes a seeded puzzle and proves its answer is the only one. The board is cut into connected regions, each holding
|
|
4
|
+
* its own size, with no two of one size touching; every square starts given, and givens are taken away for as long as
|
|
5
|
+
* the puzzle can still be solved the way the level asks: easy and medium by the rules alone (easy keeps most givens),
|
|
6
|
+
* hard by supposing one number at a time, extra-hard for as long as the answer stays single. If no board of the level
|
|
7
|
+
* is found within the attempts the next level down is tried, and a puzzle that gives every square is the last resort.
|
|
8
|
+
*/
|
|
3
9
|
export declare function generateFillomino(width?: number, height?: number, level?: FillominoLevel, seed?: number): FillominoPuzzle;
|
|
@@ -1,125 +1,93 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { checkFillomino
|
|
1
|
+
import { FILLOMINO_LEAST_GENERATED_SIDE, FILLOMINO_LEVELS, FILLOMINO_MOST_ATTEMPTS, FILLOMINO_MOST_SIDE } from "./fillomino.constants.js";
|
|
2
|
+
import { checkFillomino } from "./fillominoBoard.js";
|
|
3
|
+
import { partitionFillomino } from "./fillominoBuild.js";
|
|
4
|
+
import { fillominoModel, fillominoMostValue } from "./fillominoLogic.js";
|
|
3
5
|
import { solveFillomino } from "./fillominoSolve.js";
|
|
6
|
+
import { countCsp, decide, logicCsp, openSlots } from "./csp.js";
|
|
4
7
|
import { isKazuSeed, seededRandom, shuffled } from "./random.js";
|
|
5
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Makes a seeded puzzle and proves its answer is the only one. The board is cut into connected regions, each holding
|
|
10
|
+
* its own size, with no two of one size touching; every square starts given, and givens are taken away for as long as
|
|
11
|
+
* the puzzle can still be solved the way the level asks: easy and medium by the rules alone (easy keeps most givens),
|
|
12
|
+
* hard by supposing one number at a time, extra-hard for as long as the answer stays single. If no board of the level
|
|
13
|
+
* is found within the attempts the next level down is tried, and a puzzle that gives every square is the last resort.
|
|
14
|
+
*/
|
|
6
15
|
export function generateFillomino(width = 5, height = width, level = "medium", seed = 1) {
|
|
7
|
-
if (![width, height].every(value => Number.isInteger(value) && value >=
|
|
16
|
+
if (![width, height].every(value => Number.isInteger(value) && value >= FILLOMINO_LEAST_GENERATED_SIDE && value <= FILLOMINO_MOST_SIDE)
|
|
8
17
|
|| !FILLOMINO_LEVELS.includes(level) || !isKazuSeed(seed)) {
|
|
9
|
-
throw new RangeError("Fillomino generation supports 4–
|
|
18
|
+
throw new RangeError("Fillomino generation supports 4–12 cells per side and a valid level and seed");
|
|
10
19
|
}
|
|
11
20
|
const random = seededRandom(seed);
|
|
12
|
-
for (let
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const choices = shuffled(region, random);
|
|
23
|
-
const count = level === "easy" && region.length > 1 ? 2 : level === "hard" && random() < 0.2 ? 0 : 1;
|
|
24
|
-
for (const cell of choices.slice(0, count))
|
|
25
|
-
givens[cell] = solution[cell];
|
|
26
|
-
extraClues.push(...choices.slice(count));
|
|
27
|
-
}
|
|
28
|
-
const clueOrder = shuffled(extraClues, random);
|
|
29
|
-
const board = { width, height, givens };
|
|
30
|
-
const maximumGivens = Math.floor(width * height * ({ easy: 0.9, medium: 0.72, hard: 0.62 }[level]));
|
|
31
|
-
for (let added = 0;; added += 1) {
|
|
32
|
-
const proof = solveFillomino(board, givens);
|
|
21
|
+
for (let aim = FILLOMINO_LEVELS.indexOf(level); aim >= 0; aim -= 1) {
|
|
22
|
+
const aimed = FILLOMINO_LEVELS[aim];
|
|
23
|
+
for (let attempt = 0; attempt < FILLOMINO_MOST_ATTEMPTS; attempt += 1) {
|
|
24
|
+
const solution = partitionFillomino(width, height, aimed, random);
|
|
25
|
+
if (!solution)
|
|
26
|
+
continue;
|
|
27
|
+
const board = thin(width, height, solution, aimed, random);
|
|
28
|
+
if (!board)
|
|
29
|
+
continue;
|
|
30
|
+
const proof = solveFillomino(board);
|
|
33
31
|
if (proof.complete && proof.count === 1 && proof.solution && checkFillomino(board, proof.solution).ok) {
|
|
34
32
|
return { ...board, seed, level, solution: proof.solution };
|
|
35
33
|
}
|
|
36
|
-
if (added >= clueOrder.length || givens.filter(Boolean).length >= maximumGivens)
|
|
37
|
-
break;
|
|
38
|
-
const cell = clueOrder[added];
|
|
39
|
-
givens[cell] = solution[cell];
|
|
40
34
|
}
|
|
41
35
|
}
|
|
42
|
-
|
|
36
|
+
const board = { width, height, givens: [] };
|
|
37
|
+
const answer = partitionFillomino(width, height, "easy", random) ?? Array(width * height).fill(1);
|
|
38
|
+
return { ...board, givens: [...answer], seed, level, solution: answer };
|
|
43
39
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const remaining = entries.filter(value => value === 0).length;
|
|
56
|
-
const preferred = Array.from({ length: Math.min(maxArea, remaining) }, (_, index) => index + 1);
|
|
57
|
-
const sizes = shuffled(preferred, random).sort((left, right) => right - left);
|
|
58
|
-
for (const area of sizes) {
|
|
59
|
-
for (let proposal = 0; proposal < 5; proposal += 1) {
|
|
60
|
-
if (fillominoNeighbours(board, anchor).some(cell => entries[cell] === area))
|
|
61
|
-
continue;
|
|
62
|
-
const shape = growShape(board, anchor, area, entries, random);
|
|
63
|
-
if (!shape)
|
|
64
|
-
continue;
|
|
65
|
-
for (const cell of shape)
|
|
66
|
-
entries[cell] = area;
|
|
67
|
-
if (visit())
|
|
68
|
-
return true;
|
|
69
|
-
for (const cell of shape)
|
|
70
|
-
entries[cell] = 0;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return false;
|
|
40
|
+
/** Takes givens away from a full answer while the level's way of solving still works; null when the result is not the level. */
|
|
41
|
+
function thin(width, height, solution, level, random) {
|
|
42
|
+
const givens = [...solution];
|
|
43
|
+
let calls = 0;
|
|
44
|
+
const model = () => {
|
|
45
|
+
const { csp: inner, most } = fillominoModel({ width, height, givens }, givens);
|
|
46
|
+
const csp = { ...inner, propagate: (alive, decided) => { calls += 1; return inner.propagate(alive, decided); } };
|
|
47
|
+
const start = openSlots(csp);
|
|
48
|
+
givens.forEach((value, cell) => { if (value)
|
|
49
|
+
decide(csp, start, cell, cell * most + value - 1); });
|
|
50
|
+
return { csp, start };
|
|
74
51
|
};
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return null;
|
|
95
|
-
const next = choices[Math.floor(random() * choices.length)];
|
|
96
|
-
cells.push(next);
|
|
97
|
-
members.add(next);
|
|
52
|
+
// A region with no given can be as big as the squares with none joined together, which the solver must allow for; keeping
|
|
53
|
+
// those stretches short keeps every proof short, and keeps the board fair: no vast blank area.
|
|
54
|
+
const roomy = () => fillominoMostValue({ width, height, givens }, givens) > Math.max(12, Math.max(...solution));
|
|
55
|
+
const byRules = () => { const { csp, start } = model(); return logicCsp(csp, start, 0).solved; };
|
|
56
|
+
const unique = () => { const { csp, start } = model(); const count = countCsp(csp, start, 2, 500); return !count.exhausted && count.count === 1; };
|
|
57
|
+
const bySupposing = () => { const { csp, start } = model(); return logicCsp(csp, start, 1).solved; };
|
|
58
|
+
const keepAtLeast = level === "easy" ? Math.ceil(solution.length * .5) : 0;
|
|
59
|
+
let left = givens.length;
|
|
60
|
+
const order = shuffled(givens.map((_, i) => i), random);
|
|
61
|
+
// First as many as the rules alone allow.
|
|
62
|
+
for (const cell of order) {
|
|
63
|
+
if (left <= keepAtLeast)
|
|
64
|
+
break;
|
|
65
|
+
const was = givens[cell];
|
|
66
|
+
givens[cell] = 0;
|
|
67
|
+
if (!roomy() && byRules())
|
|
68
|
+
left -= 1;
|
|
69
|
+
else
|
|
70
|
+
givens[cell] = was;
|
|
98
71
|
}
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
if (visited.has(start))
|
|
72
|
+
if (level === "easy" || level === "medium")
|
|
73
|
+
return { width, height, givens };
|
|
74
|
+
// Then, beyond the rules: hard takes the first given that leaves a board solved by supposing, extra-hard keeps going while the answer stays single.
|
|
75
|
+
let taken = 0;
|
|
76
|
+
const from = calls, budget = width * height * 60;
|
|
77
|
+
for (const cell of order) {
|
|
78
|
+
if (!givens[cell] || calls - from > budget)
|
|
107
79
|
continue;
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
region.push(cell);
|
|
115
|
-
for (const next of fillominoNeighbours(board, cell)) {
|
|
116
|
-
if (!visited.has(next) && entries[next] === value) {
|
|
117
|
-
visited.add(next);
|
|
118
|
-
pending.push(next);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
80
|
+
const was = givens[cell];
|
|
81
|
+
givens[cell] = 0;
|
|
82
|
+
if (!roomy() && unique() && (level === "extra-hard" || bySupposing())) {
|
|
83
|
+
taken += 1;
|
|
84
|
+
if (taken >= (level === "hard" ? 3 : 6))
|
|
85
|
+
break;
|
|
121
86
|
}
|
|
122
|
-
|
|
87
|
+
else
|
|
88
|
+
givens[cell] = was;
|
|
123
89
|
}
|
|
124
|
-
|
|
90
|
+
if (!taken || byRules())
|
|
91
|
+
return null;
|
|
92
|
+
return { width, height, givens };
|
|
125
93
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Csp } from "./csp.ts";
|
|
2
|
+
import type { FillominoBoard } from "./fillomino.types.ts";
|
|
3
|
+
/** The most any square can be: a region holding a given or an entry is that number, and one with none lies among the squares with neither. */
|
|
4
|
+
export declare function fillominoMostValue(board: FillominoBoard, fixed: readonly number[]): number;
|
|
5
|
+
/**
|
|
6
|
+
* Fillomino as variables: one per square, a slot for each number it can be (1 up to `most`). A finished group of
|
|
7
|
+
* equal numbers keeps its neighbours off that number; a group still short must be able to grow, to squares that can
|
|
8
|
+
* still be that number, until it is the right size, and settles on them when there is exactly room; and a square
|
|
9
|
+
* can only be a number if the squares that can still be it, joined up, make a group big enough.
|
|
10
|
+
*/
|
|
11
|
+
export declare function fillominoModel(board: FillominoBoard, fixed: readonly number[], most?: number): {
|
|
12
|
+
csp: Csp;
|
|
13
|
+
most: number;
|
|
14
|
+
};
|