UNPKG

kokopu

Version:

A JavaScript/TypeScript library implementing the chess game rules and providing tools to read/write the standard chess file formats.

109 lines (108 loc) 5.55 kB
/*! * -------------------------------------------------------------------------- * * * * Kokopu - A JavaScript/TypeScript chess library. * * <https://www.npmjs.com/package/kokopu> * * Copyright (C) 2018-2025 Yoann Le Montagner <yo35 -at- melix.net> * * * * Kokopu is free software: you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public License * * as published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version. * * * * Kokopu is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General * * Public License along with this program. If not, see * * <http://www.gnu.org/licenses/>. * * * * -------------------------------------------------------------------------- */ import { PositionImpl } from './impl'; import { MoveDescriptorImpl } from './move_descriptor_impl'; import { MoveDescriptor } from '../move_descriptor'; /** * Whether the given position is legal and the player to play is in check. */ export declare function isCheck(position: PositionImpl): boolean; /** * Whether the given position is legal and the player to play is checkmated. */ export declare function isCheckmate(position: PositionImpl): boolean; /** * Whether the given position is legal and the player to play is stalemated. */ export declare function isStalemate(position: PositionImpl): boolean; /** * Whether the given position is legal and there is insufficient material on board for checkmate. */ export declare function isDead(position: PositionImpl, uscfRules: boolean): boolean; /** * Whether there is at least 1 possible move in the given position. * * @returns `false` if the position is not legal. */ export declare function hasMove(position: PositionImpl): boolean; /** * Return all the legal moves in the given position. */ export declare function moves(position: PositionImpl): MoveDescriptor[]; /** * For antichess, return `true` if the current player can capture something. For other variants, always returns `false`. * * Precondition: the position must be legal. */ export declare function isCaptureMandatory(position: PositionImpl): boolean; /** * Delegated method for checking whether a castling move is legal or not. WARNING: in case of Chess960, `to` represents the origin square of the rook (KxR). * * Precondition: {@link refreshEffectiveCastling} must have been invoked beforehand. */ export declare function isCastlingMoveLegal(position: PositionImpl, from: number, to: number): MoveDescriptorImpl | false; /** * Core algorithm to determine whether a move is legal or not. The verification flow is the following: * * 1. Ensure that the position itself is legal. * 2. Ensure that the origin square contains a piece (denoted as the moving-piece) * whose color is the same than the color of the player about to play. * 3. Special routine for castling detection. * 4. Ensure that the displacement is geometrically correct, with respect to the moving piece. * 5. Check the content of the destination square. * 6. For the sliding pieces (and in case of a 2-square pawn move), ensure that there is no piece * on the trajectory. * * The move is almost ensured to be legal at this point. The last condition to check * is whether the king of the current player will be in check after the move or not. * * 7. Execute the displacement from the origin to the destination square, in such a way that * it can be reversed. Only the state of the board is updated at this point. * 8. Look for king attacks. * 9. Reverse the displacement. */ export declare function isMoveLegal(position: PositionImpl, from: number, to: number): RegularMoveDescriptor | PromotionMoveDescriptor | false; interface RegularMoveDescriptor { type: 'regular'; moveDescriptor: MoveDescriptorImpl; } interface PromotionMoveDescriptor { type: 'promotion'; moveDescriptorFactory: (promotion: number) => MoveDescriptorImpl | false; } /** * Play the move corresponding to the given descriptor. */ export declare function play(position: PositionImpl, descriptor: MoveDescriptorImpl): void; /** * Determine if a null-move (i.e. switching the player about to play) can be played in the current position. * A null-move is possible if the position is legal and if the current player about to play is not in check. */ export declare function isNullMoveLegal(position: PositionImpl): boolean; /** * Play a null-move on the current position if it is legal. * * @returns `true` if the null-move has actually been played. */ export declare function playNullMove(position: PositionImpl): boolean; export {};