Duchess 1.0

Duchess is a simple chess engine, and was my excuse to play with Rust.

The engine uses a Negascout search algorithm and a very basic evaluation function, with no opening book. It was made for fun and for learning purposes, it doesn’t even get close to more serious chess engines like Stockfish. It can win against an occasional player, but it won’t fool an expert player.

What about the user interface? I was certainly not going to build one. Duchess supports UCI (Universal Chess Interface) protocol, so it can talk to any UCI-compliant GUI. I have tested it successfully with Arena.

However, GUIs feel so.. antiquate. I want to run my engine in a browser!

Finding chess UIs written in Javascript, that can run in a browser, is no problem. A very simple one to use is chessboardjs. But now the problem, how do I make my Rust chess engine talk to a Javascript UI that is running in a browser?

WebAssembly to the rescue! WebAssembly (a.k.a. Wasm) is a binary instruction format that can run in your browser. So, if my chess engine could be compiled into Wasm, it could run in the very same place where the UI runs.

Rust has very good support for compiling into Wasm. On top of that, Wasm-pack takes care of preparing all the boilerplate code necessary for interfacing with Javascript, with or without Node.js.

All that remains to do is to annotate with #[wasm_bindgen] the Rust functions to be exposed to Javascript:

#[wasm_bindgen]
/// Computes the best move from the given board
pub fn find_best_move(fromFEN:&str) -> String {
  ..
}

and import them as javascript functions:

import { find_best_move } from './pkg/duchesslib.js';

function duchessMove(fromFEN) {
  last_duchess_fen = find_best_move(fromFEN)
  board.position(last_duchess_fen)
}

If you are curious, here’s the full code.

But now it’s time to play! Enjoy and please let me know if you find bugs :)
ps: if that wasn’t clear yet, my full chess engine is running in your browser tab. How cool is that? ;)

Written on November 19, 2022