将PGN转换为nodejs中的FEN字符串列表(国际象棋符号)

n0p*_*0pe 8 chess node.js chessboard.js

我正在使用nodejs构建一个与棋相关的应用程序.我一直在尝试chess.js尽可能多地使用,但我认为我在功能方面遇到了障碍.在扩展该功能之前,我想确保没有其他工具能够满足我的需求.

我正在寻找一种方法将PGN字符串转换为FEN移动列表.我希望load_pgn()在chess.js中使用将移动加载到对象中,然后遍历每个移动并调用该fen()函数以输出当前FEN.然而,chess.js似乎没有办法在游戏中走动.除非我错过了什么.

我宁愿不必进入解析字符串,但如果必须的话.有什么建议?

解:

另请参阅下面的efirvida答案以获得解决方案

这样的事情(未经测试)似乎有效.该函数接受Chess使用chess.js已加载PGN 的对象创建的对象.

function getMovesAsFENs(chessObj) {
    var moves = chessObj.history();
    var newGame = new Chess();
    var fens = [];
    for (var i = 0; i < moves.length; i++) {
      newGame.move(moves[i]);
      fens.push(newGame.fen());
    }
    return fens;
}
Run Code Online (Sandbox Code Playgroud)

efi*_*ida 6

看看github页面.load_pgn 链接

var chess = new Chess();
pgn = ['[Event "Casual Game"]',
       '[Site "Berlin GER"]',
       '[Date "1852.??.??"]',
       '[EventDate "?"]',
       '[Round "?"]',
       '[Result "1-0"]',
       '[White "Adolf Anderssen"]',
       '[Black "Jean Dufresne"]',
       '[ECO "C52"]',
       '[WhiteElo "?"]',
       '[BlackElo "?"]',
       '[PlyCount "47"]',
       '',
       '1.e4 e5 2.Nf3 Nc6 3.Bc4 Bc5 4.b4 Bxb4 5.c3 Ba5 6.d4 exd4 7.O-O',
       'd3 8.Qb3 Qf6 9.e5 Qg6 10.Re1 Nge7 11.Ba3 b5 12.Qxb5 Rb8 13.Qa4',
       'Bb6 14.Nbd2 Bb7 15.Ne4 Qf5 16.Bxd3 Qh5 17.Nf6+ gxf6 18.exf6',
       'Rg8 19.Rad1 Qxf3 20.Rxe7+ Nxe7 21.Qxd7+ Kxd7 22.Bf5+ Ke8',
       '23.Bd7+ Kf8 24.Bxe7# 1-0'];

chess.load_pgn(pgn.join('\n'));
// -> true

chess.fen()
// -> 1r3kr1/pbpBBp1p/1b3P2/8/8/2P2q2/P4PPP/3R2K1 b - - 0 24
Run Code Online (Sandbox Code Playgroud)

就像是

moves = chess.history();
var chess1 = new Chess();
for (move in moves){
    chess1.move(move);
    fen = chess1.fen()
}
Run Code Online (Sandbox Code Playgroud)


Sco*_*yet 5

(不是真正的答案;只是需要额外格式化的评论。)

你的getMovesAsFENs函数也可以写成:

function getMovesAsFENs(chessObj) {
    return chessObj.history().map(function(move) {
        chessObj.move(move);
        return chessObj.fen();
    });
}
Run Code Online (Sandbox Code Playgroud)

也许这对你来说并不重要,但这对我的整洁感很有吸引力。