-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (114 loc) · 3.2 KB
/
index.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
let cards = [];
let clics = [];
let maxClics = 50;
let countClics = 0;
let count = 0;
let time = 60;
let ids = ["a1", "b1", "a2", "b2", "a3", "b3", "a4", "b4", "a5", "b5", "a6", "b6", "a7", "b7", "a8", "b8"]
let cardId = null;
let newCard = null;
let shuffledCards = null;
let imageUrl = null;
const cardContainer = document.getElementById("cardContainer");
const gameOver = document.getElementById("gameOver");
const gameOverTxt = document.querySelector(".gameOver");
const resetGame = document.querySelectorAll(".resetGame");
resetGame.forEach((resetGame) => {
resetGame.addEventListener("click", reset);
});
const timeCount = document.querySelector(".timeCount");
const clicsCount = document.querySelector(".clicsCount");
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function generateCards() {
for(i=0;i<ids.length;i++){
newCard = document.createElement("div");
newCard.setAttribute("id", ids[i]);
newCard.classList.add("cardBack", "card");
cards.push(newCard);
countClics = 0;
}
shuffledCards = shuffle(cards);
for (let car of shuffledCards){
cardId = car.id.charAt(1);
cardContainer.appendChild(car);
imageUrl = "./images/"+ cardId + ".jpeg";
car.style.backgroundImage = 'url('+imageUrl+')';
car.addEventListener("click", turnCard);
}
}
function turnCard(event){
const clickedCard = event.target;
clickedCard.classList.remove("cardBack");
clickedCard.classList.add("animate", "pointer-none");
setTimeout(() => {
clickedCard.classList.remove("animate");
}, 500);
clics.push(clickedCard);
countClicsAdd();
if (clics.length === 2) {
checkMatch();
}
}
function checkMatch() {
const [card1, card2] = clics;
if ((card1.id!=card2.id)&&(card1.id.charAt(1) === card2.id.charAt(1))) {
count++;
card1.removeEventListener("click", turnCard);
card2.removeEventListener("click", turnCard);
setTimeout(() => {
card1.classList.add("pointer-none");
card2.classList.add("pointer-none");
}, 200);
} else {
setTimeout(() => {
card1.classList.add("cardBack", "animate");
card1.classList.remove("pointer-none");
card2.classList.add("cardBack", "animate");
card2.classList.remove("pointer-none");
}, 700);
setTimeout(() => {
card1.classList.remove("animate");
card2.classList.remove("animate");
}, 900);
}
clics = [];
if (count === ids.length / 2) {
gameOverTxt.innerHTML="You Won!"
setTimeout(() => gOver(), 500);
}
}
function reset(event){
if(gameOver.classList.contains("flex")){
gameOver.classList.add("hidden");
gameOver.classList.remove("flex");
}
location.reload()
}
function countClicsAdd() {
countClics += 1;
clicsCount.innerHTML=maxClics-countClics;
if(countClics>=maxClics){
gOver();
}
}
const timer = setInterval(function() {
time--;
timeCount.innerHTML=time
if (time === 0) {
clearInterval(timer);
gOver();
}
}, 1000);
function gOver(){
if(gameOver.classList.contains("hidden")){
gameOver.classList.add("flex");
gameOver.classList.remove("hidden");
}
}
generateCards()