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
45 lines (44 loc) • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNegativeRayAttacks = exports.getPositiveRayAttacks = void 0;
var emptyBoardRayAttacks_1 = require("./emptyBoardRayAttacks");
/**
* Gets the reachable squares from a square in a positive direction
* @param occupiedBoard a Bitboard representing all the pieces on the board
* @param direction a string representing a positive direction
* @param position the index of a position
* @returns a Bitboard representing the reachable squares from a square in a positive direction
*/
function getPositiveRayAttacks(occupiedBoard, direction, position) {
// @TODO: add tests
var attacks = emptyBoardRayAttacks_1.EMPTY_BOARD_RAY_ATTACKS[position][direction];
// Intersect the attacks and all the pieces to find the blockers
var blockers = attacks.and(occupiedBoard);
// If there are blockers find the closest one and return the moves from the square to this blocker
if (!blockers.isZero()) {
position = blockers.bitScanForward();
attacks = attacks.xor(emptyBoardRayAttacks_1.EMPTY_BOARD_RAY_ATTACKS[position][direction]);
}
return attacks;
}
exports.getPositiveRayAttacks = getPositiveRayAttacks;
/**
* Gets the reachable squares from a square in a negative direction
* @param occupiedBoard a Bitboard representing all the pieces on the board
* @param direction a string representing a negative direction
* @param position the index of a position
* @returns a Bitboard representing the reachable squares from a square in a negative direction
*/
function getNegativeRayAttacks(occupiedBoard, direction, position) {
// @TODO: add tests
var attacks = emptyBoardRayAttacks_1.EMPTY_BOARD_RAY_ATTACKS[position][direction];
// Intersect the attacks and all the pieces to find the blockers
var blockers = attacks.and(occupiedBoard);
// If there are blockers find the closest one and return the moves from the square to this blocker
if (!blockers.isZero()) {
position = blockers.bitScanReverse();
attacks = attacks.xor(emptyBoardRayAttacks_1.EMPTY_BOARD_RAY_ATTACKS[position][direction]);
}
return attacks;
}
exports.getNegativeRayAttacks = getNegativeRayAttacks;