记忆游戏重启异常:单点击卡片自动匹配问题求助
Hey Chris, sorry to hear you're stuck with this frustrating bug—let's walk through targeted checks to figure out why single clicks are auto-matching after restarting your game:
Check for incomplete state resets on restart
The most likely culprit is leftover state from the previous game. Make sure you're resetting all match-tracking variables when launching a new game:// Example: Reset these core variables at the start of your restart function let firstCard = null; let secondCard = null; let hasFlippedCard = false; let lockBoard = false;Also verify every card's DOM state is wiped clean—remove classes like
matchedorflippedthat might persist from the last round, and clear any data attributes (likedata-matched) used to track paired status.Ensure symbols are re-shuffled on every restart
Your code creates a paired symbol array, but if you're reusing the same un-shuffled array across restarts, paired cards might land in identical positions as the last game. Combine this with leftover state, and clicking a card could trigger an accidental match with its pre-paired counterpart. Add a shuffle function that runs every time you restart:function shuffle(array) { let currentIndex = array.length, randomIndex; while (currentIndex !== 0) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; } return array; } // Call this in your restart function to generate a fresh shuffled set let symbols = shuffle([...symbol, ...symbol]);Fix duplicate event listeners
If you're adding click listeners to cards without removing old ones first, restarting the game might leave multiple listeners attached to each card. A single click could then trigger multiple rounds of your match logic, making it look like a single card is auto-matching. Either:- Remove existing listeners before generating new cards (using
removeEventListener), or - Generate entirely new card DOM elements each restart (instead of reusing old ones) and attach listeners only to the fresh elements.
- Remove existing listeners before generating new cards (using
Clear the deck before re-populating
Double-check that your restart function empties the deck container before adding new cards. If old cards are left in the deck, their leftover state (like being marked as matched) could interfere with the new game:const deck = document.querySelector('.deck'); // Clear old cards first deck.innerHTML = ''; // Then generate and append new cardsAdd debug logs to trace the match logic
Temporarily add console logs in your click handler to see what's happening when you click a card after restart:card.addEventListener('click', function() { console.log('First card in state:', firstCard); console.log('Second card in state:', secondCard); // Rest of your click logic });If
firstCardisn't null when you click the first card of a new game, that means your state wasn't properly reset—this would cause the click to immediately match with the leftoverfirstCardfrom the last round.
内容的提问来源于stack exchange,提问作者Chris Camp

