@react-chess-tools/react-chess-game
Version:
react-chess-game is a React component bridging chess.js with react-chessboard to offer a full-featured, ready-to-integrate chess board experience.
144 lines (133 loc) • 4.41 kB
text/typescript
import { ChessboardOptions } from 'react-chessboard';
import * as react from 'react';
import * as chess_js from 'chess.js';
import { Color, Chess } from 'chess.js';
type Sound = "check" | "move" | "capture" | "gameOver";
type Sounds = Record<Sound, HTMLAudioElement>;
type SoundsProps = {
sounds?: Partial<Record<Sound, string>>;
};
interface ChessGameProps {
options?: ChessboardOptions;
}
interface RootProps {
fen?: string;
orientation?: Color;
}
type useChessGameProps = {
fen?: string;
orientation?: Color;
};
declare const useChessGame: ({ fen, orientation: initialOrientation, }?: useChessGameProps) => {
game: Chess;
currentFen: string;
currentPosition: string;
orientation: Color;
currentMoveIndex: number;
isLatestMove: boolean;
info: {
turn: Color;
isPlayerTurn: boolean;
isOpponentTurn: boolean;
moveNumber: number;
lastMove: chess_js.Move | undefined;
isCheck: boolean;
isCheckmate: boolean;
isDraw: boolean;
isStalemate: boolean;
isThreefoldRepetition: boolean;
isInsufficientMaterial: boolean;
isGameOver: boolean;
isDrawn: boolean;
hasPlayerWon: boolean;
hasPlayerLost: boolean;
};
methods: {
makeMove: (move: Parameters<Chess["move"]>[0]) => boolean;
setPosition: (fen: string, orientation: Color) => void;
flipBoard: () => void;
goToMove: (moveIndex: number) => void;
goToStart: () => void;
goToEnd: () => void;
goToPreviousMove: () => void;
goToNextMove: () => void;
};
};
declare const useChessGameContext: () => {
game: chess_js.Chess;
currentFen: string;
currentPosition: string;
orientation: chess_js.Color;
currentMoveIndex: number;
isLatestMove: boolean;
info: {
turn: chess_js.Color;
isPlayerTurn: boolean;
isOpponentTurn: boolean;
moveNumber: number;
lastMove: chess_js.Move | undefined;
isCheck: boolean;
isCheckmate: boolean;
isDraw: boolean;
isStalemate: boolean;
isThreefoldRepetition: boolean;
isInsufficientMaterial: boolean;
isGameOver: boolean;
isDrawn: boolean;
hasPlayerWon: boolean;
hasPlayerLost: boolean;
};
methods: {
makeMove: (move: string | {
from: string;
to: string;
promotion?: string | undefined;
} | null) => boolean;
setPosition: (fen: string, orientation: chess_js.Color) => void;
flipBoard: () => void;
goToMove: (moveIndex: number) => void;
goToStart: () => void;
goToEnd: () => void;
goToPreviousMove: () => void;
goToNextMove: () => void;
};
};
type ChessGameContextType = ReturnType<typeof useChessGame>;
type KeyboardControlsProps = {
controls?: KeyboardControls;
};
type KeyboardControls = Record<string, (context: ChessGameContextType) => void>;
declare const KeyboardControls: React.FC<KeyboardControlsProps>;
declare const ChessGame: {
Root: react.FC<react.PropsWithChildren<RootProps>>;
Board: react.FC<ChessGameProps>;
Sounds: react.FC<SoundsProps>;
KeyboardControls: react.FC<{
controls?: KeyboardControls | undefined;
}>;
};
/**
* Returns an object with information about the current state of the game. This can be determined
* using chess.js instance, but this function is provided for convenience.
* @param game - The Chess.js instance representing the game.
* @returns An object with information about the current state of the game.
*/
declare const getGameInfo: (game: Chess, orientation: Color) => {
turn: Color;
isPlayerTurn: boolean;
isOpponentTurn: boolean;
moveNumber: number;
lastMove: chess_js.Move | undefined;
isCheck: boolean;
isCheckmate: boolean;
isDraw: boolean;
isStalemate: boolean;
isThreefoldRepetition: boolean;
isInsufficientMaterial: boolean;
isGameOver: boolean;
isDrawn: boolean;
hasPlayerWon: boolean;
hasPlayerLost: boolean;
};
type GameInfo = ReturnType<typeof getGameInfo>;
export { ChessGame, type ChessGameContextType, type ChessGameProps, type GameInfo, KeyboardControls, type RootProps, type Sound, type Sounds, type SoundsProps, useChessGame, useChessGameContext, type useChessGameProps };