From 1f754f53a3fbd0fc93d58fe1ee655be89b8a9f6e Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Fri, 13 Mar 2020 02:00:29 -0500 Subject: [PATCH] fix for castling broken by fix for swapped king and queen --- js/chess.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/js/chess.js b/js/chess.js index a63553b..28716fe 100644 --- a/js/chess.js +++ b/js/chess.js @@ -155,22 +155,22 @@ function legalMoves(board, side, type, from, canCapture){ for (const dir of ortho.concat(diag)) { scanPath(legals, board, side, from, false, dir[0], dir[1], 0); } - if (from[0] === 'd' && from[1] === (isDark(side) ? '8' : '1')) { + if (from[0] === 'e' && from[1] === (isDark(side) ? '8' : '1')) { /* check for castling conditions */ if (!hasMoved(board, side, from)) { - if (boardGet(board, 'c' + from[1], side) === ' ' && - boardGet(board, 'b' + from[1], side) === ' ' && - boardGet(board, 'a' + from[1], side) === 'r') { - if (!hasMoved(board, side, 'a' + from[1])) { - legals.push('b' + from[1]); - } - } - if (boardGet(board, 'e' + from[1], side) === ' ' && - boardGet(board, 'f' + from[1], side) === ' ' && + if (boardGet(board, 'f' + from[1], side) === ' ' && boardGet(board, 'g' + from[1], side) === ' ' && boardGet(board, 'h' + from[1], side) === 'r') { if (!hasMoved(board, side, 'h' + from[1])) { - legals.push('f' + from[1]); + legals.push('g' + from[1]); + } + } + if (boardGet(board, 'd' + from[1], side) === ' ' && + boardGet(board, 'c' + from[1], side) === ' ' && + boardGet(board, 'b' + from[1], side) === ' ' && + boardGet(board, 'a' + from[1], side) === 'r') { + if (!hasMoved(board, side, 'a' + from[1])) { + legals.push('c' + from[1]); } } } @@ -283,15 +283,15 @@ function movePiece(priorBoard, side, from, to){ if (alongside !== ' ') { board['move']['alongside'] = alongside; } - if (type === 'k' && actuallyFrom[0] === 'd' && to[0] === 'b') { + if (type === 'k' && actuallyFrom[0] === 'e' && to[0] === 'g') { board['move']['castle'] = true; - boardPut(board, 'a' + actuallyFrom[1], side, ' '); - boardPut(board, 'c' + actuallyFrom[1], side, 'r'); - } - if (type === 'k' && actuallyFrom[0] === 'd' && to[0] === 'f') { - board['move']['queen_castle'] = true; boardPut(board, 'h' + actuallyFrom[1], side, ' '); - boardPut(board, 'e' + actuallyFrom[1], side, 'r'); + boardPut(board, 'f' + actuallyFrom[1], side, 'r'); + } + if (type === 'k' && actuallyFrom[0] === 'e' && to[0] === 'c') { + board['move']['queen_castle'] = true; + boardPut(board, 'a' + actuallyFrom[1], side, ' '); + boardPut(board, 'd' + actuallyFrom[1], side, 'r'); } if (type === 'p' && (isDark(side) ? (to[1] === '1') : (to[1] === '8'))) { board['move']['promotion'] = 'q'; /* TODO: allow other choices */