Last bit of tuning for the rules
This commit is contained in:
parent
a8e42da6fd
commit
ec10f0d2ea
|
@ -10,14 +10,34 @@ export const MARKERS = {
|
|||
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 {
|
||||
constructor(wordSource) {
|
||||
this._wordSource = wordSource;
|
||||
this._progressionState = new ProgressionState();
|
||||
}
|
||||
|
||||
async start() {
|
||||
this._currentWord = await this._wordSource.getCurrentWord();
|
||||
this._guesses = 5;
|
||||
let prog = this._progressionState.getProgression();
|
||||
|
||||
this._currentWord = await this._wordSource.getCurrentWord(prog);
|
||||
this._guesses = Math.max(this._currentWord.length, 5) + 1;
|
||||
|
||||
console.log("The current word: " + this._currentWord);
|
||||
}
|
||||
|
@ -38,8 +58,15 @@ export class GameController {
|
|||
}
|
||||
|
||||
async nextWord() {
|
||||
this._currentWord = await this._wordSource.getCurrentWord();
|
||||
this._guesses = 5;
|
||||
// Increment the progress
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -57,11 +84,13 @@ export class GameController {
|
|||
let markers = new Array(guess.length);
|
||||
let misses = {};
|
||||
let hits = {};
|
||||
|
||||
if (!this._wordSource.isWord(guess)) {
|
||||
return {
|
||||
guessResult: GUESS_RESULT.FOUL,
|
||||
};
|
||||
|
||||
if (this._currentWord.length <= 5) {
|
||||
if (!this._wordSource.isWord(guess)) {
|
||||
return {
|
||||
guessResult: GUESS_RESULT.FOUL,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < guess.length; i++) {
|
||||
|
|
|
@ -21,7 +21,8 @@ export class WordSource {
|
|||
constructor() {
|
||||
this._wordData = null;
|
||||
this._pattern = null;
|
||||
this._currentWord = null;
|
||||
// this._currentWord = null;
|
||||
// this._progressionState=
|
||||
}
|
||||
|
||||
isWord(word) {
|
||||
|
@ -33,27 +34,24 @@ export class WordSource {
|
|||
return binSearch(list, word);
|
||||
}
|
||||
|
||||
async getCurrentWord() {
|
||||
if (this._currentWord) {
|
||||
return this._currentWord;
|
||||
}
|
||||
|
||||
async getCurrentWord(prog) {
|
||||
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() {
|
||||
if (this._currentWord >= this._words.length - 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this._currentWord += 1;
|
||||
return true;
|
||||
}
|
||||
// async nextWord() {
|
||||
// if (this._currentWord >= this._words.length - 1) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// this._currentWord += 1;
|
||||
// return true;
|
||||
// }
|
||||
|
||||
async _fetchAllWordsIfNecessary() {
|
||||
if (this._wordData) {
|
||||
|
|
Loading…
Reference in a new issue