Added guess outcomes and the ability mark words as invalid
This commit is contained in:
parent
a9d4227122
commit
7312190c62
4 changed files with 92 additions and 22 deletions
|
|
@ -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++) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue