determine tile correctness better#11
Open
sean-b765 wants to merge 1 commit intoWebDevSimplified:mainfrom
Open
Conversation
Author
|
My formatter must have caused the massive diff. function submitGuess() {
const activeTiles = [...getActiveTiles()]
if (activeTiles.length !== WORD_LENGTH) {
showAlert('Not enough letters')
shakeTiles(activeTiles)
return
}
const guess = activeTiles.reduce((word, tile) => {
return word + tile.dataset.letter
}, '')
if (!dictionary.includes(guess)) {
showAlert('Not in word list')
shakeTiles(activeTiles)
return
}
stopInteraction()
// My algorithm for determining classname of the letter
let matchingChars = ''
const classDictionary = { 1: '', 2: '', 3: '', 4: '', 5: '' }
for (let i = 0; i < targetWord.length; i++) {
const letter = guess[i]
console.log(matchingChars)
const rgx = new RegExp(`${letter}`, 'g')
// Need to know the total appearances of the letter in the word
const totalAppearances = (targetWord.match(rgx) || []).length
// Also need to know all prior appearances of the letter in the word,
// so we don't mistakenly tell the user that a duplicate letter exists
const priorAppearances = (matchingChars.match(rgx) || []).length
// Letter not in word
if (!targetWord.includes(letter)) {
classDictionary[i + 1] = 'wrong'
continue
}
// The letter is correct
if (targetWord[i] === guess[i]) {
classDictionary[i + 1] = 'correct'
matchingChars = matchingChars + letter
continue
}
// The letter is included in the word somewhere...
// Look ahead... If there is a correct appearance further in the word, we need to know
let futureCorrectAppearances = 0
for (let j = i; j < targetWord.length; j++) {
if (targetWord[j] === guess[j] && targetWord[j] === letter)
futureCorrectAppearances++
}
console.log(
`There are already ${futureCorrectAppearances} correct ahead of [${i}]: "${letter}"`
)
console.log(
`There are already ${priorAppearances} which come before [${i}]: "${letter}"`
)
console.log(futureCorrectAppearances, priorAppearances, totalAppearances)
// If there are already too many future / prior / future+prior appearances,
// there can not be another instance of this letter, so we must continue
if (futureCorrectAppearances >= totalAppearances) {
classDictionary[i + 1] = 'wrong'
continue
}
if (priorAppearances >= totalAppearances) {
classDictionary[i + 1] = 'wrong'
continue
}
if (priorAppearances + futureCorrectAppearances >= totalAppearances) {
classDictionary[i + 1] = 'wrong'
continue
}
classDictionary[i + 1] = 'wrong-location'
matchingChars = matchingChars + letter
}
activeTiles.forEach((value, index, array) => {
flipTile(value, index, array, guess, classDictionary[index + 1])
})
}
function flipTile(tile, index, array, guess, className) {
const letter = tile.dataset.letter
const key = keyboard.querySelector(`[data-key="${letter}"i]`)
setTimeout(() => {
tile.classList.add('flip')
}, (index * FLIP_ANIMATION_DURATION) / 2)
tile.addEventListener(
'transitionend',
() => {
tile.classList.remove('flip')
tile.dataset.state = className
key.classList.add(className)
if (index === array.length - 1) {
tile.addEventListener(
'transitionend',
() => {
startInteraction()
checkWinLose(guess, array)
},
{ once: true }
)
}
},
{ once: true }
)
} |
|
Thanks for providing a better method. This was really needed in the code. |
|
The current code in master still doesn't properly resolve a case where the player enters more of a given letter than there are in the word. For example, guessing "EERIE" when the word is "QUEER." Here's my fix for it: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
My code is commented so hopefully you can understand it, I wanted to contribute as I recently had this issue in my own rendition of a wordle clone
Addresses this issue: #10
After my changes:
