UXInteract / task_002 /solution /code /index.html
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
4.7 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>4x4 Card Memory Match</title>
<style>
body { font-family: system-ui, sans-serif; background: #1b2430; color: #fff; text-align: center; margin: 0; padding: 16px; }
h1 { color: #ffd166; }
#stats { margin: 8px; font-size: 16px; }
#grid {
display: grid;
grid-template-columns: repeat(4, 90px);
grid-template-rows: repeat(4, 90px);
gap: 10px;
justify-content: center;
margin: 20px auto;
}
.card {
width: 90px; height: 90px;
perspective: 600px;
cursor: pointer;
background: none; border: none; padding: 0;
}
.inner {
position: relative; width: 100%; height: 100%;
transition: transform 0.4s; transform-style: preserve-3d;
}
.card.flip .inner, .card.matched .inner { transform: rotateY(180deg); }
.face {
position: absolute; width: 100%; height: 100%;
backface-visibility: hidden; border-radius: 10px;
display: flex; align-items: center; justify-content: center; font-size: 40px;
}
.front { background: #3a86ff; }
.back { background: #ffd166; color: #1b2430; transform: rotateY(180deg); }
.card.matched .back { background: #06d6a0; }
button#reset { padding: 8px 18px; font-size: 16px; border: none; border-radius: 6px;
background: #ef476f; color: #fff; cursor: pointer; }
</style>
</head>
<body>
<h1>4x4 Card Memory Match</h1>
<div id="stats">Matched <span id="matched">0</span> / 8 pairs · Moves <span id="moves">0</span></div>
<div id="grid"></div>
<button id="reset">Restart</button>
<script>
const SYMBOLS = ['🍎', '🍌', '🍇', '🍑', '🍓', '🍉', '🥝', '🍒']; // 8 pairs -> 4x4 = 16 cards
const gridEl = document.getElementById('grid');
const matchedEl = document.getElementById('matched');
const movesEl = document.getElementById('moves');
let firstCard = null;
let lock = false;
let matchedPairs = 0;
let moves = 0;
let pendingMismatch = null;
function shuffle(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
function newGame(deckOverride) {
gridEl.innerHTML = '';
firstCard = null; lock = false; pendingMismatch = null; matchedPairs = 0; moves = 0;
matchedEl.textContent = '0';
movesEl.textContent = '0';
const deck = deckOverride ? [...deckOverride] : shuffle([...SYMBOLS, ...SYMBOLS]); // 16 cards
deck.forEach((sym) => {
const card = document.createElement('button');
card.className = 'card';
card.dataset.symbol = sym;
card.innerHTML = '<div class="inner">' +
'<div class="face front">❓</div>' +
'<div class="face back">' + sym + '</div></div>';
card.addEventListener('click', () => flipCard(card));
gridEl.appendChild(card);
});
}
function flipCard(card) {
if (lock || card.classList.contains('flip') || card.classList.contains('matched')) return;
card.classList.add('flip');
if (!firstCard) {
firstCard = card;
return;
}
moves++;
movesEl.textContent = moves;
if (firstCard.dataset.symbol === card.dataset.symbol) {
firstCard.classList.add('matched');
card.classList.add('matched');
firstCard = null;
matchedPairs++;
matchedEl.textContent = matchedPairs;
if (matchedPairs === SYMBOLS.length) {
setTimeout(() => alert('🎉 All pairs matched! Moves: ' + moves), 200);
}
} else {
lock = true;
const a = firstCard;
firstCard = null;
const settle = () => {
a.classList.remove('flip');
card.classList.remove('flip');
lock = false;
pendingMismatch = null;
};
pendingMismatch = { remaining: 700, settle };
setTimeout(() => { if (pendingMismatch?.settle === settle) settle(); }, 700);
}
}
document.getElementById('reset').addEventListener('click', () => newGame());
newGame();
window.gameTest = {
reset(deck) { if (deck && deck.length !== 16) throw new Error('deck must contain 16 values'); newGame(deck); },
flip(index) { gridEl.children[index]?.click(); return this.state(); },
advance(ms) { if (pendingMismatch) { pendingMismatch.remaining -= ms; if (pendingMismatch.remaining <= 0) pendingMismatch.settle(); } return this.state(); },
state() { return { cards: [...gridEl.children].map(c => ({ value: c.dataset.symbol, faceUp: c.classList.contains('flip') || c.classList.contains('matched'), matched: c.classList.contains('matched') })), moves, matchedPairs }; }
};
</script>
</body>
</html>