I have made a Stockfish-js engine web app but am adding an option to force the engine to play a user chosen move after the human user makes a first move (currently 1.e4 & where the user can choose 1…e5/1…e6/1…c6/1…d6 or Stiockfish book move) by clicking a btn. I am not an experienced webdev programmer or chess programmer & the code for this feature only works till move 3, when Stockfish chess engine stops & does not make a third move reply & ciontinue the game.Why? I feel like the comic Disney character Forky… I don’t know!? Can anyone help please find why stockish stops after move 3 & the game cannot continue. Most grateful for any helpful advice, thank you
import { Chess } from "https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.13.4/chess.js";
var game = new Chess();
var board = null;
var firstMoveChosen = false; // Flag for user's choice of engine reply
var userHasMoved = false; // Flag for user move status
// Initialize Stockfish engine worker
var stockfish = new Worker("stockfish.js");
// Event listener for Stockfish's response
stockfish.addEventListener("message", function (e) {
if (e.data.startsWith("bestmove")) {
var bestMove = e.data.split(" ")[1];
if (bestMove && (firstMoveChosen || userHasMoved)) {
const move = {
from: bestMove.slice(0, 2),
to: bestMove.slice(2, 4),
promotion: "q", // Assuming always promoting to a queen
};
// Apply the move and reset flags as needed
game.move(move);
board.position(game.fen());
updateStatus();
// Reset flags
firstMoveChosen = false;
userHasMoved = false;
// Trigger the engine's next move if the game continues
if (!game.game_over() && game.turn() === "b") {
setTimeout(() => {
stockfish.postMessage("position fen " + game.fen());
stockfish.postMessage("go depth 15");
}, 1000); // Added delay for realistic play pace
}
}
}
});
// Handle user making a move
function onDrop(source, target) {
var move = game.move({
from: source,
to: target,
promotion: "q", // NOTE: always promote to a queen for simplicity
});
// Illegal move
if (move === null) return "snapback";
updateStatus();
userHasMoved = true; // Set flag indicating the user has moved
// Trigger engine to play after the user's move if the game is ongoing
if (!game.game_over()) {
setTimeout(() => {
stockfish.postMessage("position fen " + game.fen());
stockfish.postMessage("go depth 15");
}, 1000); // Added delay for realistic play pace
}
}
function onSnapEnd() {
board.position(game.fen());
}
function updateStatus() {
var status = "";
var moveColor = "White";
if (game.turn() === "b") {
moveColor = "Black";
}
// Check game over conditions
if (game.in_checkmate()) {
status = "Game over, " + moveColor + " is in checkmate.";
} else if (game.in_draw()) {
status = "Game over, drawn position";
} else {
status = moveColor + " to move";
if (game.in_check()) {
status += ", " + moveColor + " is in check";
}
}
// Update status, FEN, and PGN
document.getElementById("status").innerText = status;
document.getElementById("fen").innerText = game.fen();
document.getElementById("pgn").innerText = game.pgn();
}
That’s it-engine function control-rather,lack of it, to achieve this engine forcing move customisation on move 1 with Stockfish as Black & after Wghite chooses Black’s reply game engine play should continue normally.I struggle to understand this level of coding & was pleased to get Stockfish working in my simple web app & this is pushed beyond my comfort level, although I know there are many good programmers who will have a good grasp of this & hope can answer this positively.