UNPKG

scrabble-solver

Version:

Scrabble Solver 2 - Free, open-source, cross-platform, multi-language analysis tool for Scrabble, Scrabble Duel, Super Scrabble, Letter League, Crossplay, Literaki, and Kelimelik. Quickly find the top-scoring words using the given board and tiles.

51 lines (50 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pattern = void 0; class Pattern { constructor(board, cells) { this.board = board; this.cells = cells; } canBePlaced(config) { const emptyCellsCount = this.getEmptyCellsCount(); const isUsedCellsCountInRange = emptyCellsCount >= 1 && emptyCellsCount <= config.rackSize; return (isUsedCellsCountInRange && (this.hasAtLeast1NonEmptyCell() || this.collides() || (this.goesThroughBoardCenter() && this.board.isEmpty()))); } clone() { return new Pattern(this.board, this.cells.map((cell) => cell.clone())); } collides() { return this.cells.some((cell) => cell.isEmpty && this.board.collides(cell)); } getIndexOfFirstCellWithoutTile() { return this.cells.findIndex((cell) => !cell.hasTile()); } getEmptyCellsCount() { return this.cells.filter((cell) => cell.isEmpty).length; } goesThroughBoardCenter() { return this.cells.some((cell) => cell.x === this.board.center.x && cell.y === this.board.center.y && cell.isEmpty); } hasAtLeast1EmptyCell() { return this.cells.some((cell) => cell.isEmpty); } hasAtLeast1NonEmptyCell() { return this.cells.some((cell) => !cell.isEmpty); } getCollisions() { return []; } toJson() { return { cells: this.cells.map((cell) => cell.toJson()), collisions: this.getCollisions().map((collision) => collision.toJson()), word: this.toString(), }; } toString() { return this.cells.reduce((result, cell) => result + cell.toString(), ''); } } exports.Pattern = Pattern;