Mini chess engine
A from-scratch move generator and mate-in-one solver, with check detection that scans outward from the king instead of enumerating the opponent's replies.

- 16M/sec
- 2.4M/sec
The problem
A move generator needs to be complete across all six piece types, including edge cases like four-way pawn promotion — and a checkmate solver needs a fast, correct way to answer “is this king in check,” since that question gets asked on nearly every candidate move while searching for a forced mate.
The approach
The board is a flat 8×8 char array, with a FEN encoder/decoder for loading and inspecting positions. Move generation covers all six piece types and produces pseudo-legal moves — legal by piece-movement rules, but not yet checked against leaving your own king exposed. The mate-in-one solver builds on top of that: for the side to move, it tries every pseudo-legal move, discards any that leave its own king in check, and for each survivor checks whether the opponent has any response that escapes check. If none do, that’s the winning move.
What was hard
Answering “is the king in check” without redoing the work move generation already did. The obvious approach — generate every opposing move and see if one lands on the king — duplicates most of that work. Instead, the check-detection routine works backward from the king’s own square: it walks outward along the eight rook/bishop directions until it hits a piece, checks the eight knight-offset squares directly, and checks the two pawn-attack squares and the eight adjacent squares for the enemy king. That one routine gets reused for both “is my king safe after this move” and “does the opponent have an escape,” which is most of what makes the solver work at all.
Result
The solver reads its test positions from an XOR-obfuscated binary file — each byte decoded against a key stored in the file itself — which points at this having started as a course assignment with grader-supplied test vectors rather than a general-purpose tool. Feed it a position and it returns the mate-in-one move, or reports that there isn’t one. Move generation and check detection are solid; there’s no evaluation function or search beyond the one-move case, so it’s not something you could actually play a full game against yet.