How michaelb7/crafty works
Purpose and entry points
Crafty is a native ANSI C chess engine descended from Cray Blitz, built around bitboard position representation and performance-oriented search. The executable lifecycle begins in src/main.c, initializes engine-wide state in src/init.c, and runs command/game control through src/crafty.c and src/input.c.
Core engine state and position transitions
Shared chess types, board state, and engine declarations are centralized in src/chess.h, src/data.h, and src/data.c. Position setup and legality boundaries are handled by src/setboard.c and src/validate.c. Search mutates positions through reversible move application in src/make.c and src/unmake.c; this is the critical state-integrity boundary.
Move generation and tactical primitives
Legal candidate generation is provided by src/movgen.c, backed by attack/bitboard computations in src/attacks.c and static-exchange evaluation in src/see.c. These components feed both full search and tactical extensions, while src/next.c controls move ordering/selection during node expansion.
Search pipeline
Root-level search orchestration is in src/root.c and iterative deepening in src/iterate.c. The main recursive alpha-beta-style search resides in src/search.c, with tactical leaf search in src/quiesce.c and pruning/reduction policy in src/reduce.c. Time and stop control form a cross-cutting control boundary through src/time.c and src/interrupt.c.
Evaluation, learning, and search memory
Static position scoring is implemented in src/evaluate.c with its interface in src/evaluate.h. Transposition caching is owned by src/hash.c; historical ordering and adaptive information are maintained in src/history.c and src/learn.c. Search probes can therefore use hash/history data before expanding moves, while evaluation supplies scores at leaves and selected search nodes.
External chess knowledge and execution environment
Opening-book support is in src/book.c, consuming repository opening data such as openings/new2500.epd and openings/4000_openings_legacy.epd. Endgame tablebase integration is represented by src/tbprobe.c, src/tbcore.c, and src/tbchess.c (Syzygy-derived code). EPD parsing/testing boundaries are in src/epd.c and src/epdglue.c.
Runtime, concurrency, and builds
The engine is a command-line native program using ANSI C plus limited Unix I/O; platform/build variants are provided by src/Makefile, src/Makefile-official, and src/makefile.win. Parallel search infrastructure is isolated in src/thread.c, while user-visible command output and analysis interaction are handled by src/output.c and src/analyze.c.
Main entry points: search.c Alpha-beta search · crafty.c Command/game control · root.c Root & iterative search · setboard.c Position setup & validation
Components 21
Runtime
- Executable entry
src/main.c - Engine initialization
src/init.c - Command/game control
src/crafty.c - Command input
src/input.c - Output & analysis
src/output.c
Position & moves
- Shared board state · Shared chess types, board state, and engine declarations.
src/data.c - Position setup & validation
src/setboard.c - Move generation
src/movgen.c - Attacks & SEE
src/attacks.c - Make/unmake moves · Critical position state-integrity boundary.
src/make.c
Search
- Root & iterative search
src/root.c - Alpha-beta search
src/search.c - Move ordering
src/next.c - Quiescence & reductions
src/quiesce.c - Time & interrupt control
src/time.c - Parallel search
src/thread.c
Knowledge & evaluation
- Static evaluation
src/evaluate.c - Hash & learning · Transposition cache plus historical and adaptive ordering data.
src/hash.c
External chess data
- Opening book
src/book.c - Syzygy tablebases
src/tbprobe.c - EPD interface
src/epd.c
Connections 24
- Executable entry → Engine initialization: starts
- Engine initialization → Shared board state: initializes
- Executable entry → Command/game control: runs
- Command/game control → Command input: reads commands
- Command/game control → Position setup & validation: sets positions
- Command/game control → Root & iterative search: requests search
- Command/game control → Output & analysis: reports
- Position setup & validation → Shared board state: validates and loads
- Root & iterative search → Alpha-beta search: iterative deepening
- Root & iterative search → Opening book: probes opening
- Root & iterative search → Time & interrupt control: sets limits
- Alpha-beta search → Move ordering: selects moves
- Move ordering → Move generation: draws candidates
- Move generation → Attacks & SEE: uses attacks
- Alpha-beta search → Make/unmake moves: applies and reverts
- Make/unmake moves → Shared board state: mutates
- Alpha-beta search → Quiescence & reductions: tactical leaves
- Quiescence & reductions → Static evaluation: scores leaves
- Alpha-beta search → Static evaluation: evaluates nodes
- Alpha-beta search → Hash & learning: probes and updates
- Alpha-beta search → Syzygy tablebases: probes endgames
- Time & interrupt control → Alpha-beta search: stops
- Parallel search → Alpha-beta search: parallelizes
- EPD interface → Position setup & validation: supplies positions