Fisher-Yates 셔플, 카드 픽 생성, 참고 블록/메타데이터 빌더 구현. Task 4(interpret API)·Task 6(UI)에서 소비됨. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
19 lines
556 B
TypeScript
19 lines
556 B
TypeScript
import type { TarotCard } from './cards';
|
|
|
|
export type Pick = { card: TarotCard; position: string; reversed: boolean };
|
|
|
|
export function fisherYates<T>(input: T[]): T[] {
|
|
const a = [...input];
|
|
for (let i = a.length - 1; i > 0; i -= 1) {
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
[a[i], a[j]] = [a[j], a[i]];
|
|
}
|
|
return a;
|
|
}
|
|
|
|
export function buildShuffle(deck: TarotCard[], size: number): (TarotCard & { reversed: boolean })[] {
|
|
return fisherYates(deck)
|
|
.slice(0, size)
|
|
.map((c) => ({ ...c, reversed: Math.random() < 0.5 }));
|
|
}
|