Skip to content

Commit

Permalink
Handle non-empty squares between king and rook square when castling i…
Browse files Browse the repository at this point in the history
…n 960
  • Loading branch information
atinm committed Dec 18, 2020
1 parent dce4d2a commit d58243b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions chess.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,13 @@ var Chess = function(fen = '', variant = 'Standard') {
(board[castling_to - 1] == null || board[castling_to - 1].type == KING)) {
var ok = true
for (var i = castling_from; i != castling_to; (castling_to >= castling_from) ? i++ : i--) {
if ((board[i] != null && board[i].type != ROOK && board[i].type != KING) || attacked(them, i)) {
if (attacked(them, i)) { // king's path should be safe from attack
ok = false
break
}
}
for (var i = ROOKS[us][KSIDE_CASTLE_INDEX].square; ok && i >= castling_to - 1; i--) {
if (board[i] != null && board[i].type != ROOK && board[i].type != KING) { // rook's path should be clear of other pieces
ok = false
break
}
Expand Down Expand Up @@ -734,7 +740,13 @@ var Chess = function(fen = '', variant = 'Standard') {
(board[castling_to + 1] == null || board[castling_to + 1].type == KING)) {
var ok = true
for (var i = castling_from; i != castling_to; (castling_to <= castling_from) ? i-- : i++) {
if ((board[i] != null && board[i].type != ROOK && board[i].type != KING) || attacked(them, i)) {
if (attacked(them, i)) { // king's path should be safe
ok = false
break
}
}
for (var i = ROOKS[us][QSIDE_CASTLE_INDEX].square; ok && i <= castling_to; i++) {
if (board[i] != null && board[i].type != ROOK && board[i].type != KING) { // rook's path should be clear of other pieces
ok = false
break
}
Expand Down

0 comments on commit d58243b

Please sign in to comment.