Code coverage report for lib/castling.js

Statements: 94.94% (75 / 79)      Branches: 85.71% (48 / 56)      Functions: 100% (16 / 16)      Lines: 94.94% (75 / 79)      Ignored: none     

All files » lib/ » castling.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  1   4       1 1             1 1 1 2465   2465         2465 2465 2465   1 1   4804     206 206       2433     2433 2433 2433 2407   26     26 26 26 26             2433 2268   165   315   315 26       139     27       27         51   51 51     51 51 27               315     2224     26     1 116 116                       1 58 58                       1       1 32 32 76 76 76   32   1 2250 2250 988 525 463 463   1262 1262 621 641 641       1 26       26                 26    
"use strict";
Object.defineProperties(exports, {
  Castling: {get: function() {
      return Castling;
    }},
  __esModule: {value: true}
});
var Point = require('./point').Point;
var $__1 = require('./brands'),
    KING = $__1.KING,
    ROOK = $__1.ROOK,
    QUEENSIDE = $__1.QUEENSIDE,
    KINGSIDE = $__1.KINGSIDE,
    WHITE = $__1.WHITE,
    BLACK = $__1.BLACK;
var CheckError = require('./error').CheckError;
var oppositeColor = require('./util').oppositeColor;
var Castling = function Castling($__8) {
  var $__10,
      $__11;
  var $__9 = $traceurRuntime.assertObject($__8),
      fenEncoding = ($__10 = $__9.fenEncoding) === void 0 ? '' : $__10,
      modes = ($__11 = $__9.modes) === void 0 ? parseCastlingModes(fenEncoding) : $__11,
      rook = $__9.rook,
      square = $__9.square;
  this.modes = modes;
  this.rook = rook;
  this.square = square;
};
var $Castling = Castling;
($traceurRuntime.createClass)(Castling, {
  isLegal: function(color, side) {
    return this.modes[$traceurRuntime.toProperty(color)][$traceurRuntime.toProperty(side)];
  },
  toString: function() {
    var modes = this.modes;
    return [modes[$traceurRuntime.toProperty(WHITE)][$traceurRuntime.toProperty(KINGSIDE)] ? 'K' : '', modes[$traceurRuntime.toProperty(WHITE)][$traceurRuntime.toProperty(QUEENSIDE)] ? 'Q' : '', modes[$traceurRuntime.toProperty(BLACK)][$traceurRuntime.toProperty(KINGSIDE)] ? 'k' : '', modes[$traceurRuntime.toProperty(BLACK)][$traceurRuntime.toProperty(QUEENSIDE)] ? 'q' : ''].join('') || '-';
  }
}, {
  analyze: function(position, king, coords) {
    var $__8 = $traceurRuntime.assertObject(king),
        brand = $__8.brand,
        color = $__8.color;
    var castling = $traceurRuntime.assertObject(position).castling;
    var side = $Castling.side(position, king, coords);
    if (side == null || !castling.isLegal(color, side)) {
      return new $Castling({modes: castling.modes});
    }
    Iif (!isValid(position, color, side)) {
      throw new CheckError();
    }
    var modes = blankModes();
    var opponent = oppositeColor(position.activeColor);
    $traceurRuntime.setProperty(modes, opponent, position.castling.modes[$traceurRuntime.toProperty(opponent)]);
    return new $Castling({
      rook: $Castling.rook(position, color, side),
      square: position.pieceCoords(king).sum($Castling.rookOffset(color, side)),
      modes: modes
    });
  },
  side: function(position, king, coords) {
    if (king.brand !== KING) {
      return null;
    }
    for (var $__6 = [KINGSIDE, QUEENSIDE][$traceurRuntime.toProperty(Symbol.iterator)](),
        $__7; !($__7 = $__6.next()).done; ) {
      var side = $__7.value;
      {
        if ($Castling.isCastlingMove(position, king, side, coords)) {
          return side;
        }
      }
    }
    return null;
  },
  rook: function(position, color, side) {
    var kingX = $traceurRuntime.assertObject(position.pieceCoords(position.one({
      brand: KING,
      color: color
    }))).x;
    for (var $__6 = position.query({
      brand: ROOK,
      color: color
    })[$traceurRuntime.toProperty(Symbol.iterator)](),
        $__7; !($__7 = $__6.next()).done; ) {
      var rook = $__7.value;
      {
        try {
          throw undefined;
        } catch (rookX) {
          {
            rookX = $traceurRuntime.assertObject(position.pieceCoords(rook)).x;
            if (color === WHITE ? ((rookX > kingX && side === KINGSIDE) || (rookX < kingX && side === QUEENSIDE)) : ((rookX > kingX && side === QUEENSIDE) || (rookX < kingX && side === KINGSIDE))) {
              return rook;
            }
          }
        }
      }
    }
  },
  isCastlingMove: function(position, king, side, coords) {
    return (position.pieceCoords(king).sum($Castling.kingOffset(king.color, side)).equal(coords));
  },
  kingOffset: function(color, side) {
    return new Point(xOffset(color, side, 2), 0);
  },
  rookOffset: function(color, side) {
    return new Point(xOffset(color, side, -1), 0).sum($Castling.kingOffset(color, side));
  }
});
var blankMode = (function() {
  var $__5;
  return (($__5 = {}, Object.defineProperty($__5, KINGSIDE, {
    value: false,
    configurable: true,
    enumerable: true,
    writable: true
  }), Object.defineProperty($__5, QUEENSIDE, {
    value: false,
    configurable: true,
    enumerable: true,
    writable: true
  }), $__5));
});
var blankModes = (function() {
  var $__5;
  return (($__5 = {}, Object.defineProperty($__5, WHITE, {
    value: blankMode(),
    configurable: true,
    enumerable: true,
    writable: true
  }), Object.defineProperty($__5, BLACK, {
    value: blankMode(),
    configurable: true,
    enumerable: true,
    writable: true
  }), $__5));
});
var sides = {
  'q': QUEENSIDE,
  'k': KINGSIDE
};
function parseCastlingModes(castling) {
  var modes = blankModes();
  String(castling || '').split('').forEach((function(mode) {
    var modeLower = mode.toLowerCase();
    var color = modeLower === mode ? BLACK : WHITE;
    $traceurRuntime.setProperty(modes[$traceurRuntime.toProperty(color)], sides[$traceurRuntime.toProperty(modeLower)], true);
  }));
  return modes;
}
function xOffset(color, side) {
  var m = arguments[2] !== (void 0) ? arguments[2] : 1;
  if (color === WHITE) {
    if (side === KINGSIDE) {
      return m;
    } else Eif (side === QUEENSIDE) {
      return -m;
    }
  } else Eif (color === BLACK) {
    if (side === KINGSIDE) {
      return -m;
    } else Eif (side === QUEENSIDE) {
      return m;
    }
  }
}
function isValid(position, color, side) {
  var loc = position.pieceCoords(position.one({
    brand: KING,
    color: color
  }));
  for (var $__6 = loc.to(loc.sum(Castling.kingOffset(color, side)))[$traceurRuntime.toProperty(Symbol.iterator)](),
      $__7; !($__7 = $__6.next()).done; ) {
    var pt = $__7.value;
    {
      if (position.isCheck(color, pt)) {
        return false;
      }
    }
  }
  return true;
}