Add memory if a guess took place for the current round

This commit is contained in:
Marcel Schwarz 2023-04-12 18:58:14 +02:00
parent e174b655af
commit d63982bff0

11
game.js
View File

@ -94,6 +94,7 @@ class Model {
#currentCpu; #currentCpu;
#nextCpu; #nextCpu;
#tookGuessOnCurrent;
#score; #score;
#highscore; #highscore;
@ -106,6 +107,8 @@ class Model {
this.#nextCpu = this.#getRandomCpu(); this.#nextCpu = this.#getRandomCpu();
this.#score = 0; this.#score = 0;
this.#highscore = localStorage.getItem("highscore_cpu") ?? 0; this.#highscore = localStorage.getItem("highscore_cpu") ?? 0;
this.#tookGuessOnCurrent = false;
} }
get highscore() { get highscore() {
@ -124,8 +127,15 @@ class Model {
return this.#nextCpu; return this.#nextCpu;
} }
get hasGuessed() {
return this.#tookGuessOnCurrent;
}
// Can be 'higher' or 'lower' // Can be 'higher' or 'lower'
postAnswer(answer) { postAnswer(answer) {
if (this.#tookGuessOnCurrent) return false;
this.#tookGuessOnCurrent = true;
let answerWasCorrect = false; let answerWasCorrect = false;
if (answer === 'higher') { if (answer === 'higher') {
answerWasCorrect = this.nextCpu.score > this.#currentCpu.score; answerWasCorrect = this.nextCpu.score > this.#currentCpu.score;
@ -146,6 +156,7 @@ class Model {
nextRound() { nextRound() {
this.#currentCpu = this.#nextCpu; this.#currentCpu = this.#nextCpu;
this.#nextCpu = this.#getRandomCpu(); this.#nextCpu = this.#getRandomCpu();
this.#tookGuessOnCurrent = false;
} }
#getRandomCpu() { #getRandomCpu() {