-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
349 lines (301 loc) · 10.8 KB
/
main.js
File metadata and controls
349 lines (301 loc) · 10.8 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
var r = document.querySelector(":root");
const scoreBoard = document.getElementById("score-board");
const ctnGame = document.getElementById("ctn-game");
const ctnStart = document.getElementById("ctn-start");
const ctnGameOver = document.getElementById("ctn-game-over");
const startGame = document.getElementById("start-game");
const restartGame = document.getElementById("restart-game");
const amiiboFigure = document.getElementById("amiibo-figure");
const marioFigure = document.getElementById("mario-figure");
const restartAmiiboFigure = document.getElementById("restart-amiibo-figure");
const restartMarioFigure = document.getElementById("restart-mario-figure");
const marioCard = document.getElementById("mario-card");
const amiiboCard = document.getElementById("amiibo-card");
const restartAmiiboCard = document.getElementById("restart-amiibo-card");
const restartMarioCard = document.getElementById("restart-mario-card");
let numberOfCards = 6;
let outPut = ``;
let typeOfAmiibo = "Figure"; // Default
let cards;
let match = 0;
const timer = document.getElementById("timer");
let startTime = 30;
let currentTime = startTime;
const bestTimeSpan = document.getElementById("best-time");
let bestTime = 0;
const flips = document.getElementById("flips");
let currentFlips = 0;
const bestFlipsSpan = document.getElementById("best-flips");
let bestFlips = 100;
const gameResults = document.getElementById("game-results");
let result = false;
let countDown;
// Sounds
const soundMarioCoin = new Audio("audio/Mario-Coin.mp3");
const soundMarioGameOverLose = new Audio("audio/Mario-Game-Over-Lose.mp3");
const soundMarioGameOverWin = new Audio("audio/Mario-Game-Over-Win.mp3");
const soundMarioLetsGo = new Audio("audio/Mario-Lets-Go.mp3");
const soundMarioMamaMia = new Audio("audio/Mario-Mama-Mia.mp3");
const soundMarioOhNo = new Audio("audio/Mario-Oh-No.mp3");
const soundMarioOhYea = new Audio("audio/Mario-Oh-Yea.mp3");
const soundMarioThemeSong = new Audio("audio/Mario-Theme-Song.mp3");
const soundMarioYahoo = new Audio("audio/Mario-Yahoo.mp3");
soundMarioThemeSong.volume = 0.7;
amiiboFigure.addEventListener("click", choseAmiiboFigure);
marioFigure.addEventListener("click", choseAmiiboFigure);
restartAmiiboFigure.addEventListener("click", choseAmiiboFigure);
restartMarioFigure.addEventListener("click", choseAmiiboFigure);
amiiboCard.addEventListener("click", choseAmiiboCard);
marioCard.addEventListener("click", choseAmiiboCard);
restartAmiiboCard.addEventListener("click", choseAmiiboCard);
restartMarioCard.addEventListener("click", choseAmiiboCard);
function choseAmiiboFigure() {
typeOfAmiibo = "Figure";
amiiboFigure.style.color = "goldenrod";
amiiboCard.style.color = "white";
restartAmiiboFigure.style.color = "goldenrod";
restartAmiiboCard.style.color = "white";
}
function choseAmiiboCard() {
typeOfAmiibo = "Card";
amiiboCard.style.color = "goldenrod";
amiiboFigure.style.color = "white";
restartAmiiboCard.style.color = "goldenrod";
restartAmiiboFigure.style.color = "white";
}
startGame.addEventListener("click", fetchAllAmiibos);
restartGame.addEventListener("click", fetchAllAmiibos);
// Track card numbers used
let numberArrayUsed = [];
let numberArrayUsedWithoutDuplicates;
// Main program code
async function fetchAllAmiibos() {
soundMarioLetsGo.play();
ctnStart.style.display = "none";
ctnGameOver.style.display = "none";
document.querySelector(".overlay").classList.add("active");
const resultsAll = await fetch("https://www.amiiboapi.com/api/amiibo/");
const dataAll = await resultsAll.json();
// Create a random array without duplicates
let chooseRandomNumber;
const randomNumberArray = [];
let randomNumberArrayWithoutDuplicates = [...new Set(randomNumberArray)];
numberArrayUsedWithoutDuplicates = [...new Set(numberArrayUsed)];
do {
chooseRandomNumber = Math.floor(Math.random() * dataAll.amiibo.length);
if (dataAll.amiibo[chooseRandomNumber].type === typeOfAmiibo) {
// Reset the array after all cards are used
if (numberArrayUsed.length >= dataAll.amiibo.length) {
numberArrayUsed = [];
}
// Push unused random numbers into both arrays, providing a new set of cards each replay
if (!numberArrayUsedWithoutDuplicates.includes(chooseRandomNumber)) {
randomNumberArray.push(chooseRandomNumber);
numberArrayUsed.push(chooseRandomNumber);
}
}
randomNumberArrayWithoutDuplicates = [...new Set(randomNumberArray)];
} while (randomNumberArrayWithoutDuplicates.length < numberOfCards);
// Flexibility to change the imgage heights, using a CSS variable
if (typeOfAmiibo === "Figure") {
r.style.setProperty("--img-height", "75px");
}
if (typeOfAmiibo === "Card") {
r.style.setProperty("--img-height", "75px");
}
let amiiboName;
let amiiboNameNoWhiteSpace;
let ammiboImage;
for (let i = 0; i < numberOfCards; i++) {
// Retrieve name of card
amiiboName = dataAll.amiibo[randomNumberArrayWithoutDuplicates[i]].name;
// Remove whitespace from the name of the card
amiiboNameNoWhiteSpace = amiiboName.split(" ").join("");
// Retrieve name of image
ammiboImage = dataAll.amiibo[randomNumberArrayWithoutDuplicates[i]].image;
// Create a set of matching cards
outPut += `
<div class="memory-card" data-image="${amiiboNameNoWhiteSpace}">
<div class="card-layout ${typeOfAmiibo.toLowerCase()} front-face">
<h5>${amiiboName}</h5>
<img src="${ammiboImage}">
</div>
<div class="card-layout ${typeOfAmiibo.toLowerCase()} back-face">
<h5>Nintendo</h5>
</div>
</div>
<div class="memory-card" data-image="${amiiboNameNoWhiteSpace}">
<div class="card-layout ${typeOfAmiibo.toLowerCase()} front-face">
<h5>${amiiboName}</h5>
<img src="${ammiboImage}">
</div>
<div class="card-layout ${typeOfAmiibo.toLowerCase()} back-face">
<h5>Nintendo</h5>
</div>
</div>
`;
}
// Add the set of matching cards to the game container
ctnGame.innerHTML = outPut;
document.querySelector(".overlay").classList.remove("active");
ctnGame.style.display = "flex";
// Set score board
timer.innerHTML = `Time: ${currentTime}`;
flips.innerHTML = `Flips: ${currentFlips}`;
scoreBoard.style.display = "flex";
cards = document.querySelectorAll(".memory-card");
// Shuffle cards by assigning a CSS order Property to each card
shuffle();
// Start the game
setInterval(checkForWin, 1000);
startCountDown();
let hasFlippedCard = false;
let lockBoard = false;
let firstCard;
let secondCard;
function flipCard() {
if (lockBoard) return;
if (this === firstCard) return;
this.classList.toggle("flip");
if (!hasFlippedCard) {
hasFlippedCard = true;
firstCard = this;
currentFlips += 1;
flips.innerHTML = `Flips: ${currentFlips}`;
} else {
secondCard = this;
currentFlips += 1;
flips.innerHTML = `Flips: ${currentFlips}`;
checkForMatch();
}
} // End of flipCard
function checkForMatch() {
if (firstCard.dataset.image === secondCard.dataset.image) {
match += 1;
disableCards();
soundMarioCoin.play();
} else {
unflipCards();
}
}
function disableCards() {
firstCard.removeEventListener("click", flipCard);
secondCard.removeEventListener("click", flipCard);
resetBoard();
}
function unflipCards() {
lockBoard = true;
setTimeout(() => {
firstCard.classList.remove("flip");
secondCard.classList.remove("flip");
resetBoard();
}, 1500);
}
function resetBoard() {
[hasFlippedCard, lockBoard] = [false, false];
[firstCard, secondCard] = [null, null];
}
cards.forEach((c) => c.addEventListener("click", flipCard));
} // End of fetchAllAmiibos
function checkForWin() {
// Check for lose
if (currentTime === 0) {
result = false;
gameResults.textContent = "You Lose!";
gameResults.style.color = "red";
ctnGame.style.display = "none";
ctnGameOver.style.display = "flex";
// Delete current cards from the game container
for (let i = 1; i < cards.length; i++) {
cards[i].remove();
}
// Check for best time
if (currentTime > bestTime) {
bestTime = currentTime;
bestTimeSpan.innerHTML = `Best Time: ${startTime - bestTime} Seconds`;
}
// Check for lowest flips
if (currentFlips < bestFlips && result === true) {
bestFlips = currentFlips;
bestFlipsSpan.innerHTML = `Least Flips: ${bestFlips}`;
} else {
// Fix display if the game is not played or lost the first time
bestFlipsSpan.innerHTML = `Least Flips: ${bestFlips}`;
}
resetVariables();
soundMarioGameOverLose.play();
soundMarioOhNo.play();
}
// Check for win
if (match === numberOfCards) {
result = true;
match = 0;
setTimeout(() => {
gameResults.textContent = "You Win!";
gameResults.style.color = "LawnGreen";
ctnGame.style.display = "none";
ctnGameOver.style.display = "flex";
// Delete current cards
for (let i = 1; i < cards.length; i++) {
cards[i].remove();
}
soundMarioThemeSong.pause();
soundMarioGameOverWin.play();
soundMarioOhYea.play();
}, 1000);
// Check for best time
if (currentTime > bestTime) {
bestTime = currentTime;
bestTimeSpan.innerHTML = `Best Time: ${startTime - bestTime} Seconds`;
}
// Check for lowest flips
if (currentFlips < bestFlips && result === true) {
bestFlips = currentFlips;
bestFlipsSpan.innerHTML = `Least Flips: ${bestFlips}`;
} else {
// Fix display if the game is not played or lost the first time
bestFlipsSpan.innerHTML = `Least Flips: ${bestFlips}`;
}
resetVariables();
}
// Fix display if the game is not played or lost the first time
if (bestFlips === 100) {
bestFlipsSpan.innerHTML = `Least Flips: `;
}
} // End of checkForWin function
function startCountDown() {
countDown = setInterval(() => {
currentTime -= 1;
timer.innerHTML = `Time: ${currentTime}`;
}, 1000);
soundMarioThemeSong.currentTime = 0;
soundMarioThemeSong.play();
}
function resetVariables() {
clearInterval(checkForWin);
clearInterval(countDown);
// Clear arrays
cards = [];
arraryRandomPosition = [];
arraryRandomPositionWithoutDuplicates = [];
// Clear variables
currentTime = startTime;
currentFlips = 0;
match = 0;
outPut = ``;
result = null;
}
// Shuffle cards by assigning a CSS order Property to each card
function shuffle() {
cards = document.querySelectorAll(".memory-card");
let arraryRandomPosition = [];
let arraryRandomPositionWithoutDuplicates = [];
do {
let randomPosition = Math.floor(Math.random() * numberOfCards * 2);
arraryRandomPosition.push(randomPosition);
arraryRandomPositionWithoutDuplicates = [...new Set(arraryRandomPosition)];
} while (arraryRandomPositionWithoutDuplicates.length < numberOfCards * 2);
for (let i = 0; i < arraryRandomPositionWithoutDuplicates.length; i++) {
cards[i].style.order = arraryRandomPositionWithoutDuplicates[i];
}
}