Negamax with alpha-beta
One search function for both sides: a position's score for one player is the negative of the other's.
C# · .NET 10 · WebAssembly
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
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
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
What happens between your move and its reply.
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.
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.
One search function for both sides: a position's score for one player is the negative of the other's.
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.
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.
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.
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.
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.
At the end of the main search it keeps following captures and promotions, so it doesn't stop scoring halfway through a trade.
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.
Bishop pair, rooks on open files and the seventh rank, doubled, isolated and passed pawns, the king's pawn shield, development and castling.
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.
A history of Zobrist keys spots repeated positions inside the search and scores them as draws.
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
Testing
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.
Stack