-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
198 lines (184 loc) · 4.71 KB
/
app.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var boxGreen = document.getElementById("boxGreen");
var boxRed = document.getElementById("boxRed");
var boxYellow = document.getElementById("boxYellow");
var boxBlue = document.getElementById("boxBlue");
var boxSet = [boxGreen, boxRed, boxYellow, boxBlue];
var audioGreen = document.getElementById("audioGreen");
var audioRed = document.getElementById("audioRed");
var audioYellow = document.getElementById("audioYellow");
var audioBlue = document.getElementById("audioBlue");
var resetBtn = document.getElementById("reset");
var strictBtn = document.getElementById("strict");
var roundBtn = document.getElementById("round");
var pattern = [];
var currentPattern = [];
var userPattern = [];
var sound;
var strictToggle = 0;
var roundCount = 1;
audioGreen.preload = 'auto';
audioRed.preload = 'auto';
audioYellow.preload = 'auto';
audioBlue.preload = 'auto';
function reset() {
alert("Ok let's play again!")
roundCount = 1;
makePattern();
currentPattern = [];
roundBtn.innerHTML = "ROUND: " + roundCount;
playPattern(1);
}
function win() {
alert("Congratulations! You win!");
reset();
}
function soundMatch(testObj) {
if (testObj == boxGreen) {
sound = audioGreen;
} else if (testObj == boxRed) {
sound = audioRed;
} else if (testObj == boxYellow) {
sound = audioYellow;
} else if (testObj == boxBlue) {
sound = audioBlue;
}
}
function simonSays(obj){
if (typeof obj == "string") {
obj = document.getElementById(obj);
}
soundMatch(obj);
if (sound.paused) {
sound.play();
}else{
sound.currentTime = 0
}
obj.style.opacity = '0.5';
setTimeout(function(){
obj.style.opacity = "1";
}, 200);
}
function makePattern() {
pattern = [];
userPattern = [];
for (var i = 0; i < 20; i++) {
var nextBox = boxSet[Math.floor(Math.random()*boxSet.length)].id;
pattern.push(nextBox);
}
}
function nextPlayFunc(nextNum) {
return roundCount - nextNum;
}
function playPattern (num) {
setTimeout(function () {
roundBtn.innerHTML = "ROUND: " + roundCount;
var nextPlay = pattern[nextPlayFunc(num)];
simonSays(nextPlay);
currentPattern.push(nextPlay);
if (--num) {
playPattern(num);
}
}, 800);
}
function testPattern() {
if (userPattern.length == currentPattern.length) {
var incorrect = 0;
for (var i = 0; i < userPattern.length; i++) {
if (userPattern[i] != currentPattern[i]) {
incorrect++;
}
}
if (incorrect == 0 && roundCount < 20) {
setTimeout(function() { alert("Correct!"); }, 600);
userPattern = [];
incorrect = 0;
currentPattern = [];
roundCount++;
playPattern(roundCount);
} else if (incorrect == 0 && roundCount == 20) {
win();
} else if (incorrect > 0) {
setTimeout(function() { alert("Incorrect!"); }, 600);
userPattern = [];
incorrect = 0;
currentPattern = [];
playPattern(roundCount);
}
}
}
function testPatternStrict() {
if (userPattern.length == currentPattern.length) {
var incorrect = 0;
for (var i = 0; i < userPattern.length; i++) {
if (userPattern[i] != currentPattern[i]) {
incorrect++;
}
}
if (incorrect == 0 && roundCount < 20) {
setTimeout(function() { alert("Correct!"); }, 600);
userPattern = [];
incorrect = 0;
currentPattern = [];
roundCount++;
playPattern(roundCount);
} else if (incorrect == 0 && roundCount == 20) {
win();
} else if (incorrect > 0) {
setTimeout(function() { alert("Sorry! You have to start over!"); }, 600);
userPattern = [];
incorrect = 0;
currentPattern = [];
setTimeout(function() { reset() }, 700);
}
}
}
boxGreen.onclick = function(){
simonSays(this);
userPattern.push("boxGreen");
if (strictToggle == 0) {
testPattern();
} else if (strictToggle == 1) {
testPatternStrict();
}
};
boxRed.onclick = function(){
simonSays(this);
userPattern.push("boxRed");
if (strictToggle == 0) {
testPattern();
} else if (strictToggle == 1) {
testPatternStrict();
}
};
boxYellow.onclick = function(){
simonSays(this);
userPattern.push("boxYellow");
if (strictToggle == 0) {
testPattern();
} else if (strictToggle == 1) {
testPatternStrict();
}
};
boxBlue.onclick = function(){
simonSays(this);
userPattern.push("boxBlue");
if (strictToggle == 0) {
testPattern();
} else if (strictToggle == 1) {
testPatternStrict();
}
};
resetBtn.onclick = function(){reset()};
strictBtn.onclick = function(){
if (strictToggle == 0) {
strictToggle = 1;
strictBtn.innerHTML = "STRICT MODE ON";
} else if (strictToggle == 1) {
strictToggle = 0;
strictBtn.innerHTML = "STRICT MODE OFF";
}
reset();
};
roundBtn.onclick = function(){
reset();
};