-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.js
More file actions
46 lines (41 loc) · 1.18 KB
/
game.js
File metadata and controls
46 lines (41 loc) · 1.18 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
class Move {
constructor(guess, message) {
this.guess = guess;
this.message = message;
}
}
class GameViewModel{
constructor() {
this.secret= this.createSecret();
this.guess = ko.observable(50);
this.tries = ko.observable(0);
this.moves = ko.observableArray([]);
}
play = () => {
this.tries(this.tries() + 1);
if (this.secret == this.guess()){
this.initializeGame("You win!");
} else if (this.tries() === 7){
this.initializeGame("You lose!");
} else {
let message = "Pick smaller number!";
if (this.guess() < this.secret)
message = "Pick larger number!";
this.moves.push(new Move(this.guess(),message));
}
}
initializeGame = (message) => {
this.tries(0);
this.guess(50);
this.moves([]);
this.moves.push(new Move(this.secret, message))
this.secret = this.createSecret();
}
createSecret = () => {
return Math.floor(Math.random() * 100) + 1;
}
}
const gameViewModel = new GameViewModel();
window.onload = () => {
ko.applyBindings(gameViewModel);
}