Fix visualization of Chess960 moves where the king does not move.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 9 Feb 2018 18:12:43 +0000 (19:12 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 9 Feb 2018 18:12:43 +0000 (19:12 +0100)
www/js/remoteglot.js

index ff566ab..f6c06bb 100644 (file)
@@ -945,6 +945,32 @@ var possibly_switch_game_from_hash = function() {
        }
 }
 
        }
 }
 
+/**
+ * If this is a Chess960 castling which doesn't move the king,
+ * move the rook instead.
+*/
+var patch_move = function(move) {
+       if (move === null) return null;
+       if (move.from !== move.to) return move;
+
+       var f = move.rook_sq & 15;
+       var r = move.rook_sq >> 4;
+       var from = ('abcdefgh'.substring(f,f+1) + '87654321'.substring(r,r+1));
+       var to = move.to;
+
+       if (move.to === 'g1') {
+               to = 'f1';
+       } else if (move.to === 'g8') {
+               to = 'f8';
+       } else if (move.to === 'b1') {
+               to = 'c1';
+       } else if (move.to === 'b8') {
+               to = 'c8';
+       }
+
+       return { from: from, to: to };
+}
+
 /** Update all the HTML on the page, based on current global state.
  */
 var update_board = function() {
 /** Update all the HTML on the page, based on current global state.
  */
 var update_board = function() {
@@ -1174,7 +1200,8 @@ var update_board = function() {
                // draw a continuation arrow as long as it's the same piece
                var last_to;
                for (var i = 0; i < data['pv'].length; i += 2) {
                // draw a continuation arrow as long as it's the same piece
                var last_to;
                for (var i = 0; i < data['pv'].length; i += 2) {
-                       var move = hiddenboard.move(data['pv'][i]);
+                       var move = patch_move(hiddenboard.move(data['pv'][i]));
+
                        if ((i >= 2 && move.from != last_to) ||
                             interfering_arrow(move.from, move.to)) {
                                break;
                        if ((i >= 2 && move.from != last_to) ||
                             interfering_arrow(move.from, move.to)) {
                                break;
@@ -1187,7 +1214,7 @@ var update_board = function() {
                var alt_moves = find_nonstupid_moves(data, 30, data['position']['toplay'] === 'B');
                for (var i = 1; i < alt_moves.length && i < 3; ++i) {
                        hiddenboard = new Chess(base_fen);
                var alt_moves = find_nonstupid_moves(data, 30, data['position']['toplay'] === 'B');
                for (var i = 1; i < alt_moves.length && i < 3; ++i) {
                        hiddenboard = new Chess(base_fen);
-                       var move = hiddenboard.move(alt_moves[i]);
+                       var move = patch_move(hiddenboard.move(alt_moves[i]));
                        if (move !== null) {
                                create_arrow(move.from, move.to, '#f66', 1, 10);
                        }
                        if (move !== null) {
                                create_arrow(move.from, move.to, '#f66', 1, 10);
                        }