Flip cards and find matching pairs. Built with HashMap logic.
Cards are randomized using the Fisher-Yates shuffle, ensuring a perfectly uniform distribution.
function shuffle<T>(array: T[]): T[] {
const arr = [...array];
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;
}When you flip a card, we store it in a Map keyed by its emoji value. Checking for a match is O(1) time complexity.
const cardMap = new Map<string, CardType>();
function checkMatch(card: CardType): boolean {
if (cardMap.has(card.emoji)) {
return cardMap.get(card.emoji)?.id !== card.id;
}
cardMap.set(card.emoji, card);
return false;
}All game state is managed with React hooks, making the logic predictable and easy to follow.