+var fmt_cp = function(v) {
+ if (v === 0) {
+ return "0.00";
+ } else if (v > 0) {
+ return "+" + (v / 100).toFixed(2);
+ } else {
+ v = -v;
+ return "-" + (v / 100).toFixed(2);
+ }
+}
+
+var format_short_score = function(score) {
+ if (!score) {
+ return "???";
+ }
+ if (score[0] === 'T' || score[0] === 't') {
+ var ret = "TB\u00a0";
+ if (score[2]) { // Is a bound.
+ ret = score[2] + "\u00a0TB\u00a0";
+ }
+ if (score[0] === 'T') {
+ return ret + Math.ceil(score[1] / 2);
+ } else {
+ return ret + "-" + Math.ceil(score[1] / 2);
+ }
+ } else if (score[0] === 'M' || score[0] === 'm') {
+ var sign = (score[0] === 'm') ? '-' : '';
+ if (score[2]) { // Is a bound.
+ return score[2] + "\u00a0M " + sign + score[1];
+ } else {
+ return "M " + sign + score[1];
+ }
+ } else if (score[0] === 'd') {
+ return "TB =0";
+ } else if (score[0] === 'cp') {
+ if (score[2]) { // Is a bound.
+ return score[2] + "\u00a0" + fmt_cp(score[1]);
+ } else {
+ return fmt_cp(score[1]);
+ }
+ }
+ return null;
+}
+
+var format_long_score = function(score) {
+ if (!score) {
+ return "???";
+ }
+ if (score[0] === 'T') {
+ if (score[1] == 0) {
+ return "Won for white (tablebase)";
+ } else {
+ return "White wins in " + Math.ceil(score[1] / 2);
+ }
+ } else if (score[0] === 't') {
+ if (score[1] == -1) {
+ return "Won for black (tablebase)";
+ } else {
+ return "Black wins in " + Math.ceil(score[1] / 2);
+ }
+ } else if (score[0] === 'M') {
+ if (score[1] == 0) {
+ return "White wins by checkmate";
+ } else {
+ return "White mates in " + score[1];
+ }
+ } else if (score[0] === 'm') {
+ if (score[1] == 0) {
+ return "Black wins by checkmate";
+ } else {
+ return "Black mates in " + score[1];
+ }
+ } else if (score[0] === 'd') {
+ return "Theoretical draw";
+ } else if (score[0] === 'cp') {
+ return "Score: " + format_short_score(score);
+ }
+ return null;
+}
+
+var compute_plot_score = function(score) {
+ if (score[0] === 'M' || score[0] === 'T') {
+ return 500;
+ } else if (score[0] === 'm' || score[0] === 't') {
+ return -500;
+ } else if (score[0] === 'd') {
+ return 0;
+ } else if (score[0] === 'cp') {
+ if (score[1] > 500) {
+ return 500;
+ } else if (score[1] < -500) {
+ return -500;
+ } else {
+ return score[1];
+ }
+ }
+ return null;
+}
+