@chess-fu/chess-game
Advanced tools
Comparing version 0.1.3 to 1.1.1
@@ -69,5 +69,4 @@ "use strict"; | ||
} | ||
ChessGame.prototype.init = function (saveHeader) { | ||
if (!saveHeader) | ||
this._headers = {}; | ||
ChessGame.prototype.init = function () { | ||
this._headers = {}; | ||
this._history = []; | ||
@@ -81,2 +80,4 @@ this._board = EMPTY_BOARD(); | ||
this._castles = EMPTY_STRING; | ||
this._cacheValid = false; | ||
this._gameResult = constants_1.ONGOING; | ||
}; | ||
@@ -88,3 +89,4 @@ ChessGame.prototype.load = function (fen) { | ||
if (fen !== constants_1.START_FEN) { | ||
this._headers = __assign({}, this._headers, { SetUp: '1', FEN: fen }); | ||
this._headers.SetUp = '1'; | ||
this._headers.FEN = fen; | ||
} | ||
@@ -164,3 +166,3 @@ }; | ||
} | ||
return this._headers; | ||
return __assign({}, this._headers, { Result: this._gameResult }); | ||
}; | ||
@@ -214,28 +216,23 @@ ChessGame.prototype.history = function (options) { | ||
}; | ||
ChessGame.prototype.updated = function (saveResult) { | ||
if (!saveResult) { | ||
delete this._headers.Result; | ||
} | ||
ChessGame.prototype.updated = function () { | ||
this._gameResult = constants_1.ONGOING; | ||
this._cacheValid = false; | ||
}; | ||
ChessGame.prototype.updateCachedState = function () { | ||
if (this._cacheValid) | ||
return; | ||
this._cacheValid = true; | ||
this._checked = this.testForCheck(this._turn); | ||
this._noValidMoves = !this.hasValidMoves(this._turn); | ||
if (!this.isOngoing()) { | ||
return; | ||
} | ||
if (this.isAutomaticDraw()) { | ||
this._headers.Result = constants_1.DRAW; | ||
} | ||
else { | ||
if (this._noValidMoves) { | ||
if (this._checked) { | ||
this._headers.Result = this._turn === constants_1.WHITE ? constants_1.BLACK_WINS : constants_1.WHITE_WINS; | ||
if (this._cacheValid !== true) { | ||
this._cacheValid = true; | ||
this._checked = this.testForCheck(this._turn); | ||
this._noValidMoves = !this.hasValidMoves(this._turn); | ||
if (this.isAutomaticDraw()) { | ||
this._gameResult = constants_1.DRAW; | ||
} | ||
else { | ||
if (this._noValidMoves) { | ||
if (this._checked) { | ||
this._gameResult = this._turn === constants_1.WHITE ? constants_1.BLACK_WINS : constants_1.WHITE_WINS; | ||
} | ||
else { | ||
this._gameResult = constants_1.DRAW; | ||
} | ||
} | ||
else { | ||
this._headers.Result = constants_1.DRAW; | ||
} | ||
} | ||
@@ -679,11 +676,5 @@ } | ||
ChessGame.prototype.getResult = function () { | ||
switch (this._headers.Result || constants_1.ONGOING) { | ||
case constants_1.WHITE_WINS: return constants_1.WHITE_WINS; | ||
case constants_1.BLACK_WINS: return constants_1.BLACK_WINS; | ||
case constants_1.DRAW: return constants_1.DRAW; | ||
default: return constants_1.ONGOING; | ||
} | ||
return this._gameResult; | ||
}; | ||
ChessGame.prototype.assertOngoing = function () { | ||
this.updateCachedState(); | ||
if (!this.isOngoing()) { | ||
@@ -751,2 +742,3 @@ throw new Error("Invalid operation, game is over: " + this.getResult() + "."); | ||
this._headers.Result = constants_1.DRAW; | ||
this._gameResult = constants_1.DRAW; | ||
}; | ||
@@ -753,0 +745,0 @@ ChessGame.prototype.isDraw = function () { |
@@ -17,6 +17,7 @@ import { Move, MoveOptions, Offset, Color, GameResult } from './chessTypes'; | ||
private _cacheValid; | ||
private _gameResult; | ||
private _checked; | ||
private _noValidMoves; | ||
constructor(fen?: string); | ||
init(saveHeader?: boolean): void; | ||
private init; | ||
load(fen?: string): void; | ||
@@ -23,0 +24,0 @@ loadPgn(pgnGame: string): void; |
{ | ||
"name": "@chess-fu/chess-game", | ||
"access": "public", | ||
"version": "0.1.3", | ||
"version": "1.1.1", | ||
"description": "Chess game logic", | ||
@@ -6,0 +6,0 @@ "author": "chess-fu.com", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
46303
1074