Have got a dictionary of words
This commit is contained in:
parent
5cb44dd17e
commit
d9fa154a01
15 changed files with 46459 additions and 39 deletions
116
site/assets/scripts/models/gamecontroller.js
Normal file
116
site/assets/scripts/models/gamecontroller.js
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
export const GUESS_RESULT = {
|
||||
MISS: 'm',
|
||||
WIN: 'w',
|
||||
FOUL: 'f'
|
||||
};
|
||||
|
||||
export const MARKERS = {
|
||||
MISS: 'm',
|
||||
RIGHT_POS: 'g',
|
||||
RIGHT_CHAR: 'y'
|
||||
};
|
||||
|
||||
export class GameController {
|
||||
constructor(wordSource) {
|
||||
this._wordSource = wordSource;
|
||||
}
|
||||
|
||||
async start() {
|
||||
this._currentWord = await this._wordSource.getCurrentWord();
|
||||
this._guesses = 5;
|
||||
|
||||
console.log("The current word: " + this._currentWord);
|
||||
}
|
||||
|
||||
wordLength() {
|
||||
this._checkHasStarted();
|
||||
return this._currentWord.length;
|
||||
}
|
||||
|
||||
guesses() {
|
||||
this._checkHasStarted();
|
||||
return this._guesses;
|
||||
}
|
||||
|
||||
async nextWord() {
|
||||
this._currentWord = await this._wordSource.getCurrentWord();
|
||||
this._guesses = 5;
|
||||
return true;
|
||||
}
|
||||
|
||||
checkGuess(guess) {
|
||||
this._checkHasStarted();
|
||||
|
||||
guess = guess.toLowerCase();
|
||||
|
||||
if (guess.length != this._currentWord.length) {
|
||||
throw Error(`Expected length to be ${this._currentWord.length} but was ${guess.length}`);
|
||||
}
|
||||
|
||||
// Check correct placements
|
||||
let guessResult;
|
||||
let markers = new Array(guess.length);
|
||||
let misses = {};
|
||||
let hits = {};
|
||||
|
||||
if (!this._wordSource.isWord(guess)) {
|
||||
return {
|
||||
guessResult: GUESS_RESULT.FOUL,
|
||||
};
|
||||
}
|
||||
|
||||
for (let i = 0; i < guess.length; i++) {
|
||||
if (guess[i] == this._currentWord[i]) {
|
||||
markers[i] = MARKERS.RIGHT_POS;
|
||||
hits[guess[i]] = MARKERS.RIGHT_POS;
|
||||
} else {
|
||||
if (this._currentWord[i] in misses) {
|
||||
misses[this._currentWord[i]] += 1;
|
||||
} else {
|
||||
misses[this._currentWord[i]] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check words that are wrong placement but are in the word
|
||||
// Distribute based on the words position
|
||||
for (let i = 0; i < guess.length; i++) {
|
||||
if (markers[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (misses[guess[i]] && misses[guess[i]] > 0) {
|
||||
misses[guess[i]] -= 1;
|
||||
markers[i] = MARKERS.RIGHT_CHAR;
|
||||
|
||||
if (!hits[guess[i]]) {
|
||||
hits[guess[i]] = MARKERS.RIGHT_CHAR;
|
||||
}
|
||||
} else {
|
||||
if (!hits[guess[i]]) {
|
||||
hits[guess[i]] = MARKERS.MISS;
|
||||
}
|
||||
markers[i] = MARKERS.MISS;
|
||||
}
|
||||
}
|
||||
|
||||
let isRight = markers.filter((x) => x == MARKERS.RIGHT_POS).length == this._currentWord.length;
|
||||
if (isRight) {
|
||||
guessResult = GUESS_RESULT.WIN;
|
||||
} else {
|
||||
guessResult = GUESS_RESULT.MISS;
|
||||
}
|
||||
|
||||
return {
|
||||
guessResult: guessResult,
|
||||
hits: hits,
|
||||
markers: markers
|
||||
};
|
||||
}
|
||||
|
||||
_checkHasStarted() {
|
||||
if (!this._currentWord) {
|
||||
throw new Error("call start() first");
|
||||
}
|
||||
}
|
||||
}
|
||||
64
site/assets/scripts/models/words.js
Normal file
64
site/assets/scripts/models/words.js
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
function binSearch(list, word) {
|
||||
let first = 0;
|
||||
let last = list.length;
|
||||
|
||||
for (;;) {
|
||||
let ptr = (first + (last - first) / 2) | 0;
|
||||
if (list[ptr] === word) {
|
||||
return true;
|
||||
} else if (last - first <= 1) {
|
||||
return false;
|
||||
} else if (list[ptr] > word) {
|
||||
last = ptr;
|
||||
} else if (list[ptr] < word) {
|
||||
first = ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class WordSource {
|
||||
constructor() {
|
||||
this._wordData = null;
|
||||
this._currentWord = null;
|
||||
}
|
||||
|
||||
isWord(word) {
|
||||
let list = this._wordData.words[word.length.toString()];
|
||||
if (!list) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return binSearch(list, word);
|
||||
}
|
||||
|
||||
async getCurrentWord() {
|
||||
if (this._currentWord) {
|
||||
return this._currentWord;
|
||||
}
|
||||
|
||||
let words = await this._fetchAllWordsIfNecessary();
|
||||
this._currentWord = words.words["4"][7];
|
||||
|
||||
return this._currentWord;
|
||||
}
|
||||
|
||||
async nextWord() {
|
||||
if (this._currentWord >= this._words.length - 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this._currentWord += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
async _fetchAllWordsIfNecessary() {
|
||||
if (this._wordData) {
|
||||
return this._wordData;
|
||||
}
|
||||
|
||||
let res = await fetch("/assets/data/words.json");
|
||||
this._wordData = await res.json();
|
||||
return this._wordData;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue