-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
270 lines (232 loc) · 8.77 KB
/
script.js
File metadata and controls
270 lines (232 loc) · 8.77 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
/*
Nom : Mohamed Talhaoui
Filière : Informatique et IA (S4) | 2024/2025
*/
let moves = document.querySelector("#moves");
let matches = document.querySelector("#matches");
let cards = document.querySelectorAll(".card");
let cardFront = document.querySelector(".card-front");
let cardBack = document.querySelector(".card-back");
let difficulty = document.querySelector("#difficulty");
let gameBoard = document.querySelector(".game-board");
let resetBtn = document.querySelector("#reset-btn");
let wrongCount = document.querySelector("#wrong-count");
let data = {};
fetch('cardsData.json')
.then(response => response.json())
.then(json => {
data = json;
gameBoardSetup();
shuffleOrder(cards); // appel la focntion pour melanger l'ordre des cartes
});
// Background Music
let musicGame = new Audio("Media/audio/gameMusic.mp3");
musicGame.loop = true;
musicGame.volume = 0.04;
function restartMusicGame(){
musicGame.currentTime = 0;
musicGame.play();
}
// Start Screen
document.querySelector(".start-btn span").onclick = function(){
document.querySelector(".start-btn").remove();
musicGame.play();
flipAllCards();
}
// Retourner toutes les cartes pour que le joueur les voie
function flipAllCards(){
stopClicking(5000);
setTimeout(() => {
cards.forEach(card => {
card.style.transform = 'rotateY(180deg)';
});
}, 500);
setTimeout(() => {
cards.forEach(card => {
card.style.transform = 'rotateY(0deg)';
});
}, 5000);
}
// Niveau de difficulte
difficulty.onchange = function(){
for (let i = 0; i < difficulty.options.length; i++) {
if (difficulty.options[i].selected) {
resetAll();
setTimeout(() => {
gameBoardSetup();
}, 100);
flipAllCards();
}
}
};
function gameBoardSetup() {
let level = difficulty.value;
let selectedLevel = data[level]; // depuis le JSON
if (!selectedLevel) return;
for (let j = 0; j < cards.length; j++) {
if (j % 2 === 0) {
cards[j].children[1].textContent = selectedLevel[j / 2].command;
cards[j].children[1].setAttribute('value', selectedLevel[j / 2].command); // ajoute value
} else {
cards[j].children[1].textContent = selectedLevel[Math.floor(j / 2)].reponse;
cards[j].children[1].setAttribute('value', selectedLevel[Math.floor(j / 2)].command); // pour matcher
}
}
}
function shuffleOrder(cards){
// on prend les indices de tableau cards
let cardsOrder = Array.from(Array(cards.length).keys());
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
console.log(cardsOrder);
// Melanger l'ordre des cartes
shuffle(cardsOrder);
// applique un ordre aleatoire à chaque carte
cards.forEach((card, i) => {
card.style.order = cardsOrder[i];
});
}
// J'ai pris la fonction shuffle depuis StackOverflow :)
function shuffle(array) {
let currentIndex = array.length;
while (currentIndex != 0) {
let randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
}
}
// Rotation cardes effect
for (let i = 0; i < cards.length; i++) {
cards[i].addEventListener("click", function(){
cards[i].style.transform = 'rotateY(180deg)';
flipedCard(cards[i]); // a chaque clic sur une carte, on appelle cette fonction
})
}
// cette fonction ajoute la classe "is-flipped" a chaque carte retournee
function flipedCard(selectedCard){
selectedCard.classList.add('is-flipped');
// Filtrer les cartes retournees pour ne garder que celles qui ont la classe "is-flipped"
let allSelectedCards = Array.from(cards).filter(selectedCard => selectedCard.classList.contains('is-flipped'));
// on recupere les deux cartes selectionnees
if (allSelectedCards.length === 2) {
checkMatches(allSelectedCards[0], allSelectedCards[1]);
stopClicking(1000);
}
}
// sound of a wrong answer
function wrongSound(){
let wrong = new Audio("Media/audio/wrong.mp3");
wrong.play();
}
// sound of a right answer
function rightSound(){
let right = new Audio("Media/audio/right.mp3");
right.play();
}
function checkMatches(firstCard, secondCard){
movesCount(); // fonction pour incrementer le nombre de mouvement
if (firstCard.children[1].getAttribute('value') === secondCard.children[1].getAttribute('value')) {
matches.textContent++;
// changer la couleur du texte en blanc
firstCard.children[1].style.color = '#F2F2F7';
secondCard.children[1].style.color = '#F2F2F7';
// les cartes deviendront vertes lorsqu'elles sont Matches
firstCard.children[1].style.backgroundColor = 'var(--green)';
secondCard.children[1].style.backgroundColor = 'var(--green)';
rightSound();
// On supprime la classe "is-flipped" car elle n'est plus necessaire
firstCard.classList.remove('is-flipped');
secondCard.classList.remove('is-flipped');
// Désactive le clic sur les cartes pour qu'elles restent visibles apres match (CSS)
firstCard.classList.add('is-matched');
secondCard.classList.add('is-matched');
afficheResultat();
} else {
// si les cartes ne correspondent pas, on les colore en rouge
firstCard.children[1].style.backgroundColor = 'var(--red)';
secondCard.children[1].style.backgroundColor = 'var(--red)';
wrongSound();
// attendons 1 seconde avant de retourner les cartes
setTimeout(() => {
firstCard.style.transform = 'rotateY(0)';
secondCard.style.transform = 'rotateY(0)';
firstCard.classList.remove('is-flipped');
secondCard.classList.remove('is-flipped');
setTimeout(() => {
firstCard.children[1].style.backgroundColor = 'transparent';
secondCard.children[1].style.backgroundColor = 'transparent';
}, 300);
}, 1000);
// Incremente le nombre d'echecs
wrongCount.textContent++;
let GameOver = new Audio('Media/audio/GameOver.mp3');
if (wrongCount.textContent == 15) {
setTimeout(() => {
musicGame.pause();
document.querySelector(".game-over").style.display = 'inline';
setTimeout(() => {
GameOver.play();
}, 500);
}, 200);
}
}
}
function stopClicking(duration){
// Desactive le clic sur la class de 'gameBoard'
gameBoard.classList.add('no-clicking');
// Desactive le clic sur les 'controles'
document.querySelector(".controls").classList.add('no-clicking');
setTimeout(() => {
gameBoard.classList.remove('no-clicking');
document.querySelector(".controls").classList.remove('no-clicking');
}, duration);
}
// Nombres de Mouvements
function movesCount(){
moves.textContent++;
document.querySelector("#moves-count").textContent = moves.textContent;
}
// Reset button
resetBtn.onclick = function() {
resetAll();
}
function resetAll(){
moves.textContent = "0";
wrongCount.textContent = "0";
matches.textContent = "0";
for (let i = 0; i < cards.length; i++) {
cards[i].style.transform = 'rotateY(0)';
cards[i].children[1].style.backgroundColor = 'var(--card-bg)';
cards[i].children[1].style.color = '#000000';
cards[i].classList.remove('is-matched');
cards[i].classList.remove('is-flipped');
}
setTimeout(() => {
// A chaque redemarrage du jeu, on melange les cartes
shuffleOrder(cards);
flipAllCards();
}, 500);
}
// Rat dancing after finishing ;)
let restartBtn = document.querySelectorAll("#restart-btn");
let musicWin = new Audio('Media/audio/RatMusic.mp3');
musicWin.loop = true;
restartBtn.forEach(restart => {
restart.addEventListener("click", () => {
musicWin.pause();
restartMusicGame();
document.querySelector(".rat-dancing").style.display = 'none';
document.querySelector(".game-over").style.display = 'none';
resetAll();
})
});
function afficheResultat(){
let isAllMatched = document.querySelectorAll(".card.is-matched");
if (isAllMatched.length == cards.length) {
musicGame.pause();
setTimeout(() => {
musicWin.currentTime = 0;
musicWin.play();
document.querySelector(".rat-dancing").style.display = 'inline';
}, 200);
}
}