-
Couldn't load subscription status.
- Fork 5k
Description
Code Battle - Memory Puzzle
Score: 0
Save Scoreconst emojis = ['π»','β‘','π₯','π','π§ ','π±οΈ','β¨οΈ','π§©'];
let cards = [...emojis, ...emojis];
cards.sort(() => 0.5 - Math.random());
const grid = document.getElementById("gameGrid");
let first = null, second = null, lock = false, score = 0;
cards.forEach((emoji, index) => {
const card = document.createElement("div");
card.className = "card";
card.dataset.emoji = emoji;
card.innerText = "?";
card.onclick = () => flip(card);
grid.appendChild(card);
});
function flip(card) {
if (lock || card.innerText !== "?") return;
card.innerText = card.dataset.emoji;
if (!first) {
first = card;
} else {
second = card;
check();
}
}
function check() {
lock = true;
if (first.dataset.emoji === second.dataset.emoji) {
score += 10;
document.getElementById("score").innerText = score;
first = second = null;
lock = false;
} else {
setTimeout(() => {
first.innerText = "?";
second.innerText = "?";
first = second = null;
lock = false;
}, 1000);
}
}
function saveScore() {
alert("Score saved locally: " + score);
}
body {
margin: 0;
font-family: Arial;
background: linear-gradient(to right, #1e3c72, #2a5298);
color: white;
text-align: center;
}
h1 {
margin-top: 20px;
}
.grid {
display: grid;
grid-template-columns: repeat(4, 100px);
gap: 10px;
justify-content: center;
margin-top: 30px;
}
.card {
width: 100px;
height: 100px;
background: white;
color: black;
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
border-radius: 8px;
user-select: none;
}
#scoreBox {
margin-top: 20px;
}
button {
padding: 10px 20px;
background: gold;
border: none;
font-weight: bold;
cursor: pointer;
}