File size: 4,704 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!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>