Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
145 lines (115 loc) · 3.59 KB
/
Copy pathgame.js
File metadata and controls
145 lines (115 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {clearGameState, getGameState, saveGameState} from './game-storage.js';
export default class Game {
#minRange; // private
#maxRange; // private
#maxAttempts; // private
#allowDuplicateGuesses;
#secretNumber;
constructor({minRange = 1, maxRange = 10, maxAttempts = 3, allowDuplicateGuesses = false} = {}) {
this.#minRange = Game.initRangeValues({
value: minRange,
lowerBounds: 0,
upperBounds: maxRange
});
this.#maxRange = Game.initRangeValues({
value: maxRange,
lowerBounds: minRange
});
this.#maxAttempts = parseInt(maxAttempts);
this.#allowDuplicateGuesses = allowDuplicateGuesses;
this.history = [];
this.#secretNumber = Math.floor(
Math.random() * (this.#maxRange - this.#minRange + 1)) + this.#minRange;
}
get minRange() {
return this.#minRange;
}
set minRange(value) {
this.#minRange = Game.initRangeValues({
value,
lowerBounds: 0,
upperBounds: this.#maxRange
});
}
get maxRange() {
return this.#maxRange;
}
set maxRange(value) {
this.#maxRange = Game.initRangeValues({
value,
lowerBounds: this.#minRange
});
}
get maxAttempts() {
return this.#maxAttempts;
}
set maxAttempts(value) {
this.#maxAttempts = value;
}
static initRangeValues({value, lowerBounds, upperBounds = 0} = {}) {
let num = Number(value);
if (isNaN(num)) {
throw {
message: 'Value must be numeric'
};
}
if (num < lowerBounds) {
throw {
message: `Value cannot be less than ${lowerBounds}`
};
}
if (upperBounds && num > upperBounds) {
throw {
message: `Value cannot be greater than ${upperBounds}`
};
}
return num;
}
static async loadSavedGame() {
const state = await getGameState();
if (!state) {
return null;
}
let game = new Game(state);
game.#secretNumber = state.secretNumber;
game.history = state.history;
return game;
}
async checkGuess(guess) {
if (this.#maxAttempts === this.history.length) {
return;
}
if (!this.#allowDuplicateGuesses && this.history.indexOf(guess) > -1) {
return;
}
this.history.push(guess);
const isCorrect = guess === this.#secretNumber;
const isLastAttempt = this.#maxAttempts === this.history.length;
const result = isCorrect ? 'correct' :
guess < this.#secretNumber ? 'too low' : 'too high';
document.dispatchEvent(new CustomEvent('game:guess', {
detail: {
guess,
result,
remainingAttempts: this.#maxAttempts - this.history.length
}
}));
if (isCorrect || isLastAttempt) {
document.dispatchEvent(new CustomEvent('game:over', {
detail: {
secretNumber: this.#secretNumber
}
}));
await clearGameState();
return;
}
await saveGameState({
minRange: this.#minRange,
maxRange: this.#maxRange,
maxAttempts: this.#maxAttempts,
allowDuplicateGuesses: this.#allowDuplicateGuesses,
secretNumber: this.#secretNumber,
history: this.history
});
}
}