-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
76 lines (73 loc) · 1.68 KB
/
script2.js
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
function playSequence(count) {
var i = 0;
function play() {
console.log(model.currentDisplay[i]);
i++;
if (i < count + 1) {
setTimeout(play, 1000)
}
}
play()
}
var model = {
randomSeries: [],
userInput: [],
count: 0,
isOn: false,
isStrict: false,
game: function() {
if (this.isOn) {
this.reset()
}
},
reset: function() {
this.randomSeries = [];
this.generateSeries();
this.currentDisplay = [this.randomSeries[0]];
this.userInput = [];
this.count = 0;
playSequence(this.count)
},
generateSeries: function() {
var num = 5;
for (var i = 0; i < num; i++) {
this.randomSeries.push(Math.floor(Math.random() * 4) + 1);
}
},
userMove: function(value) {
this.userInput.push(value)
this.compareSequence(this.userInput.length - 1);
},
compareSequence: function(element) {
if (this.compare(element)) {
if (this.isLastUserInput()) {
this.userInput = [];
this.count++
if (this.isWon()) {
console.log("WON")
this.reset()
} else {
this.currentDisplay.push(this.randomSeries[this.count])
playSequence(this.count)
}
}
} else {
if (this.isStrict) {
this.reset()
console.log("strict mode")
}
console.log('incorrect')
playSequence(this.count)
this.userInput = [];
}
},
compare: function(element) {
return this.currentDisplay[element] === this.userInput[element]
},
isLastUserInput: function() {
return this.userInput.length === this.currentDisplay.length
},
isWon: function() {
return this.count === this.randomSeries.length
}
}