feat(phase2): 타로 셔플·reference 순수 유틸 + 테스트
Fisher-Yates 셔플, 카드 픽 생성, 참고 블록/메타데이터 빌더 구현. Task 4(interpret API)·Task 6(UI)에서 소비됨. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
18
lib/tarot/shuffle.ts
Normal file
18
lib/tarot/shuffle.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
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 }));
|
||||
}
|
||||
Reference in New Issue
Block a user