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,12 +1,15 @@
import { Controller } from "https://unpkg.com/@hotwired/stimulus@v3.2.2/dist/stimulus.js"
import { MARKERS, GameController } from "../models/gamecontroller.js";
import { GUESS_RESULT, MARKERS, GameController } from "../models/gamecontroller.js";
import { WordSource } from "../models/words.js";
export default class extends Controller {
static targets = ["row"];
connect() {
this._gameController = new GameController();
this._wordSource = new WordSource();
this._gameController = new GameController(this._wordSource);
this._activeRowIndex = 0;
this._activeLetter = 0;
@ -28,36 +31,60 @@ export default class extends Controller {
this._activeLetter += 1;
if (this._activeLetter >= this._gameController.wordLength()) {
this._colorizeRow(rowElem);
this._verifyGuess(rowElem);
}
}
_verifyGuess(rowElem) {
let guessedWord = Array.from(rowElem.querySelectorAll("span")).map((x) => x.innerText).join("");
console.log("The guessed word is: " + guessedWord);
let results = this._gameController.checkGuess(guessedWord);
switch (results.guessResult) {
case GUESS_RESULT.FOUL:
console.log("not a word!");
rowElem.replaceWith(this._buildPlayfieldRow(this._gameController.wordLength()));
this._activeLetter = 0;
break;
case GUESS_RESULT.MISS:
console.log("try again!");
this._colorizeRow(rowElem, results);
this._activeRowIndex += 1;
this._activeLetter = 0;
break;
case GUESS_RESULT.WIN:
console.log("CORREcT!");
this._colorizeRow(rowElem, results);
break;
}
}
_buildPlayfield(rows, wordLength) {
let newRows = [];
for (let r = 0; r < rows; r++) {
let divElem = document.createElement("div");
divElem.classList.add("row");
divElem.setAttribute("data-playfield-target", "row");
for (let c = 0; c < wordLength; c++) {
let letterSpan = document.createElement("span");
divElem.appendChild(letterSpan);
}
newRows.push(divElem);
for (let r = 0; r < rows; r++) {
newRows.push(this._buildPlayfieldRow(wordLength));
}
this.element.replaceChildren.apply(this.element, newRows);
}
_colorizeRow(row) {
let guessedWord = Array.from(row.querySelectorAll("span")).map((x) => x.innerText).join("");
console.log("The guessed word is: " + guessedWord);
_buildPlayfieldRow(wordLength) {
let divElem = document.createElement("div");
divElem.classList.add("row");
divElem.setAttribute("data-playfield-target", "row");
let results = this._gameController.checkGuess(guessedWord);
for (let c = 0; c < wordLength; c++) {
let letterSpan = document.createElement("span");
divElem.appendChild(letterSpan);
}
return divElem;
}
_colorizeRow(row, results) {
let markers = results.markers;
for (let i = 0; i < this._gameController.wordLength(); i++) {

View file

@ -1,7 +1,5 @@
import { Application, Controller } from "https://unpkg.com/@hotwired/stimulus@v3.2.2/dist/stimulus.js"
import "./models/gamecontroller.js";
import PlayfieldController from "./controllers/playfield.js";
import KeyboardController from "./controllers/keyboard.js";

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;
}
}