Xiangqi, Chinese chess, looks familiar for about a minute. Then a horse can’t move because a piece is standing next to it, a cannon jumps over a piece to capture, and a repeated position isn’t a draw until you know who was forcing the repetition.

web-xiangqi is an analysis and game-review app built around Pikafish, the strongest open-source xiangqi engine: a C++ fork of Stockfish for a nine-by-ten board with a river across the middle and a palace at each end. I compiled Pikafish to WebAssembly and serve its 53.7 MB neural network from a static host. The boards below run the app’s own rules and notation code, paired with evaluations from the Pikafish build the app ships.

The rules

web-xiangqi's own move validation for the piece picked up. Dots are legal; crosses are moves a rule refuses, and the piece in the way is ringed.

Even if you play chess every day, four xiangqi rules will catch you out. Pick up each piece on the board above to see them:

Before the app sends a position to the engine, its own TypeScript move generator has to agree with Pikafish on every legal move. A perft test, which counts every legal sequence to a fixed depth, holds the two together: 44 moves from the starting position, 1,920 two-move sequences, and 79,666 three-move sequences, matched exactly.

Writing a move down

炮piececannon · C 二file2, from your right 平directionacross · = 五tofile 5 = C2=5
The most common first move, in Chinese and in WXF letters. 進 (+) is forward and 退 (−) back; moving straight forward or back, the last number counts steps, otherwise it names the file.

Xiangqi notation reads like a four-character sentence rather than a square name, with files numbered 1 to 9 from each player’s own right. In the classic central-cannon opening 炮二平五 (C2=5 in World Xiangqi Federation letters), Red takes the cannon (炮) on file two (二) and slides it across (平) to file five (五). When a piece moves forward (進, +) or back (退, −), the last character changes meaning with the piece. For pieces that travel along a file, it counts steps. For pieces that move diagonally, like horses, elephants, and advisors, it names the file they land on.

You can play 炮二平五 on the board above, or switch the pieces to letters if you’re still learning the characters. (Red and Black even use different characters for the same piece, such as 仕 and 士 for advisors.) To read a move someone types, in Chinese, WXF letters, or engine coordinates, the app writes every legal move in the position in all three notations and keeps the one that matches. No parser can disagree with the move generator that way.

Compiling Pikafish

Pikafish C++, pinned to one commit pikafish-wasm.patch 340 lines; command() for stdin Emscripten 6.0.1 pinned; 6.0.2 broke threads threads + SIMD · 710 KB threads · 705 KB one thread + SIMD · 659 KB one thread · 654 KB What a first visit to the engine downloads: 55.1 MB app 0.7% engine code 1.7% pikafish.nnue, 53.7 MB, 97.6% The network is already Zstandard-compressed: served with gzip it grows by 8.8 KB. Nodes searched a second, with the isolation headers one thread 414,000 eight threads 3.7 million
One patched source tree, four cores chosen at load time by what the browser supports, a network that is almost all of the download, and what the threads buy.

Pikafish is a command-line program that sits waiting on standard input for engine commands. A browser worker has no standard input to wait on, so the heart of my 340-line patch turns main() into setup code and exposes one C function that handles one command at a time:

extern "C" void command(const char* cmd) {
    uciP->process_command(cmd);
}

The rest of the patch stubs out native threads for the single-threaded builds and removes NUMA node binding. The result is four WebAssembly cores of about 700 KB each, and the loader picks one at startup based on what the browser supports.

The compiler had to be pinned as tightly as the C++ source. Emscripten 6.0.2 quietly dropped a flag that told thread workers where the engine’s script lived, so every thread it spawned became new Worker(undefined). The engine would answer uci and then hang forever before readyok. Version 6.0.1 is pinned in the build.

Beside the 700 KB core sits pikafish.nnue, a 53.7 MB weight file that makes up 97.6% of a first visit’s download. It’s already Zstandard-compressed, so gzipping it on the server makes it 8.8 KB larger. Browsers won’t keep a response that big in their ordinary HTTP cache, so the app’s service worker stores the network in Cache Storage itself. The same service worker attaches the two cross-origin isolation headers GitHub Pages can’t send, which is what unlocks threads: an eight-thread search runs at 3.7 million nodes a second, against 414,000 on one.

The halfmove clock

One endgame, searched by Pikafish at depth 14 for every value of the FEN's halfmove clock. Blue is a forced mate.

A position is passed to the engine as a FEN string, one line of text describing the board, and every FEN ends with a halfmove clock: the number of plies since the last capture. Where chess draws after 50 moves (100 plies) without a capture, xiangqi draws after 60 (120 plies), and Pikafish reads the counter straight from the FEN.

In the horse-and-advisor endgame above, mate in three takes five plies. So the forced mate is still there with the clock at 115 (115 + 5 = 120, mate on the last legal ply) and turns into a 0.00 draw at 116.

Early versions of web-xiangqi wrote 0 into the clock of every FEN they sent and ignored the clock on pasted positions. Paste an endgame near the 60-move limit and the app would promise a forced mate the engine itself would have called a draw.

Repetition

Two repeating lines, judged after each move by web-xiangqi's own check and by Pikafish's rule judge.

Repetition is where xiangqi breaks hardest from chess. Repeating a position three times isn’t an automatic draw: the result depends on who is forcing the loop. A player who gives check on every move of a cycle loses unless they deviate, and so does a player who endlessly chases an unprotected piece.

The app’s own TypeScript catches perpetual check as soon as the cycle forms, since every recorded move already carries a check flag. It deliberately doesn’t guess at perpetual chase. Deciding whether a move legally “chases” means knowing which piece became newly attacked, whether it’s defended, and a tangle of exceptions for pinned pieces, generals, and soldiers. As the source comments put it, guessing would be worse than saying nothing. So the app calls an unverified loop a draw unless a chase is involved, and the WebAssembly patch exposes Pikafish’s internal rulejudge command, which rules correctly on both lines above.

Reviewing a game

A game between the app's Easy and Medium levels, reviewed by Pikafish at depth 16 with the app's review code, opened on the move that decided it.

The review scores every position at depth 16, turns each score into a winning chance, and grades each move by the milder of two losses: centipawns and winning chances. In the game above, play stays roughly level through a pair of mutual stumbles around move 32. Then Red steps its general off the back rank with 45.帥五進一, drops from 43% winning chances to 17%, and walks into mate eleven plies later.

That game was played between two of the app’s own levels, Easy and Medium, rather than taken from a book, and there’s a reason. web-xiangqi once shipped ten “classic grandmaster games”, complete with player names, events, and dates. None of them were real. Seven shared the same eight-move opening, and two contained illegal moves that had been silently cut short when they loaded.

The app’s own review engine is what gave them away. The supposed grandmasters scored 20% to 28% accuracy with seven to ten blunders apiece, barely ahead of the 7% that random legal moves score. All ten are gone. The historical games list stays empty until there’s a sourced collection whose games can be traced to real events.

It’s the same rule as the perpetual-chase check and the halfmove clock: when the app can’t verify something, it should say less, not guess.