@chess-fu/chess-game
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -40,2 +40,3 @@ "use strict"; | ||
var PATTERN_UPPER_CASE = /[A-Z]/g; | ||
var PATTERN_PGN_QUOTED = /"|\\\\/g; | ||
var PIECE_TYPES = 'kqbnrp'.split(EMPTY_STRING); | ||
@@ -146,3 +147,49 @@ var PROMOTIONS = 'qrnb'.split(EMPTY_STRING); | ||
}; | ||
ChessGame.prototype.pgn = function (options) { return EMPTY_STRING; }; | ||
ChessGame.prototype.pgn = function (options) { | ||
var newline = (options || {}).newline_char || '\n'; | ||
var width = (options || {}).max_width || 80; | ||
if (!this._headers.Result) { | ||
this._headers.Result = this._gameResult; | ||
} | ||
var pgn = []; | ||
var headers = __assign({}, this._headers); | ||
pgn.push.apply(pgn, this.pgnHeaders(constants_1.STANDARD_PGN_HEADERS, headers)); | ||
pgn.push.apply(pgn, this.pgnHeaders(Object.keys(headers), headers)); | ||
pgn.push(); | ||
var _a = new fen_parser_1.default(this._headers.FEN || constants_1.START_FEN), turn = _a.turn, moveNumber = _a.moveNumber; | ||
var history = this._history; | ||
var moveCounter = Math.max(1, moveNumber); | ||
var line = ''; | ||
for (var ix = 0; ix < history.length; ix++) { | ||
var moveNum = (moveCounter++) + "."; | ||
var white = history[ix].san; | ||
if (ix === 0 && turn === 'b') { | ||
white = '...'; | ||
} | ||
else { | ||
ix++; | ||
} | ||
var black = ix < history.length ? history[ix].san : null; | ||
var reqLen = 1 + moveNum.length + 1 + white.length + 1 + (black ? (black.length + 1) : 0); | ||
if (line.length && (line.length + reqLen) >= width) { | ||
pgn.push(line); | ||
line = ''; | ||
} | ||
line += "" + (line.length ? ' ' : '') + moveNum + " " + white + (black ? " " + black : ''); | ||
} | ||
if (line.length) { | ||
pgn.push(line); | ||
} | ||
pgn.push(this._headers.Result); | ||
return pgn.join(newline); | ||
}; | ||
ChessGame.prototype.pgnHeaders = function (keys, headers) { | ||
return keys | ||
.filter(function (key) { return headers.hasOwnProperty(key) && headers[key] !== null && headers[key] !== undefined; }) | ||
.map(function (key) { | ||
var value = ("" + headers[key]).replace(PATTERN_PGN_QUOTED, function (m) { return '\\' + m[0]; }); | ||
headers[key] = undefined; | ||
return "[" + key + " \"" + value + "\"]"; | ||
}); | ||
}; | ||
ChessGame.prototype.squares = function () { | ||
@@ -266,14 +313,15 @@ return Object.keys(INDEXES); | ||
var movesValid = this.moves({ color: this._turn }); | ||
var findSan = san.trim() | ||
.replace(PATTERN_PROMOTE_RNB, '=Q') | ||
.replace(PATTERN_ANNOTATION, EMPTY_STRING); | ||
var found = movesValid.filter(function (m) { return findSan === m.san && m.color === _this._turn; }); | ||
var sanMove = new pgn_parser_1.default().parseMove(san) || {}; | ||
var found = movesValid | ||
.filter(function (m) { return (sanMove.san === m.san || (sanMove.to === m.to && | ||
(sanMove.piece || '').toLowerCase() === m.piece.toLowerCase() && | ||
(!sanMove.from || m.from.indexOf(sanMove.from.toLowerCase()) >= 0) && | ||
m.color === _this._turn)); }); | ||
if (found.length !== 1) { | ||
console.warn("Can not find move " + san + " in " + movesValid.map(function (m) { return m.san; }).join(',')); | ||
throw new Error("The SAN " + san + " is not valid."); | ||
} | ||
var move = found[0]; | ||
if (move.promotion) { | ||
var match = san.match(PATTERN_PROMOTE_TYPE); | ||
if (match) | ||
move.promotion = match[1]; | ||
if (move.promotion && sanMove.promotion) { | ||
move.promotion = sanMove.promotion.toLowerCase(); | ||
} | ||
@@ -647,4 +695,10 @@ return this.performMove(move, false); | ||
if (!options.simple) { | ||
var disambiguation = this.disambiguateFrom(targetIx, movingPiece.toLowerCase(), opponentColor); | ||
move.san = chessUtils_1.buildSAN(move, disambiguation); | ||
var pieceType = movingPiece.toLowerCase(); | ||
if (pieceType === constants_1.KING || (pieceType === constants_1.PAWN && !move.captured)) { | ||
move.san = chessUtils_1.buildSAN(move, EMPTY_ARRAY); | ||
} | ||
else { | ||
var disambiguation = this.disambiguateFrom(targetIx, pieceType, opponentColor); | ||
move.san = chessUtils_1.buildSAN(move, disambiguation); | ||
} | ||
} | ||
@@ -651,0 +705,0 @@ var x = move.x, y = move.y, resultMove = __rest(move, ["x", "y"]); |
@@ -28,2 +28,3 @@ "use strict"; | ||
exports.ONGOING = '*'; | ||
exports.STANDARD_PGN_HEADERS = ['SetUp', 'FEN', 'Event', 'Site', 'Date', 'Time', 'UTCDate', 'UTCTime', 'Round', 'White', 'Black', 'Result']; | ||
//# sourceMappingURL=constants.js.map |
@@ -31,2 +31,3 @@ import { Move, MoveOptions, Offset, Color, GameResult } from './chessTypes'; | ||
}): string; | ||
pgnHeaders(keys: string[], headers: any): string[]; | ||
squares(): string[]; | ||
@@ -33,0 +34,0 @@ turn(): Color; |
@@ -26,1 +26,2 @@ export declare const WHITE = "w"; | ||
export declare const ONGOING = "*"; | ||
export declare const STANDARD_PGN_HEADERS: string[]; |
{ | ||
"name": "@chess-fu/chess-game", | ||
"access": "public", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Chess game logic", | ||
@@ -38,12 +38,13 @@ "author": "chess-fu.com", | ||
"scripts": { | ||
"prebuild": "../../node_modules/.bin/rimraf ./dist", | ||
"clean": "../../node_modules/.bin/rimraf ./dist", | ||
"build": "../../node_modules/.bin/tsc", | ||
"test": "cd ../../ && ./node_modules/.bin/mocha ./modules/chess-game/*.test.ts", | ||
"prepack": "npm run build", | ||
"test": "cd ../../ && ./node_modules/.bin/mocha ./modules/chess-game/src/*.test.ts", | ||
"prepack": "npm run test && npm run clean && npm run build", | ||
"prepublish": "npm version patch", | ||
"release": "npm publish -tag latest --access public" | ||
}, | ||
"dependencies": { | ||
"@chess-fu/fen-parser": "^1.1.1", | ||
"@chess-fu/pgn-parser": "^1.1.1" | ||
"@chess-fu/fen-parser": "^1.1.3", | ||
"@chess-fu/pgn-parser": "^1.1.2" | ||
} | ||
} |
49229
1131
Updated@chess-fu/fen-parser@^1.1.3
Updated@chess-fu/pgn-parser@^1.1.2