Code coverage report for lib/algebraic.js

Statements: 94.29% (132 / 140)      Branches: 95.52% (64 / 67)      Functions: 94.12% (16 / 17)      Lines: 94.29% (132 / 140)      Ignored: none     

All files » lib/ » algebraic.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270  1   1   2     1     1     1       1       1 1             1 1       1       1       1     1 1 1197 25   1172   1 1 1172         1172 1172 2342 10080   1172 1172 1172 1172   1172 1172   115 115   115 115 115   1057 1057 1057   2112 2112   1055 1055 1055   1055 1055   1055 1055 1055   1172       1 1057         1057 7414 39809   1057       1057 1057   4442 4442   3385 3385 3385   3385 3385 3385   3385               3385 3385 3385 3385   3385 3385 3385   3385 3385   2972 2972 2972   2972 1055 1055   1055 1055   1055 1055 1055   1055 1055 1055   1917 1917 1917 1917   1917 1917           1057       1 1172   115   163   153   182   228   331     1 1172 1066   106 1   105   1 1172         1172 1172 1172     1172 3   1169 1   1168       1168                   1 25       25                 1 25   22   3     1 115          
"use strict";
var $__9 = $traceurRuntime.initGeneratorFunction(pieces),
    $__10 = $traceurRuntime.initGeneratorFunction(candidates);
Object.defineProperties(exports, {
  Algebraic: {get: function() {
      return Algebraic;
    }},
  chunker: {get: function() {
      return chunker;
    }},
  parse: {get: function() {
      return parse;
    }},
  stringify: {get: function() {
      return stringify;
    }},
  __esModule: {value: true}
});
var $__0 = require('./brands'),
    WHITE = $__0.WHITE,
    KING = $__0.KING,
    PAWN = $__0.PAWN;
var Point = require('./point').Point;
var $__2 = require('./standard'),
    King = $__2.King,
    Queen = $__2.Queen,
    Rook = $__2.Rook,
    Bishop = $__2.Bishop,
    Knight = $__2.Knight,
    Pawn = $__2.Pawn;
var EnPassantTarget = require('./eptarget').EnPassantTarget;
var $__4 = require('./util'),
    squareCoords = $__4.squareCoords,
    rankIndex = $__4.rankIndex,
    fileIndex = $__4.fileIndex;
var $__5 = require('./error'),
    ChessError = $__5.ChessError,
    AmbiguityError = $__5.AmbiguityError,
    MobilityError = $__5.MobilityError;
var Algebraic = {
  parse: parse,
  stringify: stringify,
  get chunker() {
    return chunker;
  }
};
var chunker = /([KQRBNP])?([a-h]?[1-8]?)?x?([a-h][1-8])/;
function parse(algStr, position) {
  if (algStr === 'O-O' || algStr === 'O-O-O') {
    return castlingMove(algStr, position);
  }
  return normalMove(algStr, position);
}
function stringify(move) {}
function pieces(position, source, target) {
  var i,
      Brand,
      $__6,
      $__7,
      p;
  var $arguments = arguments;
  return $traceurRuntime.createGeneratorInstance(function($ctx) {
    while (true)
      switch ($ctx.state) {
        case 0:
          i = $arguments[3] !== (void 0) ? $arguments[3] : 'p';
          Brand = pieceBrand(i);
          $ctx.state = 15;
          break;
        case 15:
          $ctx.state = (Brand === King) ? 1 : 12;
          break;
        case 1:
          $ctx.state = 2;
          return getKing(position);
        case 2:
          $ctx.maybeThrow();
          $ctx.state = -2;
          break;
        case 12:
          $__6 = candidates(position, Brand.brand, source, target)[$traceurRuntime.toProperty(Symbol.iterator)]();
          $ctx.state = 8;
          break;
        case 8:
          $ctx.state = (!($__7 = $__6.next()).done) ? 9 : -2;
          break;
        case 9:
          p = $__7.value;
          $ctx.state = 10;
          break;
        case 10:
          $ctx.state = 6;
          return p;
        case 6:
          $ctx.maybeThrow();
          $ctx.state = 8;
          break;
        default:
          return $ctx.end();
      }
  }, $__9, this);
}
function candidates(position, brand, source, target) {
  var $__6,
      $__7,
      p,
      e,
      loc;
  return $traceurRuntime.createGeneratorInstance(function($ctx) {
    while (true)
      switch ($ctx.state) {
        case 0:
          $__6 = position.query({
            brand: brand,
            color: position.activeColor
          })[$traceurRuntime.toProperty(Symbol.iterator)]();
          $ctx.state = 29;
          break;
        case 29:
          $ctx.state = (!($__7 = $__6.next()).done) ? 31 : -2;
          break;
        case 31:
          p = $__7.value;
          $ctx.state = 32;
          break;
        case 32:
          $ctx.pushTry(22, null);
          $ctx.state = 25;
          break;
        case 25:
          throw undefined;
          $ctx.state = 27;
          break;
        case 27:
          $ctx.popTry();
          $ctx.state = 29;
          break;
        case 22:
          $ctx.popTry();
          loc = $ctx.storedException;
          $ctx.state = 20;
          break;
        case 20:
          loc = position.pieceCoords(p);
          $ctx.state = 21;
          break;
        case 21:
          $ctx.state = (source == null || source.x === loc.x || source.y === loc.y) ? 14 : 29;
          break;
        case 14:
          $ctx.pushTry(12, null);
          $ctx.state = 15;
          break;
        case 15:
          position.movePiece(p, target);
          $ctx.state = 6;
          break;
        case 6:
          $ctx.state = 2;
          return p;
        case 2:
          $ctx.maybeThrow();
          $ctx.state = 4;
          break;
        case 4:
          $ctx.popTry();
          $ctx.state = 29;
          break;
        case 12:
          $ctx.popTry();
          e = $ctx.storedException;
          $ctx.state = 9;
          break;
        case 9:
          $ctx.state = (e instanceof ChessError) ? 29 : 8;
          break;
        case 8:
          throw e;
          $ctx.state = 29;
          break;
        default:
          return $ctx.end();
      }
  }, $__10, this);
}
function pieceBrand(i) {
  switch (i.toLowerCase()) {
    case 'k':
      return King;
    case 'q':
      return Queen;
    case 'r':
      return Rook;
    case 'b':
      return Bishop;
    case 'n':
      return Knight;
    case 'p':
      return Pawn;
  }
}
function parseSource(s) {
  if (s == null) {
    return null;
  }
  if (!isNaN(Number(s))) {
    return new Point(NaN, rankIndex(s));
  }
  return new Point(fileIndex(s), NaN);
}
function normalMove(algStr, position) {
  var $__8 = $traceurRuntime.assertObject(chunker.exec(algStr)),
      _ = $__8[0],
      i = $__8[1],
      s = $__8[2],
      t = $__8[3];
  var source = parseSource(s);
  var target = squareCoords(t);
  var $__8 = $traceurRuntime.spread(pieces(position, source, target, i)),
      piece = $__8[0],
      extra = $__8[1];
  if (piece == null) {
    throw new MobilityError(algStr, position);
  }
  if (extra != null) {
    throw new AmbiguityError(algStr);
  }
  var $__8 = $traceurRuntime.assertObject(EnPassantTarget.capturablePiece(position, piece, target)),
      captureTarget = $__8.captureTarget,
      capturePiece = $__8.capturePiece,
      isEnPassant = $__8.isEnPassant;
  return {
    piece: piece,
    source: position.pieceCoords(piece),
    target: target,
    isCapture: capturePiece != null,
    captureTarget: captureTarget,
    capturePiece: capturePiece,
    isEnPassant: isEnPassant
  };
}
function castlingMove(algStr, position) {
  var king = position.one({
    brand: KING,
    color: position.activeColor
  });
  return {
    piece: king,
    source: position.pieceCoords(king),
    target: getCastlingCoords(algStr, position),
    isCapture: false,
    capturePiece: null,
    isEnPassant: false
  };
}
function getCastlingCoords(algStr, position) {
  switch (algStr) {
    case 'O-O':
      return squareCoords(position.activeColor === WHITE ? 'g1' : 'g8');
    case 'O-O-O':
      return squareCoords(position.activeColor === WHITE ? 'c1' : 'c8');
  }
}
function getKing(position) {
  return position.one({
    brand: King.brand,
    color: position.activeColor
  });
}