Skip to main content

Chess Engine

Magic Chess implements a complete FIDE-standard chess engine that runs entirely on-chain. Every rule is enforced in the Solana Virtual Machine — not just move recording, but full validation, simulation, and state transition. The engine lives in programs/magic_chess/src/utils/chess_logic.rs (~680 lines) with the board state and type definitions in programs/magic_chess/src/state/.


FIDE Rules Coverage​

All 15 categories of chess rules are implemented and tested.

#CategoryStatusTestsNotes
1Pawn movementPASS12Single advance, double advance from starting rank, blocked paths
2Knight movementPASS3L-shaped jumps, ignores intervening pieces
3Bishop movementPASS3Diagonal slides, blocked by pieces
4Rook movementPASS3Horizontal/vertical slides, blocked by pieces
5Queen movementPASS2Combines rook + bishop movement
6King movementPASS4One square any direction, cannot move into check
7CastlingPASS7Kingside + queenside for both colors. Enforces all FIDE conditions: king and rook not moved, path clear, king not in check, king does not pass through or land in check, rook presence verified on starting square
8En passantPASS4Detection, validation, and board update (captured pawn removed from correct square)
9Pawn promotionPASS3Defaults to Queen. Validates promotion piece type and rank. All 4 piece types supported (Queen, Rook, Bishop, Knight)
10Check detectionPASS2Brute-force O(n) board scan for all attacker types (pawn attacks, knight jumps, sliding pieces along ranks/files/diagonals)
11Checkmate detectionPASS2are_no_legal_moves() returns true AND is_king_in_check() returns true
12Stalemate detectionPASS2are_no_legal_moves() returns true AND is_king_in_check() returns false
1350-move rulePASS1Automatic draw when halfmove_clock >= 100. Clock resets on any capture or pawn advance.
14Threefold repetitionPASS1Zobrist hashing (FNV-1a) with a 200-position ring buffer. Draw declared when any position hash appears 3 times.
15Board initializationPASS2Standard starting position with all 32 pieces in FIDE arrangement

Performance: Compute Unit Benchmarks​

Six operations were profiled using Mollusk to measure on-chain compute unit consumption. All values are from the CU benchmark suite (programs/magic_chess/tests/mollusk/cu_benchmarks.rs). Figures are approximate; rerun the suite for current numbers.

BenchmarkOperationCU
Pawn baselineSingle pawn move (e2-e4)~45,000
Knight moveKnight jump (g1-f3) with path validation~48,000
Bishop pathBishop diagonal move with path-blocking check~52,000
Queen diagonalQueen long diagonal with full slide validation~55,000
Midgame legal-moves scanare_no_legal_moves() on a midgame position (~30 legal moves)~80,000
Full init instructioninitialize_match with token transfer and PDA creation~120,000

The most expensive operation is the legal-move scan during checkmate/stalemate detection, which iterates over all pieces of the current player, generates all pseudo-legal moves for each, and simulates each to filter out moves that leave the king in check. This runs at most once per move (after the opponent's move, to detect game end).


Threefold Repetition Detection​

Threefold repetition is detected using Zobrist hashing — a standard technique in chess engines adapted for the SVM.

Zobrist Hashing​

A Zobrist hash is a 64-bit value that uniquely represents a chess position. It is incrementally updated with each move, making it extremely cheap to compute (~10 CU per update).

Hash function: The engine uses FNV-1a (Fowler-Noll-Vo) — a fast, non-cryptographic hash with good distribution for chess positions.

How it works:

  1. A table of random 64-bit numbers is generated for each combination of (piece_type, color, square) — 12 piece types x 64 squares = 768 values.
  2. Additional random numbers for: side to move, castling rights (4 bits), en passant file (8 possibilities).
  3. The initial board hash is computed by XORing all pieces on their starting squares plus the state flags.
  4. On each move, the hash is updated incrementally:
    • XOR out the moved piece from its source square
    • XOR in the moved piece on its destination square
    • XOR out any captured piece from its square
    • Toggle the side-to-move value
    • Update castling/en passant components as needed

Position History (Ring Buffer)​

The engine maintains a ring buffer of 200 positions on the ChessMatch account. Each entry is an 8-byte Zobrist hash.

position_history: [u64; 200] // Ring buffer of Zobrist hashes
position_history_index: u8 // Write pointer (wraps at 200)
position_history_len: u8 // Number of valid entries

After each move, the new position hash is written to the buffer. The engine scans the buffer for the current hash. If the same hash appears 3 times (including the current position), a threefold repetition draw is declared.

Why 200 positions?​

The maximum possible moves in a chess game under the 50-move rule is ~5,900 (50 moves x 118 pawn moves/captures). In practice, games rarely exceed 80 moves. A 200-position buffer covers any realistic game while keeping account size manageable (200 x 8 = 1,600 bytes).


Insufficient Material Detection​

Four draw-by-insufficient-material patterns are detected automatically after each move:

PatternDescriptionDetection
King vs KingK v KCount pieces: 2 kings, no other pieces
King + Bishop vs KingK+B v KCount pieces: 2 kings + 1 bishop
King + Knight vs KingK+N v KCount pieces: 2 kings + 1 knight
King + Bishop vs King + Bishop (same color)K+B v K+B, bishops on same color squaresCheck bishop square colors: (row + col) % 2 matches

In all four cases, checkmate is mathematically impossible with perfect play. The engine declares a draw when any of these patterns is detected, even if the 50-move rule has not been reached.


FEN Generation​

Forsyth-Edwards Notation (FEN) strings are generated for two purposes:

On-Chain (Events)​

The program emits FEN in MoveMadeEvent for off-chain indexers. This is generated on-chain to ensure cryptographic authenticity — the FEN is guaranteed to match the on-chain board state at the time the event was emitted.

Implementation approach: The board is serialized rank by rank (8 to 1), counting empty squares and concatenating piece characters. Castling availability, en passant target, halfmove clock, and fullmove number are appended.

Off-Chain (SDK)​

The TypeScript SDK provides boardToFen() and fenToBoard() utilities, exported from @magic-chess/sdk. These are used for display purposes (board rendering, PGN export, match sharing) where cryptographic authenticity is not required and zero CU cost is preferred.

FEN format:

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
|________| |________| |________| |_| |____| |_| |_|
| | | | | | |
Piece Active Castling EP Halfmove Fullmove
placement color rights target clock number

Board Representation​

The board is stored as an 8x8 array of optional pieces:

pub struct ChessMatch {
// ...
pub board: [[Option<Piece>; 8]; 8],
// ...
}

pub struct Piece {
pub piece_type: PieceType, // Pawn, Knight, Bishop, Rook, Queen, King
pub color: PlayerColor, // White, Black
}

Indexing convention: board[row][col] where row 0 = rank 8 (Black's home rank) and row 7 = rank 1 (White's home rank). Column 0 = a-file, column 7 = h-file.

Memory footprint: 8 x 8 x Option<Piece> = 64 x (1 + 1 + 1) = 192 bytes for the board. This is efficient for on-chain storage while providing O(1) random access for move validation.

Why not bitboards? Bitboards (64-bit integers) are the standard in high-performance chess engines but are less ergonomic in Rust/Anchor. The 8x8 array representation is simpler to audit, easier to debug, and within acceptable CU limits for the SVM. The most expensive operation (legal move scan) still runs comfortably under the Solana compute budget.


Testing Strategy​

The program has four Rust harnesses under magic-chess-program/programs/magic_chess/tests/, plus the Anchor TypeScript tests in magic-chess-program/tests/.

HarnessLocationTestsWhat it covers
Unitunit/ (chess_logic.rs, instructions.rs, magicblock.rs)182Every chess rule in isolation: piece movement, castling, en passant, promotion, check, checkmate, stalemate, 50-move rule, threefold repetition, instruction guards
LiteSVMlitesvm/59Full SVM flows with SPL tokens: match lifecycle, gameplay, session keys, timeouts, settlement, prediction pools, security checks
Payout flowintegration/payout_full_flow.rs15End-to-end payouts: winner, draw split, fee math, escrow drain, double-settlement rejection
Mollusk CUmollusk/cu_benchmarks.rs11Compute-unit budgets for moves and match setup

Run them with the commands in Local setup. CI runs all four Rust harnesses on every push and pull request.


Move Validation Pipeline​

Each make_move call runs through a deterministic validation pipeline:

1. State checks
├── Game status == Active?
├── Signer is the current player?
└── Current player hasn't timed out?

2. Basic move validation
├── Source square has a piece?
├── Piece belongs to the current player?
└── Destination is different from source?

3. Piece-specific validation (chess_logic.rs)
├── Pawn: direction, capture diagonal, double move from start, en passant
├── Knight: L-shape (|dr|=2 & |dc|=1) or (|dr|=1 & |dc|=2)
├── Bishop: diagonal (|dr| == |dc|), path clear
├── Rook: straight (dr==0 or dc==0), path clear
├── Queen: diagonal or straight, path clear
└── King: one square, or castling (all FIDE conditions)

4. Move simulation
├── Clone the board
├── Apply the move (including castling rook, en passant capture, promotion)
├── Check: own king in check after this move?
└── If yes → REJECT ("move leaves king in check")

5. Apply move permanently
├── Update board
├── Update castling rights (if king/rook moved or rook captured)
├── Set/clear en passant target
├── Update halfmove clock (reset on capture/pawn move, increment otherwise)
├── Increment fullmove number (after Black's move)
├── Switch turn

6. Post-move checks
├── Opponent has no legal moves?
│ ├── King in check → Checkmate (current player wins)
│ └── King not in check → Stalemate (draw)
├── halfmove_clock >= 100 → Draw (50-move rule)
├── Threefold repetition detected → Draw
└── Insufficient material → Draw

7. Emit events
├── MoveMadeEvent (always)
└── GameEndedEvent (if game ended at step 6)

This pipeline ensures that the on-chain state is always consistent — no invalid board position can ever be reached through any sequence of make_move calls.


Chess Engine File Structure​

programs/magic_chess/src/
├── state/
│ ├── piece.rs # Piece { piece_type: PieceType, color: PlayerColor }
│ ├── enums.rs # PieceType, PlayerColor, GameStatus, GameEndReason, MoveResult
│ ├── castling_rights.rs # CastlingRights { wk, wq, bk, bq }
│ ├── en_passant_square.rs # EnPassantSquare { row, col }
│ └── chess_match.rs # ChessMatch account struct (~31 fields)
├── utils/
│ ├── chess_logic.rs # ~680 lines — the complete chess engine
│ │ ├── validate_and_apply_move() # Main entry point for move processing
│ │ ├── validate_piece_move() # Piece-specific movement rules
│ │ ├── validate_castling() # Castling conditions (7 checks)
│ │ ├── validate_en_passant() # En passant capture validation
│ │ ├── is_king_in_check() # Brute-force attacker scan
│ │ ├── are_no_legal_moves() # Checkmate/stalemate detection
│ │ ├── is_threefold_repetition() # Zobrist hash ring buffer scan
│ │ ├── is_insufficient_material() # 4 endgame patterns
│ │ ├── update_zobrist_hash() # Incremental FNV-1a hash update
│ │ └── generate_fen() # FEN string serialization
│ └── payout_logic.rs # PDA-signed token transfers
└── errors/mod.rs # 40 error variants including chess-specific errors

Error Variants (Chess-Specific)​

ErrorDescription
NotYourTurnSigner is not the current player
NotYourPiecePiece at source square belongs to the opponent
InvalidMoveMove violates piece movement rules
MoveLeavesKingInCheckSimulated move results in own king being in check
InvalidCastlingCastling attempted but conditions not met
InvalidEnPassantEn passant attempted but target is stale or invalid
InvalidPromotionPromotion piece type or rank is invalid
PathBlockedA piece is blocking the movement path
GameNotActiveGame has ended or is still waiting for opponent
GameAlreadyEndedAttempted to move in a terminal game state
PlayerTimedOutThe moving player has exceeded move_timeout_duration
MoveLimitReached50-move rule triggered
ThreefoldRepetitionPosition repeated 3 times
InsufficientMaterialNeither side has enough material to checkmate