-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
276 lines (231 loc) · 6.01 KB
/
app.js
File metadata and controls
276 lines (231 loc) · 6.01 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
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
// tutorial reference by Yahya Elharony via YouTube. https://www.youtube.com/watch?v=G8J13lmApkQ
// holds all of the card's names
let cardNames = [
'diamond',
'paper-plane-o',
'anchor',
'bolt',
'cube',
'leaf',
'bicycle',
'bomb',
'diamond',
'paper-plane-o',
'anchor',
'bolt',
'cube',
'leaf',
'bicycle',
'bomb',
]
scorePanel = $('.score-panel'),
deck = $('.deck'),
card = $('.card'),
cards = card.children(),
movesClass = $('.moves'),
restart = $('.restart');
// Shuffle function from http://stackoverflow.com/a/2450976
function shuffle(array) {
let currentIndex = array.length,
temporaryValue, randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// start game
function startGame() {
let openCards = [],
moves = 0,
matches = 0,
modal = document.getElementById('youWin-modal'),
span = document.getElementById('close'),
timerOn = false,
starsScorePanel = $('.stars'),
starRatings = [12, 24],
clock;
cardShuffle();
// event listener for clicks on the deck and cards.
function initEventListener() {
// stops event listener on second click
deck.off('click');
// reset cards
deck.children().prop('disabled', false);
// disable matched cards
$('li').each(function (index) {
if ($(this).hasClass('match')) {
$(this).prop('disabled', true);
}
});
// if all matches
endGame(matches);
// restart
restart.click(function () {
card.removeClass();
card.addClass('card');
matches = 0;
moves = 0;
movesClass.text(moves);
stopTimer();
$('#seconds').html('00');
$('#minutes').html('00');
starsScorePanel.children().children().each(function (index, element) {
$(element).css('color', '');
});
$('.starRatingIcons').children().each(function (index, element) {
$(element).removeClass();
$(element).addClass('fa fa-star');
});
cardShuffle;
deck.children().prop('disabled', false);
openCards.splice(0, 2);
});
// star ranking based on moves
starsScorePanel.children().children().each(function (index, element) {
if (starRatings[index] === moves) {
$(element).css('color', 'white');
}
});
$('.starRatingIcons').children().each(function (index, element) {
if (starRatings[index] === moves) {
$(element).removeClass();
}
});
// deck event listener
deck.on('click', '.card', function () {
// move counter
moveCounter();
movesClass.text(moves);
if (timerOn === false) {
// timer starts
goTimer();
}
// shows cards
let showCard = $(this).addClass('open show');
// disable first clicked card
if ($(this).hasClass('open')) {
$(this).prop('disabled', true);
}
// card array
openCards.push(showCard.children());
// check for match
if (openCards.length > 1) {
let compare = compareCards(openCards);
if (compare === false) {
for (let i = 0; i < 2; i++) {
$(openCards[i]).parent().addClass('animated flash').css('background', '#070606');
}
//Found on: http://www.telegraphicsinc.com/2013/07/how-to-use-animate-css/
window.setTimeout(function () {
card.removeClass('open show animated flash').css('background', '');
}, 750);
// clears array
openCards.splice(0, 2);
initEventListener();
} else {
lockMatch();
}
}
});
}
// stops the timer
function stopTimer() {
clearInterval(clock);
timerOn = false;
}
// timer found on: https://stackoverflow.com/questions/5517597/plain-count-up-timer-in-javascript
function goTimer() {
let sec = 0;
function pad(val) {
return val > 9 ? val : "0" + val;
}
// if put let, const, or var will not stop timer
clock = setInterval(function () {
$("#seconds").html(pad(++sec % 60));
$("#minutes").html(pad(parseInt(sec / 60, 10)));
}, 1000);
timerOn = true;
}
// shuffle cards
function cardShuffle() {
shuffle(cardNames);
cards.removeClass();
cards.each(function (i) {
$(this).addClass('fa fa-' + cardNames[i]);
i++;
});
}
// increases moves
function moveCounter() {
moves++;
}
// all matches stop timer
function endGame(value) {
if (value === 8) {
stopTimer();
// opens modal
modal.style.display = 'block';
// shows time
let minutes = $('#minutes').text();
let seconds = $('#seconds').text();
$('.clearTime').text('Total Game Time: ' + minutes + ':' + seconds);
// show moves
$('.numberMoves').text('You completed the game in ' + moves + ' moves.');
// star rating
$('.starMessage').text('Here\'s your star rating: ');
// close modal
$('.close').on('click', function () {
modal.style.display = 'none';
});
// outside click close modal
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = 'none';
}
}
// play again, reset game
$('.playAgain').on('click', function () {
card.removeClass();
card.addClass('card');
matches = 0;
moves = 0;
movesClass.text(moves);
stopTimer();
$('#seconds').html('00');
$('#minutes').html('00');
starsScorePanel.children().children().each(function (i, element) {
$(element).css('color', '');
});
$('.starRatingIcons').children().each(function (i, element) {
$(element).removeClass();
$(element).addClass('fa fa-star');
});
modal.style.display = 'none';
cardShuffle();
deck.children().prop('disabled', false);
});
}
}
// looks for match
function compareCards(array) {
return openCards[0].attr('class') === openCards[1].attr('class') ? true : false;
}
// lock matches
function lockMatch() {
for (let index = 0; index < 2; index++) {
$(openCards[index]).parent().removeClass('open show');
$(openCards[index]).parent().addClass('match');
$(openCards[index]).parent().addClass('animated tada');
}
openCards.splice(0, 2);
matches++;
deck.off('click');
initEventListener();
}
initEventListener();
}
startGame();