Code coverage report for lib/piece/king/castling.js

Statements: 94.67% (71 / 75)      Branches: 86% (43 / 50)      Functions: 100% (16 / 16)      Lines: 94.67% (71 / 75)      Ignored: none     

All files » lib/piece/king/ » 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  1   4       1 1             1 1 167   167         167 167 167   1 1   221     790 790       134     134 134 134 133   1     1             134 117   17   33   33 1       16     2       2         4   4 4     4 4 2               33     230     1     1 68 68                       1 34 34                       1       1 33 33 76 76 76   33   1 231 231 67 36 31 31   164 164 82 82 82       1 1       1                 1    
"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 Castling = function Castling($__7) {
  var $__9,
      $__10;
  var $__8 = $traceurRuntime.assertObject($__7),
      fenEncoding = ($__9 = $__8.fenEncoding) === void 0 ? '' : $__9,
      modes = ($__10 = $__8.modes) === void 0 ? parseCastlingModes(fenEncoding) : $__10,
      rook = $__8.rook,
      square = $__8.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 $__7 = $traceurRuntime.assertObject(king),
        brand = $__7.brand,
        color = $__7.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();
    }
    return new $Castling({
      rook: $Castling.rook(position, color, side),
      square: position.pieceCoords(king).sum($Castling.rookOffset(color, side)),
      modes: blankModes()
    });
  },
  side: function(position, king, coords) {
    if (king.brand !== KING) {
      return null;
    }
    for (var $__5 = [KINGSIDE, QUEENSIDE][$traceurRuntime.toProperty(Symbol.iterator)](),
        $__6; !($__6 = $__5.next()).done; ) {
      var side = $__6.value;
      {
        if ($Castling.isCastlingMove(position, king, side, coords)) {
          return side;
        }
      }
    }
    return null;
  },
  rook: function(position, color, side) {
    var kingX = $traceurRuntime.assertObject(position.pieceCoords(position.query({
      brand: KING,
      color: color
    }))).x;
    for (var $__5 = position.queryAll({
      brand: ROOK,
      color: color
    })[$traceurRuntime.toProperty(Symbol.iterator)](),
        $__6; !($__6 = $__5.next()).done; ) {
      var rook = $__6.value;
      {
        try {
          throw undefined;
        } catch (rookX) {
          {
            rookX = $traceurRuntime.assertObject(position.pieceCoords(rook)).x;
            if ((rookX > kingX && side === KINGSIDE) || (rookX < kingX && side === QUEENSIDE)) {
              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 $__4;
  return (($__4 = {}, Object.defineProperty($__4, KINGSIDE, {
    value: false,
    configurable: true,
    enumerable: true,
    writable: true
  }), Object.defineProperty($__4, QUEENSIDE, {
    value: false,
    configurable: true,
    enumerable: true,
    writable: true
  }), $__4));
});
var blankModes = (function() {
  var $__4;
  return (($__4 = {}, Object.defineProperty($__4, WHITE, {
    value: blankMode(),
    configurable: true,
    enumerable: true,
    writable: true
  }), Object.defineProperty($__4, BLACK, {
    value: blankMode(),
    configurable: true,
    enumerable: true,
    writable: true
  }), $__4));
});
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.query({
    brand: KING,
    color: color
  }));
  for (var $__5 = loc.to(loc.sum(Castling.kingOffset(color, side)))[$traceurRuntime.toProperty(Symbol.iterator)](),
      $__6; !($__6 = $__5.next()).done; ) {
    var pt = $__6.value;
    {
      if (position.isCheck(color, pt)) {
        return false;
      }
    }
  }
  return true;
}