UNPKG

kokopu

Version:

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

224 lines 9.27 kB
"use strict"; /*! * -------------------------------------------------------------------------- * * * * 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/>. * * * * -------------------------------------------------------------------------- */ Object.defineProperty(exports, "__esModule", { value: true }); exports.isColor = isColor; exports.isPiece = isPiece; exports.isColoredPiece = isColoredPiece; exports.isFile = isFile; exports.isRank = isRank; exports.isSquare = isSquare; exports.isSquareCouple = isSquareCouple; exports.isCastle = isCastle; exports.isCastle960 = isCastle960; exports.isGameResult = isGameResult; exports.isGameVariant = isGameVariant; exports.forEachSquare = forEachSquare; exports.squareColor = squareColor; exports.squareToCoordinates = squareToCoordinates; exports.coordinatesToSquare = coordinatesToSquare; exports.oppositeColor = oppositeColor; exports.variantWithCanonicalStartPosition = variantWithCanonicalStartPosition; exports.nagSymbol = nagSymbol; exports.isValidECO = isValidECO; const exception_1 = require("./exception"); const base_types_impl_1 = require("./private_position/base_types_impl"); const impl_1 = require("./private_position/impl"); const common_1 = require("./private_game/common"); /** * Whether the given value represents a valid {@link Color} or not. */ function isColor(value) { return (0, base_types_impl_1.colorFromString)(value) >= 0; } /** * Whether the given value represents a valid {@link Piece} or not. */ function isPiece(value) { return (0, base_types_impl_1.pieceFromString)(value) >= 0; } /** * Whether the given value represents a valid {@link ColoredPiece} or not. */ function isColoredPiece(value) { return (0, base_types_impl_1.coloredPieceFromString)(value) >= 0; } /** * Whether the given value represents a valid {@link File} or not. */ function isFile(value) { return (0, base_types_impl_1.fileFromString)(value) >= 0; } /** * Whether the given value represents a valid {@link Rank} or not. */ function isRank(value) { return (0, base_types_impl_1.rankFromString)(value) >= 0; } /** * Whether the given value represents a valid {@link Square} or not. */ function isSquare(value) { return (0, base_types_impl_1.squareFromString)(value) >= 0; } /** * Whether the given value represents a valid {@link SquareCouple} or not. */ function isSquareCouple(value) { return typeof value === 'string' && /^[a-h][1-8][a-h][1-8]$/.test(value); } /** * Whether the given value represents a valid {@link Castle} or not. */ function isCastle(value) { return typeof value === 'string' && /^[wb][kq]$/.test(value); } /** * Whether the given value represents a valid {@link Castle960} or not. */ function isCastle960(value) { return typeof value === 'string' && /^[wb][a-h]$/.test(value); } /** * Whether the given value represents a valid {@link GameResult} or not. */ function isGameResult(value) { return (0, base_types_impl_1.resultFromString)(value) >= 0; } /** * Whether the given value represents a valid {@link GameVariant} or not. */ function isGameVariant(value) { return (0, base_types_impl_1.variantFromString)(value) >= 0; } /** * Execute the given callback on each of the 64 squares. */ function forEachSquare(callback) { for (let rank = 0; rank < 8; ++rank) { for (let file = 0; file < 8; ++file) { callback((0, base_types_impl_1.squareToString)(rank * 16 + file)); } } } /** * Return the color of a square. */ function squareColor(square) { const squareCode = (0, base_types_impl_1.squareFromString)(square); if (squareCode < 0) { throw new exception_1.IllegalArgument('squareColor()'); } return (0, base_types_impl_1.colorToString)((0, base_types_impl_1.squareColorImpl)(squareCode)); } /** * Return the coordinates of a square. */ function squareToCoordinates(square) { const squareCode = (0, base_types_impl_1.squareFromString)(square); if (squareCode < 0) { throw new exception_1.IllegalArgument('squareToCoordinates()'); } return { rank: Math.trunc(squareCode / 16), file: squareCode % 16 }; } function coordinatesToSquare(fileOrCoordinates, rankOrUndefined) { const file = typeof fileOrCoordinates === 'number' ? fileOrCoordinates : fileOrCoordinates.file; const rank = typeof fileOrCoordinates === 'number' ? rankOrUndefined : fileOrCoordinates.rank; if (!Number.isInteger(file) || !Number.isInteger(rank) || file < 0 || file >= 8 || rank < 0 || rank >= 8) { throw new exception_1.IllegalArgument('coordinatesToSquare()'); } return (0, base_types_impl_1.fileToString)(file) + (0, base_types_impl_1.rankToString)(rank); } /** * Change white to black, and black to white. */ function oppositeColor(color) { const colorCode = (0, base_types_impl_1.colorFromString)(color); if (colorCode < 0) { throw new exception_1.IllegalArgument('oppositeColor()'); } return (0, base_types_impl_1.colorToString)(1 - colorCode); } /** * Whether the given variant has a canonical start position or not. */ function variantWithCanonicalStartPosition(variant) { const variantCode = (0, base_types_impl_1.variantFromString)(variant); if (variantCode < 0) { throw new exception_1.IllegalArgument('variantWithCanonicalStartPosition()'); } return (0, impl_1.hasCanonicalStartPosition)(variantCode); } const NAG_SYMBOLS = new Map(); /* eslint-disable @stylistic/no-multi-spaces, @stylistic/space-in-parens */ NAG_SYMBOLS.set(1, '!'); // good move NAG_SYMBOLS.set(2, '?'); // bad move NAG_SYMBOLS.set(3, '!!'); // very good move NAG_SYMBOLS.set(4, '??'); // very bad move NAG_SYMBOLS.set(5, '!?'); // interesting move NAG_SYMBOLS.set(6, '?!'); // questionable move NAG_SYMBOLS.set(7, '\u25a1'); // Only move NAG_SYMBOLS.set(8, '\u25a1'); // Only move (ChessBase) NAG_SYMBOLS.set(9, '\u2612'); // Worst move (Chess.com) NAG_SYMBOLS.set(10, '='); // equal position NAG_SYMBOLS.set(11, '='); // equal position (ChessBase) NAG_SYMBOLS.set(13, '\u221e'); // unclear position NAG_SYMBOLS.set(14, '\u2a72'); // White has a slight advantage NAG_SYMBOLS.set(15, '\u2a71'); // Black has a slight advantage NAG_SYMBOLS.set(16, '\u00b1'); // White has a moderate advantage NAG_SYMBOLS.set(17, '\u2213'); // Black has a moderate advantage NAG_SYMBOLS.set(18, '+\u2212'); // White has a decisive advantage NAG_SYMBOLS.set(19, '\u2212+'); // Black has a decisive advantage NAG_SYMBOLS.set(20, '+\u2212\u2212'); // White has a crushing advantage NAG_SYMBOLS.set(21, '\u2212\u2212+'); // Black has a crushing advantage NAG_SYMBOLS.set(22, '\u2a00'); // Zugzwang NAG_SYMBOLS.set(32, '\u27f3'); // Development advantage NAG_SYMBOLS.set(36, '\u2191'); // Initiative NAG_SYMBOLS.set(40, '\u2192'); // Attack NAG_SYMBOLS.set(44, '\u2bf9'); // Compensation NAG_SYMBOLS.set(132, '\u21c6'); // Counterplay NAG_SYMBOLS.set(138, '\u2a01'); // Zeitnot NAG_SYMBOLS.set(140, '\u2206'); // With idea... NAG_SYMBOLS.set(141, '\u2207'); // Aimed against... NAG_SYMBOLS.set(142, '\u2313'); // Better is... NAG_SYMBOLS.set(143, '\u2264'); // Worse is... NAG_SYMBOLS.set(145, 'RR'); // Editorial comment NAG_SYMBOLS.set(146, 'N'); // Novelty /* eslint-enable */ /** * Return the human-readable symbol for the given [NAG](https://en.wikipedia.org/wiki/Numeric_Annotation_Glyphs). */ function nagSymbol(nag) { if (!(0, common_1.isValidNag)(nag)) { throw new exception_1.IllegalArgument('nagSymbol()'); } const result = NAG_SYMBOLS.get(nag); return result ?? '$' + nag; } /** * Whether the given string represents a valid [ECO code](https://en.wikipedia.org/wiki/List_of_chess_openings) or not. */ function isValidECO(eco) { return typeof eco === 'string' && /^[A-E][0-9][0-9]$/.test(eco); } //# sourceMappingURL=helper.js.map