@scrabble-solver/solver 2.8.9 → 2.8.10
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/generateEndIndices.js +11 -4
- package/build/generateStartIndices.js +10 -4
- package/build/generateVectors.d.ts +3 -2
- package/build/generateVectors.js +2 -4
- package/build/solve.d.ts +2 -2
- package/build/solve.js +3 -4
- package/package.json +7 -7
- package/src/generateEndIndices.test.ts +5 -0
- package/src/generateEndIndices.ts +15 -4
- package/src/generateStartIndices.test.ts +5 -0
- package/src/generateStartIndices.ts +13 -4
- package/src/generateVectors.ts +5 -8
- package/src/solve.test.ts +3 -3
- package/src/solve.ts +9 -11
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const generateEndIndices = (cells, startIndex) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
if (cells.length === 0) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
const endIndices = [];
|
|
8
|
+
for (let endIndex = startIndex + 1; endIndex < cells.length - 1; ++endIndex) {
|
|
9
|
+
if (!cells[endIndex + 1].hasTile()) {
|
|
10
|
+
endIndices.push(endIndex);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
endIndices.push(cells.length - 1);
|
|
14
|
+
return endIndices;
|
|
8
15
|
};
|
|
9
16
|
exports.default = generateEndIndices;
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const generateStartIndices = (cells) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
if (cells.length === 0) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
const startIndices = [0];
|
|
8
|
+
for (let startIndex = 1; startIndex < cells.length - 1; ++startIndex) {
|
|
9
|
+
if (!cells[startIndex - 1].hasTile()) {
|
|
10
|
+
startIndices.push(startIndex);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return startIndices;
|
|
8
14
|
};
|
|
9
15
|
exports.default = generateStartIndices;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Cell } from '@scrabble-solver/types';
|
|
2
|
-
|
|
2
|
+
interface Parameters {
|
|
3
3
|
getNthVector: (index: number) => Cell[];
|
|
4
4
|
vectorsCount: number;
|
|
5
|
-
}
|
|
5
|
+
}
|
|
6
|
+
declare const generateVectors: ({ getNthVector, vectorsCount }: Parameters) => Cell[][];
|
|
6
7
|
export default generateVectors;
|
package/build/generateVectors.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const generateVectors = ({ getNthVector, vectorsCount
|
|
4
|
-
return Array(vectorsCount)
|
|
5
|
-
.fill(0)
|
|
6
|
-
.map((_, index) => getNthVector(index));
|
|
3
|
+
const generateVectors = ({ getNthVector, vectorsCount }) => {
|
|
4
|
+
return Array.from({ length: vectorsCount }, (_, index) => getNthVector(index));
|
|
7
5
|
};
|
|
8
6
|
exports.default = generateVectors;
|
package/build/solve.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Trie } from '@kamilmielnik/trie';
|
|
2
|
-
import { Board, Config,
|
|
3
|
-
declare const solve: (trie: Trie, config: Config, board: Board, tiles: Tile[]) =>
|
|
2
|
+
import { Board, Config, ResultJson, Tile } from '@scrabble-solver/types';
|
|
3
|
+
declare const solve: (trie: Trie, config: Config, board: Board, tiles: Tile[]) => ResultJson[];
|
|
4
4
|
export default solve;
|
package/build/solve.js
CHANGED
|
@@ -3,7 +3,6 @@ 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 types_1 = require("@scrabble-solver/types");
|
|
7
6
|
const fillPattern_1 = __importDefault(require("./fillPattern"));
|
|
8
7
|
const generatePatterns_1 = __importDefault(require("./generatePatterns"));
|
|
9
8
|
const getPatternScore_1 = __importDefault(require("./getPatternScore"));
|
|
@@ -12,9 +11,9 @@ const solve = (trie, config, board, tiles) => {
|
|
|
12
11
|
const patterns = (0, generatePatterns_1.default)(config, board);
|
|
13
12
|
const filledPatterns = patterns.flatMap((pattern) => (0, fillPattern_1.default)(trie, config, pattern, tiles));
|
|
14
13
|
const uniquePatterns = (0, getUniquePatterns_1.default)(filledPatterns);
|
|
15
|
-
const results = uniquePatterns.map((pattern, index) =>
|
|
16
|
-
cells: pattern.cells,
|
|
17
|
-
collisions: pattern.getCollisions().map((collision) => collision.cells),
|
|
14
|
+
const results = uniquePatterns.map((pattern, index) => ({
|
|
15
|
+
cells: pattern.cells.map((cell) => cell.toJson()),
|
|
16
|
+
collisions: pattern.getCollisions().map((collision) => collision.cells.map((cell) => cell.toJson())),
|
|
18
17
|
id: index,
|
|
19
18
|
points: (0, getPatternScore_1.default)(config, pattern),
|
|
20
19
|
}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scrabble-solver/solver",
|
|
3
|
-
"version": "2.8.
|
|
3
|
+
"version": "2.8.10",
|
|
4
4
|
"description": "Scrabble Solver 2 - Solver",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
"clean:force": "npm run clean && rimraf package-lock.json"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@kamilmielnik/trie": "^2.0.
|
|
28
|
-
"@scrabble-solver/types": "^2.8.
|
|
27
|
+
"@kamilmielnik/trie": "^2.0.1",
|
|
28
|
+
"@scrabble-solver/types": "^2.8.10"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@scrabble-solver/configs": "^2.8.
|
|
32
|
-
"@scrabble-solver/constants": "^2.8.
|
|
33
|
-
"@scrabble-solver/dictionaries": "^2.8.
|
|
31
|
+
"@scrabble-solver/configs": "^2.8.10",
|
|
32
|
+
"@scrabble-solver/constants": "^2.8.10",
|
|
33
|
+
"@scrabble-solver/dictionaries": "^2.8.10"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "8822c74d0d632ee51bca3bf69ccfc7f517bfadb3"
|
|
36
36
|
}
|
|
@@ -6,6 +6,11 @@ describe('generateEndIndices', () => {
|
|
|
6
6
|
const emptyCell: Cell = { hasTile: () => false } as Cell;
|
|
7
7
|
const filledCell: Cell = { hasTile: () => true } as Cell;
|
|
8
8
|
|
|
9
|
+
it('returns empty array when no cells given', () => {
|
|
10
|
+
const cells: Cell[] = [];
|
|
11
|
+
expect(generateEndIndices(cells, 0).length).toBe(0);
|
|
12
|
+
});
|
|
13
|
+
|
|
9
14
|
it('finds indices where a new word can end - XXOOOX', () => {
|
|
10
15
|
const cells = [filledCell, filledCell, emptyCell, emptyCell, emptyCell, filledCell];
|
|
11
16
|
expect(generateEndIndices(cells, 3)).toEqual([5]);
|
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
import { Cell } from '@scrabble-solver/types';
|
|
2
2
|
|
|
3
3
|
const generateEndIndices = (cells: Cell[], startIndex: number): number[] => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
if (cells.length === 0) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const endIndices: number[] = [];
|
|
9
|
+
|
|
10
|
+
for (let endIndex = startIndex + 1; endIndex < cells.length - 1; ++endIndex) {
|
|
11
|
+
if (!cells[endIndex + 1].hasTile()) {
|
|
12
|
+
endIndices.push(endIndex);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
endIndices.push(cells.length - 1);
|
|
17
|
+
|
|
18
|
+
return endIndices;
|
|
8
19
|
};
|
|
9
20
|
|
|
10
21
|
export default generateEndIndices;
|
|
@@ -6,6 +6,11 @@ describe('generateStartIndices', () => {
|
|
|
6
6
|
const emptyCell: Cell = { hasTile: () => false } as Cell;
|
|
7
7
|
const filledCell: Cell = { hasTile: () => true } as Cell;
|
|
8
8
|
|
|
9
|
+
it('returns empty array when no cells given', () => {
|
|
10
|
+
const cells: Cell[] = [];
|
|
11
|
+
expect(generateStartIndices(cells).length).toBe(0);
|
|
12
|
+
});
|
|
13
|
+
|
|
9
14
|
it('finds indices where a new word can start', () => {
|
|
10
15
|
const cells = [filledCell, filledCell, emptyCell, emptyCell, emptyCell, filledCell];
|
|
11
16
|
const startIndices = generateStartIndices(cells);
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { Cell } from '@scrabble-solver/types';
|
|
2
2
|
|
|
3
3
|
const generateStartIndices = (cells: Cell[]): number[] => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
if (cells.length === 0) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const startIndices: number[] = [0];
|
|
9
|
+
|
|
10
|
+
for (let startIndex = 1; startIndex < cells.length - 1; ++startIndex) {
|
|
11
|
+
if (!cells[startIndex - 1].hasTile()) {
|
|
12
|
+
startIndices.push(startIndex);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return startIndices;
|
|
8
17
|
};
|
|
9
18
|
|
|
10
19
|
export default generateStartIndices;
|
package/src/generateVectors.ts
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import { Cell } from '@scrabble-solver/types';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
getNthVector,
|
|
5
|
-
vectorsCount,
|
|
6
|
-
}: {
|
|
3
|
+
interface Parameters {
|
|
7
4
|
getNthVector: (index: number) => Cell[];
|
|
8
5
|
vectorsCount: number;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const generateVectors = ({ getNthVector, vectorsCount }: Parameters): Cell[][] => {
|
|
9
|
+
return Array.from({ length: vectorsCount }, (_, index) => getNthVector(index));
|
|
13
10
|
};
|
|
14
11
|
|
|
15
12
|
export default generateVectors;
|
package/src/solve.test.ts
CHANGED
|
@@ -70,7 +70,7 @@ describe('solve - PL', () => {
|
|
|
70
70
|
]);
|
|
71
71
|
const tiles = generateTiles(['a', 'a', 'ą', 'r', 't', 'w', 'z']);
|
|
72
72
|
const results = solve(trie!, config, board, tiles);
|
|
73
|
-
const bestResult = getBestResult(results);
|
|
73
|
+
const bestResult = getBestResult(results.map((result) => Result.fromJson(result)));
|
|
74
74
|
expect(bestResult.word).toBe('zmartwychwstałą');
|
|
75
75
|
expect(bestResult.points).toBe(682);
|
|
76
76
|
});
|
|
@@ -95,7 +95,7 @@ describe('solve - PL', () => {
|
|
|
95
95
|
]);
|
|
96
96
|
const tiles = generateTiles(['ą', 'c', 'h', 't', 'w', 'w', 'z']);
|
|
97
97
|
const results = solve(trie!, config, board, tiles);
|
|
98
|
-
const bestResult = getBestResult(results);
|
|
98
|
+
const bestResult = getBestResult(results.map((result) => Result.fromJson(result)));
|
|
99
99
|
expect(bestResult.word).toBe('zmartwychwstałą');
|
|
100
100
|
expect(bestResult.points).toBe(1157);
|
|
101
101
|
});
|
|
@@ -132,7 +132,7 @@ describe('solve - ES', () => {
|
|
|
132
132
|
]);
|
|
133
133
|
const tiles = generateTiles(['ll', 'a', 'n', 'a']);
|
|
134
134
|
const results = solve(trie!, config, board, tiles);
|
|
135
|
-
const bestResult = getBestResult(results);
|
|
135
|
+
const bestResult = getBestResult(results.map((result) => Result.fromJson(result)));
|
|
136
136
|
expect(results.length).toBe(24);
|
|
137
137
|
expect(bestResult.points).toBe(22);
|
|
138
138
|
});
|
package/src/solve.ts
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
import { Trie } from '@kamilmielnik/trie';
|
|
2
|
-
import { Board, Config,
|
|
2
|
+
import { Board, Config, ResultJson, Tile } from '@scrabble-solver/types';
|
|
3
3
|
|
|
4
4
|
import fillPattern from './fillPattern';
|
|
5
5
|
import generatePatterns from './generatePatterns';
|
|
6
6
|
import getPatternScore from './getPatternScore';
|
|
7
7
|
import getUniquePatterns from './getUniquePatterns';
|
|
8
8
|
|
|
9
|
-
const solve = (trie: Trie, config: Config, board: Board, tiles: Tile[]):
|
|
9
|
+
const solve = (trie: Trie, config: Config, board: Board, tiles: Tile[]): ResultJson[] => {
|
|
10
10
|
const patterns = generatePatterns(config, board);
|
|
11
11
|
const filledPatterns = patterns.flatMap((pattern) => fillPattern(trie, config, pattern, tiles));
|
|
12
12
|
const uniquePatterns = getUniquePatterns(filledPatterns);
|
|
13
|
-
const results = uniquePatterns.map(
|
|
14
|
-
(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}),
|
|
21
|
-
);
|
|
13
|
+
const results = uniquePatterns.map((pattern, index) => ({
|
|
14
|
+
cells: pattern.cells.map((cell) => cell.toJson()),
|
|
15
|
+
collisions: pattern.getCollisions().map((collision) => collision.cells.map((cell) => cell.toJson())),
|
|
16
|
+
id: index,
|
|
17
|
+
points: getPatternScore(config, pattern),
|
|
18
|
+
}));
|
|
19
|
+
|
|
22
20
|
return results;
|
|
23
21
|
};
|
|
24
22
|
|