block castling out of check
This commit is contained in:
parent
d8f199d1d1
commit
e28ea59095
|
|
@ -504,19 +504,31 @@ class Game {
|
||||||
if (from === (side === DARK ? 'e8' : 'e1')) {
|
if (from === (side === DARK ? 'e8' : 'e1')) {
|
||||||
const row = from[1];
|
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 &&
|
if (this._castling[side].king &&
|
||||||
board.isEmpty('f' + row) &&
|
board.isEmpty('f' + row) &&
|
||||||
board.isEmpty('g' + row)) {
|
board.isEmpty('g' + row)) {
|
||||||
|
if (!inCheck()) {
|
||||||
yield 'g' + row;
|
yield 'g' + row;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this._castling[side].queen &&
|
if (this._castling[side].queen &&
|
||||||
board.isEmpty('d' + row) &&
|
board.isEmpty('d' + row) &&
|
||||||
board.isEmpty('c' + row) &&
|
board.isEmpty('c' + row) &&
|
||||||
board.isEmpty('b' + row)) {
|
board.isEmpty('b' + row)) {
|
||||||
|
if (!inCheck()) {
|
||||||
yield 'c' + row;
|
yield 'c' + row;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if (type === QUEEN) {
|
} else if (type === QUEEN) {
|
||||||
for (const dir of ANY_DIR) {
|
for (const dir of ANY_DIR) {
|
||||||
yield* board.scanPath(side, from, canCapture, dir[0], dir[1], 8);
|
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 */
|
/* start with the opponent's unpaired pieces, the ones that can capture */
|
||||||
let queue = [];
|
let queue = [];
|
||||||
for (const from of sim._board.findPieces(other, true, false)) {
|
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)) {
|
if (sim.isLegalMove(other, from, king, true)) {
|
||||||
/* this piece can directly capture the king */
|
/* this piece can directly capture the king */
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -616,6 +631,7 @@ class Game {
|
||||||
|
|
||||||
queue.push({ game: sim, from });
|
queue.push({ game: sim, from });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* arbitrary limit, but a human player would probably miss a 7-move chain too */
|
/* arbitrary limit, but a human player would probably miss a 7-move chain too */
|
||||||
const moveLimit = this._moves.length + 6;
|
const moveLimit = this._moves.length + 6;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue