|
|
|
|
@ -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,9 +34,9 @@ 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) => ({
|
|
|
|
|
const cardNumbers = [...numbers, ...numbers];
|
|
|
|
|
const shuffledCardNumbers = fisherYatesShuffle(cardNumbers);
|
|
|
|
|
const shuffledCards = shuffledCardNumbers.map((number, index) => ({
|
|
|
|
|
id: index,
|
|
|
|
|
number,
|
|
|
|
|
}));
|
|
|
|
|
|