-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
157 lines (140 loc) · 4.8 KB
/
Copy pathapp.js
File metadata and controls
157 lines (140 loc) · 4.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
const gameContainer = document.getElementById("game");
let numberOfAttempts = document.getElementById('number-of-attempts');
let countForNumberOfAttempts = 0;
let bestScoreDisplay = document.getElementById('best-score');
const startNewGame = document.getElementById('start-game');
const cardFlipSound = new Audio('sounds/flip.wav');
const correctMatchSound = new Audio('sounds/yes.wav');
const overlay = document.getElementById('overlay');
const endGameOverlay = document.getElementById('endame-overlay');
const userFeedbackGoood = document.getElementById('nice-work');
const userFeedbackBad = document.getElementById('try-again');
const congratulations = document.getElementById('congratulations');
let clickingAllowed = true;
const COLORS = [
"king.png",
"2.png",
"3.png",
"4.png",
"5.png",
"king.png",
"2.png",
"3.png",
"4.png",
"5.png"
];
let numberOfCardPairsLeft = COLORS.length / 2;
if (localStorage.getItem('bestScore')) {
bestScoreDisplay.textContent = localStorage.getItem('bestScore');
};
startNewGame.addEventListener('click', startGame);
function startGame () {
countForNumberOfAttempts = 0;
numberOfAttempts.textContent = '0';
numberOfCardPairsLeft = COLORS.length / 2;
cardDivs = gameContainer.firstElementChild;
while (cardDivs) {
cardDivs.classList.remove('face-up');
gameContainer.removeChild(cardDivs);
cardDivs = cardDivs = gameContainer.firstElementChild
}
let shuffledColors = shuffle(COLORS);
createDivsForColors(shuffledColors);
startNewGame.style.visibility = 'hidden';
overlay.style.display = 'none';
congratulations.style.visibility = 'hidden';
endGameOverlay.style.visibility = 'hidden';
}
function shuffle(array) {
let counter = array.length;
while (counter > 0) {
let index = Math.floor(Math.random() * counter);
counter--;
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
function createDivsForColors(cardArray) {
for (let card of cardArray) {
const newDiv = document.createElement("div");
newDiv.classList.add(card);
newDiv.style.content = "url('imgs/card.png')";
newDiv.addEventListener("click", handleCardClick);
gameContainer.append(newDiv);
}
}
let firstCardClick = true;
let firstCard = undefined;
function handleCardClick(e) {
if (!clickingAllowed) return;
if (!numberOfCardPairsLeft > 0) return;
if (e.target.tagName === 'DIV') {
if (firstCardClick) {
flipCard(e)
firstCardClick = false;
firstCard = e.target;
} else {
if (e.target !== firstCard) {
flipCard(e)
countForNumberOfAttempts++;
numberOfAttempts.textContent = countForNumberOfAttempts;
if (firstCard.className === e.target.className) {
correctMatch(e);
clickingAllowed = false;
setTimeout(function() {
userFeedbackGoood.style.visibility = 'hidden';
clickingAllowed = true;
},1500)
}
else {
clickingAllowed = false;
userFeedbackBad.style.visibility = 'visible';
setTimeout(function(){
wrongMatch(e);
firstCardClick = true;
userFeedbackBad.style.visibility = 'hidden';
clickingAllowed = true;
},1500)
}
}
}
}
}
document.body.addEventListener('click', function(){
if (numberOfCardPairsLeft === 0) {
if (bestScoreDisplay.textContent === '0') updateBestScore();
else if (Number(countForNumberOfAttempts) < Number(bestScoreDisplay.textContent)) updateBestScore();
userFeedbackGoood.style.visibility = 'hidden';
startNewGame.style.visibility = 'visible';
overlay.style.display = 'block';
endGameOverlay.style.visibility = 'visible';
congratulations.textContent = 'Congratulations';
congratulations.style.visibility = 'visible';
}
}
)
function updateBestScore() {
bestScoreDisplay.textContent = countForNumberOfAttempts;
localStorage.setItem('bestScore', countForNumberOfAttempts);
}
function flipCard(e) {
e.target.style.content = `url(imgs/${e.target.className}`;
e.target.classList.toggle('face-up');
cardFlipSound.play();
}
function wrongMatch(e) {
firstCard.style.content = "url('imgs/card.png')";
e.target.style.content = "url('imgs/card.png')";
firstCard.classList.toggle('face-up');
e.target.classList.toggle('face-up');
}
function correctMatch(e) {
correctMatchSound.play();
firstCard.removeEventListener('click',handleCardClick)
e.target.removeEventListener('click', handleCardClick)
numberOfCardPairsLeft--;
firstCardClick = true;
userFeedbackGoood.style.visibility = 'visible';
}