-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcards-age.js
126 lines (110 loc) · 4.64 KB
/
cards-age.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
const flipSound = new Audio("flipcard.mp3");
const matchSound = new Audio("match.mp3");
const winSound = new Audio("win.mp3");
var errors = 0;
var score = 0;
var matchedPairs = 0;
var totalPairs = 8;
var startTime, timerInterval;
var cardList = ["2", "3", "4", "5", "6", "7", "8", "9"];
var cardSet, board = [];
var rows = 4, columns = 4;
var card1Selected, card2Selected;
var flipTimeout;
window.onload = function() {
shuffleCards();
startGame();
document.getElementById("timer").innerText = "Time: 0s";
}
function shuffleCards() {
cardSet = cardList.concat(cardList);
for (let i = 0; i < cardSet.length; i++) {
let j = Math.floor(Math.random() * cardSet.length);
[cardSet[i], cardSet[j]] = [cardSet[j], cardSet[i]];
}
}
function startGame() {
for (let r = 0; r < rows; r++) {
let row = [];
for (let c = 0; c < columns; c++) {
let cardImg = cardSet.pop();
row.push(cardImg);
let card = document.createElement("img");
card.id = `${r}-${c}`;
card.src = cardImg + ".png";
card.classList.add("card");
card.addEventListener("click", selectCard);
document.getElementById("board").append(card);
}
board.push(row);
}
setTimeout(hideCards, 10);
}
function hideCards() {
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
document.getElementById(`${r}-${c}`).src = "back.png";
}
}
}
function startTimer() {
startTime = new Date();
timerInterval = setInterval(() => {
let timeElapsed = Math.floor((new Date() - startTime) / 1000);
document.getElementById("timer").innerText = `Time: ${timeElapsed}s`;
}, 1000);
}
function stopTimer() {
clearInterval(timerInterval);
}
function selectCard() {
if (!startTime) startTimer(); // Start timer only on first card click
if (this.src.includes("back") && !card2Selected) { // Only proceed if a card pair isn’t being processed
if (flipTimeout) clearTimeout(flipTimeout); // Clear any pending timeout to avoid freezing
if (!card1Selected) {
card1Selected = this;
revealCard(card1Selected);
matchSound.play(); // Play flip sound
} else if (this != card1Selected) {
card2Selected = this;
revealCard(card2Selected);
matchSound.play(); // Play flip sound
flipTimeout = setTimeout(checkMatch, 500); // Check for match after a short delay
}
}
}
function revealCard(card) {
let [r, c] = card.id.split("-").map(Number);
card.src = board[r][c] + ".png";
}
function checkMatch() {
if (card1Selected && card2Selected) {
if (card1Selected.src === card2Selected.src) {
let timeTaken = Math.floor((new Date() - startTime) / 1000);
let tempScore = 50 - (errors * 5) - (timeTaken * 0.2);
score += tempScore < 5 ? 5 : tempScore;
errors = 0;
document.getElementById("score").innerText = score;
matchedPairs++;
matchSound.play(); // Play match sound
if (matchedPairs === totalPairs) {
endGame();
}
} else {
card1Selected.src = "back.png";
card2Selected.src = "back.png";
errors++;
}
card1Selected = null;
card2Selected = null;
}
}
function endGame() {
stopTimer();
document.getElementById("finalScore").innerText = score;
document.getElementById("endModal").style.display = "flex";
winSound.play(); // Play win sound
}
function restartGame() {
location.reload();
}