To get started I compiled Emscripten, and the Emscripten forks
of LLVM and clang as Enscripten currently prefers to use
the "fastcomp" LLVM backend which is not the version I had
previously installed with brew. After configuring my Emscripten to
use the new clang I could run
emcc which is pretty much a drop-in replacement
I found the chess-at-nite randomly by googling for chess engine
source code. I would have liked to have used the GNU Chess engine,
but I have looked at the source previously and did not think it was a great
codebase to adapt. Fortunately the chess-at-nite source compiles with
looked like a pretty nice codebase to hack on.
When I initially tried to compile the code with Enscripten I got a heap of
'error: reference to 'move' is ambiguous' errors. Unfortunately many of the
source files use
'using namespace std;' and apparently the Emscripten
move. After updating all the usages to only include the
referenced parts of the
std namespace, it compiled and even printed the menu
in the Emscripten generated web page! And then crashed my browser.
At this point I thought it would be awesome if I could compile it as a worker
and have it automagically use
stderr. I was disappointed, but this is
a compiler option I would consider working on!
For this post the worker takes a FEN game state and returns a new FEN string with an updated game state. Luckily chess.js and chessboard.js support FEN strings which made wiring up the UI quite straight forward. As FEN strings do not provide enough information about previous moves to detect threefold repetitions, the engine is quite susceptible to threefold repetition draws, as can be seen when it plays itself.
Ideally I would have liked to have the Chess engine running in the worker,
comunicating with the xboard protocol using "standard"
the web worker
postMessage interface. However I am pretty happy
with the results and may have found a project I want to contribute too.