UNPKG

@real_one_chess_king/game-logic

Version:
57 lines 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CheckMateGlobalRule = exports.GlobalRule = void 0; const color_1 = require("../../color"); const coordinate_1 = require("../../coordinate"); const piece_constants_1 = require("../../piece/piece.constants"); const moves_tree_utils_1 = require("../../moves-tree/moves-tree.utils"); const affect_utils_1 = require("../../affect/affect.utils"); class GlobalRule { } exports.GlobalRule = GlobalRule; class CheckMateGlobalRule extends GlobalRule { constructor() { super(); } mainPieceType = piece_constants_1.PieceType.King; markNodeWithChilds(node, prevNode, board) { const currentColor = node.color; const kingCoordinate = board.findUniqPiece(currentColor, this.mainPieceType); const enemyKingCoordinate = board.findUniqPiece((0, color_1.reverseColor)(currentColor), this.mainPieceType); // let allMovesLeadToMateForCurrentColor = true; for (const movementFrom in node.movements) { for (const movementTo in node.movements[movementFrom]) { const moveResultData = node.movements[movementFrom][movementTo]; const moveAffect = (0, affect_utils_1.getUserSelectedMoveAffect)(moveResultData.affects); if (prevNode && !prevNode.underCheck && moveAffect.to[0] === enemyKingCoordinate[0] && moveAffect.to[1] === enemyKingCoordinate[1]) { prevNode.underCheck = true; continue; } const nextNode = moveResultData.next; // we search for enemy moves to current color kings position for (const nextMovementFrom in nextNode.movements) { const actualCurrentKingCoordinate = (0, coordinate_1.isCoordinateEql)( // in case king made a move kingCoordinate, moveAffect.from) ? moveAffect.to : kingCoordinate; const nextMoveToCurrentKingKey = (0, moves_tree_utils_1.serializeToCoordinate)( // it doesn't detect if has transforming choice actualCurrentKingCoordinate); const toObj = nextNode.movements[nextMovementFrom]; for (const nextMoveTo in toObj) { if (nextMoveTo.startsWith(nextMoveToCurrentKingKey)) { // leadsToCurrentColorCheck = true; moveResultData.suisidal = true; } } } } } } } exports.CheckMateGlobalRule = CheckMateGlobalRule; //# sourceMappingURL=check-mate.global-rule.js.map