From 56cce3340e143fff2d29f18aa3f6718f11f9edb8 Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Fri, 8 May 2020 20:52:22 -0500 Subject: [PATCH] prohibit castling through check --- js/pacosako.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/js/pacosako.js b/js/pacosako.js index 474929c..91683c3 100644 --- a/js/pacosako.js +++ b/js/pacosako.js @@ -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)) { - yield 'g' + row; + 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)) { - yield 'c' + row; + 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; + } + } } } }