block castling out of check

This commit is contained in:
Jesse D. McDonald 2020-04-05 05:24:26 -05:00
parent d8f199d1d1
commit e28ea59095
1 changed files with 23 additions and 7 deletions

View File

@ -504,19 +504,31 @@ class Game {
if (from === (side === DARK ? 'e8' : 'e1')) {
const row = from[1];
/* memoize isInCheck(); may not need it and don't want to look twice */
/* with any other piece this would be recursive, but kings can't capture */
let inCheck = () => {
const check = this.isInCheck(side);
inCheck = () => check;
return check;
};
if (this._castling[side].king &&
board.isEmpty('f' + row) &&
board.isEmpty('g' + row)) {
if (!inCheck()) {
yield 'g' + row;
}
}
if (this._castling[side].queen &&
board.isEmpty('d' + row) &&
board.isEmpty('c' + row) &&
board.isEmpty('b' + row)) {
if (!inCheck()) {
yield 'c' + row;
}
}
}
} else if (type === QUEEN) {
for (const dir of ANY_DIR) {
yield* board.scanPath(side, from, canCapture, dir[0], dir[1], 8);
@ -609,6 +621,9 @@ class Game {
/* start with the opponent's unpaired pieces, the ones that can capture */
let queue = [];
for (const from of sim._board.findPieces(other, true, false)) {
/* this next condition is needed to avoid recursion through legalMoves() */
/* kings can't capture anything anyway, so they can be skipped */
if (sim._board.getPiece(other, from) !== KING) {
if (sim.isLegalMove(other, from, king, true)) {
/* this piece can directly capture the king */
return true;
@ -616,6 +631,7 @@ class Game {
queue.push({ game: sim, from });
}
}
/* arbitrary limit, but a human player would probably miss a 7-move chain too */
const moveLimit = this._moves.length + 6;