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
167 lines (166 loc) • 5.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var BitBoard_1 = require("../BitBoard/BitBoard");
var Board = /** @class */ (function () {
function Board(boardString) {
this.whites = new BitBoard_1.default();
this.blacks = new BitBoard_1.default();
this.pawns = new BitBoard_1.default();
this.rooks = new BitBoard_1.default();
this.knights = new BitBoard_1.default();
this.bishops = new BitBoard_1.default();
this.kings = new BitBoard_1.default();
this.queens = new BitBoard_1.default();
this.feedBoard(boardString);
}
Board.prototype.feedBoard = function (boardString) {
var _this = this;
this.boardString = boardString;
var rows = boardString.split('/');
var pos = 0;
for (var i = rows.length - 1; i >= 0; i--) {
rows[i].split('').forEach(function (char) {
if (/[0-9]/.test(char)) {
pos += parseInt(char);
}
else if (char === '.') {
pos = pos + 1;
}
else {
_this[letterToType(char)].setBit(pos);
_this[letterToColor(char)].setBit(pos);
pos = pos + 1;
}
});
}
};
Object.defineProperty(Board.prototype, "allPieces", {
get: function () {
return this.whites.or(this.blacks);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "empty", {
get: function () {
return this.allPieces.not();
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "whitePawns", {
// WHITE PIECES
get: function () {
return this.whites.and(this.pawns);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "whiteRooks", {
get: function () {
return this.whites.and(this.rooks);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "whiteKnights", {
get: function () {
return this.whites.and(this.knights);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "whiteBishops", {
get: function () {
return this.whites.and(this.bishops);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "whiteQueens", {
get: function () {
return this.whites.and(this.queens);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "whiteKing", {
get: function () {
return this.whites.and(this.kings);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "quietDestinations", {
get: function () {
return this.whites.not().and(this.blacks.not());
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "blackPawns", {
// BLACK PIECES
get: function () {
return this.blacks.and(this.pawns);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "blackRooks", {
get: function () {
return this.blacks.and(this.rooks);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "blackKnights", {
get: function () {
return this.blacks.and(this.knights);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "blackBishops", {
get: function () {
return this.blacks.and(this.bishops);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "blackQueens", {
get: function () {
return this.blacks.and(this.queens);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Board.prototype, "blackKing", {
get: function () {
return this.blacks.and(this.kings);
},
enumerable: false,
configurable: true
});
return Board;
}());
exports.default = Board;
function letterToColor(letter) {
return letter.toUpperCase() === letter ? 'whites' : 'blacks';
}
function letterToType(letter) {
var types = {
p: 'pawns',
r: 'rooks',
n: 'knights',
b: 'bishops',
k: 'kings',
q: 'queens',
P: 'pawns',
R: 'rooks',
N: 'knights',
B: 'bishops',
K: 'kings',
Q: 'queens',
};
return types[letter];
}