Skip to content

Crossword Generator

Generates crosswords with automatic backtracking. The DB stores only words and clues; the engine builds the grid and assigns clue numbers.

Usage

const result = engine.generateCrossword(
[
{ answer: 'CAT', clue: { type: 'TEXT', value: 'A small animal' } },
{ answer: 'ACT', clue: { type: 'TEXT', value: 'To do something' } },
{ answer: 'DOG', clue: { type: 'TEXT', value: 'A loyal pet' } },
],
10, // gridSize
);

Response

{
"grid": [
["", "", "C", "", ""],
["", "", "A", "", ""],
["C", "A", "T", "", ""],
["D", "O", "G", "", ""]
],
"placements": [
{
"word": "CAT",
"clue": { "type": "TEXT", "value": "A small animal" },
"row": 2, "col": 0,
"direction": "across",
"number": 1
},
{
"word": "ACT",
"clue": { "type": "TEXT", "value": "To do something" },
"row": 0, "col": 2,
"direction": "down",
"number": 2
}
],
"size": 10
}

The number field is the clue number used as a key in answer when evaluating.

Algorithm

  1. Sorts words by length (longest first).
  2. Places the first word horizontally near the center.
  3. For each remaining word, finds valid intersections with backtracking (up to 50 attempts).
  4. Validates no perpendicular adjacent letter conflicts.
  5. Falls back to linear mode (no intersections) if backtracking fails.
  6. Assigns clue numbers left-to-right, top-to-bottom.

Connection with Evaluation

The number in each placement corresponds to the key in answer when evaluating:

// Generator: placement.number = 1 for "CAT"
// Evaluation: answer = "{\"1\":\"cat\"}"

See Crossword - evaluation.