@real_one_chess_king/game-logic
Version:
R.O.C.K. chess game logic
52 lines • 2.52 kB
JavaScript
import { reverseColor } from "../../color";
import { isCoordinateEql } from "../../coordinate";
import { PieceType } from "../../piece/piece.constants";
import { serializeToCoordinate } from "../../moves-tree/moves-tree.utils";
import { getUserSelectedMoveAffect } from "../../affect/affect.utils";
export class GlobalRule {
}
export class CheckMateGlobalRule extends GlobalRule {
constructor() {
super();
}
mainPieceType = PieceType.King;
markNodeWithChilds(node, prevNode, board) {
const currentColor = node.color;
const kingCoordinate = board.findUniqPiece(currentColor, this.mainPieceType);
const enemyKingCoordinate = board.findUniqPiece(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 = 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 = isCoordinateEql(
// in case king made a move
kingCoordinate, moveAffect.from)
? moveAffect.to
: kingCoordinate;
const nextMoveToCurrentKingKey = 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;
}
}
}
}
}
}
}
//# sourceMappingURL=check-mate.global-rule.js.map