@johnmorrisdotca/kazu 1.2.0 → 1.3.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 +23 -0
- package/README.md +58 -16
- package/dist/akari-entry.d.ts +1 -0
- package/dist/akari-entry.js +1 -0
- package/dist/akari.constants.d.ts +5 -0
- package/dist/akari.constants.js +5 -0
- package/dist/akari.types.d.ts +19 -0
- package/dist/akariGenerate.d.ts +8 -5
- package/dist/akariGenerate.js +88 -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/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/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/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/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { fillominoNeighbours } from "./fillominoBoard.js";
|
|
2
|
+
/** 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. */
|
|
3
|
+
export function fillominoMostValue(board, fixed) {
|
|
4
|
+
let most = 1;
|
|
5
|
+
for (const value of fixed)
|
|
6
|
+
if (value > most)
|
|
7
|
+
most = value;
|
|
8
|
+
const seen = new Uint8Array(fixed.length);
|
|
9
|
+
for (let start = 0; start < fixed.length; start += 1) {
|
|
10
|
+
if (fixed[start] || seen[start])
|
|
11
|
+
continue;
|
|
12
|
+
let size = 0;
|
|
13
|
+
const stack = [start];
|
|
14
|
+
seen[start] = 1;
|
|
15
|
+
while (stack.length) {
|
|
16
|
+
const cell = stack.pop();
|
|
17
|
+
size += 1;
|
|
18
|
+
for (const next of fillominoNeighbours(board, cell))
|
|
19
|
+
if (!fixed[next] && !seen[next]) {
|
|
20
|
+
seen[next] = 1;
|
|
21
|
+
stack.push(next);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (size > most)
|
|
25
|
+
most = size;
|
|
26
|
+
}
|
|
27
|
+
return most;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Fillomino as variables: one per square, a slot for each number it can be (1 up to `most`). A finished group of
|
|
31
|
+
* equal numbers keeps its neighbours off that number; a group still short must be able to grow, to squares that can
|
|
32
|
+
* still be that number, until it is the right size, and settles on them when there is exactly room; and a square
|
|
33
|
+
* can only be a number if the squares that can still be it, joined up, make a group big enough.
|
|
34
|
+
*/
|
|
35
|
+
export function fillominoModel(board, fixed, most = fillominoMostValue(board, fixed)) {
|
|
36
|
+
const cells = board.width * board.height, values = most;
|
|
37
|
+
const nb = new Int32Array(cells * 4).fill(-1);
|
|
38
|
+
for (let cell = 0; cell < cells; cell += 1)
|
|
39
|
+
fillominoNeighbours(board, cell).forEach((next, k) => { nb[cell * 4 + k] = next; });
|
|
40
|
+
const starts = Int32Array.from({ length: cells + 1 }, (_, i) => i * values);
|
|
41
|
+
const only = new Int32Array(cells), label = new Int32Array(cells), size = new Int32Array(cells + 1), stack = new Int32Array(cells + 1), members = new Int32Array(cells), seen = new Int32Array(cells);
|
|
42
|
+
const dirty = new Uint8Array(values + 1);
|
|
43
|
+
let stamp = 0;
|
|
44
|
+
const propagate = (alive) => {
|
|
45
|
+
let changed = true;
|
|
46
|
+
dirty.fill(1);
|
|
47
|
+
while (changed) {
|
|
48
|
+
changed = false;
|
|
49
|
+
for (let cell = 0; cell < cells; cell += 1) {
|
|
50
|
+
let left = 0, last = 0;
|
|
51
|
+
const base = cell * values;
|
|
52
|
+
for (let v = 0; v < values; v += 1)
|
|
53
|
+
if (alive[base + v]) {
|
|
54
|
+
left += 1;
|
|
55
|
+
last = v + 1;
|
|
56
|
+
}
|
|
57
|
+
if (!left)
|
|
58
|
+
return false;
|
|
59
|
+
only[cell] = left === 1 ? last : 0;
|
|
60
|
+
}
|
|
61
|
+
// Groups of squares already settled on the same number.
|
|
62
|
+
label.fill(0);
|
|
63
|
+
for (let start = 0; start < cells; start += 1) {
|
|
64
|
+
const v = only[start];
|
|
65
|
+
if (!v || label[start])
|
|
66
|
+
continue;
|
|
67
|
+
let count = 0, top = 0;
|
|
68
|
+
stack[top++] = start;
|
|
69
|
+
label[start] = 1;
|
|
70
|
+
while (top) {
|
|
71
|
+
const cell = stack[--top];
|
|
72
|
+
members[count++] = cell;
|
|
73
|
+
for (let k = 0; k < 4; k += 1) {
|
|
74
|
+
const next = nb[cell * 4 + k];
|
|
75
|
+
if (next >= 0 && only[next] === v && !label[next]) {
|
|
76
|
+
label[next] = 1;
|
|
77
|
+
stack[top++] = next;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (count > v)
|
|
82
|
+
return false;
|
|
83
|
+
if (count === v) {
|
|
84
|
+
for (let m = 0; m < count; m += 1)
|
|
85
|
+
for (let k = 0; k < 4; k += 1) {
|
|
86
|
+
const next = nb[members[m] * 4 + k];
|
|
87
|
+
if (next >= 0 && only[next] !== v && alive[next * values + v - 1]) {
|
|
88
|
+
alive[next * values + v - 1] = 0;
|
|
89
|
+
dirty[v] = 1;
|
|
90
|
+
changed = true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
// Short of its size: grow through squares that can still be v.
|
|
96
|
+
stamp += 1;
|
|
97
|
+
let reached = count, frontier = 0, lastFrontier = -1;
|
|
98
|
+
for (let m = 0; m < count; m += 1)
|
|
99
|
+
seen[members[m]] = stamp;
|
|
100
|
+
for (let at = 0; at < reached; at += 1) {
|
|
101
|
+
const cell = members[at];
|
|
102
|
+
for (let k = 0; k < 4; k += 1) {
|
|
103
|
+
const next = nb[cell * 4 + k];
|
|
104
|
+
if (next < 0 || seen[next] === stamp || !alive[next * values + v - 1])
|
|
105
|
+
continue;
|
|
106
|
+
seen[next] = stamp;
|
|
107
|
+
members[reached++] = next;
|
|
108
|
+
if (at < count) {
|
|
109
|
+
frontier += 1;
|
|
110
|
+
lastFrontier = next;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (reached < v)
|
|
115
|
+
return false;
|
|
116
|
+
if (reached === v) {
|
|
117
|
+
for (let m = 0; m < reached; m += 1) {
|
|
118
|
+
const base = members[m] * values;
|
|
119
|
+
for (let other = 0; other < values; other += 1)
|
|
120
|
+
if (other !== v - 1 && alive[base + other]) {
|
|
121
|
+
alive[base + other] = 0;
|
|
122
|
+
dirty[other + 1] = 1;
|
|
123
|
+
changed = true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (frontier === 1 && !only[lastFrontier]) {
|
|
128
|
+
const base = lastFrontier * values;
|
|
129
|
+
for (let other = 0; other < values; other += 1)
|
|
130
|
+
if (other !== v - 1 && alive[base + other]) {
|
|
131
|
+
alive[base + other] = 0;
|
|
132
|
+
dirty[other + 1] = 1;
|
|
133
|
+
changed = true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// A number needs room: the squares that can be it, joined up, must make a group at least that big.
|
|
138
|
+
for (let v = 2; v <= values; v += 1) {
|
|
139
|
+
if (!dirty[v])
|
|
140
|
+
continue;
|
|
141
|
+
dirty[v] = 0;
|
|
142
|
+
label.fill(0);
|
|
143
|
+
let components = 0;
|
|
144
|
+
const slot = v - 1;
|
|
145
|
+
for (let start = 0; start < cells; start += 1) {
|
|
146
|
+
if (label[start] || !alive[start * values + slot])
|
|
147
|
+
continue;
|
|
148
|
+
components += 1;
|
|
149
|
+
let count = 0, top = 0;
|
|
150
|
+
stack[top++] = start;
|
|
151
|
+
label[start] = components;
|
|
152
|
+
while (top) {
|
|
153
|
+
const cell = stack[--top];
|
|
154
|
+
count += 1;
|
|
155
|
+
for (let k = 0; k < 4; k += 1) {
|
|
156
|
+
const next = nb[cell * 4 + k];
|
|
157
|
+
if (next >= 0 && !label[next] && alive[next * values + slot]) {
|
|
158
|
+
label[next] = components;
|
|
159
|
+
stack[top++] = next;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
size[components] = count;
|
|
164
|
+
}
|
|
165
|
+
for (let cell = 0; cell < cells; cell += 1) {
|
|
166
|
+
if (label[cell] && size[label[cell]] < v) {
|
|
167
|
+
alive[cell * values + slot] = 0;
|
|
168
|
+
dirty[v] = 1;
|
|
169
|
+
changed = true;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return true;
|
|
175
|
+
};
|
|
176
|
+
return { csp: { starts, propagate }, most: values };
|
|
177
|
+
}
|
package/dist/fillominoMount.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { checkFillomino } from "./fillominoBoard.js";
|
|
2
|
+
import { fillominoMostValue } from "./fillominoLogic.js";
|
|
2
3
|
import { drawFillomino } from "./fillominoDraw.js";
|
|
3
4
|
import { decodeFillomino, encodeFillomino, hintFillomino, newFillomino, restartFillomino, setFillominoCell, undoFillomino } from "./fillominoGame.js";
|
|
4
5
|
import { FILLOMINO_PLAY_STYLE } from "./fillominoStyle.js";
|
|
@@ -98,7 +99,9 @@ export function mountFillomino(host, initial) {
|
|
|
98
99
|
const number = document.createElement("select");
|
|
99
100
|
number.setAttribute("aria-label", words.selectNumber);
|
|
100
101
|
number.dataset.number = "true";
|
|
101
|
-
|
|
102
|
+
// Only numbers an answer can hold: more than the biggest given only fits among squares that have no given.
|
|
103
|
+
const most = Math.max(fillominoMostValue(game.board, game.board.givens), ...game.entries);
|
|
104
|
+
for (let value = 1; value <= most; value += 1) {
|
|
102
105
|
const option = document.createElement("option");
|
|
103
106
|
option.value = String(value);
|
|
104
107
|
option.textContent = String(value);
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { FillominoBoard, FillominoRating } from "./fillomino.types.ts";
|
|
2
|
+
/** Rates a board with one answer by solving it as a person would: with the rules, then by supposing one number at a time. */
|
|
3
|
+
export declare function rateFillomino(board: FillominoBoard): FillominoRating;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { isFillominoBoard } from "./fillominoBoard.js";
|
|
2
|
+
import { fillominoModel } from "./fillominoLogic.js";
|
|
3
|
+
import { decide, logicCsp, openSlots, solveCsp } from "./csp.js";
|
|
4
|
+
/** Rates a board with one answer by solving it as a person would: with the rules, then by supposing one number at a time. */
|
|
5
|
+
export function rateFillomino(board) {
|
|
6
|
+
if (!isFillominoBoard(board))
|
|
7
|
+
throw new RangeError("Invalid Fillomino board");
|
|
8
|
+
const { csp, most } = fillominoModel(board, board.givens);
|
|
9
|
+
const start = openSlots(csp);
|
|
10
|
+
board.givens.forEach((value, cell) => { if (value)
|
|
11
|
+
decide(csp, start, cell, cell * most + value - 1); });
|
|
12
|
+
const proof = solveCsp(csp, start, 2, 200000);
|
|
13
|
+
if (proof.count !== 1 || proof.exhausted)
|
|
14
|
+
throw new RangeError("Fillomino rating needs a board with one answer");
|
|
15
|
+
const plain = logicCsp(csp, start, 0);
|
|
16
|
+
const probing = plain.solved ? plain : logicCsp(csp, start, 1);
|
|
17
|
+
const answer = Array.from({ length: board.givens.length }, (_, cell) => { for (let v = 0; v < most; v += 1)
|
|
18
|
+
if (proof.solution[cell * most + v])
|
|
19
|
+
return v + 1; return 0; });
|
|
20
|
+
const regions = new Map();
|
|
21
|
+
const label = Array(answer.length).fill(-1);
|
|
22
|
+
let count = 0, largest = 0, unnamed = 0;
|
|
23
|
+
for (let first = 0; first < answer.length; first += 1) {
|
|
24
|
+
if (label[first] >= 0)
|
|
25
|
+
continue;
|
|
26
|
+
const members = [first];
|
|
27
|
+
label[first] = count;
|
|
28
|
+
for (let at = 0; at < members.length; at += 1) {
|
|
29
|
+
const cell = members[at], x = cell % board.width, y = Math.floor(cell / board.width);
|
|
30
|
+
for (const [nx, ny] of [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]) {
|
|
31
|
+
if (nx < 0 || ny < 0 || nx >= board.width || ny >= board.height)
|
|
32
|
+
continue;
|
|
33
|
+
const next = ny * board.width + nx;
|
|
34
|
+
if (label[next] < 0 && answer[next] === answer[first]) {
|
|
35
|
+
label[next] = count;
|
|
36
|
+
members.push(next);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
largest = Math.max(largest, members.length);
|
|
41
|
+
if (!members.some(cell => board.givens[cell]))
|
|
42
|
+
unnamed += 1;
|
|
43
|
+
regions.set(String(count), members.length);
|
|
44
|
+
count += 1;
|
|
45
|
+
}
|
|
46
|
+
const givens = board.givens.filter(Boolean).length;
|
|
47
|
+
return {
|
|
48
|
+
depth: plain.solved ? 0 : probing.solved ? 1 : 2,
|
|
49
|
+
probes: plain.solved ? 0 : probing.probes,
|
|
50
|
+
givens,
|
|
51
|
+
givenShare: givens / board.givens.length,
|
|
52
|
+
regions: count,
|
|
53
|
+
unnamed,
|
|
54
|
+
largest,
|
|
55
|
+
meanRegion: board.givens.length / count,
|
|
56
|
+
};
|
|
57
|
+
}
|