Ich baue für jemanden eine Schachspiel-Datenbank auf und stecke an der Ziellinie fest.
Bei mir funktioniert alles (wenn eine Standard-Schach-Forsyth-Edwards-Notation (FEN) geladen ist)
Immer wenn ich ein benutzerdefiniertes Fen aus der Datenbank lade, wird das Fen einwandfrei geladen, aber wenn Sie auf „Weiter“ klicken Dabei wird auf die Standardeinstellung zurückgegriffen.
Bitte sagen Sie mir, was ich tun kann, um dieses Problem zu beheben.
ChatGPT weiß es auch nicht
Code: Select all
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 =
`[b]Tournament:[/b] ${data.tournament || "N/A"}
[b]Time Control:[/b] ${data.time_control || "N/A"}
[b]Variant:[/b] ${data.variant || "N/A"}
[b]White:[/b] ${data.white_player || "N/A"} (${data.white_elo || "N/A"})
[b]Black:[/b] ${data.black_player || "N/A"} (${data.black_elo || "N/A"})
[b]Result:[/b] ${data.result || "N/A"}
[b]Termination:[/b] ${data.termination || "N/A"}
[b]Date:[/b] ${data.date || "N/A"}
[b]Starting FEN:[/b] ${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);
}
});
};
Das Letzte, was ich versucht habe, war, das benutzerdefinierte Fen in die chess.js einzufügen
Code: Select all
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;
};
Code: Select all
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);
}
});
};