Created by Tyson Line and Rafael Robin (@Robin-in-in)
TysonChess is a fully functional chess engine implemented in Java. It is inspired by a high-performance C engine Sophia by @bartekspitza. It utilizes advanced techniques such as bitboards, magic bitboards, Zobrist hashing, and an alpha–beta search with move ordering and transposition tables. The engine supports the Universal Chess Interface (UCI) protocol, making it compatible with many chess GUIs.
- Overview
- Board Representation
- FEN Parsing and Board Initialization
- Move Generation
- Zobrist Hashing and Transposition Table
- Evaluation Function
- Search Algorithm: Alpha–Beta with Move Ordering
- SAN (Standard Algebraic Notation) Conversion
- UCI Protocol Interface
- Interactive UI
- Algorithms Summary
- How to Play
TysonChess is a chess engine written in Java. It leverages modern techniques for fast move generation and efficient search and communicates via the UCI protocol. Whether you use a graphical interface or a physical board, TysonChess can calculate moves for you.
- Concept:
The board is represented by 64-bit long integers (bitboards), with each bit corresponding to one square (0 = a1, …, 63 = h8). - Piece Representation:
There are 12 bitboards in total (one for each piece type: white pawns, white knights, …, black kings). - Occupancy Masks:
The engine maintains separate masks for white, black, and overall occupancy, allowing quick lookups and fast move generation.
- FEN (Forsyth–Edwards Notation):
FEN strings encode a complete chess position, including:- Piece placement (e.g.,
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR) - Side to move (
worb) - Castling rights (e.g.,
KQkq) - En passant target square (e.g.,
-ore3) - Halfmove clock and fullmove number
- Piece placement (e.g.,
- Initialization:
The engine uses the FEN string (stored asConstants.START_FENfor the standard starting position) to set up its internal board via theFen.javaclass.
- Pawns:
- Pushing: Single and double pawn pushes are generated by shifting the pawn bitboards.
- Attacks: Precomputed attack tables for pawns (east and west) are used.
- Promotion: Moves are generated for each promotion possibility when a pawn reaches the last rank.
- Knights and Kings:
Precomputed move masks (stored in arrays such asKNIGHT_MOVEMENTandKING_MOVEMENT) are used for fast move generation.
- Magic Bitboards:
- For bishops and rooks (and hence queens), the engine uses magic numbers and occupancy masks to quickly look up legal moves.
- Each square has a movement mask (computed via helper methods such as
bishopMovementandrookMovement) and a magic number (stored inMagics.java). - The product of the occupancy (restricted to the mask) and the magic number, shifted by a fixed amount, yields an index into a precomputed attack table.
- Initialization:
TheinitBishopRookAttackTables()method (with helper methodsbishopMovementandrookMovement) computes these tables.
- Castling:
Generated by checking castling rights and ensuring that squares between the king and rook are unoccupied. Castling moves are represented by the king’s move (e.g.,e1g1for white kingside). - En Passant:
When a pawn moves two squares, an en passant square is set. Special handling inpushMove(viamakeEnPassantMove) processes these moves. - Promotions:
When a pawn reaches the final rank, moves are generated for promotion to queen, rook, bishop, or knight.
- Zobrist Hashing:
Each board position is assigned a unique 64-bit hash computed by XORing precomputed random bitboards for each piece on each square, for en passant squares, for castling rights, and for the side to move (seeZobrist.java). - Transposition Table (TT):
A fixed-size TT caches evaluation results and best moves for positions (seeTT.javaandTTEntry.java), which is used during search to avoid redundant computations.
- Evaluation:
The engine evaluates a position by summing the values of all pieces and adds bonuses or penalties based on piece–square tables (seeEvaluation.java). - Piece-Square Tables:
Separate tables for white and mirrored versions for black provide positional scoring. - Special Cases:
Checkmate, stalemate, and insufficient material are handled to adjust the evaluation score.
- Alpha–Beta Search:
The engine uses a depth-first search with alpha–beta pruning to efficiently explore the game tree. - Move Ordering:
Moves are scored using MVV–LVA (Most Valuable Victim – Least Valuable Aggressor) heuristics and prioritized to improve pruning. - Transposition Table Integration:
The search routine first checks the TT. If a result exists for the current position (and with sufficient depth), the stored evaluation is used. - Implementation:
SeeSearch.javafor the recursive alpha–beta search and board copy routines.
- Conversion Functions:
TheSan.javaclass converts moves to and from coordinate notation (e.g.,e2e4,e7e8qfor promotions, ande1g1for castling).
- UCI Commands:
The engine supports commands such asisready,position,go,quit, anducivia the UCI protocol. - Communication:
TheUCI.javaclass listens for UCI commands, updates the internal board position accordingly, and initiates the search when prompted.
- Command-Line Play:
TheInteractiveUI.javaclass (if included) allows you to play a game interactively from the console by typing moves in coordinate notation. - Physical Board Use:
Use your physical board to follow along as the engine calculates its moves.
- Bitwise Operations:
Used extensively for fast board updates and move generation. - Magic Bitboards:
Provide efficient sliding piece move generation via precomputed masks and magic numbers. - Zobrist Hashing:
Quickly computes unique identifiers for board positions, essential for the transposition table. - Alpha–Beta Search:
A recursive search that prunes branches that will not affect the final decision. - Transposition Table:
Caches evaluations of positions to prevent redundant calculations. - Move Ordering (MVV–LVA):
Prioritizes captures and critical moves to improve search efficiency. - Evaluation Function:
Combines material values and positional bonuses for an overall score.
If you have a physical board, you can play interactively with the engine using the provided command-line UI:
- Run the engine (e.g., via
InteractiveUI.java). - Type your move in coordinate notation (e.g.,
e2e4). - The engine will calculate its move and output it (e.g.,
e7e5). - Update your physical board and repeat by entering your next move.
- To exit, type
quit.
Alternatively, load TysonChess in a UCI-compatible GUI to play against it.
TysonChess is a full-featured chess engine built using advanced techniques in move generation, evaluation, and search. It supports UCI for compatibility with graphical interfaces and also provides an interactive command-line UI for direct play. Enjoy playing with TysonChess!