kokopu
Version:
A JavaScript/TypeScript library implementing the chess game rules and providing tools to read/write the standard chess file formats.
154 lines • 7.24 kB
JavaScript
"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.MoveDescriptorImpl = void 0;
const base_types_impl_1 = require("./base_types_impl");
const exception_1 = require("../exception");
const move_descriptor_1 = require("../move_descriptor");
/* eslint-disable @stylistic/no-multi-spaces */
const CASTLING_FLAG = 0x01;
const EN_PASSANT_FLAG = 0x02;
const CAPTURE_FLAG = 0x04;
const PROMOTION_FLAG = 0x08;
/* eslint-enable */
/**
* Implementation of {@link MoveDescriptor}.
*/
class MoveDescriptorImpl extends move_descriptor_1.MoveDescriptor {
/**
* Instantiate a normal (aka. non-en-passant, non-promotion, non-castling) move.
*/
static make(from, to, movingColoredPiece, capturedColoredPiece) {
const flags = capturedColoredPiece === -1 /* SpI.EMPTY */ ? 0x00 : CAPTURE_FLAG;
return new MoveDescriptorImpl(flags, from, to, movingColoredPiece, movingColoredPiece, capturedColoredPiece, -1, -1);
}
/**
* Instantiate a castling move.
*/
static makeCastling(from, to, rookFrom, rookTo, color) {
const movingColoredKing = 0 /* PieceImpl.KING */ * 2 + color;
const movingColoredRook = 2 /* PieceImpl.ROOK */ * 2 + color;
return new MoveDescriptorImpl(CASTLING_FLAG, from, to, movingColoredKing, movingColoredKing, movingColoredRook, rookFrom, rookTo);
}
/**
* Instantiate a *en-passant* capture.
*/
static makeEnPassant(from, to, enPassantSquare, color) {
const flags = EN_PASSANT_FLAG | CAPTURE_FLAG;
const movingColoredPawn = 5 /* PieceImpl.PAWN */ * 2 + color;
const capturedColoredPawn = 5 /* PieceImpl.PAWN */ * 2 + 1 - color;
return new MoveDescriptorImpl(flags, from, to, movingColoredPawn, movingColoredPawn, capturedColoredPawn, enPassantSquare, -1);
}
/**
* Instantiate a promotion.
*/
static makePromotion(from, to, color, capturedColoredPiece, promotion) {
const flags = PROMOTION_FLAG | (capturedColoredPiece === -1 /* SpI.EMPTY */ ? 0x00 : CAPTURE_FLAG);
const movingColoredPawn = 5 /* PieceImpl.PAWN */ * 2 + color;
const finalColoredPiece = promotion * 2 + color;
return new MoveDescriptorImpl(flags, from, to, movingColoredPawn, finalColoredPiece, capturedColoredPiece, -1, -1);
}
constructor(flags, from, to, movingColoredPiece, finalColoredPiece, optionalColoredPiece, optionalSquare1, optionalSquare2) {
super();
this._flags = flags;
this._from = from;
this._to = to;
this._movingColoredPiece = movingColoredPiece;
this._finalColoredPiece = finalColoredPiece;
this._optionalColoredPiece = optionalColoredPiece; // Captured (colored) piece in case of capture, moving (colored) rook in case of castling.
this._optionalSquare1 = optionalSquare1; // Rook-from or en-passant square.
this._optionalSquare2 = optionalSquare2; // Rook-to.
}
isCastling() {
return (this._flags & CASTLING_FLAG) !== 0;
}
isEnPassant() {
return (this._flags & EN_PASSANT_FLAG) !== 0;
}
isCapture() {
return (this._flags & CAPTURE_FLAG) !== 0;
}
isPromotion() {
return (this._flags & PROMOTION_FLAG) !== 0;
}
from() {
return (0, base_types_impl_1.squareToString)(this._from);
}
to() {
return (0, base_types_impl_1.squareToString)(this._to);
}
color() {
return (0, base_types_impl_1.colorToString)(this._movingColoredPiece % 2);
}
movingPiece() {
return (0, base_types_impl_1.pieceToString)(Math.trunc(this._movingColoredPiece / 2));
}
movingColoredPiece() {
return (0, base_types_impl_1.coloredPieceToString)(this._movingColoredPiece);
}
capturedPiece() {
if (!this.isCapture()) {
throw new exception_1.IllegalArgument('MoveDescriptor.capturedPiece()');
}
return (0, base_types_impl_1.pieceToString)(Math.trunc(this._optionalColoredPiece / 2));
}
capturedColoredPiece() {
if (!this.isCapture()) {
throw new exception_1.IllegalArgument('MoveDescriptor.capturedColoredPiece()');
}
return (0, base_types_impl_1.coloredPieceToString)(this._optionalColoredPiece);
}
rookFrom() {
if (!this.isCastling()) {
throw new exception_1.IllegalArgument('MoveDescriptor.rookFrom()');
}
return (0, base_types_impl_1.squareToString)(this._optionalSquare1);
}
rookTo() {
if (!this.isCastling()) {
throw new exception_1.IllegalArgument('MoveDescriptor.rookTo()');
}
return (0, base_types_impl_1.squareToString)(this._optionalSquare2);
}
enPassantSquare() {
if (!this.isEnPassant()) {
throw new exception_1.IllegalArgument('MoveDescriptor.enPassantSquare()');
}
return (0, base_types_impl_1.squareToString)(this._optionalSquare1);
}
promotion() {
if (!this.isPromotion()) {
throw new exception_1.IllegalArgument('MoveDescriptor.promotion()');
}
return (0, base_types_impl_1.pieceToString)(Math.trunc(this._finalColoredPiece / 2));
}
coloredPromotion() {
if (!this.isPromotion()) {
throw new exception_1.IllegalArgument('MoveDescriptor.coloredPromotion()');
}
return (0, base_types_impl_1.coloredPieceToString)(this._finalColoredPiece);
}
}
exports.MoveDescriptorImpl = MoveDescriptorImpl;
//# sourceMappingURL=move_descriptor_impl.js.map