UNPKG

chess-legal-moves

Version:

Analyses a given chess game position in Fen notation to return legal moves and provides the next game position after a given move

58 lines (57 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertNumbersToPoints = exports.getRankCells = exports.getRanks = exports.parseBoard = void 0; /** * Parses a string representing the chess board an array of 64 strings * @param boardString - a string representing the board as the first part of FEN notation * @returns an array of 64 strings representing the pieces and empty squares * * @internal */ function parseBoard(boardString) { var boardCells = []; var rankList = getRanks(boardString); for (var i = rankList.length - 1; i >= 0; i--) { boardCells.push.apply(boardCells, getRankCells(rankList[i])); } return boardCells; } exports.parseBoard = parseBoard; /** * Splits a board represented as a string into 8 strings representing the chessboard ranks * @param boardString - a string representing the board as the first part of FEN notation * @returns an array of 8 strings representing the chessboard ranks * * @internal */ function getRanks(boardString) { return boardString.split('/'); } exports.getRanks = getRanks; /** * Splits a chessboard rank represented as a string into an array of 8 characters reprenting the pieces and empty squares * @param rank - a string representing one of the chessboard ranks * @returns an array of 8 characters reprenting the pieces and empty squares * * @internal */ function getRankCells(rank) { rank = convertNumbersToPoints(rank); return rank.split(''); } exports.getRankCells = getRankCells; /** * Converts a chessboard rank represented as a string possibly containing numbers to represent the number of consecutive empty squares * into a rank containing points to represent each empty square * @param rank - a string representing one of the chessboard ranks * @returns a rank with dots to represent empty squares * * @internal */ function convertNumbersToPoints(rank) { for (var i = 1; i <= 8; i++) { rank = rank.replaceAll(i.toString(), '.'.repeat(i)); } return rank; } exports.convertNumbersToPoints = convertNumbersToPoints;