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:
25
src/pages/tarot/hooks/useTarotShuffle.js
Normal file
25
src/pages/tarot/hooks/useTarotShuffle.js
Normal 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 };
|
||||
}
|
||||
Reference in New Issue
Block a user