-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (124 loc) · 3.62 KB
/
Copy pathindex.js
File metadata and controls
140 lines (124 loc) · 3.62 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
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
//variables
userSeq=[];
simonSeq=[];
const NUM_OF_LEVELS= 20;
var gameOn = false;
var id, color, level=0;
var boardSound = [
"https://s3.amazonaws.com/freecodecamp/simonSound1.mp3",
"https://s3.amazonaws.com/freecodecamp/simonSound2.mp3",
"https://s3.amazonaws.com/freecodecamp/simonSound3.mp3",
"https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"
];
//1. Start board sequence
$(document).ready(function() {
//turning on switch
$(".switch").click(function() {
level=0;
simonSeq = []
userSeq = [];
gameOn = (gameOn == false) ? true : false;
console.log(gameOn);
if(gameOn) {
$(".inner-switch").addClass("inner-inactive");
$(".switch").addClass("outer-active");
$(".display").text("00");
//starting game
$(".start-btn").click(function(){
level=0;
level++;
simonSeq = []
userSeq = [];
simonSequence();
})
//user pad listener
$(".pad").click(function(){
id = $(this).attr("id");
color = $(this).attr("class").split(" ")[1];
userSeq.push(id);
console.log(id+" "+color);
addClassSound(id, color);
//checking order of user sequence
if(!checkUserSeq()) {
displayError();//displaying error
userSeq=[];
}
//checking end of sequence
if(userSeq.length == simonSeq.length && userSeq.length < NUM_OF_LEVELS) {
level++;
userSeq=[];
simonSequence();
}
//checking for winner
if(userSeq.length == NUM_OF_LEVELS){
$(".display").text("win");
}
})
}
else {
$(".inner-switch").removeClass("inner-inactive");
$(".switch").removeClass("outer-active");
$(".display").text("");
return;
}
})
})
//checking user sequence against simon
function checkUserSeq(){
for(var i = 0; i < userSeq.length; i++) {
if(userSeq[i] != simonSeq[i]){
return false;
}
}
return true;
}
//displaying error
function displayError() {
console.log("error");
var counter = 0;
var myError = setInterval(function(){
$(".display").text("Err");
counter++;
if(counter==3 || gameOn==false){
$(".display").text(level);
clearInterval(myError);
userSeq=[];
counter=0;
}
},500);
}
//simon sequence
function simonSequence() {
console.log(level);
$(".display").text(level);
getRandomNum();
var i=0;
var myInterval = setInterval(function(){
id = simonSeq[i];
color = $("#"+id).attr("class").split(" ")[1];
console.log(id+" "+color);
addClassSound(id, color);
i++;
if(i == simonSeq.length || gameOn==false){
clearInterval(myInterval);
}
},1000);
}
//generate random number
function getRandomNum(){
var random = Math.floor(Math.random() *4);
simonSeq.push(random);
}
//add temporary class and sound
function addClassSound(id, color){
$("#"+id).addClass(color+"-active");
playSound(id);
setTimeout(function(){
$("#"+id).removeClass(color+"-active");
}, 500);
}
//play board sound
function playSound(id) {
var sound = new Audio(boardSound[id]);
sound.play();
}