@scrabble-solver/solver 2.12.1 → 2.12.3
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 +1 -1
- package/build/areDigraphsValid.d.ts +3 -0
- package/build/areDigraphsValid.js +16 -0
- package/build/getPatternHash.js +2 -2
- package/build/index.d.ts +1 -0
- package/build/index.js +3 -1
- package/build/solve.js +5 -1
- package/package.json +7 -7
- package/src/areDigraphsValid.ts +20 -0
- package/src/fillPattern.test.ts +3 -3
- package/src/generatePatterns.test.ts +13 -0
- package/src/getPatternHash.ts +2 -2
- package/src/getPatternScore.test.ts +13 -3
- package/src/index.ts +1 -0
- package/src/solve.test.ts +93 -7
- package/src/solve.ts +6 -1
package/README.md
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const areDigraphsValid = (config, pattern) => {
|
|
4
|
+
const { twoCharacterTiles } = config;
|
|
5
|
+
const { cells } = pattern;
|
|
6
|
+
for (let index = 0; index < cells.length - 1; ++index) {
|
|
7
|
+
const current = cells[index];
|
|
8
|
+
const next = cells[index + 1];
|
|
9
|
+
const digraphCandidate = current.tile.character + next.tile.character;
|
|
10
|
+
if (twoCharacterTiles.includes(digraphCandidate)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
};
|
|
16
|
+
exports.default = areDigraphsValid;
|
package/build/getPatternHash.js
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const getPatternHash = (pattern) => {
|
|
4
4
|
return pattern.cells
|
|
5
|
+
.filter((cell) => cell.isEmpty)
|
|
5
6
|
.map((cell) => {
|
|
6
7
|
const blank = cell.tile.isBlank ? '!' : '';
|
|
7
8
|
const tile = cell.tile.character + blank;
|
|
8
|
-
|
|
9
|
-
return cell.x + ',' + cell.y + ',' + tile;
|
|
9
|
+
return [cell.x, cell.y, tile].join(',');
|
|
10
10
|
})
|
|
11
11
|
.join('-');
|
|
12
12
|
};
|
package/build/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { default as areDigraphsValid } from './areDigraphsValid';
|
|
1
2
|
export { default as fillPattern } from './fillPattern';
|
|
2
3
|
export { default as generateEndIndices } from './generateEndIndices';
|
|
3
4
|
export { default as generateHorizontalPatterns } from './generateHorizontalPatterns';
|
package/build/index.js
CHANGED
|
@@ -3,7 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.solve = exports.getUniquePatterns = exports.getPatternScore = exports.getPatternHash = exports.getCellsScore = exports.generateVerticalPatterns = exports.generateVectors = exports.generateStartIndices = exports.generatePatterns = exports.generatePattern = exports.generateHorizontalPatterns = exports.generateEndIndices = exports.fillPattern = void 0;
|
|
6
|
+
exports.solve = exports.getUniquePatterns = exports.getPatternScore = exports.getPatternHash = exports.getCellsScore = exports.generateVerticalPatterns = exports.generateVectors = exports.generateStartIndices = exports.generatePatterns = exports.generatePattern = exports.generateHorizontalPatterns = exports.generateEndIndices = exports.fillPattern = exports.areDigraphsValid = void 0;
|
|
7
|
+
var areDigraphsValid_1 = require("./areDigraphsValid");
|
|
8
|
+
Object.defineProperty(exports, "areDigraphsValid", { enumerable: true, get: function () { return __importDefault(areDigraphsValid_1).default; } });
|
|
7
9
|
var fillPattern_1 = require("./fillPattern");
|
|
8
10
|
Object.defineProperty(exports, "fillPattern", { enumerable: true, get: function () { return __importDefault(fillPattern_1).default; } });
|
|
9
11
|
var generateEndIndices_1 = require("./generateEndIndices");
|
package/build/solve.js
CHANGED
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const areDigraphsValid_1 = __importDefault(require("./areDigraphsValid"));
|
|
6
7
|
const fillPattern_1 = __importDefault(require("./fillPattern"));
|
|
7
8
|
const generatePatterns_1 = __importDefault(require("./generatePatterns"));
|
|
8
9
|
const getPatternScore_1 = __importDefault(require("./getPatternScore"));
|
|
@@ -10,7 +11,10 @@ const getUniquePatterns_1 = __importDefault(require("./getUniquePatterns"));
|
|
|
10
11
|
const solve = (trie, config, board, tiles) => {
|
|
11
12
|
const patterns = (0, generatePatterns_1.default)(config, board);
|
|
12
13
|
const filledPatterns = patterns.flatMap((pattern) => (0, fillPattern_1.default)(trie, config, pattern, tiles));
|
|
13
|
-
const
|
|
14
|
+
const validPatterns = config.twoCharacterTiles.length > 0
|
|
15
|
+
? filledPatterns.filter((pattern) => (0, areDigraphsValid_1.default)(config, pattern))
|
|
16
|
+
: filledPatterns;
|
|
17
|
+
const uniquePatterns = (0, getUniquePatterns_1.default)(validPatterns);
|
|
14
18
|
const results = uniquePatterns.map((pattern, index) => ({
|
|
15
19
|
cells: pattern.cells.map((cell) => cell.toJson()),
|
|
16
20
|
collisions: pattern.getCollisions().map((collision) => collision.cells.map((cell) => cell.toJson())),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scrabble-solver/solver",
|
|
3
|
-
"version": "2.12.
|
|
3
|
+
"version": "2.12.3",
|
|
4
4
|
"description": "Scrabble Solver 2 - Solver",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"bugs": {
|
|
18
18
|
"url": "https://github.com/kamilmielnik/scrabble-solver/issues"
|
|
19
19
|
},
|
|
20
|
-
"homepage": "https://
|
|
20
|
+
"homepage": "https://scrabble-solver.org",
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsc --project .",
|
|
23
23
|
"clean": "rimraf build/ node_modules/",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@kamilmielnik/trie": "^2.0.1",
|
|
28
|
-
"@scrabble-solver/types": "^2.12.
|
|
28
|
+
"@scrabble-solver/types": "^2.12.3"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@scrabble-solver/configs": "^2.12.
|
|
32
|
-
"@scrabble-solver/constants": "^2.12.
|
|
33
|
-
"@scrabble-solver/dictionaries": "^2.12.
|
|
31
|
+
"@scrabble-solver/configs": "^2.12.3",
|
|
32
|
+
"@scrabble-solver/constants": "^2.12.3",
|
|
33
|
+
"@scrabble-solver/dictionaries": "^2.12.3"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "5400e276feee01557044e4e758b11e86b83fb085"
|
|
36
36
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Config, Pattern } from '@scrabble-solver/types';
|
|
2
|
+
|
|
3
|
+
const areDigraphsValid = (config: Config, pattern: Pattern): boolean => {
|
|
4
|
+
const { twoCharacterTiles } = config;
|
|
5
|
+
const { cells } = pattern;
|
|
6
|
+
|
|
7
|
+
for (let index = 0; index < cells.length - 1; ++index) {
|
|
8
|
+
const current = cells[index];
|
|
9
|
+
const next = cells[index + 1];
|
|
10
|
+
const digraphCandidate = current.tile.character + next.tile.character;
|
|
11
|
+
|
|
12
|
+
if (twoCharacterTiles.includes(digraphCandidate)) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return true;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default areDigraphsValid;
|
package/src/fillPattern.test.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Trie } from '@kamilmielnik/trie';
|
|
2
|
-
import {
|
|
2
|
+
import { getConfig } from '@scrabble-solver/configs';
|
|
3
3
|
import { dictionaries } from '@scrabble-solver/dictionaries';
|
|
4
|
-
import { Board, Cell, FinalPattern, Locale, Pattern, Tile, VerticalPattern } from '@scrabble-solver/types';
|
|
4
|
+
import { Board, Cell, FinalPattern, Game, Locale, Pattern, Tile, VerticalPattern } from '@scrabble-solver/types';
|
|
5
5
|
|
|
6
6
|
import fillPattern, { fillPatternRecursive } from './fillPattern';
|
|
7
7
|
|
|
8
8
|
const board = Board.fromStringArray([' t ', 'do ', ' ']);
|
|
9
9
|
const locale = Locale.PL_PL;
|
|
10
|
-
const config =
|
|
10
|
+
const config = getConfig(Game.Literaki, locale);
|
|
11
11
|
|
|
12
12
|
describe('fillPattern', () => {
|
|
13
13
|
let trie: Trie | undefined;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { getConfig } from '@scrabble-solver/configs';
|
|
2
|
+
import { Board, Game, Locale } from '@scrabble-solver/types';
|
|
3
|
+
|
|
4
|
+
import generatePatterns from './generatePatterns';
|
|
5
|
+
|
|
6
|
+
describe('generatePatterns', () => {
|
|
7
|
+
it('Generates all patterns for an empty board', () => {
|
|
8
|
+
const config = getConfig(Game.Scrabble, Locale.EN_US);
|
|
9
|
+
const board = Board.create(config.boardWidth, config.boardHeight);
|
|
10
|
+
|
|
11
|
+
expect(generatePatterns(config, board).length).toEqual(54);
|
|
12
|
+
});
|
|
13
|
+
});
|
package/src/getPatternHash.ts
CHANGED
|
@@ -2,11 +2,11 @@ import { Pattern } from '@scrabble-solver/types';
|
|
|
2
2
|
|
|
3
3
|
const getPatternHash = (pattern: Pattern): string => {
|
|
4
4
|
return pattern.cells
|
|
5
|
+
.filter((cell) => cell.isEmpty)
|
|
5
6
|
.map((cell) => {
|
|
6
7
|
const blank = cell.tile.isBlank ? '!' : '';
|
|
7
8
|
const tile = cell.tile.character + blank;
|
|
8
|
-
|
|
9
|
-
return cell.x + ',' + cell.y + ',' + tile;
|
|
9
|
+
return [cell.x, cell.y, tile].join(',');
|
|
10
10
|
})
|
|
11
11
|
.join('-');
|
|
12
12
|
};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Board, Cell, HorizontalPattern, Locale, Pattern, Tile, VerticalPattern } from '@scrabble-solver/types';
|
|
1
|
+
import { getConfig } from '@scrabble-solver/configs';
|
|
2
|
+
import { Board, Cell, Game, HorizontalPattern, Locale, Pattern, Tile, VerticalPattern } from '@scrabble-solver/types';
|
|
3
3
|
|
|
4
4
|
import getPatternScore from './getPatternScore';
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const locale = Locale.PL_PL;
|
|
7
|
+
const config = getConfig(Game.Literaki, locale);
|
|
7
8
|
const board = Board.fromStringArray([
|
|
8
9
|
' kasom ',
|
|
9
10
|
' i ',
|
|
@@ -57,4 +58,13 @@ describe('getPatternScore', () => {
|
|
|
57
58
|
|
|
58
59
|
expect(getPatternScore(config, pattern)).toBe(2);
|
|
59
60
|
});
|
|
61
|
+
|
|
62
|
+
it('gives 0 score for unknown characters', () => {
|
|
63
|
+
const pattern = new HorizontalPattern(board, [
|
|
64
|
+
new Cell({ x: 13, y: 14, tile: new Tile({ character: '?', isBlank: false }), isEmpty: true }),
|
|
65
|
+
new Cell({ x: 12, y: 14, tile: new Tile({ character: 't' }), isEmpty: false }),
|
|
66
|
+
]);
|
|
67
|
+
|
|
68
|
+
expect(getPatternScore(config, pattern)).toBe(2);
|
|
69
|
+
});
|
|
60
70
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { default as areDigraphsValid } from './areDigraphsValid';
|
|
1
2
|
export { default as fillPattern } from './fillPattern';
|
|
2
3
|
export { default as generateEndIndices } from './generateEndIndices';
|
|
3
4
|
export { default as generateHorizontalPatterns } from './generateHorizontalPatterns';
|
package/src/solve.test.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
/* eslint-disable max-lines */
|
|
2
|
+
|
|
1
3
|
import { Trie } from '@kamilmielnik/trie';
|
|
2
|
-
import {
|
|
4
|
+
import { getConfig } from '@scrabble-solver/configs';
|
|
3
5
|
import { dictionaries } from '@scrabble-solver/dictionaries';
|
|
4
|
-
import { Board, Locale, Result, Tile } from '@scrabble-solver/types';
|
|
6
|
+
import { Board, Game, Locale, Result, Tile } from '@scrabble-solver/types';
|
|
5
7
|
|
|
6
8
|
import solve from './solve';
|
|
7
9
|
|
|
@@ -16,9 +18,9 @@ const getBestResult = ([firstResult, ...results]: Result[]): Result => {
|
|
|
16
18
|
);
|
|
17
19
|
};
|
|
18
20
|
|
|
19
|
-
describe('solve - PL', () => {
|
|
21
|
+
describe('solve - pl-PL', () => {
|
|
20
22
|
const locale = Locale.PL_PL;
|
|
21
|
-
const config =
|
|
23
|
+
const config = getConfig(Game.Literaki, locale);
|
|
22
24
|
let trie: Trie | undefined;
|
|
23
25
|
|
|
24
26
|
beforeAll(() => {
|
|
@@ -47,7 +49,7 @@ describe('solve - PL', () => {
|
|
|
47
49
|
]);
|
|
48
50
|
const tiles = generateTiles(['l', 'i', 'n', 'o']);
|
|
49
51
|
const results = solve(trie!, config, board, tiles);
|
|
50
|
-
expect(results.length).toBe(
|
|
52
|
+
expect(results.length).toBe(60);
|
|
51
53
|
});
|
|
52
54
|
|
|
53
55
|
it('zmartwychwstałą x9', () => {
|
|
@@ -99,11 +101,34 @@ describe('solve - PL', () => {
|
|
|
99
101
|
expect(bestResult.word).toBe('zmartwychwstałą');
|
|
100
102
|
expect(bestResult.points).toBe(1157);
|
|
101
103
|
});
|
|
104
|
+
|
|
105
|
+
it('does not duplicate results', () => {
|
|
106
|
+
const board = Board.fromStringArray([
|
|
107
|
+
' a ',
|
|
108
|
+
' a',
|
|
109
|
+
' ',
|
|
110
|
+
' ',
|
|
111
|
+
' ',
|
|
112
|
+
' ',
|
|
113
|
+
' ',
|
|
114
|
+
' ',
|
|
115
|
+
' ',
|
|
116
|
+
' ',
|
|
117
|
+
' ',
|
|
118
|
+
' ',
|
|
119
|
+
' ',
|
|
120
|
+
' ',
|
|
121
|
+
' ',
|
|
122
|
+
]);
|
|
123
|
+
const tiles = generateTiles(['d']);
|
|
124
|
+
const results = solve(trie!, config, board, tiles);
|
|
125
|
+
expect(results.length).toBe(4);
|
|
126
|
+
});
|
|
102
127
|
});
|
|
103
128
|
|
|
104
|
-
describe('solve - ES', () => {
|
|
129
|
+
describe('solve - es-ES', () => {
|
|
105
130
|
const locale = Locale.ES_ES;
|
|
106
|
-
const config =
|
|
131
|
+
const config = getConfig(Game.Scrabble, locale);
|
|
107
132
|
let trie: Trie | undefined;
|
|
108
133
|
|
|
109
134
|
beforeAll(() => {
|
|
@@ -136,4 +161,65 @@ describe('solve - ES', () => {
|
|
|
136
161
|
expect(results.length).toBe(24);
|
|
137
162
|
expect(bestResult.points).toBe(22);
|
|
138
163
|
});
|
|
164
|
+
|
|
165
|
+
it('chooho - does not use C + H to imitate CH', () => {
|
|
166
|
+
const board = Board.fromStringArray([
|
|
167
|
+
' ',
|
|
168
|
+
' ',
|
|
169
|
+
' ',
|
|
170
|
+
' ',
|
|
171
|
+
' ',
|
|
172
|
+
' ',
|
|
173
|
+
' ',
|
|
174
|
+
' ',
|
|
175
|
+
' ',
|
|
176
|
+
' ',
|
|
177
|
+
' ',
|
|
178
|
+
' ',
|
|
179
|
+
' ',
|
|
180
|
+
' ',
|
|
181
|
+
' ',
|
|
182
|
+
]);
|
|
183
|
+
const tiles = generateTiles(['ch', 'o', 'o', 'c', 'h']);
|
|
184
|
+
const results = solve(trie!, config, board, tiles);
|
|
185
|
+
const words = results.map((result) => result.cells.map((cell) => cell.tile?.character).join(''));
|
|
186
|
+
expect(words).not.toContain('chocho');
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('solve - en-GB', () => {
|
|
191
|
+
const locale = Locale.EN_GB;
|
|
192
|
+
const config = getConfig(Game.Scrabble, locale);
|
|
193
|
+
let trie: Trie | undefined;
|
|
194
|
+
|
|
195
|
+
beforeAll(() => {
|
|
196
|
+
return dictionaries.get(locale).then((loadedTrie) => {
|
|
197
|
+
trie = loadedTrie;
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('no', () => {
|
|
202
|
+
const board = Board.fromStringArray([
|
|
203
|
+
' ',
|
|
204
|
+
' ko',
|
|
205
|
+
' ',
|
|
206
|
+
' ',
|
|
207
|
+
' ',
|
|
208
|
+
' ',
|
|
209
|
+
' ',
|
|
210
|
+
' ',
|
|
211
|
+
' ',
|
|
212
|
+
' ',
|
|
213
|
+
' ',
|
|
214
|
+
' ',
|
|
215
|
+
' ',
|
|
216
|
+
' ',
|
|
217
|
+
' ',
|
|
218
|
+
]);
|
|
219
|
+
const tiles = generateTiles(['n']);
|
|
220
|
+
const results = solve(trie!, config, board, tiles).map(Result.fromJson);
|
|
221
|
+
expect(results.some((result) => result.word === 'no')).toBe(true);
|
|
222
|
+
expect(results.some((result) => result.word === 'on')).toBe(true);
|
|
223
|
+
expect(results.length).toBe(2);
|
|
224
|
+
});
|
|
139
225
|
});
|
package/src/solve.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Trie } from '@kamilmielnik/trie';
|
|
2
2
|
import { Board, Config, ResultJson, Tile } from '@scrabble-solver/types';
|
|
3
3
|
|
|
4
|
+
import areDigraphsValid from './areDigraphsValid';
|
|
4
5
|
import fillPattern from './fillPattern';
|
|
5
6
|
import generatePatterns from './generatePatterns';
|
|
6
7
|
import getPatternScore from './getPatternScore';
|
|
@@ -9,7 +10,11 @@ import getUniquePatterns from './getUniquePatterns';
|
|
|
9
10
|
const solve = (trie: Trie, config: Config, board: Board, tiles: Tile[]): ResultJson[] => {
|
|
10
11
|
const patterns = generatePatterns(config, board);
|
|
11
12
|
const filledPatterns = patterns.flatMap((pattern) => fillPattern(trie, config, pattern, tiles));
|
|
12
|
-
const
|
|
13
|
+
const validPatterns =
|
|
14
|
+
config.twoCharacterTiles.length > 0
|
|
15
|
+
? filledPatterns.filter((pattern) => areDigraphsValid(config, pattern))
|
|
16
|
+
: filledPatterns;
|
|
17
|
+
const uniquePatterns = getUniquePatterns(validPatterns);
|
|
13
18
|
const results = uniquePatterns.map((pattern, index) => ({
|
|
14
19
|
cells: pattern.cells.map((cell) => cell.toJson()),
|
|
15
20
|
collisions: pattern.getCollisions().map((collision) => collision.cells.map((cell) => cell.toJson())),
|