From b73ff68d173ecbfdb0dd1a471b5f765c192844aa Mon Sep 17 00:00:00 2001 From: Brent Schroeter Date: Tue, 5 Jan 2021 13:46:16 -0500 Subject: [PATCH] Fix pangram scoring. --- src/game-view.tsx | 3 ++- src/scoring.ts | 20 ++++++++++++++++++++ src/store.tsx | 17 ++++------------- 3 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 src/scoring.ts diff --git a/src/game-view.tsx b/src/game-view.tsx index 1994b67..b96464b 100644 --- a/src/game-view.tsx +++ b/src/game-view.tsx @@ -4,6 +4,7 @@ import React, { } from 'react'; import Honeycomb from './honeycomb'; +import { isPangram } from './scoring'; import { StoreContext } from './store'; const GameView: FunctionComponent = () => { @@ -46,7 +47,7 @@ const GameView: FunctionComponent = () => { )} {words.map((word) => (
{word} diff --git a/src/scoring.ts b/src/scoring.ts new file mode 100644 index 0000000..51766c1 --- /dev/null +++ b/src/scoring.ts @@ -0,0 +1,20 @@ +import twl06 = require('./twl06.json'); + +export const isPangram = (letters: string[], word: string): boolean => { + const chars = word.split(''); + return letters.every((letter) => chars.includes(letter)); +}; + +export const scoreWord = (letters: string[], words: string[], word: string): number => { + if (word.length < 4 || words.includes(word) || !word.split('').includes(letters[0]) + || !twl06.includes(word.toLowerCase())) { + return 0; + } + if (word.length === 4) { + return 1; + } + if (isPangram(letters, word)) { + return word.length + 7; + } + return word.length; +}; diff --git a/src/store.tsx b/src/store.tsx index b347b7c..2ce1d94 100644 --- a/src/store.tsx +++ b/src/store.tsx @@ -7,7 +7,7 @@ import React, { useReducer, } from 'react'; -import twl06 = require('./twl06.json'); +import { scoreWord } from './scoring'; export interface State { currentWord: string; @@ -69,22 +69,13 @@ const shuffle = (letters: string[]): string[] => { return newLetters; }; -const canSubmitWord = (state: State): boolean => { - const { currentWord, words } = state; - return currentWord.length > 3 && !words.includes(currentWord) - && currentWord.split('').includes(state.letters[0]) - && twl06.includes(currentWord.toLowerCase()); -}; - -const scoreWord = (word: string): number => (word.length === 4 ? 1 : word.length); - const enterWordReducer: Reducer = (state) => { - const valid = canSubmitWord(state); + const score = scoreWord(state.letters, state.words, state.currentWord); return { ...state, currentWord: '', - score: state.score + (valid ? scoreWord(state.currentWord) : 0), - words: valid ? [...state.words, state.currentWord] : state.words, + score: state.score + score, + words: score > 0 ? [...state.words, state.currentWord] : state.words, }; };