Last bit of tuning for the rules

This commit is contained in:
Leon Mika 2025-01-25 11:52:07 +11:00
parent a8e42da6fd
commit ec10f0d2ea
2 changed files with 53 additions and 26 deletions

View file

@ -10,14 +10,34 @@ export const MARKERS = {
RIGHT_CHAR: 'y' RIGHT_CHAR: 'y'
}; };
class ProgressionState {
getProgression() {
let prog = localStorage.getItem('progression');
if (prog) {
return JSON.parse(prog);
}
prog = {wordLength: 4, wordIndex: {"4": 0, "5": 0, "6": 0}};
localStorage.setItem('progression', JSON.stringify(prog));
return prog;
}
saveProgression(prog) {
localStorage.setItem('progression', JSON.stringify(prog));
}
}
export class GameController { export class GameController {
constructor(wordSource) { constructor(wordSource) {
this._wordSource = wordSource; this._wordSource = wordSource;
this._progressionState = new ProgressionState();
} }
async start() { async start() {
this._currentWord = await this._wordSource.getCurrentWord(); let prog = this._progressionState.getProgression();
this._guesses = 5;
this._currentWord = await this._wordSource.getCurrentWord(prog);
this._guesses = Math.max(this._currentWord.length, 5) + 1;
console.log("The current word: " + this._currentWord); console.log("The current word: " + this._currentWord);
} }
@ -38,8 +58,15 @@ export class GameController {
} }
async nextWord() { async nextWord() {
this._currentWord = await this._wordSource.getCurrentWord(); // Increment the progress
this._guesses = 5; let prog = this._progressionState.getProgression();
prog.wordIndex[prog.wordLength + ""] += 1;
prog.wordLength = (((Math.random() * 23) | 0) / 10 | 0) + 4;
this._progressionState.saveProgression(prog);
this._currentWord = await this._wordSource.getCurrentWord(prog);
this._guesses = Math.max(this._currentWord.length, 5) + 1;
return true; return true;
} }
@ -57,11 +84,13 @@ export class GameController {
let markers = new Array(guess.length); let markers = new Array(guess.length);
let misses = {}; let misses = {};
let hits = {}; let hits = {};
if (!this._wordSource.isWord(guess)) { if (this._currentWord.length <= 5) {
return { if (!this._wordSource.isWord(guess)) {
guessResult: GUESS_RESULT.FOUL, return {
}; guessResult: GUESS_RESULT.FOUL,
};
}
} }
for (let i = 0; i < guess.length; i++) { for (let i = 0; i < guess.length; i++) {

View file

@ -21,7 +21,8 @@ export class WordSource {
constructor() { constructor() {
this._wordData = null; this._wordData = null;
this._pattern = null; this._pattern = null;
this._currentWord = null; // this._currentWord = null;
// this._progressionState=
} }
isWord(word) { isWord(word) {
@ -33,27 +34,24 @@ export class WordSource {
return binSearch(list, word); return binSearch(list, word);
} }
async getCurrentWord() { async getCurrentWord(prog) {
if (this._currentWord) {
return this._currentWord;
}
let words = await this._fetchAllWordsIfNecessary(); let words = await this._fetchAllWordsIfNecessary();
let idx = this._pattern.index["4"][7];
this._currentWord = words.words["4"][idx]; let wordLengthKey = prog.wordLength + "";
let wordIndex = prog.wordIndex[wordLengthKey];
let idx = this._pattern.index[wordLengthKey][wordIndex];
return this._currentWord; return words.words[wordLengthKey][idx];
} }
async nextWord() { // async nextWord() {
if (this._currentWord >= this._words.length - 1) { // if (this._currentWord >= this._words.length - 1) {
return false; // return false;
} // }
//
this._currentWord += 1; // this._currentWord += 1;
return true; // return true;
} // }
async _fetchAllWordsIfNecessary() { async _fetchAllWordsIfNecessary() {
if (this._wordData) { if (this._wordData) {