<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Card Game</title>
<style>
body {
font-family: 'Arial', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: #f0f8ff;
margin: 0;
padding: 20px;
}
h1 {
color: #2c3e50;
margin-bottom: 10px;
}
.game-container {
display: flex;
flex-direction: column;
align-items: center;
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.stats {
display: flex;
justify-content: space-between;
width: 100%;
margin-bottom: 15px;
font-weight: bold;
}
.board {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
margin-bottom: 15px;
}
.card {
width: 80px;
height: 80px;
background-color: #3498db;
border-radius: 5px;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
cursor: pointer;
transition: all 0.3s;
color: white;
user-select: none;
}
.card.flipped {
background-color: #2ecc71;
transform: rotateY(180deg);
}
.card.matched {
background-color: #9b59b6;
cursor: default;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
.success-message {
margin-top: 15px;
font-weight: bold;
color: #27ae60;
display: none;
}
</style>
</head>
<body>
<div class="game-container">
<h1>Memory Card Game</h1>
<div class="stats">
<div>Moves: <span id="moves">0</span></div>
<div>Pairs found: <span id="pairs">0</span>/6</div>
</div>
<div class="board" id="board"></div>
<button id="restart-btn">Restart Game</button>
<div class="success-message" id="success-message">
Congratulations! You won in <span id="final-moves">0</span> moves!
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const board = document.getElementById('board');
const movesDisplay = document.getElementById('moves');
const pairsDisplay = document.getElementById('pairs');
const restartBtn = document.getElementById('restart-btn');
const successMessage = document.getElementById('success-message');
const finalMovesDisplay = document.getElementById('final-moves');
let cards = ['🐶', '🐱', '🐭', '🐹', '🐰', '🦊', '🐶', '🐱', '🐭', '🐹', '🐰', '🦊'];
let flippedCards = [];
let moves = 0;
let pairsFound = 0;
// Shuffle the cards
function shuffleCards() {
cards.sort(() => Math.random() - 0.5);
}
// Initialize the game
function initGame() {
shuffleCards();
board.innerHTML = '';
flippedCards = [];
moves = 0;
pairsFound = 0;
movesDisplay.textContent = moves;
pairsDisplay.textContent = pairsFound;
successMessage.style.display = 'none';
cards.forEach((card, index) => {
const cardElement = document.createElement('div');
cardElement.classList.add('card');
cardElement.textContent = '?';
cardElement.dataset.index = index;
cardElement.dataset.value = card;
cardElement.addEventListener('click', flipCard);
board.appendChild(cardElement);
});
}
// Flip card function
function flipCard() {
if (flippedCards.length === 2 || this.classList.contains('flipped') || this.classList.contains('matched')) {
return;
}
this.classList.add('flipped');
this.textContent = this.dataset.value;
flippedCards.push(this);
if (flippedCards.length === 2) {
moves++;
movesDisplay.textContent = moves;
checkMatch();
}
}
// Check if the two flipped cards match
function checkMatch() {
const [card1, card2] = flippedCards;
if (card1.dataset.value === card2.dataset.value) {
// Match found
card1.classList.add('matched');
card2.classList.add('matched');
pairsFound++;
pairsDisplay.textContent = pairsFound;
if (pairsFound === 6) {
// Game won
finalMovesDisplay.textContent = moves;
successMessage.style.display = 'block';
}
} else {
// No match - flip back after delay
setTimeout(() => {
card1.classList.remove('flipped');
card2.classList.remove('flipped');
card1.textContent = '?';
card2.textContent = '?';
}, 1000);
}
flippedCards = [];
}
// Event listeners
restartBtn.addEventListener('click', initGame);
// Start the game
initGame();
});
</script>
</body>
</html>