-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
160 lines (145 loc) · 3.59 KB
/
script.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
$(document).ready(function() {
var sequence = []; // random moves list
var playerSeq = []; // player moves list
var rounds = 0;
var index = 0;
var gameOn = false;
var strict = false;
$('.game-btn').off('click',playerClick);
$('.display').html("PRESS <i>START</i> TO BEGIN <i class='fa fa-arrow-down' aria-hidden='true'></i>");
// funkyStart();
// start game
$('.start-btn, .display').click(startGame);
// reset game
$('.reset-btn').click(resetGame);
$('.reset-btn').click(function(){
$('.display').html("PRESS <i>START</i> TO BEGIN <i class='fa fa-arrow-down' aria-hidden='true'></i>");
});
// toggle strict mode
$('.strict-btn').click(toggleStrict);
// on player click
// $('.game-btn').click(playerClick);
function playerClick() {
var playerStep = $(this).data('num');
playerSeq.push(playerStep);
dispPlayerStep(playerStep);
if (sequence[index] === playerSeq[index]) {
index++;
} else {
playSound('wrong');
if(strict){
console.log(strict);
resetGame();
giveSteps();
return;
}
playerSeq.length = 0;
index = 0;
dispSequence();
return;
}
if (index === sequence.length) {
rounds++;
$('.display').html("ROUND: "+rounds);
if (rounds === 5) {
$('.display').html("YOU WIN!!");
return;
}
index = 0;
playerSeq.length = 0;
giveSteps();
}
}
// FUNCTIONS
// get & display step sequence
function giveSteps() {
var step = getStep(1, 4.5);
sequence.push(step);
// present all steps to player
dispSequence();
};
// display sequence
function dispSequence() {
$('.game-btn').off('click', playerClick);
var counter = 0;
var dispInterval = setInterval(function() {
dispBtn(sequence[counter]);
counter++;
if (counter === sequence.length) {
setTimeout(function() {
clearInterval(dispInterval);
$('.game-btn').on('click', playerClick);
}, 450);
}
}, 900);
}
// highlight display button
function dispBtn(s) {
$('.game-btn#btn' + s).animate({
opacity: '0.3'
}, 450).animate({
opacity: '1'
}, 450);
playSound(s);
}
function dispPlayerStep(s) {
$('.game-btn#btn' + s).animate({
opacity: '0.3'
}, 200).animate({
opacity: '1'
}, 200);
playSound(s);
}
// start game
function startGame() {
if (!gameOn) {
$('.game-btn').on('click', playerClick);
gameOn = true;
$('.display').html("ROUND: "+rounds);
giveSteps();
}
}
// reset game
function resetGame() {
gameOn = false;
sequence.length = 0;
playerSeq.length = 0;
rounds = 0;
$('.display').html("ROUND: "+rounds);
index = 0;
$('.game-btn').off('click', playerClick);
}
// strict mode
function toggleStrict() {
strict = (strict === true) ? false : true;
$('.strict-btn').toggleClass('strict-btn-on');
}
// get random step
function getStep(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
// sound notification
function playSound(ps) {
var sound = $("#audio" + ps)[0]; //+ audio);
sound.play();
}
// starting animation
// highlight display button
function funkyStart() {
var i = 4;
var inter = setInterval(function() {
dis(i % 4 + 1);
i++;
if (i === 16) {
clearInterval(inter);
}
}, 130);
}
function dis(x) {
$('.game-btn#btn' + x).animate({
opacity: '0.3'
}, 150).animate({
opacity: '1'
}, 150);
}
});