UNPKG

chess-easy

Version:

Chess engine that makes writing chessgame easier than writing a calculator

59 lines 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fieldToFenSymbol = exports.fenSymbolsToPiecesMapping = exports.cloneGameState = exports.fieldToIndexes = exports.indexesToField = exports.mapLetterToColumnIndex = exports.mapColumnIndexToLetter = void 0; const common_1 = require("./types/common"); function mapColumnIndexToLetter(column) { return String.fromCharCode(column + 97); } exports.mapColumnIndexToLetter = mapColumnIndexToLetter; function mapLetterToColumnIndex(letter) { return letter.charCodeAt(0) - 97; } exports.mapLetterToColumnIndex = mapLetterToColumnIndex; function indexesToField(row, column) { return `${mapColumnIndexToLetter(column)}${row + 1}`; } exports.indexesToField = indexesToField; function fieldToIndexes(field) { const column = mapLetterToColumnIndex(field[0]); const row = Number(field[1]) - 1; return [column, row]; } exports.fieldToIndexes = fieldToIndexes; function cloneGameState(gameState) { const newGameState = []; gameState.forEach(row => { const newRow = []; row.forEach(element => { if (!element) { newRow.push(null); } else { newRow.push({ ...element }); } }); newGameState.push(newRow); }); return newGameState; } exports.cloneGameState = cloneGameState; exports.fenSymbolsToPiecesMapping = { p: { color: common_1.Colors.BLACK, piece: common_1.ChessPieces.PAWN }, r: { color: common_1.Colors.BLACK, piece: common_1.ChessPieces.ROOK }, n: { color: common_1.Colors.BLACK, piece: common_1.ChessPieces.KNIGHT }, b: { color: common_1.Colors.BLACK, piece: common_1.ChessPieces.BISHOP }, q: { color: common_1.Colors.BLACK, piece: common_1.ChessPieces.QUEEN }, k: { color: common_1.Colors.BLACK, piece: common_1.ChessPieces.KING }, P: { color: common_1.Colors.WHITE, piece: common_1.ChessPieces.PAWN }, R: { color: common_1.Colors.WHITE, piece: common_1.ChessPieces.ROOK }, N: { color: common_1.Colors.WHITE, piece: common_1.ChessPieces.KNIGHT }, B: { color: common_1.Colors.WHITE, piece: common_1.ChessPieces.BISHOP }, Q: { color: common_1.Colors.WHITE, piece: common_1.ChessPieces.QUEEN }, K: { color: common_1.Colors.WHITE, piece: common_1.ChessPieces.KING }, }; const fieldToFenSymbol = (field) => { return Object.keys(exports.fenSymbolsToPiecesMapping).find(key => exports.fenSymbolsToPiecesMapping[key].piece === field.piece && exports.fenSymbolsToPiecesMapping[key].color === field.color); }; exports.fieldToFenSymbol = fieldToFenSymbol; //# sourceMappingURL=utils.js.map