lettras 0.1.0 → 0.2.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/README.md +13 -1
- package/bin/lettras.js +15 -4
- package/engine/index.js +3 -3
- package/engine/lettras_engine.d.ts +21 -0
- package/engine/lettras_engine.js +32 -0
- package/package.json +1 -1
- package/src/index.d.ts +14 -2
- package/src/index.js +28 -2
package/README.md
CHANGED
|
@@ -17,8 +17,16 @@ puzzle.placements; // where each word is hidden: start (r, c), step (dr, dc), l
|
|
|
17
17
|
puzzle.unplaced; // words that did not fit
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
+
```js
|
|
21
|
+
import { generate, fill, render } from 'lettras';
|
|
22
|
+
|
|
23
|
+
const puzzle = generate({ words: ['gato', 'perro', 'piña'], rows: 9, cols: 12, position: 'mixed', seed: 8 });
|
|
24
|
+
const done = fill(puzzle, { lang: 'es', accents: true }); // random letters in the empty cells
|
|
25
|
+
console.log(done.grid.map((row) => row.join(' ')).join('\n'));
|
|
26
|
+
```
|
|
27
|
+
|
|
20
28
|
```bash
|
|
21
|
-
npx lettras --words gato,perro,piña --rows 9 --cols 12 --position mixed
|
|
29
|
+
npx lettras --words gato,perro,piña --rows 9 --cols 12 --position mixed --random --accents on
|
|
22
30
|
```
|
|
23
31
|
|
|
24
32
|
## Options
|
|
@@ -34,6 +42,10 @@ npx lettras --words gato,perro,piña --rows 9 --cols 12 --position mixed
|
|
|
34
42
|
| `classicMode` | Strip accents in the grid. |
|
|
35
43
|
| `fill` | Empty-cell character, default `-`. |
|
|
36
44
|
|
|
45
|
+
`generate` leaves empty cells as `-`. **`fill(puzzleOrGrid, { lang, accents, seed })`** completes them: letters follow the
|
|
46
|
+
language's letter frequency, `accents: false` limits them to A-Z, and without a `seed` every call is different. Passing the
|
|
47
|
+
puzzle (instead of a bare matrix) also protects its words from accidental extra copies. CLI: `--random` and `--accents on|off`.
|
|
48
|
+
|
|
37
49
|
CLI flags mirror these: `--words a,b,c --rows N --cols N --position P --difficulty N --clustering N --seed N --lang xx
|
|
38
50
|
--classic --fill C`, plus `--json`, `--solution` and `--stdin`.
|
|
39
51
|
|
package/bin/lettras.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// lettras --words sol,luna,mar --rows 8 --cols 8 [--position horizontal|vertical|mixed] [--seed 7]
|
|
3
|
-
// [--difficulty 1-4] [--clustering 0-1] [--lang es] [--classic] [--fill -] [--
|
|
3
|
+
// [--difficulty 1-4] [--clustering 0-1] [--lang es] [--classic] [--fill -] [--random] [--accents on|off]
|
|
4
|
+
// [--json] [--solution]
|
|
4
5
|
// or: echo '{"words":["sol"],"rows":6,"cols":6}' | lettras --stdin
|
|
5
6
|
import { readFileSync } from 'node:fs';
|
|
6
7
|
import { generate, renderPuzzle } from '../engine/index.js';
|
|
8
|
+
import { fill } from '../src/index.js';
|
|
7
9
|
|
|
8
10
|
const HELP = `lettras: word-search generator
|
|
9
11
|
|
|
@@ -18,12 +20,14 @@ const HELP = `lettras: word-search generator
|
|
|
18
20
|
--lang xx es en pt fr de it
|
|
19
21
|
--classic strip accents in the grid
|
|
20
22
|
--fill C empty-cell character (default -)
|
|
23
|
+
--random fill the empty cells with random letters
|
|
24
|
+
--accents on|off with --random: use the language's accented letters (default on; off with --classic)
|
|
21
25
|
--json print the JSON result
|
|
22
26
|
--solution show only the hidden words
|
|
23
27
|
--stdin read the JSON input from stdin`;
|
|
24
28
|
|
|
25
29
|
function parse(argv) {
|
|
26
|
-
const flags = new Set(['classic', 'json', 'solution', 'stdin', 'help']);
|
|
30
|
+
const flags = new Set(['classic', 'json', 'solution', 'stdin', 'help', 'random']);
|
|
27
31
|
const opts = {};
|
|
28
32
|
for (let i = 0; i < argv.length; i++) {
|
|
29
33
|
const a = argv[i];
|
|
@@ -57,9 +61,16 @@ function main() {
|
|
|
57
61
|
if (o.classic) input.classicMode = true;
|
|
58
62
|
if (o.fill) input.fill = o.fill;
|
|
59
63
|
}
|
|
60
|
-
|
|
64
|
+
let outJson = generate(JSON.stringify(input));
|
|
65
|
+
let out = JSON.parse(outJson);
|
|
66
|
+
if (o.random) {
|
|
67
|
+
if (o.accents !== undefined && !['on', 'off'].includes(o.accents)) throw new Error('--accents must be on or off');
|
|
68
|
+
const accents = o.accents ? o.accents === 'on' : !input.classicMode;
|
|
69
|
+
const filled = fill(out, { lang: input.lang, accents, seed: input.seed });
|
|
70
|
+
out = { ...out, grid: filled.grid };
|
|
71
|
+
outJson = JSON.stringify(out);
|
|
72
|
+
}
|
|
61
73
|
if (o.json) return console.log(outJson);
|
|
62
|
-
const out = JSON.parse(outJson);
|
|
63
74
|
if (o.solution) {
|
|
64
75
|
const hidden = new Set(out.placements.flatMap((p) => Array.from({ length: p.length }, (_, i) => `${p.r + p.dr * i},${p.c + p.dc * i}`)));
|
|
65
76
|
return console.log(out.grid.map((row, r) => row.map((ch, c) => (hidden.has(`${r},${c}`) ? ch : '·')).join(' ')).join('\n'));
|