From cf4051c29a8de44abe8517bee5dd05a1705fb645 Mon Sep 17 00:00:00 2001 From: Kyle Florence Date: Mon, 5 Jan 2026 18:00:56 -0800 Subject: [PATCH] Add best possible score to challenge mode. Fixes #23 --- src/components/game.js | 3 ++- src/components/grid.js | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/components/game.js b/src/components/game.js index a65e4df..16e407b 100644 --- a/src/components/game.js +++ b/src/components/game.js @@ -297,7 +297,8 @@ export class Game { { name: 'Progress', value: `${statistics.progress}%` }, { name: 'Average Word Length', value: statistics.averageWordLength }, { name: 'Hidden Words Found', value: hiddenWords }, - { name: 'Your Best Score', value: `${statistics.best} (${statistics.bestDiff})` } + { name: 'Your Best Score', value: `${statistics.best} (${statistics.bestDiff})` }, + { name: 'Best Possible Score', value: statistics.bestPossible } ].map((item) => { const $content = document.createElement('span') $content.textContent = item.name diff --git a/src/components/grid.js b/src/components/grid.js index 3bb60b0..08593b4 100644 --- a/src/components/grid.js +++ b/src/components/grid.js @@ -171,7 +171,7 @@ export class Grid { getStatistics (state) { state ??= this.getState() - return new Grid.Statistics(this.#configuration, state, this.getWords(state)) + return new Grid.Statistics(this.#configuration, state, this.getWords(state), this.#cells) } getSwaps () { @@ -1104,6 +1104,7 @@ export class Grid { averageWordLength best bestDiff + bestPossible hiddenWordCount hiddenWordsGuessed moves @@ -1112,7 +1113,7 @@ export class Grid { swapCount wordCount - constructor (configuration, state, words) { + constructor (configuration, state, words, cells) { const { size } = configuration const { length, points } = words.reduce( (acc, word) => ({ length: acc.length + word.content.length, points: acc.points + word.points }), @@ -1122,9 +1123,18 @@ export class Grid { const score = points + (length === size ? size : 0) const diff = state.user.highScore - score + const hiddenWords = state.configuration.words.map((content, index) => + new Word( + configuration.width, + state.configuration.wordIndexes[index].map((index) => cells[index]), + new Grid.Move(Grid.Move.Types.Spell, { match: Grid.Match.Exact }) + ) + ) + this.averageWordLength = length === 0 ? 0 : (length / words.length).toPrecision(2) this.best = state.user.highScore this.bestDiff = diff === 0 ? '=' : (diff < 0 ? diff : `+${diff}`) + this.bestPossible = hiddenWords.reduce((points, word) => points + word.points, 0) + size this.hiddenWordCount = state.configuration.words.length this.hiddenWordsGuessed = state.configuration.words .filter((content) => words.some((word) => word.content === content)).length