feat(tarot): useTarotShuffle hook (Fisher-Yates + reversed 플래그) (T9)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-24 00:33:03 +09:00
parent cdf8759aef
commit 1a7dfe73e4
2 changed files with 72 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
import { useCallback, useState } from 'react';
export function fisherYates(input) {
const a = [...input];
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function buildShuffle(deck, size) {
return fisherYates(deck).slice(0, size).map((c) => ({
...c,
reversed: Math.random() < 0.5,
}));
}
export function useTarotShuffle(deck, size = 16) {
const [slice, setSlice] = useState(() => buildShuffle(deck, size));
const reshuffle = useCallback(() => {
setSlice(buildShuffle(deck, size));
}, [deck, size]);
return { slice, reshuffle };
}