Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2910x 2910x 2910x 2910x 9845x 9845x 4234x 57x 9845x 9845x 9845x 2910x 2910x 1x 408x 408x 408x 1x 1x 1x 1x 1173x 1173x 1173x 1173x 1173x 1173x 1169x 1169x 1169x 1169x 1169x 1173x 2988x 2988x 623x 78x 78x 2988x 624x 624x 1173x 396x 396x 396x 1173x 737x 408x 408x 1535x 1535x 405x 737x 393x 393x 1173x 1x 1x 1x 1x 34x 34x 34x 34x 33x 33x 34x 70x 1x 1x 70x 33x 33x 34x 70x 1x 1x 70x 33x 33x 33x 33x 33x 34x 70x 67x 39x 67x 166x 2x 2x 2x 166x 39x 67x 70x 70x 70x 70x 67x 39x 39x 39x 39x 67x 25x 25x 25x 25x 2x 2x 2x 2x 2x 2x 67x 14x 14x 14x 14x 2x 2x 2x 2x 2x 2x 14x 67x 67x 70x 70x 70x 70x 67x 28x 28x 28x 28x 28x 28x 28x 28x 67x 127x 127x 28x 28x 28x 67x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 28x 28x 28x 67x 19x 19x 19x 19x 19x 19x 38x 187x 1x 1x 1x 187x 38x 38x 19x 18x 36x 180x 180x 36x 36x 18x 19x 19x 1x 1x 19x 67x 67x 70x 70x 34x 8x 8x 8x 18x 18x 13x 8x 13x 7x 7x 7x 13x 13x 18x 18x 18x 18x 8x 15x 15x 14x 7x 7x 7x 14x 15x 8x 8x 18x 3x 3x 18x 8x 33x 33x 34x 1x 1x 1x 117x 117x 117x 1x 192x 192x 192x 1x 332x 332x 332x 5642x 5642x 5642x 332x 332x 1x 117x 117x 117x 117x 117x 1x 215x 215x 215x 445x 445x 445x 445x 445x 1465x 5759x 5759x 1465x 445x 215x 215x 1x 192x 192x 192x 192x 603x 603x 192x 192x 1x 388x 388x 388x 388x 545x 305x 305x 545x 195x 98x 98x 98x 545x 110x 62x 62x 62x 110x 545x 232x 388x 1x 232x 232x 232x 232x 232x 211x 129x 129x 129x 129x 129x 211x 417x 417x 211x 99x 99x 99x 99x 99x 211x 184x 645x 645x 153x 211x 132x 485x 485x 123x 59x 192x 232x 1x 31x 31x 7x 17x 17x 7x 7x 7x 31x 17x 17x 43x 11x 43x 24x 10x 10x 10x 10x 24x 11x 17x 7x 31x 1x 117x 117x 117x 117x 117x 117x 117x 117x 332x 332x 117x 117x 117x 215x 215x 215x 215x 332x 1161x 1161x 1161x 1161x 1161x 192x 192x 192x 192x 192x 192x 192x 23x 23x 23x 332x 117x 117x 117x 1x 1x 20x 20x 20x 1x 1x 19x 19x 19x 19x 19x 19x 19x 20x 43x 43x 43x 43x 43x 37x 43x 6x 6x 43x 19x 19x 20x 3x 3x 3x 3x 3x 9x 9x 13x 9x 9x 9x 9x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 19x 20x 2x 2x 17x 17x 17x 17x 17x 20x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 244x 244x 244x 244x 244x 117x 117x 117x 84x 84x 84x 84x 84x 84x 84x 84x 84x 117x 117x 117x 117x 20x 36x 36x 17x 17x 17x 17x 20x | /**
* gridgen.js - Standalone grid generation module
*
* Exports: make_grids, isValidPlacement, validateGrid
* No dependencies.
*/
const MAX_CANDIDATES = 5;
const MAX_DROP_RETRIES = 5;
// --- Helpers ---
function getCells(placement) {
const { word, row, col, direction, reversed } = placement;
const cells = [];
for (let i = 0; i < word.length; i++) {
let r = row, c = col;
if (direction === 'horizontal') c += i;
else if (direction === 'vertical') r += i;
else if (direction === 'diagonal-dr') { r += i; c += i; }
const letter = reversed ? word[word.length - 1 - i] : word[i];
cells.push({ r, c, letter });
}
return cells;
}
function cellSet(placement) {
return getCells(placement).map(({ r, c }) => `${r},${c}`);
}
// --- isValidPlacement ---
export function isValidPlacement(placement, grid, existingPlacements, width, height, all_cross) {
const { word, row, col, direction } = placement;
// Bounds check
if (direction === 'horizontal' && col + word.length > width) return false;
if (direction === 'vertical' && row + word.length > height) return false;
if (direction === 'diagonal-dr' && (col + word.length > width || row + word.length > height)) return false;
const newCells = getCells(placement);
let intersects = false;
// Check each cell against the grid
for (const { r, c, letter } of newCells) {
const existing = grid[r][c];
if (existing !== '') {
if (existing !== letter) return false;
intersects = true;
}
}
// all_cross: must intersect if there are existing placements
if (all_cross && existingPlacements.length > 0 && !intersects) return false;
// Check for parallel (same-direction) overlap
const newCellKeys = new Set(newCells.map(({ r, c }) => `${r},${c}`));
for (const ep of existingPlacements) {
if (ep.direction === direction) {
const epKeys = cellSet(ep);
for (const key of epKeys) {
if (newCellKeys.has(key)) return false;
}
}
}
return true;
}
// --- validateGrid ---
export function validateGrid(placements, options) {
const { all_cross, allow_diagonals, allow_reverse } = options;
const issues = [];
if (placements.length === 0) return { valid: true, issues: [] };
// Check diagonal constraint
for (const p of placements) {
if (!allow_diagonals && p.direction.startsWith('diagonal')) {
issues.push(`${p.word} uses diagonal direction but diagonals are not allowed`);
}
}
// Check reverse constraint
for (const p of placements) {
if (!allow_reverse && p.reversed) {
issues.push(`${p.word} is reversed but reverse is not allowed`);
}
}
// Compute cell positions for each placement
const placementCells = placements.map(p => getCells(p));
// Parallel intersection check (same-direction words sharing cells)
for (let i = 0; i < placements.length; i++) {
for (let j = i + 1; j < placements.length; j++) {
if (placements[i].direction !== placements[j].direction) continue;
const keysI = new Set(placementCells[i].map(({ r, c }) => `${r},${c}`));
for (const { r, c } of placementCells[j]) {
if (keysI.has(`${r},${c}`)) {
issues.push(`Parallel intersection between ${placements[i].word} and ${placements[j].word}`);
break;
}
}
}
}
// Perpendicular adjacency check (parallel words touching on adjacent rows/cols)
for (let i = 0; i < placements.length; i++) {
for (let j = i + 1; j < placements.length; j++) {
if (placements[i].direction !== placements[j].direction) continue;
const cellsI = placementCells[i];
const cellsJ = placementCells[j];
const dir = placements[i].direction;
if (dir === 'horizontal') {
// Check if on consecutive rows with overlapping columns
const rowI = cellsI[0].r;
const rowJ = cellsJ[0].r;
if (Math.abs(rowI - rowJ) === 1) {
const colsI = new Set(cellsI.map(c => c.c));
const overlap = cellsJ.some(c => colsI.has(c.c));
if (overlap) {
issues.push(`Invalid perpendicular adjacency between ${placements[i].word} and ${placements[j].word}`);
}
}
} else if (dir === 'vertical') {
// Check if on consecutive columns with overlapping rows
const colI = cellsI[0].c;
const colJ = cellsJ[0].c;
if (Math.abs(colI - colJ) === 1) {
const rowsI = new Set(cellsI.map(c => c.r));
const overlap = cellsJ.some(c => rowsI.has(c.r));
if (overlap) {
issues.push(`Invalid perpendicular adjacency between ${placements[i].word} and ${placements[j].word}`);
}
}
}
}
}
// Cross-direction end-abutment check
for (let i = 0; i < placements.length; i++) {
for (let j = i + 1; j < placements.length; j++) {
if (placements[i].direction === placements[j].direction) continue;
const cellsI = placementCells[i];
const cellsJ = placementCells[j];
const keysI = new Set(cellsI.map(({ r, c }) => `${r},${c}`));
const keysJ = new Set(cellsJ.map(({ r, c }) => `${r},${c}`));
// Find shared cells
const shared = [];
for (const key of keysI) {
if (keysJ.has(key)) shared.push(key);
}
// Check if any shared cell is at the start or end of either word
// That would be an abutment rather than a proper crossing
if (shared.length > 0) {
for (const key of shared) {
const idxI = cellsI.findIndex(({ r, c }) => `${r},${c}` === key);
const idxJ = cellsJ.findIndex(({ r, c }) => `${r},${c}` === key);
// A proper crossing: not at endpoint of either word's direction axis
// For horizontal word: crossing must not be at start/end if the other word is ending there
// For vertical word ending exactly one cell before the horizontal word starts
const iIsEnd = idxI === 0 || idxI === cellsI.length - 1;
const jIsEnd = idxJ === 0 || idxJ === cellsJ.length - 1;
// Both at endpoints means they just touch at corners - that's actually fine for T-shapes
// The real issue is when they share a cell but letters don't match - handled elsewhere
}
}
// Check for abutment: one word ends adjacent to a cell of the other word
// without any proper crossing between the two words
if (shared.length === 0) {
// No crossing - check if they abut (one cell apart along the non-shared axis)
const endCellsI = [cellsI[0], cellsI[cellsI.length - 1]];
const endCellsJ = [cellsJ[0], cellsJ[cellsJ.length - 1]];
let abuts = false;
for (const ei of endCellsI) {
for (const cj of cellsJ) {
if (Math.abs(ei.r - cj.r) + Math.abs(ei.c - cj.c) === 1) {
abuts = true;
break;
}
}
if (abuts) break;
}
if (!abuts) {
for (const ej of endCellsJ) {
for (const ci of cellsI) {
if (Math.abs(ej.r - ci.r) + Math.abs(ej.c - ci.c) === 1) {
abuts = true;
break;
}
}
if (abuts) break;
}
}
if (abuts) {
issues.push(`Cross-direction end-abutment between ${placements[i].word} and ${placements[j].word}`);
}
}
}
}
// all_cross constraint: all words must be connected
if (all_cross && placements.length >= 2) {
// Build adjacency via shared cells between different-direction words
const adj = placements.map(() => new Set());
for (let i = 0; i < placements.length; i++) {
const keysI = new Set(placementCells[i].map(({ r, c }) => `${r},${c}`));
for (let j = i + 1; j < placements.length; j++) {
if (placements[i].direction === placements[j].direction) continue;
const intersects = placementCells[j].some(({ r, c }) => keysI.has(`${r},${c}`));
if (intersects) {
adj[i].add(j);
adj[j].add(i);
}
}
}
// BFS to find connected component from word 0
const visited = new Set([0]);
const queue = [0];
while (queue.length > 0) {
const cur = queue.shift();
for (const neighbor of adj[cur]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
for (let i = 0; i < placements.length; i++) {
if (!visited.has(i)) {
issues.push(`${placements[i].word} does not intersect any other word`);
}
}
}
return { valid: issues.length === 0, issues };
}
// --- make_grids ---
function createEmptyGrid(width, height) {
return Array.from({ length: height }, () => Array(width).fill(''));
}
function cloneGrid(grid) {
return grid.map(row => [...row]);
}
function shuffle(arr) {
const a = [...arr];
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function getDirections(allow_diagonals) {
const dirs = ['horizontal', 'vertical'];
if (allow_diagonals) dirs.push('diagonal-dr');
return dirs;
}
function generateAllPositions(word, width, height, directions) {
const positions = [];
for (const direction of directions) {
const maxCol = direction === 'vertical' ? width : width - word.length + 1;
const maxRow = direction === 'horizontal' ? height : height - word.length + 1;
const colLimit = direction === 'horizontal' || direction === 'diagonal-dr' ? width - word.length + 1 : width;
const rowLimit = direction === 'vertical' || direction === 'diagonal-dr' ? height - word.length + 1 : height;
for (let r = 0; r < rowLimit; r++) {
for (let c = 0; c < colLimit; c++) {
positions.push({ word, row: r, col: c, direction });
}
}
}
return positions;
}
function placeWord(grid, placement) {
const cells = getCells(placement);
const newGrid = cloneGrid(grid);
for (const { r, c, letter } of cells) {
newGrid[r][c] = letter;
}
return newGrid;
}
function checkPerpendicularAdjacency(placement, existingPlacements, grid, width, height) {
const newCells = getCells(placement);
for (const ep of existingPlacements) {
if (ep.direction !== placement.direction) continue;
const epCells = getCells(ep);
if (placement.direction === 'horizontal') {
if (Math.abs(newCells[0].r - epCells[0].r) === 1) {
const epCols = new Set(epCells.map(c => c.c));
if (newCells.some(c => epCols.has(c.c))) return false;
}
} else if (placement.direction === 'vertical') {
if (Math.abs(newCells[0].c - epCells[0].c) === 1) {
const epRows = new Set(epCells.map(c => c.r));
if (newCells.some(c => epRows.has(c.r))) return false;
}
}
}
return true;
}
function checkEndAbutment(placement, existingPlacements) {
const newCells = getCells(placement);
const newKeys = new Set(newCells.map(({ r, c }) => `${r},${c}`));
for (const ep of existingPlacements) {
if (ep.direction === placement.direction) continue;
const epCells = getCells(ep);
const epKeys = new Set(epCells.map(({ r, c }) => `${r},${c}`));
// Check if they share any cells (proper crossing)
let hasCrossing = false;
for (const key of newKeys) {
if (epKeys.has(key)) { hasCrossing = true; break; }
}
if (hasCrossing) continue;
// Check for abutment
const newEnds = [newCells[0], newCells[newCells.length - 1]];
const epEnds = [epCells[0], epCells[epCells.length - 1]];
for (const ne of newEnds) {
for (const ec of epCells) {
if (Math.abs(ne.r - ec.r) + Math.abs(ne.c - ec.c) === 1) return false;
}
}
for (const ee of epEnds) {
for (const nc of newCells) {
if (Math.abs(ee.r - nc.r) + Math.abs(ee.c - nc.c) === 1) return false;
}
}
}
return true;
}
function areAllConnected(placements) {
if (placements.length <= 1) return true;
const cellMaps = placements.map(p => {
const keys = new Set(getCells(p).map(({ r, c }) => `${r},${c}`));
return keys;
});
const visited = new Set([0]);
const queue = [0];
while (queue.length > 0) {
const cur = queue.shift();
for (let j = 0; j < placements.length; j++) {
if (visited.has(j)) continue;
// Check if placements cur and j share any cell
for (const key of cellMaps[cur]) {
if (cellMaps[j].has(key)) {
visited.add(j);
queue.push(j);
break;
}
}
}
}
return visited.size === placements.length;
}
function tryPlaceWords(words, width, height, all_cross, allow_diagonals, deadline) {
const directions = getDirections(allow_diagonals);
const grid = createEmptyGrid(width, height);
const placements = [];
const usedWords = [];
const shuffledWords = shuffle(words);
function backtrack(grid, wordIdx) {
if (Date.now() > deadline) return null;
if (wordIdx >= shuffledWords.length) {
if (all_cross && !areAllConnected(placements)) return null;
return { grid, placements: [...placements] };
}
const word = shuffledWords[wordIdx];
const positions = shuffle(generateAllPositions(word, width, height, directions));
for (const pos of positions) {
if (Date.now() > deadline) return null;
if (!isValidPlacement(pos, grid, placements, width, height, all_cross)) continue;
if (!checkPerpendicularAdjacency(pos, placements, grid, width, height)) continue;
if (!checkEndAbutment(pos, placements)) continue;
const newGrid = placeWord(grid, pos);
placements.push(pos);
usedWords.push(word);
const result = backtrack(newGrid, wordIdx + 1);
if (result) return result;
placements.pop();
usedWords.pop();
}
// Skip this word and continue
return backtrack(grid, wordIdx + 1);
}
return backtrack(grid, 0);
}
export function make_grids(words, width, height, timeout, all_cross, allow_diagonals, allow_reverse, progressCallback) {
const info = { message: '', lost_words: [], used_words: [] };
if (words.length === 0) {
return { info, payload: [] };
}
// Pre-filter words that are too long
const maxHorizontal = width;
const maxVertical = height;
const maxDiagonal = Math.min(width, height);
const fittingWords = [];
for (const word of words) {
let fits = false;
if (word.length <= maxHorizontal) fits = true;
if (word.length <= maxVertical) fits = true;
if (allow_diagonals && word.length <= maxDiagonal) fits = true;
if (fits) {
fittingWords.push(word);
} else {
info.lost_words.push(word);
}
}
// When all_cross=true, filter words that share no letters with any other word
if (all_cross && fittingWords.length > 1) {
let changed = true;
while (changed && fittingWords.length > 1) {
changed = false;
const toRemove = [];
for (let i = 0; i < fittingWords.length; i++) {
const wordLetters = new Set(fittingWords[i]);
const canCross = fittingWords.some((w, j) => {
if (i === j) return false;
return [...w].some(ch => wordLetters.has(ch));
});
if (!canCross) toRemove.push(i);
}
if (toRemove.length > 0) {
changed = true;
// If all remaining words are isolated, keep the first one
if (toRemove.length === fittingWords.length) {
for (let k = toRemove.length - 1; k >= 1; k--) {
info.lost_words.push(fittingWords[toRemove[k]]);
fittingWords.splice(toRemove[k], 1);
}
} else {
for (let k = toRemove.length - 1; k >= 0; k--) {
info.lost_words.push(fittingWords[toRemove[k]]);
fittingWords.splice(toRemove[k], 1);
}
}
}
}
}
if (fittingWords.length === 0) {
return { info, payload: [] };
}
const deadline = Date.now() + timeout;
const candidates = [];
const allUsedWords = new Set();
for (let attempt = 0; attempt < MAX_CANDIDATES * 3 && candidates.length < MAX_CANDIDATES; attempt++) {
if (Date.now() > deadline) break;
let wordsToTry = [...fittingWords];
let result = null;
for (let drop = 0; drop <= MAX_DROP_RETRIES; drop++) {
if (Date.now() > deadline) break;
result = tryPlaceWords(wordsToTry, width, height, all_cross, allow_diagonals, deadline);
if (result && result.placements.length > 0) break;
// Drop last word and retry
if (wordsToTry.length > 1) {
wordsToTry = wordsToTry.slice(0, -1);
} else {
break;
}
}
if (result && result.placements.length > 0) {
const candidateUsedWords = result.placements.map(p => p.word);
const candidateUnusedWords = fittingWords.filter(w => !candidateUsedWords.includes(w));
// Check for duplicate candidate (same set of placements)
const sig = result.placements
.map(p => `${p.word}:${p.row},${p.col},${p.direction}`)
.sort()
.join('|');
const isDuplicate = candidates.some(c => {
const cSig = c.placements
.map(p => `${p.word}:${p.row},${p.col},${p.direction}`)
.sort()
.join('|');
return cSig === sig;
});
if (!isDuplicate) {
candidates.push({
grid: result.grid,
placements: result.placements,
used_words: candidateUsedWords,
unused_words: candidateUnusedWords
});
candidateUsedWords.forEach(w => allUsedWords.add(w));
if (progressCallback) progressCallback(candidates.length, MAX_CANDIDATES);
}
}
}
// Words that were never used across any candidate go to lost_words
for (const w of fittingWords) {
if (!allUsedWords.has(w)) {
if (!info.lost_words.includes(w)) {
info.lost_words.push(w);
}
}
}
info.used_words = [...allUsedWords];
return { info, payload: candidates };
}
|