Added guess outcomes and the ability mark words as invalid

This commit is contained in:
Leon Mika 2025-01-22 21:50:12 +11:00
parent a9d4227122
commit 7312190c62
4 changed files with 92 additions and 22 deletions

View file

@ -1,3 +1,9 @@
export const GUESS_RESULT = {
MISS: 'm',
WIN: 'w',
FOUL: 'f'
};
export const MARKERS = {
MISS: 'm',
RIGHT_POS: 'g',
@ -5,8 +11,9 @@ export const MARKERS = {
};
export class GameController {
constructor() {
this._currentWord = "DEERS";
constructor(wordSource) {
this._wordSource = wordSource;
this._currentWord = wordSource.getCurrentWord();
this._guesses = 5;
}
@ -24,10 +31,17 @@ export class GameController {
}
// 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;
@ -63,7 +77,15 @@ export class GameController {
}
}
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
};

View file

@ -0,0 +1,23 @@
export class WordSource {
constructor() {
this._words = [
"MUNCH",
"HOUSE",
"MOON"
];
this._currentWord = 0;
}
isWord(word) {
return this._words.filter((x) => x == word).length > 0;
}
getCurrentWord() {
return this._words[this._currentWord];
}
nextWord() {
this._currentWord += 1;
}
}