-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
284 lines (258 loc) · 9.39 KB
/
js.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
$(document).ready(function () {
//properties
var stepsArr = [];
var ifGameOn = false;//is the game on?
var ifQuarterClickable = false;
var rounds = 0;
var ifStrict = false;
var timeOut;//for user fail to click on time
var timeInter; //for clear blink buttons
var displayTimeout; //for clear series of button to highlight
var winTimeout;//timeout when user wins the game
var ifSwitchOn = false;//is the swich turned on?
// const leftTopAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');//left top sound
// const rightTopAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3')//right top sound
// const leftBottomAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');//right sound
// const rightBottomAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');//right sound
//Web Audio API section
var frequencies = [329.63, 261.63, 220, 164.81];
var ctx = new (window.AudioContext || window.webkitAudioContext)();
if (!ctx) alert('sorry but your browser does not support the Web Audio Api,please consider downloading the latest google chrome!')
var osc;
//plauAudio func
var playAudio = function (num, delay, ifWrong) {
osc = ctx.createOscillator();
osc.frequency.value = ifWrong ? num : frequencies[num]; // value in hertz
osc.type = 'triangle';
osc.start(ctx.currentTime);
osc.connect(ctx.destination);
setTimeout(function () {
osc.disconnect(ctx.destination);
osc = null;
}, delay)
}
//event driven button clicked helper functions
var afterBtnClicked = function (bool, ifFail) {
clearTimeout(timeOut);//clear
rounds = 0;
startBtnSeries(bool);
if (ifFail) {
$('#counter').find('span').text('! !');
blinkBtn();
}
}
var btnClicked = function (num) {
if (num == stepsArr[rounds]) {//right btn
playAudio(num, 150);//shorter sound
rounds++;
if (rounds >= 20) win();//20 rounds to win
else if (rounds == stepsArr.length) {//round finish
afterBtnClicked(true);
}
}
else {//wrong btn pressed
playAudio(100, 500, true);
if (ifStrict) {//strict mode
strictFail();
return;
}
afterBtnClicked(false, true);
}
}
//
//event listener for the pies
$('.quarter').on('click', function () {
if (ifQuarterClickable) {
clearTimeout(timeOut);//clear
setTimeOut();//reset timeout
highLightBtnCol(this, true)//highLigh btn
let num = $(this).attr('id').slice(-1);
btnClicked(num);
}
})
// $(".quarter").mouseup(function() {
// });
//check checkbox state to determine the status of the game
$('input').change(function () {
if (this.checked) {
ifSwitchOn = true;
$('#counter').find('span').text('- -');
}
else {
reset(true);
}
});
//start button clicked
$('#start').on('click', function () {
if (!ifSwitchOn) return;
if (ifGameOn) {//restart the game
reset(false, false, true);
startBtnSeries(true);
return;
}
blinkBtn();
ifGameOn = true; // game is on
startBtnSeries(true);
})
//strict button clicked
$('#strict').on('click', function () {
if (!ifSwitchOn) return;
if ($('#indicator').css('background-color') == 'rgb(255, 0, 0)') {
$('#indicator').css('background-color', 'black');
ifStrict = false;
} else {
$('#indicator').css('background-color', 'red');
ifStrict = true;
}
})
//start of button series display function
var startBtnSeries = function (ifIncrement) {
let len = stepsArr.length;
ifQuarterClickable = false;
//when new game or when user win the current round
if (ifIncrement) {
let step = Math.floor((Math.random() * 4));
stepsArr.push(step);
len = stepsArr.length;//new len
}
displayHighLightBtns(len);
}
var displayHighLightBtns = function (i) {
// var delay = i == stepsArr.length?1000:1500;
if (displayTimeout) clearTimeout(displayTimeout);
displayTimeout = setTimeout(function () {
let len = stepsArr.length;
let num = stepsArr[len - i];
playAudio(num, 500);//longer sound
let ele = $('#quarter' + num)
highLightBtnCol(ele, false);
displayStepsNum(len);
if (--i) { // If i > 0, keep going
displayHighLightBtns(i); // Call the loop again, and pass it the current value of i
} else {
userToClickQuaters(600);
}
}, 1500);
};
//start of user click quarters
var userToClickQuaters = function (delay) {
setTimeout(function () {
ifQuarterClickable = true;
//user is expected to click a quarter within 3000 ms, otherwise is lost, click event will clear the function
setTimeOut();
}, delay)
}
//end of buttons display
//universal reset function with parameters
var reset = function (ifResetIndicator, ifWin, ifRestart) {
stepsArr = [];
ifQuarterClickable = false;
rounds = 0;
if (timeOut) clearTimeout(timeOut);//clear
if (winTimeout) clearTimeout(winTimeout);//in case reset right after win
if (ifResetIndicator) {
ifStrict = false;
ifGameOn = false;
ifSwitchOn = false;
$('#counter').find('span').text('');
$('#indicator').css('background-color', 'black');
} else {
if (ifRestart) {
$('#counter').find('span').text('- -');
blinkBtn();
return;
}
$('#counter').find('span').text('! !');
blinkBtn();
}
if (ifWin) ifGameOn = true;
}
//function to handle if fail under strict mode
var strictFail = function () {
reset(false);
startBtnSeries(true);
}
//function to handle win
var win = function () {
reset(false, true);
$('#counter').find('span').text('game');
winTimeout = setTimeout(function () {
startBtnSeries(true);
}, 1500)
}
//if user fails to play button on time in not strict mode
var timeoutNotStrict = function () {
$('#counter').find('span').text('! !');
blinkBtn();
startBtnSeries(false);
}
//set certain time limit for user to play next button
var setTimeOut = function () {
if (timeOut) clearTimeout(timeOut); //??????????
if(!ifGameOn ) return; //if game turned off, return
timeOut = setTimeout(function () {
//timeout user not click on time
playAudio(100, 500, true);
if (ifStrict) strictFail();
else timeoutNotStrict();
}, 3900)
}
//function to blink btns
var blinkBtn = function () {
if (timeInter) clearInterval(timeInter);//in case malicious multiple clicks
var elem = $('#counter').find('span');
timeInter = setInterval(function () {
if (elem.css('visibility') == 'hidden') {
elem.css('visibility', 'visible');
} else {
elem.css('visibility', 'hidden');
}
}, 200);
setTimeout(function () {
if (timeInter) clearInterval(timeInter);
elem.css('visibility', 'visible');
}, 800)
}
//function to display steps in console
var displayStepsNum = function (num) {
num = num < 10 ? '0' + num : num;
$('#counter').find('span').text(num);
}
//start of color helper functions
var LightenDarkenColor = function (col, amt) {
var usePound = false;
if (col[0] == "#") {
col = col.slice(1);
usePound = true;
}
var num = parseInt(col, 16);
var r = (num >> 16) + amt;
if (r > 255) r = 255;
else if (r < 0) r = 0;
var b = ((num >> 8) & 0x00FF) + amt;
if (b > 255) b = 255;
else if (b < 0) b = 0;
var g = (num & 0x0000FF) + amt;
if (g > 255) g = 255;
else if (g < 0) g = 0;
return (usePound ? "#" : "") + (g | (b << 8) | (r << 16)).toString(16);
}
var convertRbGToHex = function (rgb) {
rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
return (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1], 10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2], 10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3], 10).toString(16)).slice(-2) : '';
}
var highLightBtnCol = function (ele, ifUser) {
var delay = ifUser ? 150 : 500;
var color = $(ele).css('background-color');
color = convertRbGToHex(color);
var lighter = LightenDarkenColor(color, 50);
$(ele).css('background-color', lighter);
setTimeout(function () {
$(ele).css('background-color', color);
}, delay)
}
//end of color heler functions
})