-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
135 lines (110 loc) · 3.3 KB
/
script.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
"use strict";
const cards = document.querySelectorAll(".card");
const cardSideFront = document.querySelector(".card-side__front");
const cardSideBack = document.querySelector(".card-side__back");
const matches = document.querySelector(".matches");
const moves = document.querySelector(".moves");
const newGame = document.querySelector(".btn");
// Variables
let match = 0;
let move = 10;
let limit = 0;
let openCards = [];
let flippedCards = [];
// Functions
const init = function () {
match = 0;
move = 10;
matches.textContent = 0;
moves.textContent = 10;
document.querySelector(".loser").style.opacity = "0";
document.querySelector(".loser").style.visibility = "hidden";
document.querySelector(".winner").style.opacity = "0";
document.querySelector(".winner").style.visibility = "hidden";
document.querySelector(".cards").style.display = "grid";
const flippedCards = document.querySelectorAll(".flipped");
flippedCards.forEach((flippedCard) => {
flippedCard.classList.remove("flipped");
});
const openCards = document.querySelectorAll(".matched");
openCards.forEach((openCard) => {
openCard.classList.remove("matched");
});
setTimeout(() => {
shuffle();
}, 500);
};
const shuffle = function () {
cards.forEach((card) => {
let order = Math.floor(Math.random() * 16);
card.style.order = order;
});
};
const gameWon = function () {
setTimeout(() => {
document.querySelector(".winner").style.opacity = "1";
document.querySelector(".winner").style.visibility = "visible";
document.querySelector(".cards").style.display = "none";
}, 1500);
};
const gameLost = function () {
setTimeout(() => {
document.querySelector(".loser").style.opacity = "1";
document.querySelector(".loser").style.visibility = "visible";
document.querySelector(".cards").style.display = "none";
}, 1500);
};
const cardsMatched = function (flippedCards) {
match++;
matches.textContent = match;
const isGameWon = match === 8;
flippedCards.forEach((flippedCard) => {
flippedCard.classList.add("matched");
flippedCard.classList.remove("flipped");
if (isGameWon) {
gameWon();
}
});
};
const closeCards = function (flippedCards) {
flippedCards.forEach((flippedCard) => {
setTimeout(() => {
flippedCard.classList.remove("flipped");
}, 1000);
});
move--;
moves.textContent = move;
const isGameLost = move === 0;
if (isGameLost) {
gameLost();
}
};
init();
cards.forEach((card) => {
card.addEventListener("click", function () {
openCard(card);
});
});
const openCard = function (card) {
if (
!card.classList.contains("flipped") &&
!card.classList.contains("matched")
) {
card.classList.add("flipped");
flippedCards.push(card);
let cardName = card.getAttribute("data-card-name");
openCards.push(cardName);
const isTwoCardsOpened = flippedCards.length === 2;
const isCardsMatched = openCards[0] === openCards[1];
if (isTwoCardsOpened) {
if (isCardsMatched) {
cardsMatched(flippedCards);
} else {
closeCards(flippedCards);
}
openCards = [];
flippedCards = [];
}
}
};
newGame.addEventListener("click", init);