|
1 | 1 | import { LetterState, type Letter } from "./Letter"; |
2 | 2 | import { words as wordsEN } from "../../words/words-en.json"; |
3 | | -import { words as wordsNL } from "../../words/words-debug.json"; |
4 | | -import { words as wordsNAL } from "../../words/words-nl.json"; |
| 3 | +import { words as wordsNL } from "../../words/words-nl.json"; |
5 | 4 |
|
6 | 5 | export type Language = "nl" | "en"; |
7 | 6 |
|
@@ -29,29 +28,24 @@ export function checkWord(game: Game, guessedWord: string): Game { |
29 | 28 | if (finalWordLetters[index] == letter) { |
30 | 29 | letterState = LetterState.CORRECT; |
31 | 30 | } else { |
32 | | - // If not, get all positions of the same, remaning letters. |
33 | | - // This is needed to determine if the letter is in the word or not. |
34 | | - const lettersIndexes = finalWordLetters.flatMap((fletter, index) => |
35 | | - fletter == letter ? [index] : [], |
| 31 | + // If not, get all positions of the same, misplaced letters. |
| 32 | + const lettersIndexes = finalWordLetters.flatMap((fletter, findex) => |
| 33 | + fletter == letter && finalWordLetters[findex] != letters[findex] |
| 34 | + ? [findex] |
| 35 | + : [], |
36 | 36 | ); |
37 | 37 |
|
38 | | - for (const letterIndex in lettersIndexes) { |
39 | | - if (finalWordLetters[letterIndex] == letters[letterIndex]) { |
40 | | - continue; |
41 | | - } |
42 | | - |
43 | | - // Get the count of other letters that are placed incorrectly in the word. |
44 | | - const letterCount = finalWordLetters.filter( |
45 | | - (l, index) => |
46 | | - l == letter && finalWordLetters[index] != letters[index], |
47 | | - ).length; |
| 38 | + for (let i = 0; lettersIndexes.length; i++) { |
| 39 | + // Get how many IN_WORD marks already have been made for this letter. |
| 40 | + // When all misplaced letters have already been marked, this letter is not |
| 41 | + // marked as IN_WORD anymore. |
48 | 42 | const hintedLetterCount = hintedLetters.get(letter); |
49 | 43 |
|
50 | | - // When the hitedLetterCount is higher or equal then the letterCount, all in word |
51 | | - // positions have been marked (or are correct). |
| 44 | + // When the hitedLetterCount is higher or equal then the letterCount, all IN_WORD |
| 45 | + // positions for this have been marked (or are correctly placed). |
52 | 46 | if ( |
53 | 47 | hintedLetterCount != undefined && |
54 | | - hintedLetterCount >= letterCount |
| 48 | + hintedLetterCount >= lettersIndexes.length |
55 | 49 | ) { |
56 | 50 | continue; |
57 | 51 | } |
|
0 commit comments