
Game by marketing2advertising
π§© Memory Matching Game
Click any two cards to find matching pairs
const emojis = [‘π’,’π’,’π’,’π’,’π’,’π’];
let cards = […emojis, …emojis]; // Duplicate for matching
cards = cards.sort(() => 0.5 – Math.random()); // Shuffle
const gameContainer = document.getElementById(‘memory-game’);
let firstCard = null;
let secondCard = null;
let lockBoard = false;
let matches = 0;
function createCard(emoji, index) {
const card = document.createElement(‘div’);
card.classList.add(‘card’);
card.dataset.emoji = emoji;
card.dataset.index = index;
card.style.width = ’80px’;
card.style.height = ’80px’;
card.style.border = ‘2px solid #000’;
card.style.borderRadius = ‘8px’;
card.style.display = ‘flex’;
card.style.justifyContent = ‘center’;
card.style.alignItems = ‘center’;
card.style.fontSize = ’36px’;
card.style.cursor = ‘pointer’;
card.style.backgroundColor = ‘#f0f0f0’;
card.textContent = ”;
card.addEventListener(‘click’, handleFlip);
gameContainer.appendChild(card);
}
function handleFlip(e) {
if (lockBoard) return;
const card = e.currentTarget;
const emoji = card.dataset.emoji;
if (card === firstCard || card.textContent) return;
card.textContent = emoji;
if (!firstCard) {
firstCard = card;
} else {
secondCard = card;
lockBoard = true;
if (firstCard.dataset.emoji === secondCard.dataset.emoji) {
matches++;
document.getElementById(‘match-msg’).textContent = `β
Match! ${matches} / ${emojis.length}`;
resetTurn();
if (matches === emojis.length) {
document.getElementById(‘match-msg’).textContent = “π You matched all pairs!”;
}
} else {
setTimeout(() => {
firstCard.textContent = ”;
secondCard.textContent = ”;
resetTurn();
}, 1000);
}
}
}
function resetTurn() {
[firstCard, secondCard] = [null, null];
lockBoard = false;
}
// Initialize game
cards.forEach((emoji, index) => createCard(emoji, index));