-
-
Notifications
You must be signed in to change notification settings - Fork 161
determine tile correctness better #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
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: |
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:
