prohibit castling through check

This commit is contained in:
Jesse D. McDonald 2020-05-08 20:52:22 -05:00
parent b1caa69920
commit 56cce3340e
1 changed files with 26 additions and 3 deletions

View File

@ -40,8 +40,9 @@ const COLUMNS = 'abcdefgh';
* effect when the game was started.
*
* Version 1: Initial version. (Default if version field is missing.)
* Version 2: Prohibit moving through check.
*/
const CURRENT_VERSION = 1;
const CURRENT_VERSION = 2;
function otherSide(side) {
if (side === LIGHT) {
@ -622,7 +623,18 @@ class Game {
board.isEmpty('f' + row) &&
board.isEmpty('g' + row)) {
if (!this.isInCheck(side)) {
if (this._version < 2) {
yield 'g' + row;
} else {
/* Prohibit castling through check */
const testGame = new Game(this);
testGame._board.putPiece(side, 'e' + row, EMPTY);
testGame._board.putPiece(side, 'f' + row, KING);
testGame._checkCache = {};
if (!testGame.isInCheck(side)) {
yield 'g' + row;
}
}
}
}
@ -631,7 +643,18 @@ class Game {
board.isEmpty('c' + row) &&
board.isEmpty('b' + row)) {
if (!this.isInCheck(side)) {
if (this._version < 2) {
yield 'c' + row;
} else {
/* Prohibit castling through check */
const testGame = new Game(this);
testGame._board.putPiece(side, 'e' + row, EMPTY);
testGame._board.putPiece(side, 'd' + row, KING);
testGame._checkCache = {};
if (!testGame.isInCheck(side)) {
yield 'c' + row;
}
}
}
}
}