Regenerated dictionary and started showing guesses

This commit is contained in:
Leon Mika 2025-03-10 15:50:07 +11:00
parent c668b1e266
commit af1c5ace72
12 changed files with 13275 additions and 47 deletions

View file

@ -18,7 +18,17 @@ class ProgressionState {
return JSON.parse(prog);
}
prog = {wordLength: 4, wordIndex: {"4": 0, "5": 0, "6": 0}};
this.clearProgression("");
return prog;
}
clearProgression(shuffleId) {
let prog = {
shuffleId: shuffleId,
wordLength: 4,
wordIndex: {"4": 0, "5": 0, "6": 0},
currentGuesses: []
};
localStorage.setItem('progression', JSON.stringify(prog));
return prog;
}
@ -37,6 +47,11 @@ export class GameController {
async start() {
let prog = this._progressionState.getProgression();
if (await this._wordSource.needToResetProgression(prog)) {
console.log("Clearing patten shuffle id")
prog = this._progressionState.clearProgression(await this._wordSource.getPattenShuffleID());
}
this._currentWord = await this._wordSource.getCurrentWord(prog);
this._guesses = Math.max(this._currentWord.length, 5) + 1;
@ -53,6 +68,14 @@ export class GameController {
return this._guesses;
}
currentGuesses() {
let prog = this._progressionState.getProgression();
if (!prog || !prog.currentGuesses) {
return [];
}
return prog.currentGuesses;
}
currentWord() {
this._checkHasStarted();
return this._currentWord;
@ -63,6 +86,7 @@ export class GameController {
let prog = this._progressionState.getProgression();
prog.wordIndex[prog.wordLength + ""] += 1;
prog.wordLength = (((Math.random() * 23) | 0) / 10 | 0) + 4;
prog.currentGuesses = [];
this._progressionState.saveProgression(prog);
@ -70,6 +94,22 @@ export class GameController {
this._guesses = Math.max(this._currentWord.length, 5) + 1;
return true;
}
showHint() {
let hints = new Array(this._currentWord.length);
let priorGuesses = this._progressionState.getProgression().currentGuesses;
hints[0] = this._currentWord[0];
for (let i = 1; i < this._currentWord.length; i++) {
for (let guess of priorGuesses) {
if (guess[i] === this._currentWord[i]) {
hints[i] = this._currentWord[i];
}
}
}
return hints;
}
checkGuess(guess) {
this._checkHasStarted();
@ -141,6 +181,11 @@ export class GameController {
} else {
guessResult = GUESS_RESULT.MISS;
}
// Add this guess to the progression state
let prog = this._progressionState.getProgression();
prog.currentGuesses.push(guess);
this._progressionState.saveProgression(prog);
return {
guessResult: guessResult,

View file

@ -33,6 +33,15 @@ export class WordSource {
return binSearch(list, word);
}
async needToResetProgression(prog) {
await this._fetchAllWordsIfNecessary();
return (!prog.shuffleId) || this._pattern.id !== prog.shuffleId;
}
async getPattenShuffleID() {
return this._pattern.id;
}
async getCurrentWord(prog) {
let words = await this._fetchAllWordsIfNecessary();
@ -44,15 +53,6 @@ export class WordSource {
return words.words[wordLengthKey][idx];
}
// async nextWord() {
// if (this._currentWord >= this._words.length - 1) {
// return false;
// }
//
// this._currentWord += 1;
// return true;
// }
async _fetchAllWordsIfNecessary() {
if (this._wordData) {
return this._wordData;