Copied this question from stackoverflow. Not 100% sure it belongs here but we'll see...
I am building a web interface for the stockfish chess engine using Node, and I noticed that the behaviour of the exact same commands behaves differently.
Here are the UCI commands I use when calling stockfish from the command line:
position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
go movetime 15000
The engine then responds as expected: Does analysis for 15 seconds while outputting every step on stdout, and ends with a suggestion in the following format:
bestmove e2e4 ponder e7e6
Here is the code that makes the same call as a node js child process:
app.get('/next_move/:fen', (req, res) => {
const stockfish = spawn('bin/stockfish');
stockfish.stdin.write(`position fen ${req.params.fen}\ngo movetime 15000`);
console.info(`Sending UCI command to stockfish: position fen ${req.params.fen}\ngo movetime 15000`);
stockfish.stdin.end();
stockfish.stdout.on('error', (error)=>{
console.error(`${error}`);
res.status(500).send('Internal server error');
stockfish.kill();
});
stockfish.stdout.on('message', (data)=>{
console.log(`Message: ${data}`);
});
stockfish.stdout.on('data', (data) =>{
let output = data.toString();
// Ignore the first line in stdout
if (output.substring(0, 9) === 'Stockfish') {
return;
}
console.info(`Stockfish output: ${output}`);
if (output.substring(0, 8) === 'bestmove') {
res.json({'bestMove': output.substring(8).trim()});
} else {
res.status(400).send('Bad FEN string');
}
stockfish.kill();
});
});
After I make this code run via web, get a result almost instantly, and also get a different output from stockfish:
bestmove a2a3
Why is this happening? Is this related to Nodejs or to Stockfish?