-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
150 lines (133 loc) · 3.36 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
$(document).ready(function() {
var green = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
var red = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
var yellow = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
var blue = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
var Simon = {
init: function() {
this.colors = ["green", "red", "yellow", "blue"];
this.round = 0;
this.previous = [];
this.userInput = [];
this.strict = false;
this.started = false;
}
};
var Game = Object.create(Simon);
Game.start = function() {
Game.newRound();
this.round++;
};
Game.newRound = function() {
this.previous.push(this.colors[Math.floor(Math.random() * (4 - 0) + 0)]);
this.userInput = [];
$('.count').html("Round: " + (this.round + 1));
Game.displayPieces();
};
Game.displayPieces = function() {
var i = 0;
setTimeout(function() {
(function lightUp() {
$("#" + Game.previous[i]).addClass('light');
if (Game.previous[i] === 'green') {
green.play();
}
else if (Game.previous[i] === 'red') {
red.play();
}
else if (Game.previous[i] === 'yellow') {
yellow.play();
}
else if (Game.previous[i] === 'blue') {
blue.play();
}
setTimeout(function() {
$('#' + Game.previous[i]).removeClass('light');
}, 200);
if (++i < Game.previous.length) {
setTimeout(lightUp, 1000);
}
})();
}, 800);
}
Game.getUserInput = function(color) {
this.userInput.push(color);
};
Game.checkForEquals = function(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
};
Game.strictMode = function() {
this.strict = !this.strict;
};
Game.restart = function() {
Game.init();
Game.start();
};
Game.init();
$('.start').click(function() {
Game.started = !Game.started;
if (Game.started === true) {
Game.restart();
}
else {
Game.start();
}
});
$('.strict').click(function() {
Game.strictMode();
if (Game.strict === true) $('.strict').html("strict ON");
else $('.strict').html("strict OFF");
});
$('#green, #red, #yellow, #blue').mousedown(function() {
var selection = $(this).attr('id');
$(this).removeClass('light');
if ($(this).attr('id') === 'green') {
green.play();
}
else if ($(this).attr('id') === 'red') {
red.play();
}
else if ($(this).attr('id') === 'yellow') {
yellow.play();
}
else if($(this).attr('id') === 'blue') {
blue.play();
}
Game.getUserInput(selection);
if (Game.userInput.length === Game.previous.length) {
var correct = Game.checkForEquals(Game.userInput, Game.previous);
if (correct === true) {
Game.newRound();
Game.round++;
console.log("GOOD JOB");
}
else {
if (Game.strict === false) {
$('.count').html("Try again!");
Game.userInput = [];
Game.displayPieces();
}
else {
$('.count').html("Restarting");
setTimeout(function() {
Game.restart();
Game.strict = true;
}, 2000)
}
}
}
if (Game.round > 20) {
$('.count').html("YOU WIN");
Game.init();
setTimeout(function() {
Game.start();
}, 5000);
}
});
});