From 44de1992e3aca16676b0c3738786665a4c57c47f Mon Sep 17 00:00:00 2001 From: prathoseraaj Date: Fri, 3 Oct 2025 14:42:45 +0530 Subject: [PATCH] fix: copilot issues --- memory-game/components/MemoryGame.jsx | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/memory-game/components/MemoryGame.jsx b/memory-game/components/MemoryGame.jsx index 4b00a026..4ee828ed 100644 --- a/memory-game/components/MemoryGame.jsx +++ b/memory-game/components/MemoryGame.jsx @@ -2,6 +2,16 @@ import React, { useEffect, useState } from "react"; +// Fisher-Yates shuffle for unbiased randomization +function fisherYatesShuffle(array) { + const arr = array.slice(); + 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; +} + const MemoryGame = () => { const [gridSize, setGridSize] = useState(2); @@ -24,12 +34,12 @@ const MemoryGame = () => { const pairCount = Math.floor(totalCards / 2); const numbers = [...Array(pairCount).keys()].map((n) => n + 1); - const shuffledCards = [...numbers, ...numbers] - .sort(() => Math.random() - 0.5) - .map((number, index) => ({ - id: index, - number, - })); + const cardNumbers = [...numbers, ...numbers]; + const shuffledCardNumbers = fisherYatesShuffle(cardNumbers); + const shuffledCards = shuffledCardNumbers.map((number, index) => ({ + id: index, + number, + })); setArray(shuffledCards); setFlipped([]); @@ -92,7 +102,7 @@ const MemoryGame = () => {

Memory Game

{/* Grid Size */}
- +