-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
225 lines (187 loc) · 6.62 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
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
const gameContainer = document.getElementById("game");
const cardsCollection = [];
const cardsFlippedDisplay = document.querySelector("#flipped-cards-display")
const bestScoreDisplay = document.querySelector("#best-score-display");
const playBtn = document.querySelector("#playBtn");
const restartBtn = document.querySelector("#restartBtn");
const playAgainBtn = document.querySelector("#playAgainBtn");
const cardsFlippedElement = document.querySelector("#cardsCounterElement");
const winModal = document.querySelector("#win-container");
const modalExitBtn = document.querySelector("#exit-button");
let flipByTwoCounter = 0; // Resets when two cards have been flipped
let bestScore = (localStorage.getItem("score") || 0);
let CARDS_FLIPPED = 0;
let GAME_WON = false;
const COLORS = [
"red-back",
"blue-back",
"green-back",
"orange-back",
"purple-back",
"red-back",
"blue-back",
"green-back",
"orange-back",
"purple-back"
];
const cardCheckArray = [];
// here is a helper function to shuffle an array
// it returns the same array with values shuffled
// it is based on an algorithm called Fisher Yates if you want ot research more
function shuffle(array) {
let counter = array.length;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
let index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
let shuffledColors = shuffle(COLORS);
// this function loops over the array of colors
// it creates a new div and gives it a class with the value of the color
// it also adds an event listener for a click for each card
function createDivsForColors(colorArray) {
for (let color of colorArray) {
// create a new div
const newDiv = document.createElement("div");
// Card Container
newDiv.classList.add("card-container");
// scene for 3d card flip
const sceneDiv = document.createElement("div");
sceneDiv.classList.add("scene-div");
sceneDiv.appendChild(newDiv);
const newDivFront = document.createElement("div");
newDivFront.classList.add("card-front");
newDivFront.classList.add("card-face");
newDiv.appendChild(newDivFront);
const newDivBack = document.createElement("div");
newDivBack.classList.add(color);
newDivBack.classList.add("card-face");
newDiv.appendChild(newDivBack);
// call a function handleCardClick when a div is clicked on
newDiv.addEventListener("click", handleCardClick);
// Add each card element to an array
cardsCollection.push(sceneDiv);
// append the div to the element with an id of game
gameContainer.append(sceneDiv);
}
}
// TODO: Implement this function!
function handleCardClick(event) {
if (event.target.parentElement.parentElement.classList.contains("clicked") || event.target.parentElement.parentElement.classList.contains("disabled")) { // If card has already been clicked, exit the function
return;
}
// Update cards flipped display
CARDS_FLIPPED++;
cardsFlippedDisplay.innerText = CARDS_FLIPPED;
// Match checking logic
flipByTwoCounter++;
if (flipByTwoCounter > 2) { // Array full, reset and insert new card
cardCheckArray.splice(0, cardCheckArray.length)
cardCheckArray.push(event.target.nextElementSibling);
flipByTwoCounter = 1;
} else if (flipByTwoCounter === 2) { // Two cards ready to be checked
cardCheckArray.push(event.target.nextElementSibling);
if (cardCheckArray[0].classList.item(0) !== cardCheckArray[1].classList.item(0)) {
handleFail();
}
} else { // Insert first card into array
cardCheckArray.push(event.target.nextElementSibling);
}
event.target.parentElement.parentElement.classList.add("clicked"); // Flagging the element as clicked
event.target.parentElement.classList.add("is-flipped") // CSS flip animation
// Check to see if user has won
GAME_WON = cardsCollection.every(function (card) {
return card.classList.contains("clicked");
});
if (GAME_WON) handleWin();
}
function handleWin() {
if (localStorage.getItem("score")) {
if (Number(localStorage.getItem("score")) > CARDS_FLIPPED) {
localStorage.setItem("score", CARDS_FLIPPED);
bestScore = localStorage.getItem("score");
}
} else {
localStorage.setItem("score", CARDS_FLIPPED);
bestScore = localStorage.getItem("score");
}
setBestScore();
updateView("win-screen");
}
function handleFail() {
cardCheckArray.splice(0, cardCheckArray.length); // Empty array
flipByTwoCounter = 0;
for (let card of cardsCollection) { // Temporarily disable click events
card.classList.add("disabled");
}
flipCardsToStartingOrientation();
}
function flipCardsToStartingOrientation() {
setTimeout(() => {
for (let card of cardsCollection) {
card.classList.remove("disabled");
card.classList.remove("clicked");
card.firstElementChild.classList.remove("is-flipped");
}
}, 1000);
}
function setBestScore() {
bestScoreDisplay.innerText = bestScore;
}
function updateView(gameState) {
switch (gameState) {
case 'mainmenu-screen':
break;
case 'play-screen':
playBtn.classList.add("hidden");
gameContainer.classList.remove("hidden");
restartBtn.classList.remove("hidden");
cardsFlippedElement.classList.remove("hidden");
winModal.classList.add("hidden");
break;
case 'win-screen':
winModal.classList.remove("hidden");
break;
default:
console.log("Error invalid argument");
}
}
function restartGame() {
updateView('play-screen');
for (let card of cardsCollection) {
card.remove();
}
cardsCollection.splice(0, cardsCollection.length);
shuffledColors = shuffle(COLORS);
createDivsForColors(shuffledColors);
cardCheckArray.splice(0, cardCheckArray.length); // Clear array
CARDS_FLIPPED = 0;
GAME_WON = false;
flipByTwoCounter = 0
cardsFlippedDisplay.innerText = CARDS_FLIPPED;
for (let card of cardsCollection) {
card.classList.remove("disabled");
card.classList.remove("clicked");
card.firstElementChild.classList.remove("is-flipped");
}
}
// when the DOM loads
createDivsForColors(shuffledColors);
setBestScore();
// Event Listeners
playBtn.addEventListener('click', () => {
updateView("play-screen");
})
modalExitBtn.addEventListener('click', () => {
winModal.classList.add("hidden");
})
restartBtn.addEventListener('click', restartGame);
playAgainBtn.addEventListener('click', restartGame);