feat : add move classification

This commit is contained in:
GuillaumeSD
2024-03-29 03:53:16 +01:00
parent 714ed1039e
commit d9b322d9fa
9 changed files with 257 additions and 16 deletions

View File

@@ -60,5 +60,26 @@ export const useChessActions = (chessAtom: PrimitiveAtom<Chess>) => {
setGame(newGame);
}, [copyGame, setGame]);
return { setPgn, reset, makeMove, undoMove };
const goToMove = useCallback(
(moveIdx: number, game: Chess) => {
if (moveIdx < 0) return;
const newGame = new Chess();
newGame.loadPgn(game.pgn());
const movesNb = game.history().length;
if (moveIdx > movesNb) return;
let lastMove: Move | null = null;
for (let i = movesNb; i > moveIdx; i--) {
lastMove = newGame.undo();
}
setGame(newGame);
playSoundFromMove(lastMove);
},
[setGame]
);
return { setPgn, reset, makeMove, undoMove, goToMove };
};