]> git.sesse.net Git - remoteglot/blobdiff - www/js/remoteglot.js
Fix visualization of Chess960 moves where the king does not move.
[remoteglot] / www / js / remoteglot.js
index ff566ab85c81c4ba4b33ff8a9853bec23d8f89ca..f6c06bb56aa8acb9de5178458787d6b33a57f10d 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() {
@@ -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) {
-                       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;
@@ -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 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);
                        }