A purely functional chess engine and HTTP API in Scala 3, Cats Effect, and Http4s.
A purely functional chess engine implementation in Scala 3 that demonstrates advanced functional programming concepts and concurrent system design. Implements perft-validated move generation, FEN notation parsing, alpha-beta search with transposition tables and Zobrist hashing, piece-square evaluation, and a stateless HTTP API. Built with Cats Effect for side-effect management and Http4s for the REST API layer.
The goal is to create a production-ready chess library similar to Lichess's scalachess, with WebSocket support for live games and a stateless HTTP API for move validation and engine suggestions.
Core engine is complete and tested. A basic frontend player-vs-engine mode is also working:
- Domain model — Piece, Board, Square, Move, GameState, FEN notation, castling rights
- Move generation — pseudo-legal + legal moves, perft-validated across 4 positions (depths 1–4)
- Search — negamax with alpha-beta pruning, transposition table, Zobrist hashing
- Evaluation — material counting + piece-square tables
- HTTP API — health check, best-move suggestion, move validation
- Multiplayer backend — GameRegistry with game creation, state lookup, move application
- PGN parser — tag parser, SAN resolver with disambiguation, SAN encoder
- Frontend MVP — Vite + React dark UI, visual board, legal move highlighting, engine auto-reply
- Tests — 259 tests across 9 suites, all passing
# rebuild devcontainer once (if not done already) devcontainer up --workspace-folder . --remove-existing-container # backend (terminal 1) devcontainer exec --workspace-folder . sbt "~reStart" # frontend (terminal 2) devcontainer exec --workspace-folder . npm --prefix frontend run dev # enter workspace folder in neovim devcontainer exec --workspace-folder . nvim .
| Language | Scala 3.8.2 |
| Build | sbt 1.12.8 |
| Effects | Cats Effect 3, Http4s 0.23, Circe 0.14 |
| Test | MUnit + munit-cats-effect |
| Formatter | scalafmt 3.10.7 (scala3 dialect, remove-optional-braces) |
| Plugins | sbt-tpolecat (warnings-as-errors), sbt-revolver, sbt-assembly |
src/main/scala/chessengine/
├── domain/ Piece, Board, Square, Move, GameState, Fen, CastlingRights, Color, Role, Side
├── logic/ MoveGenerator — pseudo-legal + legal move generation, check/stalemate detection
├── engine/ Search (negamax + alpha-beta), Evaluation (material + PST),
│ TranspositionTable, Zobrist hashing, Score, TTEntry
├── game/ GameRegistry — Ref-backed multiplayer game state
└── api/ Http4s server (port 8080), routes (health, best-move, validate-move,
game create/lookup/move), DTOs
frontend/src/
├── api/ typed fetch client generated from OpenAPI types
├── chess/ UI-only FEN helpers
├── components/ Board, Landing, GameView
├── modes/ PlayerVsEngine
└── types/ generated OpenAPI types
src/test/scala/chessengine/
├── domain/ SquareSuite (139), FenSuite (23), BoardSuite (9), GameStateSuite (23)
├── logic/ MoveGeneratorSuite (14) — perft tests
├── engine/ SearchSuite (4), EvaluationSuite (6)
└── api/ ChessRoutesSuite (8), GameRoutesSuite (7)
Squareis an opaque type wrappingInt(0–63), row-major:rank * 8 + file. a1=0, h8=63.Boardis aVector[Option[Piece]]of length 64.GameState.applyMoveuses incremental Zobrist hashing — the hash updates on each move rather than recomputing.TranspositionTableis a fixed-size array with a dual-bucket strategy (depth-preferred + recent-replace per bucket). Size must be a power of 2.Searchuses negamax with alpha-beta pruning. Checkmate scores are ply-adjusted before TT storage.MoveGenerator.isLegalapplies the move then checks if the moving player's king is attacked.GameRegistrywraps aRef[IO, Map[UUID, GameState]]for thread-safe multiplayer game storage.
| Priority | What | Status |
|---|---|---|
| — | Multiplayer backend (GameRegistry, game routes, PGN parser, Fen.write) | Done |
| — | Frontend player-vs-engine MVP (visual board, dark UI, engine replies) | Paused |
| 1 | Multiplayer frontend + backend game status/clocks | Next |
| 2 | Bitboard optimization | Later |
MIT