Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
209 lines (167 loc) · 5.98 KB
/
Copy pathindex.js
File metadata and controls
209 lines (167 loc) · 5.98 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
document.addEventListener("DOMContentLoaded", function () {
let cards = document.getElementsByClassName("card");
let isMobile = window.innerWidth < 600 ? true : false;
let isGameOver = false;
let gameMode = "easy";
let flipped = [];
let score = 0;
const gameModes = new Map();
gameModes.set("easy", 8);
gameModes.set("medium", 12);
gameModes.set("hard", 16);
// Add event listener for window resize
window.addEventListener("resize", function () {
isMobile = window.innerWidth < 600 ? true : false;
});
function removeEventListeners() {
for (let card of cards) {
card.removeEventListener("click", flipCard);
}
}
function renderCards(gameMode) {
let numberOfCards = gameModes.get(gameMode);
let container = document.querySelector(".card-rows");
let cardRows = [];
let numberOfRows = 2;
gameMode == "easy" ? (numberOfRows = 2) : "";
gameMode == "medium" ? (numberOfRows = 3) : "";
gameMode == "hard" ? (numberOfRows = 4) : "";
let numberOfCols = numberOfCards / numberOfRows;
// Remove existing cards
container.innerHTML = "";
removeEventListeners();
for (let i = 0; i < numberOfRows; i++) {
cardRows[i] = document.createElement("div");
// Create new cards based on the selected game mode
for (let j = 0; j < numberOfCols; j++) {
let cardColumn = document.createElement("div");
let card = document.createElement("div");
card.classList.add("card");
let cardInner = document.createElement("div");
cardInner.classList.add("card-inner");
let cardFront = document.createElement("div");
cardFront.classList.add("card-face", "card-front");
cardFront.textContent = "?";
cardInner.appendChild(cardFront);
card.appendChild(cardInner);
cardColumn.appendChild(card);
cardRows[i].appendChild(cardColumn);
container.appendChild(cardRows[i]);
// Add column styles depending on game mode
gameMode == "easy" ? renderEasy(cardColumn, card, isMobile) : "";
gameMode == "medium" ? renderMedium(cardColumn, card, isMobile) : "";
gameMode == "hard" ? renderHard(cardColumn, card, isMobile) : "";
}
renderRows(cardRows[i]);
}
// Populate the board with new card images
populateBoard(gameModes.get(gameMode));
let fruits = document.getElementsByClassName("fruits");
for (let fruit of fruits) {
gameMode == "hard" ? fruit.style.margin = "-1vw 0vw 0vw 0vw": "";
gameMode == "hard" && isMobile ? fruit.style.margin = "-10vw 0vw 0vw 0vw": "";
}
// Re-add event listeners for flipping cards
flipCard(gameMode);
}
// Populate the back of the cards with images
function populateBoard(numberOfCards) {
let cards = document.getElementsByClassName("card-inner");
let images = [
"images/apple.png",
"images/banana.png",
"images/coconut.png",
"images/grapes.png",
"images/orange.png",
"images/watermelon.png",
"images/pineapple.png",
"images/strawberry.png",
];
images = images.slice(0, numberOfCards / 2);
let imageCounts = {};
for (let card of cards) {
let img = document.createElement("img");
let randomImage = getRandomImage(images, imageCounts);
img.src = randomImage;
img.classList = "fruits";
card.appendChild(img);
}
}
// Find a random image
function getRandomImage(images, imageCounts) {
let randomIndex = Math.floor(Math.random() * images.length);
let randomImage = images[randomIndex];
// Check if the image has already been used twice
while (imageCounts[randomImage] && imageCounts[randomImage] >= 2) {
randomIndex = Math.floor(Math.random() * images.length);
randomImage = images[randomIndex];
}
// Update the image count
imageCounts[randomImage] = (imageCounts[randomImage] || 0) + 1;
return randomImage;
}
// Flip a card when selected
function flipCard(gameMode) {
for (let card of cards) {
card.addEventListener("click", function () {
// Flip a card if chosen
if (flipped.length < 2 && !card.classList.contains("card-flipped")) {
card.classList.add("card-flipped");
flipped.push(card);
}
// If 2 cards flipped and they aren't a match
if (flipped.length == 2 && !getMatch(flipped)) {
const card1 = flipped[0];
const card2 = flipped[1];
if (score > 0) {
score -= 1;
}
printScore(score);
setTimeout(function () {
card1.classList.remove("card-flipped");
card2.classList.remove("card-flipped");
}, 1000);
}
// If 2 cards are a match
if (flipped.length == 2 && getMatch(flipped)) {
const card1 = flipped[0];
const card2 = flipped[1];
score += 5;
printScore(score);
card1.removeEventListener("click", flipCard);
card2.removeEventListener("click", flipCard);
}
if (flipped.length == 2) {
// Reset flip count
flipped.length = 0;
}
gameOver(isGameOver, gameMode, gameModes, resetCards, resetScore, renderCards);
});
}
}
// Check if the flipped cards match
function getMatch(flipped) {
let image1 = flipped[0].querySelector(".fruits");
let image2 = flipped[1].querySelector(".fruits");
if (image1.getAttribute("src") == image2.getAttribute("src")) {
return true;
}
return false;
}
// Make all cards face down
function resetCards() {
let flipped = document.getElementsByClassName("card-flipped");
for (let i = flipped.length - 1; i >= 0; i--) {
flipped[i].classList.remove("card-flipped");
}
}
function resetScore() {
score = 0;
return score;
}
gameMode = setGameMode(gameModes, gameMode, renderCards, resetScore);
renderCards(gameMode);
printScore(score);
flipCard(gameMode);
newGame(resetCards, gameMode, resetScore, renderCards);
});