-
Notifications
You must be signed in to change notification settings - Fork 14
Engine
The chess engine implements move generation and position analysis using bitboards. The engine focuses on efficient move calculation through optimized bitboard operations and lookup tables.
The engine uses two primary methods for move generation:
-
standardMoves: Generates all regular piece moves -
castleMoves: Generates castling moves separately
func standardMoves(pos *Position, first bool) []MoveThe standard move generator:
- Uses a move pool to reduce allocations
- Generates moves for each piece type
- Handles promotions for pawns
- Validates moves against check
- Supports early exit with
firstflag for single move validation
Key optimizations:
- Pre-allocated move array pool (
maxPossibleMoves = 218) - Reuse of Move struct for temporary operations
- Bitboard operations for quick move validation
- Early filtering of invalid squares
func castleMoves(pos *Position) []MoveGenerates castling moves with validation for:
- Castle rights
- Empty squares between king and rook
- Squares not under attack
- King not in check
func isInCheck(pos *Position) boolDetermines if the current side to move is in check by:
- Locating the king
- Checking for attacks on the king square
func squaresAreAttacked(pos *Position, sqs ...Square) boolEfficiently determines if squares are under attack using:
- Directional attack patterns (diagonal, horizontal, vertical)
- Piece-specific move patterns
- Optimized bitboard operations
The engine validates moves through several steps:
- Basic move generation
- Check validation
- Pin detection
- Special move validation (castling, en passant)
The engine uses pre-calculated lookup tables for piece movements:
var (
bbFiles [8]bitboard // File masks
bbRanks [8]bitboard // Rank masks
bbDiagonals [64]bitboard // Diagonal masks
bbAntiDiagonals [64]bitboard // Anti-diagonal masks
bbKnightMoves [64]bitboard // Knight move patterns
bbBishopMoves [64]bitboard // Bishop move patterns
bbRookMoves [64]bitboard // Rook move patterns
bbQueenMoves [64]bitboard // Queen move patterns
bbKingMoves [64]bitboard // King move patterns
)func linearAttack(occupied, pos, mask bitboard) bitboardGenerates sliding piece attacks (rook, bishop, queen) using:
- Occupation patterns
- Position masks
- Direction masks
func pawnMoves(pos *Position, sq Square) bitboardHandles complex pawn move generation including:
- Single and double pushes
- Capture moves
- En passant captures
- Promotion validation
- Move Pool
var movePool = sync.Pool{
New: func() interface{} {
return &[maxPossibleMoves]Move{}
},
}- Reuses move arrays to reduce allocations
- Pre-sized for maximum possible moves
- Thread-safe implementation
The engine uses efficient bitboard operations for:
- Move Generation
- Piece movement patterns
- Attack detection
- Pin detection
- Position Analysis
- Square control
- King safety
- Piece mobility
- First Move Flag
- Quick validation of single moves
- Early return for move existence checks
- Attack Detection
- Quick checks before detailed analysis
- Efficient filtering of impossible attacks
func (engine) Status(pos *Position) MethodDetermines game status by analyzing:
- Check status
- Available moves
- Special conditions (stalemate, checkmate)