]> git.sesse.net Git - remoteglot/blob - www/js/remoteglot.js
cb38ebe91e5ea6413a236277effd25475e1d30c4
[remoteglot] / www / js / remoteglot.js
1 (function() {
2
3 /** @type {window.ChessBoard} @private */
4 var board = null;
5
6 /** @type {window.ChessBoard} @private */
7 var hiddenboard = null;
8
9 /** @type {Array.<{
10  *      from_col: number,
11  *      from_row: number,
12  *      to_col: number,
13  *      to_row: number,
14  *      line_width: number,
15  *      arrow_size: number,
16  *      fg_color: string
17  * }>}
18  * @private
19  */
20 var arrows = [];
21
22 /** @type {Array.<Array.<boolean>>} */
23 var occupied_by_arrows = [];
24
25 var refutation_lines = [];
26
27 /** @type {!number} @private */
28 var move_num = 1;
29
30 /** @type {!string} @private */
31 var toplay = 'W';
32
33 /** @type {number} @private */
34 var ims = 0;
35
36 /** @type {boolean} @private */
37 var sort_refutation_lines_by_score = true;
38
39 /** @type {!string|undefined} @private */
40 var highlight_from = undefined;
41
42 /** @type {!string|undefined} @private */
43 var highlight_to = undefined;
44
45 /** @type {!number} @private */
46 var unique = Math.random();
47
48 /** The current position on the board, represented as a FEN string.
49  * @type {?string}
50  * @private
51  */
52 var fen = null;
53
54 /** @typedef {{
55  *    start_fen: string,
56  *    uci_pv: Array.<string>,
57  *    pretty_pv: Array.<string>
58  * }} DisplayLine
59  */
60
61 /** @type {Array.<DisplayLine>}
62  * @private
63  */
64 var display_lines = [];
65
66 /** @type {?DisplayLine} @private */
67 var current_display_line = null;
68
69 /** @type {?number} @private */
70 var current_display_move = null;
71
72 var request_update = function() {
73         $.ajax({
74                 url: "http://analysis.sesse.net/analysis.pl?ims=" + ims + "&unique=" + unique
75                 //url: "http://analysis.sesse.net:5000/analysis.pl?ims=" + ims + "&unique=" + unique
76         }).done(function(data, textstatus, xhr) {
77                 ims = xhr.getResponseHeader('X-Remoteglot-Last-Modified');
78                 var num_viewers = xhr.getResponseHeader('X-Remoteglot-Num-Viewers');
79                 update_board(data, num_viewers);
80         }).fail(function() {
81                 // Wait ten seconds, then try again.
82                 setTimeout(function() { request_update(); }, 10000);
83         });
84 }
85
86 var clear_arrows = function() {
87         for (var i = 0; i < arrows.length; ++i) {
88                 if (arrows[i].svg) {
89                         arrows[i].svg.parentElement.removeChild(arrows[i].svg);
90                         delete arrows[i].svg;
91                 }
92         }
93         arrows = [];
94
95         occupied_by_arrows = [];
96         for (var y = 0; y < 8; ++y) {
97                 occupied_by_arrows.push([false, false, false, false, false, false, false, false]);
98         }
99 }
100
101 var redraw_arrows = function() {
102         for (var i = 0; i < arrows.length; ++i) {
103                 position_arrow(arrows[i]);
104         }
105 }
106
107 /** @param {!number} x
108  * @return {!number}
109  */
110 var sign = function(x) {
111         if (x > 0) {
112                 return 1;
113         } else if (x < 0) {
114                 return -1;
115         } else {
116                 return 0;
117         }
118 }
119
120 /** See if drawing this arrow on the board would cause unduly amount of confusion.
121  * @param {!string} from The square the arrow is from (e.g. e4).
122  * @param {!string} to The square the arrow is to (e.g. e4).
123  * @return {boolean}
124  */
125 var interfering_arrow = function(from, to) {
126         var from_col = from.charCodeAt(0) - "a1".charCodeAt(0);
127         var from_row = from.charCodeAt(1) - "a1".charCodeAt(1);
128         var to_col   = to.charCodeAt(0) - "a1".charCodeAt(0);
129         var to_row   = to.charCodeAt(1) - "a1".charCodeAt(1);
130
131         occupied_by_arrows[from_row][from_col] = true;
132
133         // Knight move: Just check that we haven't been at the destination before.
134         if ((Math.abs(to_col - from_col) == 2 && Math.abs(to_row - from_row) == 1) ||
135             (Math.abs(to_col - from_col) == 1 && Math.abs(to_row - from_row) == 2)) {
136                 return occupied_by_arrows[to_row][to_col];
137         }
138
139         // Sliding piece: Check if anything except the from-square is seen before.
140         var dx = sign(to_col - from_col);
141         var dy = sign(to_row - from_row);
142         var x = from_col;
143         var y = from_row;
144         do {
145                 x += dx;
146                 y += dy;
147                 if (occupied_by_arrows[y][x]) {
148                         return true;
149                 }
150                 occupied_by_arrows[y][x] = true;
151         } while (x != to_col || y != to_row);
152
153         return false;
154 }
155
156 /** Find a point along the coordinate system given by the given line,
157  * <t> units forward from the start of the line, <u> units to the right of it.
158  * @param {!number} x1
159  * @param {!number} x2
160  * @param {!number} y1
161  * @param {!number} y2
162  * @param {!number} t
163  * @param {!number} u
164  * @return {!string} The point in "x y" form, suitable for SVG paths.
165  */
166 var point_from_start = function(x1, y1, x2, y2, t, u) {
167         var dx = x2 - x1;
168         var dy = y2 - y1;
169
170         var norm = 1.0 / Math.sqrt(dx * dx + dy * dy);
171         dx *= norm;
172         dy *= norm;
173
174         var x = x1 + dx * t + dy * u;
175         var y = y1 + dy * t - dx * u;
176         return x + " " + y;
177 }
178
179 /** Find a point along the coordinate system given by the given line,
180  * <t> units forward from the end of the line, <u> units to the right of it.
181  * @param {!number} x1
182  * @param {!number} x2
183  * @param {!number} y1
184  * @param {!number} y2
185  * @param {!number} t
186  * @param {!number} u
187  * @return {!string} The point in "x y" form, suitable for SVG paths.
188  */
189 var point_from_end = function(x1, y1, x2, y2, t, u) {
190         var dx = x2 - x1;
191         var dy = y2 - y1;
192
193         var norm = 1.0 / Math.sqrt(dx * dx + dy * dy);
194         dx *= norm;
195         dy *= norm;
196
197         var x = x2 + dx * t + dy * u;
198         var y = y2 + dy * t - dx * u;
199         return x + " " + y;
200 }
201
202 var position_arrow = function(arrow) {
203         if (arrow.svg) {
204                 arrow.svg.parentElement.removeChild(arrow.svg);
205                 delete arrow.svg;
206         }
207         if (current_display_line !== null) {
208                 return;
209         }
210
211         var pos = $(".square-a8").position();
212
213         var zoom_factor = $("#board").width() / 400.0;
214         var line_width = arrow.line_width * zoom_factor;
215         var arrow_size = arrow.arrow_size * zoom_factor;
216
217         var square_width = $(".square-a8").width();
218         var from_y = (7 - arrow.from_row + 0.5)*square_width;
219         var to_y = (7 - arrow.to_row + 0.5)*square_width;
220         var from_x = (arrow.from_col + 0.5)*square_width;
221         var to_x = (arrow.to_col + 0.5)*square_width;
222
223         var SVG_NS = "http://www.w3.org/2000/svg";
224         var XHTML_NS = "http://www.w3.org/1999/xhtml";
225         var svg = document.createElementNS(SVG_NS, "svg");
226         svg.setAttribute("width", /** @type{number} */ ($("#board").width()));
227         svg.setAttribute("height", /** @type{number} */ ($("#board").height()));
228         svg.setAttribute("style", "position: absolute");
229         svg.setAttribute("position", "absolute");
230         svg.setAttribute("version", "1.1");
231         svg.setAttribute("class", "c1");
232         svg.setAttribute("xmlns", XHTML_NS);
233
234         var x1 = from_x;
235         var y1 = from_y;
236         var x2 = to_x;
237         var y2 = to_y;
238
239         // Draw the line.
240         var outline = document.createElementNS(SVG_NS, "path");
241         outline.setAttribute("d", "M " + point_from_start(x1, y1, x2, y2, arrow_size / 2, 0) + " L " + point_from_end(x1, y1, x2, y2, -arrow_size / 2, 0));
242         outline.setAttribute("xmlns", XHTML_NS);
243         outline.setAttribute("stroke", "#666");
244         outline.setAttribute("stroke-width", line_width + 2);
245         outline.setAttribute("fill", "none");
246         svg.appendChild(outline);
247
248         var path = document.createElementNS(SVG_NS, "path");
249         path.setAttribute("d", "M " + point_from_start(x1, y1, x2, y2, arrow_size / 2, 0) + " L " + point_from_end(x1, y1, x2, y2, -arrow_size / 2, 0));
250         path.setAttribute("xmlns", XHTML_NS);
251         path.setAttribute("stroke", arrow.fg_color);
252         path.setAttribute("stroke-width", line_width);
253         path.setAttribute("fill", "none");
254         svg.appendChild(path);
255
256         // Then the arrow head.
257         var head = document.createElementNS(SVG_NS, "path");
258         head.setAttribute("d",
259                 "M " +  point_from_end(x1, y1, x2, y2, 0, 0) +
260                 " L " + point_from_end(x1, y1, x2, y2, -arrow_size, -arrow_size / 2) +
261                 " L " + point_from_end(x1, y1, x2, y2, -arrow_size * .623, 0.0) +
262                 " L " + point_from_end(x1, y1, x2, y2, -arrow_size, arrow_size / 2) +
263                 " L " + point_from_end(x1, y1, x2, y2, 0, 0));
264         head.setAttribute("xmlns", XHTML_NS);
265         head.setAttribute("stroke", "#000");
266         head.setAttribute("stroke-width", "1");
267         head.setAttribute("fill", arrow.fg_color);
268         svg.appendChild(head);
269
270         $(svg).css({ top: pos.top, left: pos.left });
271         document.body.appendChild(svg);
272         arrow.svg = svg;
273 }
274
275 /**
276  * @param {!string} from_square
277  * @param {!string} to_square
278  * @param {!string} fg_color
279  * @param {number} line_width
280  * @param {number} arrow_size
281  */
282 var create_arrow = function(from_square, to_square, fg_color, line_width, arrow_size) {
283         var from_col = from_square.charCodeAt(0) - "a1".charCodeAt(0);
284         var from_row = from_square.charCodeAt(1) - "a1".charCodeAt(1);
285         var to_col   = to_square.charCodeAt(0) - "a1".charCodeAt(0);
286         var to_row   = to_square.charCodeAt(1) - "a1".charCodeAt(1);
287
288         // Create arrow.
289         var arrow = {
290                 from_col: from_col,
291                 from_row: from_row,
292                 to_col: to_col,
293                 to_row: to_row,
294                 line_width: line_width,
295                 arrow_size: arrow_size,
296                 fg_color: fg_color
297         };
298
299         position_arrow(arrow);
300         arrows.push(arrow);
301 }
302
303 var compare_by_sort_key = function(refutation_lines, a, b) {
304         var ska = refutation_lines[a]['sort_key'];
305         var skb = refutation_lines[b]['sort_key'];
306         if (ska < skb) return -1;
307         if (ska > skb) return 1;
308         return 0;
309 };
310
311 var compare_by_score = function(refutation_lines, a, b) {
312         var sa = parseInt(refutation_lines[b]['score_sort_key'], 10);
313         var sb = parseInt(refutation_lines[a]['score_sort_key'], 10);
314         return sa - sb;
315 }
316
317 /**
318  * Fake multi-PV using the refutation lines. Find all “relevant” moves,
319  * sorted by quality, descending.
320  *
321  * @param {!Object} data
322  * @param {number} margin The maximum number of centipawns worse than the
323  *     best move can be and still be included.
324  * @return {Array.<string>} The UCI representation (e.g. e1g1) of all
325  *     moves, in score order.
326  */
327 var find_nonstupid_moves = function(data, margin) {
328         // First of all, if there are any moves that are more than 0.5 ahead of
329         // the primary move, the refutation lines are probably bunk, so just
330         // kill them all. 
331         var best_score = undefined;
332         var pv_score = undefined;
333         for (var move in data['refutation_lines']) {
334                 var score = parseInt(data['refutation_lines'][move]['score_sort_key'], 10);
335                 if (move == data['pv_uci'][0]) {
336                         pv_score = score;
337                 }
338                 if (best_score === undefined || score > best_score) {
339                         best_score = score;
340                 }
341                 if (!(data['refutation_lines'][move]['depth'] >= 8)) {
342                         return [];
343                 }
344         }
345
346         if (best_score - pv_score > 50) {
347                 return [];
348         }
349
350         // Now find all moves that are within “margin” of the best score.
351         // The PV move will always be first.
352         var moves = [];
353         for (var move in data['refutation_lines']) {
354                 var score = parseInt(data['refutation_lines'][move]['score_sort_key'], 10);
355                 if (move != data['pv_uci'][0] && best_score - score <= margin) {
356                         moves.push(move);
357                 }
358         }
359         moves = moves.sort(function(a, b) { return compare_by_score(data['refutation_lines'], a, b) });
360         moves.unshift(data['pv_uci'][0]);
361
362         return moves;
363 }
364
365 /**
366  * @param {number} x
367  * @return {!string}
368  */
369 var thousands = function(x) {
370         return String(x).split('').reverse().join('').replace(/(\d{3}\B)/g, '$1,').split('').reverse().join('');
371 }
372
373 /**
374  * @param {!string} fen
375  * @param {Array.<string>} uci_pv
376  * @param {Array.<string>} pretty_pv
377  * @param {number} move_num
378  * @param {!string} toplay
379  * @param {number=} opt_limit
380  */
381 var print_pv = function(fen, uci_pv, pretty_pv, move_num, toplay, opt_limit) {
382         display_lines.push({
383                 start_fen: fen,
384                 uci_pv: uci_pv,
385                 pretty_pv: pretty_pv 
386         });
387
388         var pv = '';
389         var i = 0;
390         if (toplay == 'B') {
391                 var move = "<a class=\"move\" href=\"javascript:show_line(" + (display_lines.length - 1) + ", " + 0 + ");\">" + pretty_pv[0] + "</a>";
392                 pv = move_num + '. … ' + move;
393                 toplay = 'W';
394                 ++i;
395                 ++move_num;
396         }
397         for ( ; i < pretty_pv.length; ++i) {
398                 var move = "<a class=\"move\" href=\"javascript:show_line(" + (display_lines.length - 1) + ", " + i + ");\">" + pretty_pv[i] + "</a>";
399
400                 if (toplay == 'W') {
401                         if (i > opt_limit) {
402                                 return pv + ' (…)';
403                         }
404                         if (pv != '') {
405                                 pv += ' ';
406                         }
407                         pv += move_num + '. ' + move;
408                         ++move_num;
409                         toplay = 'B';
410                 } else {
411                         pv += ' ' + move;
412                         toplay = 'W';
413                 }
414         }
415         return pv;
416 }
417
418 var update_highlight = function() {
419         $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
420         if (current_display_line === null && highlight_from !== undefined && highlight_to !== undefined) {
421                 $("#board").find('.square-' + highlight_from).addClass('nonuglyhighlight');
422                 $("#board").find('.square-' + highlight_to).addClass('nonuglyhighlight');
423         }
424 }
425
426 var update_refutation_lines = function() {
427         if (fen === null) {
428                 return;
429         }
430         if (display_lines.length > 1) {
431                 display_lines = [ display_lines[0] ];
432         }
433
434         var tbl = $("#refutationlines");
435         tbl.empty();
436
437         var moves = [];
438         for (var move in refutation_lines) {
439                 moves.push(move);
440         }
441         var compare = sort_refutation_lines_by_score ? compare_by_score : compare_by_sort_key;
442         moves = moves.sort(function(a, b) { return compare(refutation_lines, a, b) });
443         for (var i = 0; i < moves.length; ++i) {
444                 var line = refutation_lines[moves[i]];
445
446                 var tr = document.createElement("tr");
447
448                 var move_td = document.createElement("td");
449                 tr.appendChild(move_td);
450                 $(move_td).addClass("move");
451                 if (line['pv_uci'].length == 0) {
452                         $(move_td).text(line['pretty_move']);
453                 } else {
454                         var move = "<a class=\"move\" href=\"javascript:show_line(" + display_lines.length + ", " + 0 + ");\">" + line['pretty_move'] + "</a>";
455                         $(move_td).html(move);
456                 }
457
458                 var score_td = document.createElement("td");
459                 tr.appendChild(score_td);
460                 $(score_td).addClass("score");
461                 $(score_td).text(line['pretty_score']);
462
463                 var depth_td = document.createElement("td");
464                 tr.appendChild(depth_td);
465                 $(depth_td).addClass("depth");
466                 $(depth_td).text("d" + line['depth']);
467
468                 var pv_td = document.createElement("td");
469                 tr.appendChild(pv_td);
470                 $(pv_td).addClass("pv");
471                 $(pv_td).html(print_pv(fen, line['pv_uci'], line['pv_pretty'], move_num, toplay, 10));
472
473                 tbl.append(tr);
474         }
475
476         // Make one of the links clickable and the other nonclickable.
477         if (sort_refutation_lines_by_score) {
478                 $("#sortbyscore0").html("<a href=\"javascript:resort_refutation_lines(false)\">Move</a>");
479                 $("#sortbyscore1").html("<strong>Score</strong>");
480         } else {
481                 $("#sortbyscore0").html("<strong>Move</strong>");
482                 $("#sortbyscore1").html("<a href=\"javascript:resort_refutation_lines(true)\">Score</a>");
483         }
484 }
485
486 /**
487  * @param {Object} data
488  * @param {number} num_viewers
489  */
490 var update_board = function(data, num_viewers) {
491         display_lines = [];
492
493         // The headline.
494         var headline;
495         if (data['position']['player_w'] && data['position']['player_b']) {
496                 headline = data['position']['player_w'] + '–' +
497                         data['position']['player_b'] + ', analysis';
498         } else {
499                 headline = 'Analysis';
500         }
501         if (data['position']['last_move'] !== 'none') {
502                 headline += ' after '
503                 if (data['position']['toplay'] == 'W') {
504                         headline += (data['position']['move_num']-1) + '… ';
505                 } else {
506                         headline += data['position']['move_num'] + '. ';
507                 }
508                 headline += data['position']['last_move'];
509         }
510
511         $("#headline").text(headline);
512
513         if (num_viewers === null) {
514                 $("#numviewers").text("");
515         } else if (num_viewers == 1) {
516                 $("#numviewers").text("You are the only current viewer");
517         } else {
518                 $("#numviewers").text(num_viewers + " current viewers");
519         }
520
521         // The engine id.
522         if (data['id'] && data['id']['name'] !== null) {
523                 $("#engineid").text(data['id']['name']);
524         }
525
526         // The score.
527         if (data['score'] !== null) {
528                 $("#score").text(data['score']);
529         }
530
531         // The search stats.
532         if (data['nodes'] && data['nps'] && data['depth']) {
533                 var stats = thousands(data['nodes']) + ' nodes, ' + thousands(data['nps']) + ' nodes/sec, depth ' + data['depth'] + ' ply';
534                 if (data['seldepth']) {
535                         stats += ' (' + data['seldepth'] + ' selective)';
536                 }
537                 if (data['tbhits'] && data['tbhits'] > 0) {
538                         if (data['tbhits'] == 1) {
539                                 stats += ', one Syzygy hit';
540                         } else {
541                                 stats += ', ' + thousands(data['tbhits']) + ' Syzygy hits';
542                         }
543                 }
544
545                 $("#searchstats").text(stats);
546         }
547
548         // Update the board itself.
549         fen = data['position']['fen'];
550         update_displayed_line();
551
552         if (data['position']['last_move_uci']) {
553                 highlight_from = data['position']['last_move_uci'].substr(0, 2);
554                 highlight_to = data['position']['last_move_uci'].substr(2, 4);
555         } else {
556                 highlight_from = highlight_to = undefined;
557         }
558         update_highlight();
559
560         // Print the PV.
561         $("#pv").html(print_pv(data['position']['fen'], data['pv_uci'], data['pv_pretty'], data['position']['move_num'], data['position']['toplay']));
562
563         // Update the PV arrow.
564         clear_arrows();
565         if (data['pv_uci'].length >= 1) {
566                 // draw a continuation arrow as long as it's the same piece
567                 for (var i = 0; i < data['pv_uci'].length; i += 2) {
568                         var from = data['pv_uci'][i].substr(0, 2);
569                         var to = data['pv_uci'][i].substr(2,4);
570                         if ((i >= 2 && from != data['pv_uci'][i - 2].substr(2, 4)) ||
571                              interfering_arrow(from, to)) {
572                                 break;
573                         }
574                         create_arrow(from, to, '#f66', 6, 20);
575                 }
576
577                 var alt_moves = find_nonstupid_moves(data, 30);
578                 for (var i = 1; i < alt_moves.length && i < 3; ++i) {
579                         create_arrow(alt_moves[i].substr(0, 2),
580                                      alt_moves[i].substr(2, 4), '#f66', 1, 10);
581                 }
582         }
583
584         // See if all semi-reasonable moves have only one possible response.
585         if (data['pv_uci'].length >= 2) {
586                 var nonstupid_moves = find_nonstupid_moves(data, 300);
587                 var response = data['pv_uci'][1];
588                 for (var i = 0; i < nonstupid_moves.length; ++i) {
589                         if (nonstupid_moves[i] == data['pv_uci'][0]) {
590                                 // ignore the PV move for refutation lines.
591                                 continue;
592                         }
593                         if (!data['refutation_lines'] ||
594                             !data['refutation_lines'][nonstupid_moves[i]] ||
595                             !data['refutation_lines'][nonstupid_moves[i]]['pv_uci'] ||
596                             data['refutation_lines'][nonstupid_moves[i]]['pv_uci'].length < 1) {
597                                 // Incomplete PV, abort.
598                                 response = undefined;
599                                 break;
600                         }
601                         var this_response = data['refutation_lines'][nonstupid_moves[i]]['pv_uci'][1];
602                         if (response !== this_response) {
603                                 // Different response depending on lines, abort.
604                                 response = undefined;
605                                 break;
606                         }
607                 }
608
609                 if (nonstupid_moves.length > 0 && response !== undefined) {
610                         create_arrow(response.substr(0, 2),
611                                      response.substr(2, 4), '#66f', 6, 20);
612                 }
613         }
614
615         // Update the refutation lines.
616         fen = data['position']['fen'];
617         move_num = data['position']['move_num'];
618         toplay = data['position']['toplay'];
619         refutation_lines = data['refutation_lines'];
620         update_refutation_lines();
621
622         // Next update.
623         setTimeout(function() { request_update(); }, 100);
624 }
625
626 /**
627  * @param {boolean} sort_by_score
628  */
629 var resort_refutation_lines = function(sort_by_score) {
630         sort_refutation_lines_by_score = sort_by_score;
631         update_refutation_lines();
632 }
633 window['resort_refutation_lines'] = resort_refutation_lines;
634
635 /**
636  * @param {number} line_num
637  * @param {number} move_num
638  */
639 var show_line = function(line_num, move_num) {
640         if (line_num == -1) {
641                 current_display_line = null;
642                 current_display_move = null;
643         } else {
644                 current_display_line = display_lines[line_num];
645                 current_display_move = move_num;
646         }
647         update_displayed_line();
648         update_highlight();
649         redraw_arrows();
650 }
651 window['show_line'] = show_line;
652
653 var prev_move = function() {
654         if (current_display_move > 0) {
655                 --current_display_move;
656         }
657         update_displayed_line();
658 }
659 window['prev_move'] = prev_move;
660
661 var next_move = function() {
662         if (current_display_line && current_display_move < current_display_line.pretty_pv.length - 1) {
663                 ++current_display_move;
664         }
665         update_displayed_line();
666 }
667 window['next_move'] = next_move;
668
669 var update_displayed_line = function() {
670         if (current_display_line === null) {
671                 $("#linenav").hide();
672                 $("#linemsg").show();
673                 board.position(fen);
674                 return;
675         }
676
677         $("#linenav").show();
678         $("#linemsg").hide();
679
680         if (current_display_move == 0) {
681                 $("#prevmove").html("Previous");
682         } else {
683                 $("#prevmove").html("<a href=\"javascript:prev_move();\">Previous</a></span>");
684         }
685         if (current_display_move == current_display_line.uci_pv.length - 1) {
686                 $("#nextmove").html("Next");
687         } else {
688                 $("#nextmove").html("<a href=\"javascript:next_move();\">Next</a></span>");
689         }
690
691         hiddenboard.position(current_display_line.start_fen, false);
692         for (var i = 0; i <= current_display_move; ++i) {
693                 var move = current_display_line.uci_pv[i];
694                 move = move.substr(0, 2) + "-" + move.substr(2, 4);
695                 hiddenboard.move(move, false);
696
697                 // chessboard.js does not automatically move the rook on castling
698                 // (issue #51; marked as won't fix), so update it ourselves.
699                 if (move == "e1-g1" && hiddenboard.position().g1 == "wK") {  // white O-O
700                         hiddenboard.move("h1-f1", false);
701                 } else if (move == "e1-c1" && hiddenboard.position().c1 == "wK") {  // white O-O-O
702                         hiddenboard.move("a1-d1", false);
703                 } else if (move == "e8-g8" && hiddenboard.position().g8 == "bK") {  // black O-O
704                         hiddenboard.move("h8-f8", false);
705                 } else if (move == "e8-c8" && hiddenboard.position().c8 == "bK") {  // black O-O-O
706                         hiddenboard.move("a8-d8", false);
707                 }
708         }
709         board.position(hiddenboard.position());
710 }
711
712 var init = function() {
713         // Create board.
714         board = new window.ChessBoard('board', 'start');
715         hiddenboard = new window.ChessBoard('hiddenboard', 'start');
716
717         request_update();
718         $(window).resize(function() {
719                 board.resize();
720                 update_highlight();
721                 redraw_arrows();
722         });
723         $(window).keyup(function(event) {
724                 if (event.which == 39) {
725                         next_move();
726                 } else if (event.which == 37) {
727                         prev_move();
728                 }
729         });
730 };
731 $(document).ready(init);
732
733 })();