How to setup position with FEN string in stockfish.js?

Viewed 2406

I want to make a chess puzzle on my website for my student. I use stockfish.js to play with the engine. How to change the start position on the board? I try to change all FEN string but did not work. Where to look for the function or something? Anybody can help me, please?

1 Answers

Interacting with the javascript port of Stockfish is (at time of writing) still like communicating with a chess engine that uses/supports UCI (Universal Chess Interface).

The UCI position command should suffice:

   var fenString = "rnbqkbnr/ppppp1pp/8/5p2/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2"
   // start UCI
   stockfish.postMessage("uci");
   // start new game
   stockfish.postMessage("ucinewgame");
   // set new game position
   stockfish.postMessage("position fen " + fenString);
   // start search
   stockfish.postMessage("go depth 10");

Edited: Updated case for postMessage() function.

Related