@scrabble-solver/solver 2.8.3 → 2.8.5
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/build/PatternsFiller.js +2 -6
- package/build/PatternsGenerator.js +13 -9
- package/build/Solver.js +3 -4
- package/package.json +8 -8
- package/src/PatternsFiller.ts +2 -8
- package/src/PatternsGenerator.ts +17 -16
- package/src/Solver.ts +4 -8
package/build/PatternsFiller.js
CHANGED
|
@@ -16,7 +16,7 @@ class PatternsFiller {
|
|
|
16
16
|
const tilesPermutations = this.generateBlankTilesPermutations(tiles);
|
|
17
17
|
for (let index = 0; index < tilesPermutations.length; ++index) {
|
|
18
18
|
const tilesPermutation = tilesPermutations[index];
|
|
19
|
-
this.fillPattern(pattern,
|
|
19
|
+
this.fillPattern(pattern, pattern.toString(), tilesPermutation, onPatternFound);
|
|
20
20
|
}
|
|
21
21
|
return patterns;
|
|
22
22
|
}
|
|
@@ -51,11 +51,7 @@ class PatternsFiller {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
canAddPattern(pattern, word) {
|
|
54
|
-
return
|
|
55
|
-
pattern
|
|
56
|
-
.getCollisions()
|
|
57
|
-
.map(String)
|
|
58
|
-
.every((collision) => this.trie.has(collision)));
|
|
54
|
+
return this.trie.has(word) && pattern.getCollisions().every((collision) => this.trie.has(collision.toString()));
|
|
59
55
|
}
|
|
60
56
|
generateBlankTilesPermutations(tiles) {
|
|
61
57
|
const { alphabet } = this.config;
|
|
@@ -35,16 +35,20 @@ class PatternsGenerator {
|
|
|
35
35
|
.map((_, index) => getNthVector(index));
|
|
36
36
|
}
|
|
37
37
|
generateCellsPatterns({ board, cells, PatternModel, }) {
|
|
38
|
-
return this.generateStartIndices(cells).
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
38
|
+
return this.generateStartIndices(cells).flatMap((startIndex) => {
|
|
39
|
+
const endIndices = this.generateEndIndices(cells, startIndex);
|
|
40
|
+
const patterns = [];
|
|
41
|
+
for (const endIndex of endIndices) {
|
|
42
|
+
const pattern = new PatternModel({
|
|
43
|
+
board,
|
|
44
|
+
cells: cells.slice(startIndex, endIndex + 1),
|
|
45
|
+
});
|
|
46
|
+
if (pattern.canBePlaced(this.config)) {
|
|
47
|
+
patterns.push(pattern);
|
|
48
|
+
}
|
|
45
49
|
}
|
|
46
|
-
return
|
|
47
|
-
}
|
|
50
|
+
return patterns;
|
|
51
|
+
});
|
|
48
52
|
}
|
|
49
53
|
generateStartIndices(cells) {
|
|
50
54
|
return Array(cells.length - 1)
|
package/build/Solver.js
CHANGED
|
@@ -15,10 +15,9 @@ class Solver {
|
|
|
15
15
|
this.scoresCalculator = new ScoresCalculator_1.default(config);
|
|
16
16
|
}
|
|
17
17
|
solve(board, tiles) {
|
|
18
|
-
const patterns = this.patternsGenerator
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const uniquePatterns = (0, uniqBy_1.default)(patterns, (pattern) => JSON.stringify(pattern.toJson()));
|
|
18
|
+
const patterns = this.patternsGenerator.generate(board);
|
|
19
|
+
const filledPatterns = patterns.flatMap((pattern) => this.patternsFiller.fill(pattern, tiles));
|
|
20
|
+
const uniquePatterns = (0, uniqBy_1.default)(filledPatterns, (pattern) => JSON.stringify(pattern.toJson()));
|
|
22
21
|
const results = uniquePatterns.map((pattern, index) => new types_1.Result({
|
|
23
22
|
cells: pattern.cells,
|
|
24
23
|
id: index,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scrabble-solver/solver",
|
|
3
|
-
"version": "2.8.
|
|
3
|
+
"version": "2.8.5",
|
|
4
4
|
"description": "Scrabble Solver 2 - Solver",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -24,15 +24,15 @@
|
|
|
24
24
|
"clean:force": "npm run clean && rimraf package-lock.json"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@kamilmielnik/trie": "^
|
|
28
|
-
"@scrabble-solver/types": "^2.8.
|
|
27
|
+
"@kamilmielnik/trie": "^2.0.0",
|
|
28
|
+
"@scrabble-solver/types": "^2.8.5",
|
|
29
29
|
"lodash": "^4.17.21"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@scrabble-solver/configs": "^2.8.
|
|
33
|
-
"@scrabble-solver/constants": "^2.8.
|
|
34
|
-
"@scrabble-solver/dictionaries": "^2.8.
|
|
35
|
-
"@types/lodash": "^4.14.
|
|
32
|
+
"@scrabble-solver/configs": "^2.8.5",
|
|
33
|
+
"@scrabble-solver/constants": "^2.8.5",
|
|
34
|
+
"@scrabble-solver/dictionaries": "^2.8.5",
|
|
35
|
+
"@types/lodash": "^4.14.186"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "79ba3664458e7dc8d805599a3cf3cd1faba86309"
|
|
38
38
|
}
|
package/src/PatternsFiller.ts
CHANGED
|
@@ -24,7 +24,7 @@ class PatternsFiller {
|
|
|
24
24
|
|
|
25
25
|
for (let index = 0; index < tilesPermutations.length; ++index) {
|
|
26
26
|
const tilesPermutation = tilesPermutations[index];
|
|
27
|
-
this.fillPattern(pattern,
|
|
27
|
+
this.fillPattern(pattern, pattern.toString(), tilesPermutation, onPatternFound);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
return patterns;
|
|
@@ -66,13 +66,7 @@ class PatternsFiller {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
public canAddPattern(pattern: Pattern, word: string): boolean {
|
|
69
|
-
return (
|
|
70
|
-
this.trie.has(word) &&
|
|
71
|
-
pattern
|
|
72
|
-
.getCollisions()
|
|
73
|
-
.map(String)
|
|
74
|
-
.every((collision) => this.trie.has(collision))
|
|
75
|
-
);
|
|
69
|
+
return this.trie.has(word) && pattern.getCollisions().every((collision) => this.trie.has(collision.toString()));
|
|
76
70
|
}
|
|
77
71
|
|
|
78
72
|
public generateBlankTilesPermutations(tiles: Tile[]): Tile[][] {
|
package/src/PatternsGenerator.ts
CHANGED
|
@@ -66,22 +66,23 @@ class PatternsGenerator {
|
|
|
66
66
|
cells: Cell[];
|
|
67
67
|
PatternModel: new (parameters: { board: Board; cells: Cell[] }) => P;
|
|
68
68
|
}): P[] {
|
|
69
|
-
return this.generateStartIndices(cells).
|
|
70
|
-
(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
69
|
+
return this.generateStartIndices(cells).flatMap((startIndex) => {
|
|
70
|
+
const endIndices = this.generateEndIndices(cells, startIndex);
|
|
71
|
+
const patterns: P[] = [];
|
|
72
|
+
|
|
73
|
+
for (const endIndex of endIndices) {
|
|
74
|
+
const pattern = new PatternModel({
|
|
75
|
+
board,
|
|
76
|
+
cells: cells.slice(startIndex, endIndex + 1),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
if (pattern.canBePlaced(this.config)) {
|
|
80
|
+
patterns.push(pattern);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return patterns;
|
|
85
|
+
});
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
public generateStartIndices(cells: Cell[]): number[] {
|
package/src/Solver.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Trie } from '@kamilmielnik/trie';
|
|
2
|
-
import { Board, Config,
|
|
2
|
+
import { Board, Config, Result, Tile } from '@scrabble-solver/types';
|
|
3
3
|
import uniqBy from 'lodash/uniqBy';
|
|
4
4
|
|
|
5
5
|
import PatternsFiller from './PatternsFiller';
|
|
@@ -20,13 +20,9 @@ class Solver {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
public solve(board: Board, tiles: Tile[]): Result[] {
|
|
23
|
-
const patterns = this.patternsGenerator
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
(filledPatterns, pattern) => filledPatterns.concat(this.patternsFiller.fill(pattern, tiles)),
|
|
27
|
-
[],
|
|
28
|
-
);
|
|
29
|
-
const uniquePatterns = uniqBy(patterns, (pattern) => JSON.stringify(pattern.toJson()));
|
|
23
|
+
const patterns = this.patternsGenerator.generate(board);
|
|
24
|
+
const filledPatterns = patterns.flatMap((pattern) => this.patternsFiller.fill(pattern, tiles));
|
|
25
|
+
const uniquePatterns = uniqBy(filledPatterns, (pattern) => JSON.stringify(pattern.toJson()));
|
|
30
26
|
const results = uniquePatterns.map(
|
|
31
27
|
(pattern, index) =>
|
|
32
28
|
new Result({
|