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
93 lines (92 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var createNewGameState_1 = require("./createNewGameState/createNewGameState");
var createNewGameScan_1 = require("./createNewGameScan");
var validate_1 = require("./helpers/validate");
var fen_1 = require("./helpers/fen");
// @TODO use getters and setters ?
/**
* The Game class
*
* @public
*/
var Game = /** @class */ (function () {
/**
* The class constructor to build a Game instance
* @param fenString - a string in FEN notation representing a particular game position
*
* @public
*/
function Game(fenString) {
// @TODO test everything in Game class
this.state = {
fenBoard: '',
hasToPlay: 'w',
availableCastlings: '',
enPassantTarget: '',
halfMoveClock: 0,
fullMoveClock: 0,
};
this.scan = {
legalMoves: [],
kingState: {
isChecked: false,
isCheckMated: false,
isDraw: false,
},
};
validate_1.default.fenStringSyntax(fenString);
this.updateFen(fenString);
this.updateState((0, fen_1.fenToState)(fenString));
this.updateScan();
}
/**
* Updates fen property with a new string in FEN notation
* @param newFen - a new string in FEN notation to update fen property
*
* @internal
*/
Game.prototype.updateFen = function (newFen) {
this.fen = newFen;
};
/**
* Updates state property with a new state
* @param newState - a new state to update state property
*
* @internal
*/
Game.prototype.updateState = function (newState) {
this.state = newState;
};
/**
* Updates scan property with a new scan
*
* @internal
*/
Game.prototype.updateScan = function () {
this.scan = (0, createNewGameScan_1.createNewGameScan)(this.state);
};
/**
* Adds a move to the game
* @param move - a move in long UCI notation to be added to the game
* @returns a new string in FEN notation to represent the game after the added move
*
* @public
*/
Game.prototype.addMove = function (move) {
// @TODO add lots of edge cases tests for this method
validate_1.default.moveSyntax(move);
this.checkIfLegalMove(move);
this.updateState((0, createNewGameState_1.default)(move, this.state));
this.updateFen((0, fen_1.stateToFen)(this.state));
return this.fen;
};
Game.prototype.checkIfLegalMove = function (move) {
// @TODO if the move is not legal : throw new Error('The provided move is not legal')
// @TODO ILegalMoves may not have the easier to consume data structure
// it could be easier with a map instead of array
// investigate how it is used on the frontend and in this method.
};
return Game;
}());
exports.default = Game;