-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (109 loc) · 3.57 KB
/
Copy pathscript.js
File metadata and controls
129 lines (109 loc) · 3.57 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
let gameBoard, movesEl, timeEl, bestScoreEl, messageEl;
let firstCard, secondCard, lockBoard, moves, matchedPairs, timer, seconds;
let sounds = {};
document.addEventListener("DOMContentLoaded", () => {
gameBoard = document.getElementById("game-board");
movesEl = document.getElementById("moves");
timeEl = document.getElementById("time");
bestScoreEl = document.getElementById("best-score");
messageEl = document.getElementById("message");
// Load sounds
sounds.flip = new Audio("sounds/flip.mp3");
sounds.match = new Audio("sounds/match.mp3");
sounds.wrong = new Audio("sounds/wrong.mp3");
sounds.win = new Audio("sounds/win.mp3");
loadBestScore();
});
function startGame() {
clearInterval(timer);
moves = 0;
matchedPairs = 0;
seconds = 0;
firstCard = null;
secondCard = null;
lockBoard = false;
movesEl.textContent = moves;
timeEl.textContent = seconds;
messageEl.textContent = "";
let difficulty = document.getElementById("difficulty").value;
let rows, cols, pairs;
if (difficulty === "easy") { rows = 4; cols = 4; pairs = 8; }
if (difficulty === "medium") { rows = 4; cols = 5; pairs = 10; }
if (difficulty === "hard") { rows = 6; cols = 6; pairs = 18; }
gameBoard.style.gridTemplateColumns = `repeat(${cols}, 80px)`;
const emojis = ["😀","🎉","🚀","🐱","🍕","⚽","🎧","🌈","🔥","💡","🎂","✈️","📚","💻","🦋","🍫","🥇","🧩"];
let cards = emojis.slice(0, pairs);
cards = [...cards, ...cards]; // duplicate
cards.sort(() => 0.5 - Math.random()); // shuffle
gameBoard.innerHTML = "";
cards.forEach((emoji) => {
const card = document.createElement("div");
card.classList.add("card");
card.dataset.emoji = emoji;
card.innerHTML = "❓";
card.addEventListener("click", flipCard);
gameBoard.appendChild(card);
});
timer = setInterval(() => {
seconds++;
timeEl.textContent = seconds;
}, 1000);
}
function flipCard() {
if (lockBoard || this.classList.contains("flipped") || this.classList.contains("matched")) return;
sounds.flip.play();
this.classList.add("flipped");
this.innerHTML = this.dataset.emoji;
if (!firstCard) {
firstCard = this;
return;
}
secondCard = this;
moves++;
movesEl.textContent = moves;
checkMatch();
}
function checkMatch() {
if (firstCard.dataset.emoji === secondCard.dataset.emoji) {
sounds.match.play();
firstCard.classList.add("matched");
secondCard.classList.add("matched");
matchedPairs++;
resetTurn();
if (matchedPairs === gameBoard.children.length / 2) {
endGame();
}
} else {
lockBoard = true;
sounds.wrong.play();
setTimeout(() => {
firstCard.classList.remove("flipped");
secondCard.classList.remove("flipped");
firstCard.innerHTML = "❓";
secondCard.innerHTML = "❓";
resetTurn();
}, 1000);
}
}
function resetTurn() {
[firstCard, secondCard, lockBoard] = [null, null, false];
}
function endGame() {
clearInterval(timer);
sounds.win.play();
messageEl.textContent = `🎉 You won in ${seconds}s and ${moves} moves!`;
// Save best score
let best = JSON.parse(localStorage.getItem("bestScore")) || {time: Infinity, moves: Infinity};
if (seconds < best.time || (seconds === best.time && moves < best.moves)) {
localStorage.setItem("bestScore", JSON.stringify({time: seconds, moves: moves}));
loadBestScore();
}
}
function loadBestScore() {
let best = JSON.parse(localStorage.getItem("bestScore"));
if (best) {
bestScoreEl.textContent = `${best.time}s, ${best.moves} moves`;
} else {
bestScoreEl.textContent = "--";
}
}