-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
175 lines (152 loc) · 4.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
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
const gameContainer = document.getElementById("game");
const scoreCard = document.querySelector('#score p');
/* Empty Global Array to log events */
const eventLog = [];
/* A flag to check if click events should run */
let noClick = false;
let gameStart = false;
let guessCount = 0;
let cardsMatched = 0;
const COLORS = [
"Salmon",
"PaleTurquoise",
"PaleGreen",
"PeachPuff",
"Orchid",
"#ad5cad",
"Salmon",
"PaleTurquoise",
"PaleGreen",
"PeachPuff",
"Orchid",
"#ad5cad"
];
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;
}
let shuffledColors = shuffle(COLORS);
function createDivsForColors(colorArray) {
for (let color of colorArray) {
const newDiv = document.createElement('div');
newDiv.classList.add(color);
newDiv.addEventListener('click', handleCardClick);
gameContainer.append(newDiv);
}
}
/* Logs the event target into the eventLog */
function logEvent(target) {
eventLog.push(target);
}
/* Clears elements in the eventLog */
function clearEventLog() {
eventLog.length = 0;
}
/* Checks if the target already contains the 'clicked' id.
Throws an exception if true */
function checkIdExists(target) {
if (target.id === 'clicked') {
throw 'You already selected this card!';
}
}
function handleCardClick(event) {
/* If noClick is true, prevent any clicks from running this code */
if (noClick) return;
let target = event.target;
/* Flip over the card */
target.style.backgroundColor = target.classList;
/* Increment the guess count and update the score */
try {
checkIdExists(target);
target.setAttribute('id', 'clicked');
logEvent(target);
} catch(error) {
target.removeAttribute('id');
target.removeAttribute('style');
clearEventLog();
// alert(error);
scoreCard.innerText = `Your Score: ${guessCount}`;
return;
}
guessCount += 1
if (eventLog.length === 2) {
/* While two cards are in play,
prevent any further clicks */
noClick = true;
const firstCard = eventLog[0];
const secondCard = eventLog[1];
if (firstCard.className === secondCard.className) {
firstCard.removeEventListener('click', handleCardClick);
secondCard.removeEventListener('click', handleCardClick);
clearEventLog();
noClick = false;
cardsMatched += 2;
} else {
setTimeout(function() {
firstCard.removeAttribute('style');
secondCard.removeAttribute('style');
firstCard.removeAttribute('id');
secondCard.removeAttribute('id');
clearEventLog();
noClick = false;
}, 1000);
}
}
scoreCard.innerText = `Your Score: ${guessCount}`;
if (cardsMatched === COLORS.length) {
playAgain();
}
}
const start = document.getElementById('start');
if (!gameStart) {
const bestScore = document.querySelector('#bestscore p');
if (localStorage.length !== 0) {
bestScore.innerText = `Best Score: ${getBestScore()}`;
}
start.addEventListener('click', function startGame() {
gameStart = true;
createDivsForColors(shuffledColors);
start.removeEventListener('click', startGame);
start.remove();
});
}
function playAgain() {
const buttonLocation = document.querySelector('#button');
const newGame = document.createElement('button');
const bestScore = document.querySelector('#bestscore p');
newGame.innerText = 'Play Again?';
buttonLocation.append(newGame);
newGame.addEventListener('click', function restartGame() {
saveScore();
bestScore.innerText = `Best Score: ${getBestScore()}`;
cardsMatched = 0;
guessCount = 0;
gameContainer.innerHTML = '';
scoreCard.innerHTML = 'Your Score: 0';
shuffle(COLORS);
createDivsForColors(shuffledColors);
newGame.removeEventListener('click', restartGame);
newGame.remove();
});
}
function saveScore() {
if (localStorage.length !== 0) {
const savedScore = getBestScore();
if (guessCount < savedScore) {
localStorage.removeItem('Best Score');
localStorage.setItem('Best Score', guessCount);
}
} else {
localStorage.setItem('Best Score', guessCount);
}
}
function getBestScore() {
return localStorage.getItem('Best Score');
}