Chess.js加载自定义FEN后,通过PGN移动时回退至默认FEN导致游戏异常的问题求助
Chess.js加载自定义FEN后,通过PGN移动时回退至默认FEN导致游戏异常的问题求助
抱歉,这可能是个很新手的问题,但我实在卡在最后一步了😅。我正在帮别人做一个国际象棋PGN数据库,现在遇到了一个棘手的问题:
- 当加载标准的Forsyth–Edwards Notation(FEN)时,所有功能都正常工作
- 但从数据库加载自定义FEN时,棋盘能正确显示初始位置,可一点击“下一步”按钮,就会跳回默认的起始FEN,完全乱套了
我试了好多种方法都没解决,连ChatGPT都帮不上忙,只能来求助各位大佬了!
我当前的核心代码:
window.loadPGN = function(id) { $.getJSON('php/get_pgn.php?id=' + id, function(data) { if (data.success) { const startingFen = data.starting_fen || 'start'; // Initialize the chessboard with the starting FEN board = ChessBoard('annotation-board', { pieceTheme: cfg.pieceTheme, position: startingFen, }); // Initialize the Chess.js game object with the starting FEN game = new Chess(startingFen); // Load the PGN into the Chess.js game instance game.load_pgn(data.pgn_data); // Display PGN and metadata $('#move-window').html(data.pgn_data); let metadata = `<strong>Tournament:</strong> ${data.tournament || "N/A"}<br> <strong>Time Control:</strong> ${data.time_control || "N/A"}<br> <strong>Variant:</strong> ${data.variant || "N/A"}<br> <strong>White:</strong> ${data.white_player || "N/A"} (${data.white_elo || "N/A"})<br> <strong>Black:</strong> ${data.black_player || "N/A"} (${data.black_elo || "N/A"})<br> <strong>Result:</strong> ${data.result || "N/A"}<br> <strong>Termination:</strong> ${data.termination || "N/A"}<br> <strong>Date:</strong> ${data.date || "N/A"}<br> <strong>Starting FEN:</strong> ${startingFen}`; $('#annotation-window').html(metadata); // Reset move history and controls moves = game.history({ verbose: true }); currentMoveIndex = 0; $('#nextBtn').on('click', function() { if (currentMoveIndex < moves.length) { const move = moves[currentMoveIndex]; // Get the next move game.move(move); // Apply the move to the game board.position(game.fen()); // Update the board with the new position currentMoveIndex++; } else { console.log("No more moves."); } }); $('#prevBtn').on('click', function() { if (currentMoveIndex > 0) { game.undo(); // Undo the last move board.position(game.fen()); // Update the board with the new position currentMoveIndex--; } else { console.log("Already at the first move."); } }); $('#startPositionBtn').on('click', function() { game.reset(); // Reset the game to its initial state game.load(startingFen); // Reload the game with the starting FEN board.position(startingFen); // Set the board to the starting FEN currentMoveIndex = 0; }); $('#endPositionBtn').on('click', function() { while (currentMoveIndex < moves.length) { const move = moves[currentMoveIndex]; game.move(move); // Apply the move currentMoveIndex++; } board.position(game.fen()); // Update the board to the final position }); } else { console.error("Failed to fetch PGN:", data.message); } }); };
我最后尝试的解决方案(没用):
我试着修改Chess.js的默认FEN,然后在加载时注入自定义值:
Chess.js里的修改:
var DEFAULT_POSITION = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'; // Allow injection of a custom FEN for game initialization window.setCustomFEN = function(fen) { DEFAULT_POSITION = fen; };
然后更新index.html里的loadPGN函数:
window.loadPGN = function(id) { $.getJSON('php/get_pgn.php?id=' + id, function(data) { if (data.success) { setCustomFEN(data.starting_fen); // ✅ Injecting the custom FEN game = new Chess(DEFAULT_POSITION); // ✅ Using the overridden FEN board.position(DEFAULT_POSITION); game.load_pgn(data.pgn_data); moves = game.history({ verbose: true }); currentMoveIndex = 0; $('#move-window').html(data.pgn_data); } }); };
但还是没解决问题,点击下一步还是会跳回默认FEN。有没有大佬能帮我看看哪里出问题了?
备注:内容来源于stack exchange,提问作者Werner Roos
相关产品推荐
相关产品推荐

