-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
214 lines (204 loc) · 5.68 KB
/
main.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
205
206
207
208
209
210
211
212
213
214
/*Start Variables*/
let btnStart = document.querySelector("main .content-start button");
let theContentStart = document.querySelector("main .content-start");
let btnRestart = document.querySelector(
"main .memory-game .info-memory-game .restart"
);
let countTimer = document.querySelector(
"main .memory-game .info-memory-game .timer p span"
);
let nbrTries = document.querySelector(
"main .memory-game .info-memory-game .tries p span"
);
//Cards
let theCards = document.querySelector("main .content-memory-game .cards");
//Array Of Cards
let AllCardsArray = [...theCards.children];
//Card
let theCard = document.querySelector("main .content-memory-game .cards .card");
//Audios
let audioSame = document.getElementById("same");
let audioDifferent = document.getElementById("different");
let audioBackground = document.getElementById("back-naruto-song");
let audioLose = document.getElementById("lose");
let audioWin = document.getElementById("win");
let pName = document.querySelector(
".memory-game .info-memory-game .player p span"
);
/*End Variables*/
//Event : Start Game with Click The Start Button and Write Your Name
btnStart.onclick = () => {
Swal.fire({
title: "Your Name!",
text: "Write Your Name !",
input: "text",
showCancelButton: true,
showCloseButton: true,
}).then((result) => {
if (result.value) {
pName.innerHTML = result.value;
theContentStart.style.display = "none";
audioBackground.play();
timer();
} else {
pName.innerHTML = "Unkown";
theContentStart.style.display = "none";
audioBackground.play();
timer();
}
});
};
//Event : Restart Game with Click The Restart Button To Refresh The Game
btnRestart.onclick = () => {
location.reload();
};
//Event : Click Card To Flipped
function flipNow() {
AllCardsArray.forEach((card, index) => {
card.addEventListener("click", () => {
flipCard(card);
});
});
}
flipNow();
//Function To Flip Cards
let ArrFlippedCards;
function flipCard(cardClicked) {
//Event : Click Card and Add Flipped Class To It
cardClicked.classList.add("flippedCard");
//Filter The Choosen Cards Flipped in An Array
ArrFlippedCards = AllCardsArray.filter((card, index) => {
return card.classList.contains("flippedCard");
});
//Choose Two Cards
if (ArrFlippedCards.length == 2) {
//Stop Event Of Click
stopChoose();
//Check Same Or Different Cards
checkCardsSameDifferent(ArrFlippedCards[0], ArrFlippedCards[1]);
}
}
//Fuction To Stop Choose Cards More Than Two
function stopChoose() {
//Add Class No Click
theCards.classList.add("noClick");
//Remove Class No Click
setTimeout(() => {
theCards.classList.remove("noClick");
}, 1000);
}
//Function To Check The Two Choosen Cards Same Or Different
let numT = 0;
function checkCardsSameDifferent(cardOne, cardTwo) {
if (cardOne.dataset.anime === cardTwo.dataset.anime) {
setTimeout(() => {
cardOne.classList.add("box-sha");
cardTwo.classList.add("box-sha");
}, 1000);
cardOne.classList.remove("flippedCard");
cardTwo.classList.remove("flippedCard");
cardOne.classList.add("done");
cardTwo.classList.add("done");
//Play Audio : Same Cards
audioSame.play();
//Win Game
winGame(AllCardsArray);
} else {
setTimeout(() => {
numT++;
numT < 10
? (nbrTries.innerHTML = `0${parseInt(numT)}`)
: (nbrTries.innerHTML = `${parseInt(numT)}`);
if (numT > 14) {
//Lose Game
loseGame();
}
cardOne.classList.remove("flippedCard");
cardTwo.classList.remove("flippedCard");
//Play Audio : Different Cards
audioDifferent.play();
}, 1000);
}
}
//Function Timer Count Down
let time = 60;
function timer() {
let timerInter = setInterval(() => {
time--;
time < 10
? (countTimer.innerHTML = `0${time}s`)
: (countTimer.innerHTML = `${time}s`);
if (time == 0) {
clearInterval(timerInter);
//Lose Game
loseGame();
}
}, 1000);
}
//Add Order To Cards
//Order Marge Array
let orderMarge = [...Array(AllCardsArray.length).keys()];
//Random Order Marge Array
RandCards(orderMarge);
//Function To Order Cards
function orderCards() {
AllCardsArray.forEach((card, index) => {
card.style.order = orderMarge[index];
});
}
orderCards();
// Function To Make Images Of Cards Random
function RandCards(arrCards) {
let current = arrCards.length,
rand;
while (current > 0) {
rand = Math.floor(Math.random() * current);
current--;
[arrCards[current], arrCards[rand]] = [arrCards[rand], arrCards[current]];
}
return arrCards;
}
//Function Lose Game : If Number Of Tries More Than 15 Or End Time
function loseGame(numOfTries) {
setTimeout(() => {
audioBackground.pause();
audioLose.play();
Swal.fire({
title: "Oops!",
text: "You Lose - Game Over",
icon: "error",
confirmButtonText: "Ok",
// showCancelButton: true,
showCloseButton: true,
});
setTimeout(() => {
location.reload();
}, 4000);
}, 1000);
}
//Function Win Game : If All Cards Have Class Done
function winGame(AllCards) {
let numCardsDone = 0;
AllCards.forEach((card, index) => {
if (card.classList.contains("done")) {
numCardsDone++;
if (numCardsDone == AllCardsArray.length) {
setTimeout(() => {
audioBackground.pause();
audioWin.play();
Swal.fire({
title: "Wow!",
text: "You Win - Done ",
icon: "success",
confirmButtonText: "Ok",
// showCancelButton: true,
showCloseButton: true,
});
setTimeout(() => {
location.reload();
}, 4000);
}, 1000);
}
}
});
}