-
Notifications
You must be signed in to change notification settings - Fork 0
/
FccSimon.js
204 lines (191 loc) · 8.18 KB
/
FccSimon.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
$(document).ready(function () {
'use strict';
var interval, interval2, interval3, cpuindex = 0, playerindex = 0, redo = false, poweron = false, gamestarted = false, strict = false, computerdone = false, count = 0, sequence = [],
sound = ["", new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"),
new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"),
new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"),
new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3")];
function computerplay() {
if (cpuindex < sequence.length) {
setTimeout(function () {
sound[sequence[cpuindex]].play();
$("#" + sequence[cpuindex]).addClass("pushed");
}, 300);
setTimeout(function () {
$("#" + sequence[cpuindex]).removeClass("pushed");
cpuindex += 1;
}, 600);
} else {
clearInterval(interval3);
computerdone = true;
playerindex = 0;
redo = false;
}
} //end computerplay()
function computerstart() {
cpuindex = 0;
if (!redo) { //do this if its not a redo -- set in $(".btn").click area based on conditions
count += 1;
$(".lcd").html(count);
sequence.push(1 + Math.floor(Math.random() * 4));
}
interval3 = setInterval(computerplay, 900);
} //end computerstart()
function intro() { //some random patterns of lights when the power is turned on
$("#1, #2, #3, #4").removeClass("pushed");
var random = 1 + Math.floor(Math.random() * 3), random2 = 200 + Math.floor(Math.random() * 800);
function intro1() {
if ($("#1").hasClass("pushed")) {
$("#1, #2, #3, #4").removeClass("pushed");
$("#2, #3").addClass("pushed");
} else {
$("#1, #2, #3, #4").removeClass("pushed");
$("#1, #4").addClass("pushed");
}
} //end intro1
function intro2() {
if ($("#1").hasClass("pushed")) {
$("#1, #2, #3, #4").removeClass("pushed");
} else {
$("#1, #2, #3, #4").addClass("pushed");
}
} //end intro2()
function intro3() {
if ($("#1").hasClass("pushed")) {
$("#1, #2, #3, #4").removeClass("pushed");
$("#2").addClass("pushed");
} else if ($("#2").hasClass("pushed")) {
$("#1, #2, #3, #4").removeClass("pushed");
$("#4").addClass("pushed");
} else if ($("#4").hasClass("pushed")) {
$("#1, #2, #3, #4").removeClass("pushed");
$("#3").addClass("pushed");
} else {
$("#1, #2, #3, #4").removeClass("pushed");
$("#1").addClass("pushed");
}
} //end intro3()
switch (random) {
case 1:
clearInterval(interval2);
interval2 = setInterval(intro1, random2);
break;
case 2:
clearInterval(interval2);
interval2 = setInterval(intro2, random2);
break;
case 3:
clearInterval(interval2);
interval2 = setInterval(intro3, random2);
break;
default:
break;
} //end switch(random)
} //intro()
$(".power-switch").click(function () { //turns power on and off
$(".power-switch").toggleClass("margin-toggle");
if (!poweron) { //power on -- start it up
poweron = true;
$(".wrapcolors").css("box-shadow", "0 0 20vmin white");
$(".lcd").html("--");
intro();
interval = setInterval(intro, 8000);
} else { //power off -- reset all
clearInterval(interval);
clearInterval(interval2);
clearInterval(interval3);
strict = false;
poweron = false;
gamestarted = false;
computerdone = false;
count = 0;
sequence = [];
$("#1, #2, #3, #4").removeClass("pushed");
$(".start-button, .strict-button").removeClass("button-on");
$(".wrapcolors").css("box-shadow", "0 0 8vmin #999");
$(".lcd").empty();
}
}); //end actual-switch.click (power switch)
$(".start-button").click(function () { //starts a game
if (poweron && !gamestarted) { //button only works when power is on, and game not started yet
gamestarted = true;
clearInterval(interval);
clearInterval(interval2);
$("#1, #2, #3, #4").removeClass("pushed");
$(".start-button").addClass("button-on");
computerstart();
}
}); //end start-button.click
$(".strict-button").click(function () { //toggles strict mode
if (poweron) {
$(".strict-button").toggleClass("button-on");
if (strict) {
strict = false;
} else {
strict = true;
}
}
}); //end strict-button.click
$(".btn").mousedown(function () {
var id = $(this).attr("id");
if (poweron && gamestarted && computerdone) { //can't use buttons when cpu is going or other conditions not met
$(this).addClass("pushed");
if (id == sequence[playerindex]) { //means it is the right button
sound[id].play();
if (playerindex === 19) { //player reached winning length - game won
gamestarted = false;
computerdone = false;
strict = false;
count = 0;
sequence = [];
$(".lcd").html("--");
$(".start-button, .strict-button").removeClass("button-on");
intro();
interval = setInterval(intro, 8000);
alert("YOU WIN!");
}
} else { //wrong button clicked
sound[1].play();
sound[2].play();
sound[3].play();
sound[4].play();
console.log("mousedown wrongbutton");
if (strict) { //wrong button and strict === true
$("#1, #2, #3, #4").addClass("pushed");
count = 0;
sequence = [];
} else { //wrong button and strict === false
$("#1, #2, #3, #4").addClass("pushed");
redo = true;
} //end wrong button and strict === false
} //end wrong button clicked
} //end if (poweron && gamestarted && computerdone)
}).mouseup(function () { //end .mousedown
var id = $(this).attr("id");
if (poweron && gamestarted && computerdone) { //can't use buttons when cpu is going or other conditions not met
$("#1, #2, #3, #4").removeClass("pushed");
if (id == sequence[playerindex]) { //means right button was clicked
if (playerindex == sequence.length - 1) { //right button clicked and turn is over
computerdone = false;
computerstart();
} else { //sequence length not reached, this turn is not over
playerindex += 1;
}
} else { //end right button clicked -- means wrong button was clicked
if (strict) { //wrong button and strict === true
gamestarted = false;
computerdone = false;
strict = false;
$(".lcd").html("--");
$(".start-button, .strict-button").removeClass("button-on");
intro();
interval = setInterval(intro, 8000);
alert("YOU LOSE!");
} else { //wrong button and strict === false
computerdone = false;
computerstart();
} //end wrong button and strict === false
} //end wrong button clicked
}
}); //end .mouseup
}); //end document.ready