UNPKG

chess-legal-moves

Version:

Analyses a given chess game position in Fen notation to return legal moves and provides the next game position after a given move

186 lines (185 loc) 6.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var int32Utils_1 = require("./int32Utils"); var BitBoard = /** @class */ (function () { function BitBoard(low, high) { this.low = (low || 0) >>> 0; this.high = (high || 0) >>> 0; } BitBoard.prototype.setBit = function (pos) { if (pos >= 32 && pos < 64) { this.high = (this.high | (1 << (pos - 32))) >>> 0; } else if (pos >= 0 && pos < 32) { this.low = (this.low | (1 << pos)) >>> 0; } }; BitBoard.prototype.clearBit = function (pos) { if (pos >= 32) { return new BitBoard(this.low, (this.high & ~(1 << (pos - 32))) >>> 0); } else { return new BitBoard((this.low & ~(1 << pos)) >>> 0, this.high); } }; BitBoard.prototype.and = function (other) { return new BitBoard(this.low & other.low, this.high & other.high); }; BitBoard.prototype.or = function (other) { return new BitBoard(this.low | other.low, this.high | other.high); }; BitBoard.prototype.xor = function (other) { return new BitBoard(this.low ^ other.low, this.high ^ other.high); }; BitBoard.prototype.not = function () { return new BitBoard(~this.low, ~this.high); }; BitBoard.prototype.equals = function (other) { return this.low === other.low && this.high === other.high; }; BitBoard.prototype.greaterThan = function (other) { return this.high > other.high || this.low > other.low; }; BitBoard.prototype.lessThan = function (other) { return this.high < other.high || this.low < other.low; }; BitBoard.prototype.isZero = function () { return this.high === 0 && this.low === 0; }; BitBoard.prototype.extractBits = function () { var lowCopy = this.low; var highCopy = this.high; var extracted = []; while (lowCopy) { extracted.push(int32Utils_1.default.bitScanForward32(lowCopy)); lowCopy = int32Utils_1.default.clearLeastSigBit32(lowCopy); } while (highCopy) { extracted.push(int32Utils_1.default.bitScanForward32(highCopy) + 32); highCopy = int32Utils_1.default.clearLeastSigBit32(highCopy); } return extracted; }; BitBoard.prototype.shiftRight = function (numBits) { var newLowBits, newHighBits; if (numBits <= 0) { return new BitBoard(this.low, this.high); } else if (numBits > 63) { return new BitBoard(); } else if (numBits >= 32) { newLowBits = this.high >>> (numBits - 32); newHighBits = 0; } else { newLowBits = (this.low >>> numBits) | (this.high << (32 - numBits)); newHighBits = this.high >>> numBits; } return new BitBoard(newLowBits, newHighBits); }; BitBoard.prototype.shiftLeft = function (numBits) { var newLowBits, newHighBits; if (numBits <= 0) { return new BitBoard(this.low, this.high); } else if (numBits > 63) { return new BitBoard(); } else if (numBits >= 32) { newLowBits = 0; newHighBits = (this.low << (numBits - 32)) >>> 0; } else { newLowBits = (this.low << numBits) >>> 0; newHighBits = ((this.low >>> (32 - numBits)) | (this.high << numBits)) >>> 0; } return new BitBoard(newLowBits, newHighBits); }; BitBoard.prototype.bitScanForward = function () { // returns the less significant bit index if (this.low) { return int32Utils_1.default.bitScanForward32(this.low); } else if (this.high) { return int32Utils_1.default.bitScanForward32(this.high) + 32; } else { return null; } }; BitBoard.prototype.hasSetBit = function (pos) { if (pos < 32) { return Boolean(this.low & (1 << pos)); } else { return Boolean(this.high & (1 << (pos - 32))); } }; BitBoard.prototype.bitScanReverse = function () { // returns the most significant bit index if (this.high) { return int32Utils_1.default.bitScanReverse32(this.high) + 32; } else if (this.low) { return int32Utils_1.default.bitScanReverse32(this.low); } else { return null; } }; BitBoard.fromPos = function (pos) { var res = new BitBoard(); res.setBit(pos); return res; }; BitBoard.fromPositions = function (positions) { var res = new BitBoard(); positions.forEach(function (pos) { res.setBit(pos); }); return res; }; BitBoard.fromHex = function (hexNumberAsString) { var high = parseInt("0x".concat(hexNumberAsString.slice(0, 8))); var low = parseInt("0x".concat(hexNumberAsString.slice(8))); var res = new BitBoard(low, high); return res; }; BitBoard.prototype.print = function () { var row = ''; var pow = 63; var posVal; console.log('##############'); console.log('# #'); while (pow >= 32) { posVal = (Math.pow(2, pow - 32) & this.high) === 0 ? '0' : '1'; row = posVal + row; if (row.length === 8) { console.log('# ' + row + ' #'); row = ''; } pow--; } while (pow >= 0) { posVal = (Math.pow(2, pow) & this.low) === 0 ? '0' : '1'; row = posVal + row; if (row.length === 8) { console.log('# ' + row + ' #'); row = ''; } pow--; } console.log('# #'); console.log('##############'); }; BitBoard.prototype.printBinary = function () { console.log({ high: this.high, low: this.low, }); }; return BitBoard; }()); exports.default = BitBoard;