X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fbase_rules.js;h=924f737426ab174eb999f4b9655fe960524bfc35;hb=89021f181ac0689bbc785ce0ebd9a910e66352b0;hp=cc8292c482f70f61c04255063f51b10b16cc168c;hpb=0e16cb26f6380f57f1079ece9bdb654243073bde;p=vchess.git diff --git a/client/src/base_rules.js b/client/src/base_rules.js index cc8292c4..924f7374 100644 --- a/client/src/base_rules.js +++ b/client/src/base_rules.js @@ -59,7 +59,7 @@ export const ChessRules = class ChessRules return (f.charCodeAt()<=90 ? "w"+f.toLowerCase() : "b"+f); } - // Check if FEN describe a position + // Check if FEN describe a board situation correctly static IsGoodFen(fen) { const fenParsed = V.ParseFen(fen); @@ -748,10 +748,11 @@ export const ChessRules = class ChessRules continue; // If this code is reached, rooks and king are on initial position - // Nothing on the path of the king ? - // (And no checks; OK also if y==finalSquare) - let step = finalSquares[castleSide][0] < y ? -1 : 1; - for (i=y; i!=finalSquares[castleSide][0]; i+=step) + // Nothing on the path of the king ? (and no checks) + const finDist = finalSquares[castleSide][0] - y; + let step = finDist / Math.max(1, Math.abs(finDist)); + i = y; + do { if (this.isAttacked([x,i], [oppCol]) || (this.board[x][i] != V.EMPTY && // NOTE: next check is enough, because of chessboard constraints @@ -760,10 +761,12 @@ export const ChessRules = class ChessRules { continue castlingCheck; } + i += step; } + while (i!=finalSquares[castleSide][0]); // Nothing on the path to the rook? - step = castleSide == 0 ? -1 : 1; + step = (castleSide == 0 ? -1 : 1); for (i = y + step; i != this.INIT_COL_ROOK[c][castleSide]; i += step) { if (this.board[x][i] != V.EMPTY)