Fix pangram scoring.

This commit is contained in:
Brent Schroeter 2021-01-05 13:46:16 -05:00
parent 5cb4eeae72
commit b73ff68d17
3 changed files with 26 additions and 14 deletions

View file

@ -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) => (
<div
className="m-1"
className={`m-1 ${isPangram(letters, word) ? 'text-yellow-400' : ''}`}
key={word}
>
{word}

20
src/scoring.ts Normal file
View file

@ -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;
};

View file

@ -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, EnterWordAction> = (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,
};
};