useKifuPlayer
useKifuPlayer は、KIF形式などの棋譜文字列をパースし、盤面状態・評価値・候補手などを一元管理するメインのフックです。ShogiBoard や EvalGraph 等に渡す全てのデータをこのフックから取得します。
基本的な使い方
import { useKifuPlayer, ShogiBoard, ControlBar, EvalGraph, CandidateList } from 'react-kifu-player';
function Player({ kifuString }) {
const player = useKifuPlayer(kifuString);
return (
<div>
<ShogiBoard
position={player.position}
lastMove={player.lastMoveCoords || undefined}
onForward={player.forward}
onBackward={player.backward}
/>
<ControlBar
currentPly={player.currentPly}
totalPlies={player.totalPlies}
onForward={player.forward}
onBackward={player.backward}
onGoToStart={player.goToStart}
onGoToEnd={player.goToEnd}
/>
{/* 解析済み棋譜であれば自動でデータが取れる */}
<EvalGraph
data={player.evaluations}
currentPly={player.currentPly}
onPlyClick={(ply) => player.goto(ply)}
/>
<CandidateList
candidates={player.candidates}
onCandidateClick={(cand) => player.playVariation(cand.readMoves, cand.score)}
/>
</div>
);
}
返り値
| プロパティ/メソッド | 型 | 説明 |
|---|---|---|
position | Position | 現在の盤面・持ち駒の状態 |
currentPly | number | 現在の手数 |
totalPlies | number | 本線の総手数 |
lastMoveCoords | {from, to} | null | 最終手の座標(盤面ハイライト用) |
evaluations | EvaluationPoint[] | 各手数の評価値リスト(解析済み棋譜から自動抽出) |
candidates | Candidate[] | 現在手数の候補手リスト(解析済み棋譜から自動抽出) |
header | GameHeader | null | 対局者名・日時などのヘッダー情報 |
moves | MoveNode[] | 棋譜の指し手ノード配列(MoveList コンポーネントに渡す) |
isOnBranch | boolean | 現在分岐(変化手順)上にいるかどうか |
error | string | null | パースエラー情報 |
forward() | () => void | 1手進む |
backward() | () => void | 1手戻る |
goto(ply) | (ply: number) => void | 指定した手数へシーク |
goToStart() | () => void | 初期局面に戻る |
goToEnd() | () => void | 最終局面に進む |
playVariation(moves, score?) | (moves: string, score?: number) => void | 候補手の読み筋を一時的な変化として再生する |
returnToMainLine() | () => void | 分岐再生から本線に戻る |
対応する棋譜形式
useKifuPlayer は以下の形式を自動認識します。
- KIF形式 (
.kif): 最も一般的。変化手順や解析コメント(** 解析 候補N...)も自動パース - KI2形式 (
.ki2) - CSA形式 (
.csa) - SFEN/USI形式
実際の動作デモ
以下は解析情報付きの demo.kifu を読み込み、評価値グラフ・候補手リストと連動するデモです。候補手をクリックすると読み筋が盤面上で再生されます。
Loading Demo...