C# · .NET 10 · WebAssembly

StrangeChess

A chess engine written from scratch in C#. It uses magic bitboards, alpha-beta search and a tapered evaluation, with no chess libraries. The same code runs on this page, compiled to WebAssembly, so you can play it below.

line it keeps line it prunes

0perft nodes / secmove generator, depth 6 · Ryzen 5 5600G
0search nodes / secalpha-beta, 4 positions at 5 s each · Ryzen 5 5600G
0Elo stronger than Stockfish 2000from 100 games · likely range +122 to +279
0perft(6) nodesmatches the known count exactly
0transposition table slotsZobrist-hashed
0lines of C#6 files

Speeds are from the native .NET 10 Release build on one thread of an AMD Ryzen 5 5600G. The WebAssembly build in the play section runs at about 0.8–1.0M nodes/s on the same machine.

Live

Play the engine in your browser

This board runs the same C# engine, compiled to WebAssembly and running in a Web Worker. Legal moves, check detection and the search all come from the engine.

Inside the engine

Alpha-beta search, magic bitboards and a tapered evaluation

What happens between your move and its reply.

Alpha-beta pruning, step by step

The engine looks ahead through a tree of moves. Squares are positions where it picks the best score for itself; circles are where the opponent picks the worst. Once a branch provably can't change the answer, it stops looking at it.

Magic bitboards

The board is stored as 64-bit integers, one bit per square. To find where a queen can go, the engine does two lookups, one for its rook moves and one for its bishop moves. Each time, the pieces in the way are masked, multiplied by a precomputed "magic" number and shifted. The result indexes a table that already holds every square the piece can reach.

Click squares to add or remove blockers. The magic numbers, shifts and variable names are the engine's own, for a queen on d4.

Negamax with alpha-beta

One search function for both sides: a position's score for one player is the negative of the other's.

Iterative deepening

It searches to depth 1, then 2, then 3 and so on until time runs out, so there is always a finished answer to play. From depth 3, each search starts in a ±50 centipawn window around the last score and widens only if the score lands outside it.

Transposition table

1,048,576 slots keyed by a Zobrist hash. A position reached by a different move order is looked up instead of searched again, and its stored best move is tried first.

Null-move pruning

The engine gives the opponent a free move. If the position still beats the cutoff, the real search is skipped. It's turned off in check and when the side to move has only pawns left, where passing can be the best move.

Late move reductions

Quiet moves after the first four are searched 1 to 2 plies shallower. If one scores better than expected, it gets a full-depth re-search.

Move ordering

The move stored in the transposition table goes first, then captures sorted by most valuable victim and least valuable attacker, then two killer moves per ply. Better order means earlier cutoffs.

Quiescence search

At the end of the main search it keeps following captures and promotions, so it doesn't stop scoring halfway through a trade.

Tapered evaluation

Separate middlegame and endgame piece-square tables, blended by the material left on the board. The king shelters early and walks to the centre late.

Positional terms

Bishop pair, rooks on open files and the seventh rank, doubled, isolated and passed pawns, the king's pawn shield, development and castling.

Mop-up evaluation

When it's ahead in an endgame, it pushes the losing king to the edge and brings its own king closer, to help turn winning positions into mates.

Repetition detection

A history of Zobrist keys spots repeated positions inside the search and scores them as draws.

UCI protocol

It speaks the Universal Chess Interface, so it works with UCI chess GUIs such as cutechess. The console next to the board shows the same kind of output.

Build pipeline

From C# source to WebAssembly in your browser

  1. Engine/*.csBoard, move generator, search, evaluation
  2. .NET 10 + Mono AOTcompiled ahead of time with the wasm-tools workload
  3. WebAssemblyabout 1.3 MB with Brotli compression (5.5 MB raw)
  4. Web Workersearches off the main thread, so the page stays responsive while it thinks
  5. This boardTypeScript UI talking to the engine through JSExport

Testing

Against Stockfish

The latest run was games against , with , played with cutechess-cli, 4 games at a time and no opening book. Stockfish's Elo limit is calibrated at that time control. From that score, cutechess-cli estimates StrangeChess at about 200 Elo above this Stockfish, so roughly 2200, with a likely range of +122 to +279.

wins
draws
losses

Stack

Engine in C# and .NET 10, site in TypeScript on Vercel