chlss
Version:
Open-Source Chess Engine in TypeScript.
101 lines (100 loc) • 3.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UCI = void 0;
const boardPosition_1 = require("./boardPosition");
const moveMaker_1 = require("./moveMaker");
const square_1 = require("./square");
const boardNotation_1 = require("./boardNotation");
const piece_1 = require("./piece");
class UCI {
static write(move) {
if (moveMaker_1.MoveMaker.isCastlingMove(move)) {
return this.writeCastle(move);
}
function getPromotionCodeOrEmpty(figure) {
if (figure == undefined) {
return "";
}
else if (figure == "bishop") {
return "b";
}
else if (figure == "queen") {
return "q";
}
else if (figure == "rook") {
return "r";
}
else if (figure == "knight") {
return "n";
}
throw new Error("");
}
const fromN = boardNotation_1.BoardNotation.toBoardNotation(move.from);
const toN = boardNotation_1.BoardNotation.toBoardNotation(move.to);
return `${fromN}${toN}${getPromotionCodeOrEmpty(move.promotion)}`;
}
static parse(move, legalMoves, boardPosition) {
const result = UCI.regex.exec(move);
if (result == null) {
throw new Error("");
}
const fromNotation = result[1];
const toNotation = result[2];
const promotionString = result[3];
function promotionToFigureOrUndefined(promotion) {
if (promotion == "") {
return undefined;
}
else if (promotion == "q")
return "queen";
else if (promotion == "r")
return "rook";
else if (promotion == "b")
return "bishop";
else if (promotion == "n")
return "knight";
throw new Error("");
}
const from = boardNotation_1.BoardNotation.fromBoardNotation(fromNotation);
const to = boardNotation_1.BoardNotation.fromBoardNotation(toNotation);
const figure = promotionToFigureOrUndefined(promotionString);
if (this.isCastlingMoveUCI(fromNotation, toNotation, boardPosition)) {
function validateTo(to) {
return (toNotation == "g1" && to == 63) || (toNotation == "c1" && to == 56)
|| (toNotation == "g8" && to == 7) || (toNotation == "c8" && to == 0);
}
return legalMoves.find(x => piece_1.Piece.isKing(x.piece) && x.from == from && validateTo(x.to));
}
let pickedMove = legalMoves.find(x => x.from == from && x.to == to);
pickedMove.promotion = figure;
return pickedMove;
}
static isCastlingMoveUCI(from, to, boardPosition) {
const piece = boardPosition_1.BoardPosition.getPiece(boardPosition, boardNotation_1.BoardNotation.fromBoardNotation(from));
return piece_1.Piece.isKing(piece) && ((from == "e8" && to == "c8") ||
(from == "e1" && to == "c1") ||
(from == "e8" && to == "g8") ||
(from == "e1" && to == "g1"));
}
static writeCastle(move) {
if (move.from == (0, square_1.sq)(4)) {
if (move.to == (0, square_1.sq)(0)) {
return "e8c8";
}
else if (move.to == (0, square_1.sq)(7)) {
return "e8g8";
}
}
else if (move.from == (0, square_1.sq)(60)) {
if (move.to == (0, square_1.sq)(56)) {
return "e1c1";
}
else if (move.to == (0, square_1.sq)(63)) {
return "e1g1";
}
}
throw new Error("UCI -> Bad castle move to write.");
}
}
UCI.regex = /^([abcdefgh][1-8])([abcdefgh][1-8])([qrbn]?)$/;
exports.UCI = UCI;