Fix pangram scoring.
This commit is contained in:
parent
5cb4eeae72
commit
b73ff68d17
3 changed files with 26 additions and 14 deletions
|
@ -4,6 +4,7 @@ import React, {
|
||||||
} from 'react';
|
} from 'react';
|
||||||
|
|
||||||
import Honeycomb from './honeycomb';
|
import Honeycomb from './honeycomb';
|
||||||
|
import { isPangram } from './scoring';
|
||||||
import { StoreContext } from './store';
|
import { StoreContext } from './store';
|
||||||
|
|
||||||
const GameView: FunctionComponent = () => {
|
const GameView: FunctionComponent = () => {
|
||||||
|
@ -46,7 +47,7 @@ const GameView: FunctionComponent = () => {
|
||||||
)}
|
)}
|
||||||
{words.map((word) => (
|
{words.map((word) => (
|
||||||
<div
|
<div
|
||||||
className="m-1"
|
className={`m-1 ${isPangram(letters, word) ? 'text-yellow-400' : ''}`}
|
||||||
key={word}
|
key={word}
|
||||||
>
|
>
|
||||||
{word}
|
{word}
|
||||||
|
|
20
src/scoring.ts
Normal file
20
src/scoring.ts
Normal 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;
|
||||||
|
};
|
|
@ -7,7 +7,7 @@ import React, {
|
||||||
useReducer,
|
useReducer,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
|
|
||||||
import twl06 = require('./twl06.json');
|
import { scoreWord } from './scoring';
|
||||||
|
|
||||||
export interface State {
|
export interface State {
|
||||||
currentWord: string;
|
currentWord: string;
|
||||||
|
@ -69,22 +69,13 @@ const shuffle = (letters: string[]): string[] => {
|
||||||
return newLetters;
|
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 enterWordReducer: Reducer<State, EnterWordAction> = (state) => {
|
||||||
const valid = canSubmitWord(state);
|
const score = scoreWord(state.letters, state.words, state.currentWord);
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
currentWord: '',
|
currentWord: '',
|
||||||
score: state.score + (valid ? scoreWord(state.currentWord) : 0),
|
score: state.score + score,
|
||||||
words: valid ? [...state.words, state.currentWord] : state.words,
|
words: score > 0 ? [...state.words, state.currentWord] : state.words,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue