🏠 back to Observable

Require Stockfish

It seems the library (https://github.com/nmrugg/stockfish.js/blob/master/example/stockfish.asm.js) is not packaged as an AMD module. I tried via html<script src="" />, but won’t get access to the var engineGame as proposed in the example: https://github.com/nmrugg/stockfish.js/blob/master/example/index.html#L109

However, there is a var STOCKFISH with a postMessage method, but I believe using it needs the Worker running, which is probably best, because it doesn’t block the UI.

Did anybody has more success?

Here’s what the API docs say:

In a web browser, Stockfish.js can be run in a web-worker, which can be created like this:

var stockfish = new Worker("stockfish.js");

The output of the engine is again posted as a message. To receive it, you need to add a message handler:

stockfish.onmessage = function onmessage(event) {
    console.log(event.data);
};

Input (standard UCI commands) to the engine is posted as a message to the worker:

stockfish.postMessage("go depth 15");

Here’s a working example:

2 Likes

Wow, that looks great, many thanks.

1 Like

I’ve build on Mike’s foundation and added a little interaction, like loading a board via Forsyth–Edwards Notation ( FEN ) . Here’s a session:

0000 ==> isready 
0000 <== readyok

0002 ==> uci 
0002     id name Stockfish.js 10 by T. Romstad, M. Costalba, J. Kiiski, G. Linscott, D. Dugovic, F. Fichter, N. Fiekas, et al.
0002     
0002     option name Debug Log File type string default 
0002     option name Contempt type spin default 24 min -100 max 100
0002     option name Analysis Contempt type combo default Both var Both var Off var White var Black
0002     option name Threads type spin default 1 min 1 max 1
0002     option name Hash type spin default 16 min 16 max 16
0002     option name Clear Hash type button
0002     option name Ponder type check default false
0002     option name MultiPV type spin default 1 min 1 max 500
0002     option name Skill Level type spin default 20 min 0 max 20
0002     option name Move Overhead type spin default 30 min 0 max 5    000
0002     option name Minimum Thinking Time type spin default 20 min 0 max 5    000
0002     option name Slow Mover type spin default 84 min 10 max 1    000
0002     option name nodestime type spin default 0 min 0 max 1    0000
0002     option name UCI_Chess960 type check default false
0002     option name UCI_Variant type combo default chess var chess
0002     option name UCI_AnalyseMode type check default false
0002     option name Skill Level Maximum Error type spin default 200 min 0 max 5    000
0002     option name Skill Level Probability type spin default 128 min 1 max 1    000
0002 <== uciok

0004 ==> ucinewgame 

0005 ==> position fen N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34

0006 ==> go depth 3
0006     info depth 1 seldepth 1 multipv 1 score cp 32 nodes 47 nps 7833 time 6 pv a8c7 e3a7 bmc 1
0006     info depth 2 seldepth 2 multipv 1 score cp 11 nodes 95 nps 10555 time 9 pv a8b6 a5a7 bmc 1.517
0006     info depth 3 seldepth 3 multipv 1 score cp 24 nodes 151 nps 12583 time 12 pv a8b6 a5a7 b6c4 bmc 0.784289
0006 <== bestmove a8b6 ponder a5a7

0008 ==> d 
0008     
0008      +---+---+---+---+---+---+---+---+
0008      | N |   |   |   |   |   |   |   |
0008      +---+---+---+---+---+---+---+---+
0008      | P |   |   |   | p | k |   | p |
0008      +---+---+---+---+---+---+---+---+
0008      |   |   |   | p |   |   | p |   |
0008      +---+---+---+---+---+---+---+---+
0008      | r |   |   |   |   | p |   |   |
0008      +---+---+---+---+---+---+---+---+
0008      |   |   |   |   |   |   |   |   |
0008      +---+---+---+---+---+---+---+---+
0008      |   |   |   |   | b |   |   | B |
0008      +---+---+---+---+---+---+---+---+
0008      |   |   |   |   | P |   | K | P |
0008      +---+---+---+---+---+---+---+---+
0008      |   | R |   |   |   |   |   |   |
0008      +---+---+---+---+---+---+---+---+
0008     
0008     Fen: N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34
0008     Key: 4E025494EA4A7523
0008     Checkers: 
0008 <== Legal uci moves: a8b6 a8c7 h3g4 h3f5 b1a1 b1c1 b1d1 b1e1 b1f1 b1g1 b1h1 b1b2 b1b3 b1b4 b1b5 b1b6 b1b7 b1b8 g2f1 g2g3 g2h1 g2f3
1 Like

It might be a fun project to try to set up a graphical interface with chess.js and chessboard.js:

1 Like