Added saving of guesses in progress
All checks were successful
/ publish (push) Successful in 1m20s

This commit is contained in:
Leon Mika 2025-03-10 16:22:34 +11:00
parent af1c5ace72
commit 947ed37974
3 changed files with 79 additions and 26 deletions

View file

@ -75,13 +75,15 @@ export default class extends Controller {
let guessedWord = Array.from(rowElem.querySelectorAll("span")).map((x) => x.innerText).join("");
console.log("The guessed word is: " + guessedWord);
let results = this._gameController.checkGuess(guessedWord);
let results = this._gameController.submitGuess(guessedWord);
switch (results.guessResult) {
case GUESS_RESULT.FOUL:
this.overlayOutlet.showMessage("Not in dictionary");
rowElem.replaceWith(this._buildPlayfieldRow(this._gameController.wordLength()));
let newRow = this._buildPlayfieldRow(this._gameController.wordLength());
this._showHints(newRow);
rowElem.replaceWith(newRow);
this._activeLetter = 0;
window.dispatchEvent(new CustomEvent("guessResults", {
@ -93,8 +95,7 @@ export default class extends Controller {
this._activeRowIndex += 1;
if (this._activeRowIndex >= this._gameController.guesses()) {
this.topMessageTarget.innerText = this._gameController.currentWord().toUpperCase();
this.nextPuzzleButtonsTarget.classList.remove("hide");
this._revealAnswer();
} else {
this._activeLetter = 0;
this._showHints(this.rowTargets[this._activeRowIndex], results.markers);
@ -103,9 +104,7 @@ export default class extends Controller {
break;
case GUESS_RESULT.WIN:
this._colorizeRow(rowElem, results);
this.topMessageTarget.innerText = "Hooray! You did it.";
this.nextPuzzleButtonsTarget.classList.remove("hide");
this._showWin();
break;
}
}
@ -119,36 +118,76 @@ export default class extends Controller {
}
}
_showWin() {
this.topMessageTarget.innerText = "Hooray! You did it.";
this.nextPuzzleButtonsTarget.classList.remove("hide");
}
_revealAnswer() {
this.topMessageTarget.innerText = this._gameController.currentWord().toUpperCase();
this.nextPuzzleButtonsTarget.classList.remove("hide");
}
_buildPlayfield() {
let rows = this._gameController.guesses();
let wordLength = this._gameController.wordLength();
let {currentGuesses, wasWin} = this._gameController.currentState();
this._activeRowIndex = 0;
this._activeRowIndex = currentGuesses.length;
this._activeLetter = 0;
window.dispatchEvent(new CustomEvent("resetKeyColors"));
let newRows = [];
for (let r = 0; r < rows; r++) {
newRows.push(this._buildPlayfieldRow(wordLength));
let currentGuess = null;
let currentGuessResults = null;
if (r < currentGuesses.length) {
currentGuess = currentGuesses[r];
currentGuessResults = this._gameController.checkGuess(currentGuess);
}
newRows.push(this._buildPlayfieldRow(wordLength, currentGuess, currentGuessResults));
if (currentGuessResults) {
window.dispatchEvent(new CustomEvent("guessResults", {
detail: currentGuessResults
}));
}
}
if (wasWin) {
this._showWin();
} else if (currentGuesses.length >= rows) {
// User has already used up all their guesses so just show the results;
this._revealAnswer();
} else {
this._showHints(newRows[currentGuesses.length]);
this.topMessageTarget.innerHTML = "&nbsp;"
this.nextPuzzleButtonsTarget.classList.add("hide");
}
this._showHints(newRows[0]);
this.playfieldTarget.replaceChildren.apply(this.playfieldTarget, newRows);
this.topMessageTarget.innerHTML = "&nbsp;"
this.nextPuzzleButtonsTarget.classList.add("hide");
window.dispatchEvent(new CustomEvent("resetKeyColors"));
}
_buildPlayfieldRow(wordLength) {
_buildPlayfieldRow(wordLength, currentGuess, currentGuessResults) {
let divElem = document.createElement("div");
divElem.classList.add("row");
divElem.setAttribute("data-playfield-target", "row");
for (let c = 0; c < wordLength; c++) {
let letterSpan = document.createElement("span");
if (currentGuess) {
letterSpan.innerText = currentGuess[c].toUpperCase();
}
divElem.appendChild(letterSpan);
}
if (currentGuess) {
this._colorizeRow(divElem, currentGuessResults);
}
return divElem;
}