castro

API Documentation

Module Package

castro

src/castro.h
author

Konstantinos Despoinidis (KDesp73)

Line 61
license

MIT

Line 62
function

version

Line 86

Fills in the provided pointers with the current version numbers.

param major Pointer to an int to store the major version
param minor Pointer to an int to store the minor version
param patch Pointer to an int to store the patch version
Module Package

square

src/castro.h
type

Square

Line 114

Represents a square on the chessboard (0-63).

macro

SQUARE_PRINT

Line 118

Prints a square's name and index to stdout.

param square The square to print
example
e4 = E4 28
macro

COORDS

Line 131

Macro to convert a square index to (row, col) coordinates.

param square Square index in [0, 63]
example
board[COORDS(s)] = ...;` → expands to `board[(s)/8][(s)%8]
function

SquareToName

Line 139

Converts a square index (0-63) to algebraic notation (e.g. "e4").

param buffer [out] A 3-character buffer (e.g., `char name[3]`) to store the result.
param square [in] Square index from 0 to 63.
function

NameToSquare

Line 146

Converts algebraic notation (e.g. "e4") to a square index (0-63).

param buffer A null-terminated string containing the square name.
returns Square
function

Rank

Line 154

Returns the rank (0–7) of a square index.

param square The square as an index
returns int
function

File

Line 162

Returns the file (0–7) of a square index.

param square The square as an index
returns int
function

IsSquareValid

Line 170

Checks whether a square index is valid (0–63).

param square The square as an index
returns bool
function

SquareFromCoords

Line 178

Converts (rank, file) coordinates to a square index.

param y Rank (0 = rank 1, 7 = rank 8)
param x File (0 = file A, 7 = file H)
returns Square
function

SquareFromName

Line 187

Converts a square name (e.g. "d2") to an index.

param name A 2-character string like "e4".
returns Square
macro

SR

Line 194

Flips a square vertically (used for mirror board logic).

param s The square to flip
example
SR(E2) returns the square index of E7.
const

SQUARE_NONE

Line 284

Special marker for an invalid or uninitialized square

Module Package

bitboard

src/castro.h
type

Bitboard

Line 295

A 64-bit bitboard where each bit represents a square.

enum

Direction

Line 299

Cardinal and diagonal directions for sliding piece movement or bitboard shifting.

macro

BB

Line 312

Converts a square index to a bitboard with a single set bit.

param square The Square to covert into a bitboard
returns Bitboard
function

lsb

Line 320

Returns the index of the least significant bit set (LSB).

param b Input bitboard.
returns Square
function

msb

Line 328

Returns the index of the most significant bit set (MSB).

param b Input bitboard.
returns Square
function

shift

Line 335

Shifts a bitboard in a specified direction.

param b Input bitboard.
param D Direction to shift in.
returns Resulting
function

poplsb

Line 344

Pops and returns the index of the least significant bit set. The bit is cleared from the input bitboard.

param b Pointer to bitboard.
returns Square
function

popcount

Line 352

Counts the number of bits set in the bitboard.

param bb Input bitboard.
returns Number
function

on

Line 360

Sets the bit corresponding to the square in the bitboard.

param bitboard Pointer to the bitboard
param square The square we want to turn on
function

off

Line 367

Clears the bit corresponding to the square in the bitboard.

param bitboard Pointer to the bitboard
param square The square we want to turn off
Module Package

pseudo

src/castro.h
function

PawnAttacks

Line 379

Computes pseudo-legal pawn attacks.

param pawn Square of pawn.
param enemySquares Bitboard of enemy pieces.
param color 0 = white, 1 = black.
returns The
function

PawnPushes

Line 389

Computes pseudo-legal pawn forward pushes.

param pawn Square of pawn.
param emptySquares Bitboard of empty squares.
param color 0 = white, 1 = black.
returns The
function

PawnPromotions

Line 399

Computes pawn promotions (non-capturing).

param pawns Bitboard of pawns eligible to promote.
param emptySquares Bitboard of empty target squares.
param color 0 = white, 1 = black.
returns The
function

PawnPromotionCaptures

Line 409

Computes pawn promotion captures.

param pawns Bitboard of pawns eligible to promote by capture.
param opponentPieces Bitboard of capturable opponent pieces.
param color 0 = white, 1 = black.
returns The
function

KnightAttacks

Line 419

Computes knight attacks from a given square.

param knights Bitboard of knights
param emptySquares Bitboard with the empty squares on the board
param enemySquares Bitboard containing the enemy pieces positions
returns The
function

KingAttacks

Line 429

Computes king attacks from a given square.

param king Bitboard containing the king's position
param emptySquares Bitboard with the empty squares on the board
param enemySquares Bitboard containing the enemy pieces positions
returns The
function

BishopAttacksFromOccupancy

Line 439

Computes bishop attacks based on an occupancy bitboard

param square The square to calculate from
param occupancy The occupancy bitboard
returns The
function

RookAttacksFromOccupancy

Line 448

Computes rook attacks based on an occupancy bitboard

param square The square to calculate from
param occupancy The occupancy bitboard
returns The
function

BishopAttacks

Line 457

Computes bishop attacks using a sliding attack method.

param bishops The bishop's position
param emptySquares Bitboard with the empty squares on the board
param enemySquares Bitboard containing the enemy pieces positions
returns The
function

RookAttacks

Line 467

Computes rook attacks using a sliding attack method.

param rook The rook's position
param emptySquares Bitboard with the empty squares on the board
param enemySquares Bitboard containing the enemy pieces positions
returns The
function

QueenAttacks

Line 477

Computes queen attacks as the union of rook and bishop attacks.

param queen The queen's position
param emptySquares Bitboard with the empty squares on the board
param enemySquares Bitboard containing the enemy pieces positions
returns The
function

InitMagic

Line 487

This function initializes the magic tables

function

BishopAttacksMagic

Line 491
function

RookAttacksMagic

Line 495
Module Package

kingsafety

src/castro.h
function

IsKingInCheck

Line 502

Checks whether the king is in check.

param kingPosition Bitboard with one bit set where the king is.
param enemyAttacks Bitboard of all enemy attacks.
returns bool
Module Package

hashing

src/castro.h
type

HashEntry

Line 520

Represents a single hash entry (position and repetition count).

const

HASH_TABLE_CAPACITY

Line 527

Capacity of the repetition hash table (power of two for fast modulo).

type

HashTable

Line 531

Tracks position repetition using Zobrist hashes. Uses open addressing (linear probing). Empty buckets have hash == 0.

function

InitHashTable

Line 540

Initializes a hash table from a FEN string. Parses the FEN, computes the initial Zobrist hash, and sets up the table.

param table Pointer to an uninitialized HashTable
param starting_fen FEN string of the initial position
function

InitHashTableHash

Line 548

Initializes a hash table directly from a known Zobrist hash.

param table Pointer to HashTable
param starting_hash Precomputed Zobrist hash of the position
function

UpdateHashTable

Line 555

Adds a new position hash or updates an existing entry. If the hash already exists, increments the count.

param table Pointer to HashTable
param hash New Zobrist hash to insert
returns true
function

HashTableDecrement

Line 565

Decrements the repetition count for a position (used on unmake). Call with the hash that was last added before the move being undone.

param table Pointer to HashTable
param hash The hash of the position to decrement its repetition count
function

FreeHashTable

Line 573

Frees all memory used by the hash table.

Module Package

history

src/castro.h
const

MAX_MOVES

Line 584

Maximum number of moves stored in history

type

Undo

Line 588

Stores the necessary data to undo a move.

const

NULL_UNDO

Line 598

Null undo object representing no previous move

function

UndoPrint

Line 602

Prints the contents of an Undo struct (for debugging).

param undo The undo struct to print
type

History

Line 608

Stores full game history for repetition detection and undo functionality.

function

HistoryRemove

Line 616

Removes the last move from history (pop operation). Updates position table and count.

param history Pointer to a history struct
function

HistoryGetLast

Line 623

Returns the most recent Undo record from history.

param history Pointer to a history struct
returns Undo
Module Package

board

src/castro.h
enum

PieceType

Line 653

Piece type used for indexing and logic

const

EMPTY_SQUARE

Line 664

Used to indicate no piece on a square

const

STARTING_FEN

Line 668

Standard starting position in Forsyth-Edwards Notation (FEN)

const

BOARD_SIZE

Line 672

The board dimensions

const

PIECE_TYPES

Line 676

The number of different pieces

type

Board

Line 686

Core board structure combining bitboards and grid for performance and simplicity.

function

AddUndo

Line 706

Records an undo step into the board's history.

param board The board to update
param move The move to be undone later
returns true
function

LoadLastUndo

Line 715

Loads and removes the last undo record.

param board The board to restore
returns Undo
const

PIECES

Line 723

All 12 supported pieces, as characters

enum

PieceColor

Line 751

Color of pieces

function

PromotionToChar

Line 759

Converts a promotion code to its corresponding character.

param promotion Numeric code (0 = queen, 1 = rook, etc.)
returns Promotion
function

CharToPromotion

Line 767

Converts a promotion piece character to a numeric code.

param promotion Piece character (e.g., 'q', 'n')
returns Numeric
function

BoardInitFen

Line 775

Initializes a board from a FEN string. Must be freed with BoardFree().

param board Pointer to the board
param fen The FEN string
function

BoardInitFenHeap

Line 783

Heap-allocates and initializes a board from FEN. Must be freed with BoardFree().

param fen The FEN string
returns Board*
function

BoardFree

Line 792

Frees heap-allocated board (from BoardInitFenHeap).

param board The pointer to the board
function

BoardUpdateOccupancy

Line 798

Recomputes and stores white, black, empty from bitboards. Call after any direct bitboard change.

param board The pointer to the board
function

GetWhite

Line 805

Returns a bitboard of all white pieces.

param board Pointer to the board
returns Bitboard
function

GetBlack

Line 813

Returns a bitboard of all black pieces.

param board Pointer to the board
returns Bitboard
function

GetEnemyColor

Line 821

Returns a bitboard of all opponent pieces.

param board Pointer to the board
param us The color of the active player
returns Bitboard
function

GetEnemy

Line 830

Returns a bitboard of all enemy pieces (based on current turn).

param board Pointer to the board
returns Bitboard
function

GetEmpty

Line 838

Returns a bitboard of all empty squares.

param board Pointer to the board
returns Bitboard
function

CountPieces

Line 846

Counts the number of a specific piece color/type on the board.

param board Pointer to the board
param color The color of the pieces to count
param type The type of piece to count
function

HasCastlingRights

Line 856

Checks if a board has certain castling rights.

param board Pointer to the board
param castling_rights The byte representing the castling rights
returns int
function

RevokeCastlingRights

Line 865

Revokes specific castling rights from a board.

param board Pointer to the board
param castling_rights The byte contining the castle rights to revoke
function

IsSquareAttacked

Line 872

Checks if a square is attacked by a given color.

param board Pointer to the board
param square The square we want to check
param color The color of the side that might attack the square
returns bool
function

IsSquareEmpty

Line 882

Checks if a square is empty.

param board Pointer to the board
param square The square we want to check
returns bool
function

castro_IsSquareOccupiedBy

Line 891

Checks if a square is occupied by a given color.

param board Pointer to the board
param square The square we want to check
param color The color to check for
returns bool
function

castro_NumberOfPieces

Line 901

Returns the number of pieces on the board for a given color.

param board Pointer to the board
param color The color to count
returns size_t
function

castro_IsInCheckColor

Line 910

Checks if a specific color is in check.

param board Pointer to the board
param color The color to check
returns bool
function

castro_IsInCheck

Line 919

Checks if the player currently to move is in check.

param board Pointer to the board
returns bool
function

castro_BoardCopy

Line 928

Returns a deep copy of the board.

param board Pointer to the source board
returns Board
Module Package

printing

src/castro.h
function

Uint32Print

Line 939

Prints a 32-bit unsigned integer (e.g. in binary or hex).

param value The uint32_t number
function

Uint64Print

Line 944

Prints a 64-bit unsigned integer (e.g. in binary or hex).

param value The uint64_t number
function

BitboardPrint

Line 949

Prints a visual representation of a bitboard. Useful for debugging. Marks set bits on an 8x8 grid

param bitboard The bitboard to print
function

castro_BoardPrintSquares

Line 955

Prints a list of squares (e.g. legal moves) on the board.

param board Pointer to the board
param squares Array of squares to print
param count Number of squares in the array
function

castro_BoardPrintBitboard

Line 963

Highlights a bitboard on the board (used for debugging).

param board Pointer to the board
param highlight The bitboard to overlay/highlight
function

castro_BoardPrint

Line 970

Prints the board with a list of highlighted squares.

param board Pointer to the board
param first The first square to highlight (followed by variadic list)
function

castro_BoardPrintBitboards

Line 977

Prints all bitboards in the board structure (for debugging).

param board The board structure
function

castro_BoardPrintGrid

Line 983

Prints the character grid of the board.

param board Pointer to the board
Module Package

zobrist

src/castro.h
const

CASTLING_OPTIONS

Line 996

Total number of castling rights encoded (K, Q, k, q)

var

zobrist_table

Line 1000

Zobrist random numbers for each piece on each square. Dimensions: - PIECE_TYPES: 12 (black/white * 6 types) - BOARD_SIZE: 8x8 squares Indexed as [piece][rank][file]

var

zobrist_castling

Line 1008

Zobrist keys for each of the 4 castling rights (K, Q, k, q)

var

zobrist_en_passant

Line 1012

Zobrist keys for each en passant file (a-h)

var

zobrist_black_to_move

Line 1016

Zobrist key to represent "black to move"

var

InitZobrist

Line 1020

Initializes the Zobrist tables.

function

CalculateZobristHash

Line 1024

Calculates the Zobrist hash of a board. This includes: - Pieces on the board - Side to move - Castling rights - En passant square

param board Pointer to the Board structure
returns 64-bit
function

CalculateZobristHashFen

Line 1037

Convenience function to calculate a Zobrist hash directly from a FEN string.

param fen Forsyth-Edwards Notation string
returns 64-bit
function

ZobristPieceToIndex

Line 1045

Translates piece as a character to expected zobrist index

param piece The piece as a character (example: white queen -> 'Q')
returns int
Module Package

masks

src/castro.h
type

Bitboard

Line 1055

A 64-bit unsigned integer representing the squares of the chessboard. Each bit corresponds to a square (0-63), where a set bit (1) indicates the presence of a piece or a targeted square, and a cleared bit (0) indicates its absence. This allows for high-performance board manipulation using bitwise operators.

function

InitMasks

Line 1114

Initializes all masks

function

GeneralOccupancy

Line 1118

Computes the general occupancy into a bitboard

param whitePieces The bitboard containing the positions of the white pieces
param blackPieces The bitboard containing the positions of the black pieces
returns Bitboard
function

BlockerMasks

Line 1127

Calculates the blocker mask for a sliding piece

param slidingPiece The bitboard with the sliding piece's square bit turned on
param occupancy The general occupancy bitboard
returns Bitboard
function

ComputeDiagonalMask

Line 1138

Generates a mask for the diagonal passing through the given square

param square The target square index
returns Bitboard
function

ComputeAntiDiagonalMask

Line 1144

Generates a mask for the anti-diagonal passing through the given square

param square The target square index
returns Bitboard
function

ComputeHorizontalMask

Line 1150

Generates a mask for the rank (row) of the given square

param square The target square index
returns Bitboard
function

ComputeVerticalMask

Line 1156

Generates a mask for the file (column) of the given square

param square The target square index
returns Bitboard
function

DiagonalMask

Line 1162

Retrieves the pre-computed diagonal mask for a square

param square The target square index
returns Bitboard
function

AntiDiagonalMask

Line 1168

Retrieves the pre-computed anti-diagonal mask for a square

param square The target square index
returns Bitboard
function

HorizontalMask

Line 1174

Retrieves the pre-computed horizontal mask for a square

param square The target square index
returns Bitboard
function

VerticalMask

Line 1180

Retrieves the pre-computed vertical mask for a square

param square The target square index
returns Bitboard
function

ComputePawnPushMask

Line 1188

Calculates the single-square forward push for a pawn

param square The starting square of the pawn
param color The color of the pawn (determines direction)
returns Bitboard
function

ComputePawnDoublePushMask

Line 1195

Calculates the two-square initial push for a pawn

param square The starting square of the pawn
param color The color of the pawn
returns Bitboard
function

ComputePawnPromotionMask

Line 1202

Generates a mask for squares where a pawn push results in promotion

param square The starting square of the pawn
param color The color of the pawn
returns Bitboard
function

ComputePawnPromotionAttackMask

Line 1209

Generates a mask for diagonal attacks that result in a promotion

param square The starting square of the pawn
param color The color of the pawn
returns Bitboard
function

ComputePawnAttackMask

Line 1216

Calculates standard diagonal attack squares for a pawn

param square The starting square of the pawn
param color The color of the pawn
returns Bitboard
function

ComputeKnightMoveMask

Line 1223

Generates all possible L-shaped jumps for a knight on a given square

param square The starting square of the knight
returns Bitboard
function

ComputeBishopMoveMask

Line 1229

Calculates all diagonal sliding moves for a bishop (ignoring blockers)

param square The starting square of the bishop
returns Bitboard
function

ComputeRookMoveMask

Line 1235

Calculates all horizontal and vertical sliding moves for a rook (ignoring blockers)

param square The starting square of the rook
returns Bitboard
function

ComputeQueenMoveMask

Line 1241

Calculates the union of rook and bishop moves for a queen

param square The starting square of the queen
returns Bitboard
function

ComputeKingMoveMask

Line 1247

Generates a mask for all adjacent squares reachable by a king

param square The starting square of the king
returns Bitboard
function

PawnPushMask

Line 1253

Retrieves pre-computed single push mask

param square starting square
param color piece color
returns Bitboard
function

PawnDoublePushMask

Line 1260

Retrieves pre-computed double push mask

param square starting square
param color piece color
returns Bitboard
function

PawnPromotionMask

Line 1267

Retrieves pre-computed promotion mask

param square starting square
param color piece color
returns Bitboard
function

PawnPromotionAttackMask

Line 1274

Retrieves pre-computed promotion attack mask

param square starting square
param color piece color
returns Bitboard
function

PawnAttackMask

Line 1281

Retrieves pre-computed pawn attack mask

param square starting square
param color piece color
returns Bitboard
function

KnightMoveMask

Line 1288

Retrieves pre-computed knight move mask

param square starting square
returns Bitboard
function

BishopMoveMask

Line 1294

Retrieves pre-computed bishop move mask

param square starting square
returns Bitboard
function

RookMoveMask

Line 1300

Retrieves pre-computed rook move mask

param square starting square
returns Bitboard
function

QueenMoveMask

Line 1306

Retrieves pre-computed queen move mask

param square starting square
returns Bitboard
function

KingMoveMask

Line 1312

Retrieves pre-computed king move mask

param square starting square
returns Bitboard
Module Package

move

src/castro.h
enum

Flag

Line 1331

Flags representing special move types.

enum

Promotion

Line 1342

Types of piece promotions.

enum

Castling bytes

Line 1352

Bit flags representing castling rights.

type

NullMoveState

Line 1361

Used to store minimal board state when making a null move.

var

nullState

Line 1370

Stores the state of the board before a null move is made

type

Move

Line 1374

Encoded move type (bitfield). Format: - bits 0–5: from square - bits 6–11: to square - bits 12–14: promotion type - bits 15–17: move flag

const

NULL_MOVE

Line 1382

Special constant representing no move

const

MOVES_CAPACITY

Line 1386

Max number of moves in a move list

type

Moves

Line 1390

Represents a dynamic list of moves.

const

NO_MOVES

Line 1397

Empty move list constant

function

MovesAppend

Line 1401

Appends a move to a move list.

param moves Pointer to the Moves list structure.
param move The specific move to add to the list.
function

MovesAppendList

Line 1407

Appends one move list to another.

param dest Pointer to the destination Moves list.
param src The source Moves list to copy from.
function

MovesCombine

Line 1413

Combines two move lists into a new one.

param m1 The first move list.
param m2 The second move list.
returns Moves
function

MakeUndo

Line 1420

Creates an Undo struct representing a move played on a board.

param board Pointer to the current board state.
param move The move intended to be played.
returns Undo
macro

MOVE_DECODE

Line 1451

Decodes a move into its components (used inside a scope).

function

MoveIsValid

Line 1462

Checks whether a move is legal and does not leave the king in check.

param board Pointer to the constant board state.
param move The move to validate.
param color The color of the player making the move.
returns _Bool
function

MoveIsCapture

Line 1470

Returns true if the move captures a piece (or en passant).

param board Pointer to the constant board state.
param move The move to check.
returns _Bool
function

MoveGivesCheck

Line 1477

Returns true if the move gives check. Temporarily modifies the board (make/unmake).

param board Pointer to the board state (will be modified and restored).
param move The move to test.
returns _Bool
function

PieceValueFromType

Line 1484

Piece value for MVV-LVA (pawn=1, knight/bishop=3, rook=5, queen=9, king=0).

param piece_type The character representation of the piece.
returns int
function

OrderLegalMoves

Line 1490

Reorders a legal move list for search: hash move first, then captures (MVV-LVA), killers, then checks, then quiet moves.

param board Pointer to the current board.
param moves Pointer to the list of moves to be sorted in place.
param hash_move The move retrieved from the transposition table (use NULL_MOVE if none).
param killer0 The first killer move for the current ply.
param killer1 The second killer move for the current ply.
param score_checks Boolean to determine if check detection should be part of the ordering.
function

MoveEncode

Line 1500

Encodes a move from components into a 32-bit integer.

param from The starting square index.
param to The destination square index.
param promotion The piece type to promote to (if applicable).
param flag Metadata flags (capture, double push, etc.).
returns Move
function

MoveEncodeNames

Line 1509

Encodes a move from algebraic names ("e2", "e4", etc.).

param from String representing the starting square.
param to String representing the destination square.
param promotion Promotion type index.
param flag Move flag index.
returns Move
function

MoveDecode

Line 1518

Decodes a move into from-square, to-square, promotion, and flag.

param move The encoded 32-bit move.
param from Pointer to store the extracted starting square.
param to Pointer to store the extracted destination square.
param promotion Pointer to store the extracted promotion type.
param flag Pointer to store the extracted flag.
function

MoveSetFlag

Line 1527

Sets the move flag field in an existing move.

param move Pointer to the move to modify.
param flag The flag value to set.
function

MoveSetPromotion

Line 1533

Sets the promotion field in an existing move.

param move Pointer to the move to modify.
param promotion The promotion piece type.
function

DoMove

Line 1543

Applies a move on a bitboard by updating the piece positions.

param current Pointer to the specific bitboard (e.g., WhitePawns) to be updated.
param move The encoded move containing source and destination squares.
returns Bitboard
function

UndoMove

Line 1550

Undoes a move on a bitboard, reverting the piece to its previous position.

param current Pointer to the bitboard to be reverted.
param move The encoded move that was previously applied.
returns Bitboard
function

MakeMove

Line 1561

Makes a move and updates board state accordingly. Handles piece movement, captures, and state updates (castling rights, en passant, etc.).

param board Pointer to the board state to modify.
param move The encoded move to execute.
returns bool
function

UnmakeMove

Line 1569

Unmakes the last move and restores previous board state using stored history.

param board Pointer to the board state to restore.
function

MakeNullMove

Line 1574

Performs a null move (swapping sides without moving a piece). Used primarily in search algorithms like Null Move Pruning.

param board Pointer to the board state to modify.
function

UnmakeNullMove

Line 1580

Reverts a null move and restores the original side to move and state.

param board Pointer to the board state to restore.
function

Castle

Line 1589

Executes a castling move, updating both the king and the rook positions on the bitboards.

param board Pointer to the board state to modify.
param move The encoded castling move.
returns bool
function

IsCastle

Line 1596

Checks if a move is a castling move based on piece type and move flags.

param board Pointer to the current board state.
param move Pointer to the move to check.
returns bool
function

Enpassant

Line 1603

Executes an en passant capture, removing the opponent's pawn and moving the current pawn.

param board Pointer to the board state.
param move The encoded en passant move.
returns bool
function

IsEnpassant

Line 1610

Checks if a move is an en passant capture by checking the target square and pawn flags.

param board Pointer to the board state.
param move Pointer to the move to check.
returns bool
function

IsDoublePawnPush

Line 1617

Checks if a move is a two-square pawn advance from the starting rank.

param board Pointer to the board state.
param move The move to check.
returns bool
function

IsPromotion

Line 1624

Checks if a move is a pawn promotion.

param board Pointer to the board state.
param move Pointer to the move to check.
returns bool
function

IsCapture

Line 1631

Checks if a move is a capture (regular or en passant).

param board Pointer to the board state.
param move The move to check.
returns bool
function

IsInCheckAfterMove

Line 1638

Simulates a move to check if it leaves the friendly king in check (illegal move detection).

param board Pointer to the board state.
param move The move to simulate.
returns bool
function

MoveMake

Line 1645

Makes a move with full legality rules, ensuring the move is valid and updates all board state.

param board Pointer to the board state.
param move The move to execute.
returns _Bool
function

MoveFreely

Line 1652

Applies a move directly to the bitboards, ignoring turn-order or check legality.

param board Pointer to the board state.
param move The move to apply.
param color The color of the piece being moved.
function

MovePrint

Line 1659

Prints a move to stdout in algebraic format (e.g., "e2e4").

param move The move to print.
function

StringToMove

Line 1664

Converts an algebraic string (e.g., "e2e4") to an encoded 32-bit Move integer.

param str The source algebraic string.
returns Move
function

MoveToString

Line 1670

Converts a move to a string in algebraic format (e.g., "e2e4").

param move The move to convert.
param buffer Pointer to the character buffer to store the result.
function

MoveCmp

Line 1680

Compares two moves for equality by checking only the source and destination squares, ignoring internal metadata like flags or search scores.

param m1 The first move to compare.
param m2 The second move to compare.
returns bool
function

MoveCmpStrict

Line 1687

Compares two moves strictly, ensuring that the squares, promotion piece, and all metadata flags are identical.

param m1 The first move to compare.
param m2 The second move to compare.
returns bool
function

GetFrom

Line 1698

Extracts and returns the source (starting) square index from an encoded move.

param move The encoded 32-bit move.
returns Square
function

GetTo

Line 1704

Extracts and returns the destination (target) square index from an encoded move.

param move The encoded 32-bit move.
returns Square
function

GetPromotion

Line 1710

Retrieves the promotion piece type from the move bitfield.

param move The encoded 32-bit move.
returns uint8_t
function

GetFlag

Line 1716

Extracts the metadata flag associated with the move (e.g., double pawn push, en passant, or castling).

param move The encoded 32-bit move.
returns uint8_t
function

UpdateHalfmove

Line 1726

Updates the 50-move counter (halfmove clock). The counter is reset if a pawn is moved or a capture occurs; otherwise, it is incremented.

param board Pointer to the board state to update.
param move The move being executed.
param piece_count_before Total piece count on the board before the move.
param piece_count_after Total piece count on the board after the move.
param piece The character type of the piece that was moved.
function

UpdateCastlingRights

Line 1735

Updates the castling rights bitmask after a move. Rights are lost if the king or a rook moves, or if a rook is captured on its starting square.

param board Pointer to the current board.
param move The move that may affect castling rights.
returns uint8_t
function

UpdateEnpassantSquare

Line 1742

Updates the en passant target square. This is set if a pawn makes a double-square push, otherwise it is cleared.

param board Pointer to the current board.
param move The move being executed.
returns Square
function

BitboardToMoves

Line 1753

Iterates through all set bits in a destination bitboard and appends a corresponding move to a Moves list, using the provided source square.

param bitboard The bitboard representing valid target squares.
param from The starting square shared by all generated moves.
returns Moves
function

MovesToBitboard

Line 1760

Aggregates the destination squares of all moves in a list into a single bitboard representation.

param moves The list of moves to process.
returns Bitboard
function

BoardPrintMove

Line 1770

Prints a move on a board (highlighted view).

param board Pointer to the current board
param move The move to highlight on the board
macro

MOVE_PRINT

Line 1776

Debug macro alias for MovePrint

Module Package

piece

src/castro.h
type

Piece

Line 1787

Represents a chess piece. Each piece is defined by a type (character) and a color. - Uppercase letters: white pieces (e.g., 'P', 'N') - Lowercase letters: black pieces (e.g., 'p', 'n')

macro

PIECE_PRINT

Line 1797

Prints the contents of a Piece struct (for debugging).

macro

IS_PAWN

Line 1806

Checks if a piece is a pawn.

macro

IS_KNIGHT

Line 1811

Checks if a piece is a knight.

macro

IS_BISHOP

Line 1816

Checks if a piece is a bishop.

macro

IS_ROOK

Line 1821

Checks if a piece is a rook.

macro

IS_QUEEN

Line 1826

Checks if a piece is a queen.

macro

IS_KING

Line 1831

Checks if a piece is a king.

macro

IS_COLOR

Line 1836

Checks if a piece has a given color.

macro

IS_WHITE

Line 1841

Checks if a piece is white.

macro

IS_BLACK

Line 1846

Checks if a piece is black.

function

GetPieceColor

Line 1855

Gets the color of a piece given its character representation (e.g., 'P' for white, 'p' for black).

param piece The character representing a piece.
returns int
function

PieceAt

Line 1861

Returns the Piece located at a specific square on the board by inspecting the active bitboards.

param board Pointer to the board state.
param square Square index (0–63).
returns Piece
function

PieceCmp

Line 1868

Compares two Piece structs for type and color equality.

param p1 First piece to compare.
param p2 Second piece to compare.
returns bool
Module Package

notation

src/castro.h
function

FenImport

Line 1885

Imports a FEN string into a board. Sets up position, castling rights, side to move, en passant, etc.

param board Pointer to the board to initialize
param fen FEN string
function

FenExport

Line 1893

Exports the current board state to a FEN string.

param board Board to serialize
param fen Output buffer (must be large enough)
const

MAX_HEADER_LENGTH

Line 1900

Maximum length for PGN header fields (Event, Site, etc.)

type

SanMove

Line 1908

Represents a move in Standard Algebraic Notation (e.g., "e4", "Nf3").

type

Game

Line 1914

PGN/notation-based game format. Stores metadata and the list of SAN moves.

function

move_name

Line 1929

Parses a SAN move and applies it to the board and game state.

param board Current board
param game Game context
param move_str SAN move string (e.g., "e4", "O-O")
macro

MOVE

Line 1939

Shorthand for move_name(board, game, move)

function

GameInit

Line 1948

Initializes a Game object with basic metadata and a starting FEN string.

param game Pointer to the Game structure to initialize.
param event The name of the tournament or event.
param site The location of the game.
param white The name of the player playing white.
param black The name of the player playing black.
param fen The starting position in Forsyth-Edwards Notation.
function

GameRun

Line 1958

Runs a game move-by-move, showing each updated board. Useful for debugging or terminal-based visualization.

param game The game object to execute.
function

GamePrint

Line 1963

Prints the full PGN representation of a game, including metadata tags and the formatted move list.

param game The game object to print.
function

GameAddMove

Line 1968

Appends a move in Standard Algebraic Notation (SAN) to the game's move history.

param game Pointer to the Game structure.
param move The SanMove object to append.
function

GameSetEvent

Line 1974

Setters for PGN metadata: Sets the event field.

param game Pointer to the Game structure.
param event The event string.
function

GameSetSite

Line 1980

Setters for PGN metadata: Sets the site field.

param game Pointer to the Game structure.
param site The site string.
function

GameSetDate

Line 1986

Setters for PGN metadata: Sets the date field.

param game Pointer to the Game structure.
param date The date string (typically YYYY.MM.DD).
function

GameSetWhite

Line 1992

Setters for PGN metadata: Sets the white player's name.

param game Pointer to the Game structure.
param white The player's name.
function

GameSetBlack

Line 1998

Setters for PGN metadata: Sets the black player's name.

param game Pointer to the Game structure.
param black The player's name.
function

GameSetFen

Line 2004

Setters for PGN metadata: Sets the starting FEN position.

param game Pointer to the Game structure.
param fen The FEN string.
function

GameSetResult

Line 2010

Setters for PGN metadata: Sets the game result (e.g., "1-0", "0-1", "1/2-1/2").

param game Pointer to the Game structure.
param result The result string.
function

PgnImport

Line 2020

Parses a standard PGN string and populates the Game object with its metadata and move history.

param game Pointer to the Game structure to be populated.
param pgn The source string containing the PGN data.
function

PgnExport

Line 2026

Serializes the current Game state into a PGN formatted string.

param game Pointer to the Game structure to serialize.
param pgn Buffer to store the resulting PGN string.
function

PgnExportFile

Line 2032

Serializes the Game to PGN format and writes it directly to a specified file path.

param game Pointer to the Game structure to save.
param path The filesystem path where the PGN file will be created or overwritten.
function

Notate

Line 2042

Converts a Move to a SAN notation string (e.g., "Nf3", "O-O"). This requires the board state to determine move ambiguity (e.g., which knight moved) and whether the move results in check or checkmate.

param board Pointer to the current board state.
param move The internal encoded move to be converted.
param san Pointer to the SanMove structure where the resulting string will be stored.
function

SanToMove

Line 2049

Converts a SAN move string back into an internal 32-bit Move. It validates the notation against the current board to find the correct source square and piece type.

param board Pointer to the current board state.
param san The SanMove structure containing the notation string.
returns Move
Module Package

result

src/castro.h
enum

Result

Line 2063

Enumeration of possible game outcomes.

const

result_score

Line 2076

String representations of results for PGN output. Matches the Result enum index: - "*" for ongoing - "1-0", "0-1" for wins - "1/2-1/2" for all draws

const

result_message

Line 2092

Human-readable messages describing the result. Matches the Result enum index.

function

IsResult

Line 2109

Determines the current result of the game. Checks for checkmate, stalemate, 3-fold repetition, 50-move rule, or insufficient material.

param board Current board state
function

IsCheckmate

Line 2118

Determines if the current position is checkmate.

param board Pointer to the board
function

IsStalemate

Line 2125

Determines if the current position is stalemate.

param board Pointer to the board
function

IsInsufficientMaterial

Line 2132

Checks if neither player has sufficient material to checkmate.

param board Pointer to the board
function

IsThreefoldRepetition

Line 2139

Determines if the current position has occurred three times (3-fold repetition). Uses the board's `History` and `HashTable` to detect repeated positions.

param board Pointer to the board
Module Package

movegen

src/castro.h
enum

MoveType

Line 2154

Enumeration of move types to control legality enforcement.

function

GeneratePseudoLegalMoves

Line 2167

Generates all pseudo-legal moves for the current position. Includes moves that may leave the king in check.

param board Pointer to the constant board state.
returns Moves
function

GeneratePseudoLegalMovesBitboard

Line 2173

Generates a bitboard representing all pseudo-legal destination squares for all pieces of the side to move.

param board Pointer to the constant board state.
returns Bitboard
function

GeneratePseudoLegalPawnMoves

Line 2179

Pseudo-legal push moves for pawns, including single and double pushes.

param pawns Bitboard of the pawns to move.
param enemy Bitboard of all enemy pieces.
param color The color of the pawns.
returns Bitboard
function

GeneratePseudoLegalPawnAttacks

Line 2187

Pseudo-legal pawn attacks. If strict is true, diagonal movement is restricted to squares with capturable enemies.

param pawns Bitboard of the attacking pawns.
param enemy Bitboard of all enemy pieces.
param color The color of the pawns.
param strict Boolean to enforce capture-only diagonal movement.
returns Bitboard
function

GeneratePseudoLegalKnightAttacks

Line 2196

Pseudo-legal attacks for knights.

param knights Bitboard of knights.
param empty Bitboard of empty squares.
param enemy Bitboard of enemy pieces.
returns Bitboard
function

GeneratePseudoLegalBishopAttacks

Line 2204

Pseudo-legal attacks for bishops.

param bishops Bitboard of bishops.
param empty Bitboard of empty squares.
param enemy Bitboard of enemy pieces.
returns Bitboard
function

GeneratePseudoLegalRookAttacks

Line 2212

Pseudo-legal attacks for rooks.

param rooks Bitboard of rooks.
param empty Bitboard of empty squares.
param enemy Bitboard of enemy pieces.
returns Bitboard
function

GeneratePseudoLegalQueenAttacks

Line 2220

Pseudo-legal attacks for queens.

param queens Bitboard of queens.
param empty Bitboard of empty squares.
param enemy Bitboard of enemy pieces.
returns Bitboard
function

GeneratePseudoLegalKingAttacks

Line 2228

Pseudo-legal attacks for kings.

param kings Bitboard of kings.
param empty Bitboard of empty squares.
param enemy Bitboard of enemy pieces.
returns Bitboard
function

GeneratePseudoLegalAttacks

Line 2236

Generates a combined bitboard of all squares attacked by a given color.

param board Pointer to the constant board state.
param color The color of the attacking side.
returns Bitboard
function

GeneratePawnMoves

Line 2243

Generates potential destination squares for a single pawn.

param board Pointer to the board state.
param piece Square index of the pawn.
param color Color of the pawn.
returns Bitboard
function

GenerateKnightMoves

Line 2251

Generates potential destination squares for a single knight.

param board Pointer to the board state.
param piece Square index of the knight.
param color Color of the knight.
returns Bitboard
function

GenerateBishopMoves

Line 2259

Generates potential destination squares for a single bishop.

param board Pointer to the board state.
param piece Square index of the bishop.
param color Color of the bishop.
returns Bitboard
function

GenerateRookMoves

Line 2267

Generates potential destination squares for a single rook.

param board Pointer to the board state.
param piece Square index of the rook.
param color Color of the rook.
returns Bitboard
function

GenerateQueenMoves

Line 2275

Generates potential destination squares for a single queen.

param board Pointer to the board state.
param piece Square index of the queen.
param color Color of the queen.
returns Bitboard
function

GenerateKingMoves

Line 2283

Generates potential destination squares for a single king.

param board Pointer to the board state.
param piece Square index of the king.
param color Color of the king.
returns Bitboard
struct

LegalityContext

Line 2295

Contains pre-calculated data used to accelerate legality checks, such as check evasions and pins.

function

CalculateLegality

Line 2303

Pre-calculates the check and pin masks for the current board state.

param board Pointer to the board state.
returns LegalityContext
function

IsLegal

Line 2309

Returns whether a move is fully legal, ensuring it doesn't leave the friendly king in check.

param board Pointer to the constant board state.
param move The move to validate.
returns bool
function

GenerateLegalMoves

Line 2316

Generates all strictly legal moves for the current board position.

param board Pointer to the constant board state.
returns Moves
function

GenerateLegalCaptures

Line 2322

Generates only legal captures (including en passant). Ideal for quiescence search algorithms.

param board Pointer to the constant board state.
returns Moves
function

GenerateLegalMovesSquare

Line 2328

Generates all legal moves that originate from a specific square.

param board Pointer to the board.
param square The source square index.
returns Moves
function

GenerateLegalMovesBitboard

Line 2335

Returns a bitboard of all legal destination squares for the side to move.

param board Pointer to the board.
returns Bitboard
function

GenerateLegalPawnMoves

Line 2341

Generates legal pawn moves, applying check and pin constraints.

param board Pointer to the board.
param pieces Bitboard of pawns.
param color Color of the side to move.
param ctx Pointer to the pre-calculated legality context.
param moves Pointer to the list to append moves to.
param captures_only If true, only captures are generated.
function

GenerateLegalKnightMoves

Line 2351

Generates legal knight moves, applying check and pin constraints.

param board Pointer to the board.
param pieces Bitboard of knights.
param color Color of the side to move.
param ctx Pointer to the legality context.
param moves Pointer to the list to append moves to.
param captures_only If true, only captures are generated.
function

GenerateLegalBishopMoves

Line 2361

Generates legal bishop moves, applying check and pin constraints.

param board Pointer to the board.
param pieces Bitboard of bishops.
param color Color of the side to move.
param ctx Pointer to the legality context.
param moves Pointer to the list to append moves to.
param captures_only If true, only captures are generated.
function

GenerateLegalRookMoves

Line 2371

Generates legal rook moves, applying check and pin constraints.

param board Pointer to the board.
param pieces Bitboard of rooks.
param color Color of the side to move.
param ctx Pointer to the legality context.
param moves Pointer to the list to append moves to.
param captures_only If true, only captures are generated.
function

GenerateLegalQueenMoves

Line 2381

Generates legal queen moves, applying check and pin constraints.

param board Pointer to the board.
param pieces Bitboard of queens.
param color Color of the side to move.
param ctx Pointer to the legality context.
param moves Pointer to the list to append moves to.
param captures_only If true, only captures are generated.
function

GenerateLegalKingMoves

Line 2391

Generates legal king moves, ensuring the king does not move into check.

param board Pointer to the board.
param pieces Bitboard of kings.
param color Color of the side to move.
param ctx Pointer to the legality context.
param moves Pointer to the list to append moves to.
param captures_only If true, only captures are generated.
function

GenerateMoves

Line 2405

Dispatches to legal or pseudo-legal move generation.

param board The board to generate moves from
param type MoveType
Module Package

perft

src/castro.h
type

u64

Line 2415
function

Perft

Line 2418

See [https://www.chessprogramming.org/Perft](https://www.chessprogramming.org/Perft)

function

PerftPseudoLegal

Line 2422

Pseudo-legal perft: same node count as legal perft, faster (no pin/check pre-filter).

Module Package

polyglot

src/castro.h
type

PolyglotEntry

Line 2428

Represents a single entry in a Polyglot-formatted opening book (.bin). This structure maps a specific board position to a recommended move with associated metadata for move selection.

macro

U64

Line 2439

cross-platform u64 macro

const

Random64

Line 2447

See [http://hgm.nubati.net/book_format.html](http://hgm.nubati.net/book_format.html)

function

ConvertMove

Line 2669

Converts a move from the Polyglot 16-bit format (typically used in .bin opening books) to the Castro 32-bit internal move format.

param polyglotMove The 16-bit integer representing the move in Polyglot format.
returns Move
function

LookupBookMove

Line 2675

Probes an external Polyglot opening book file to find a move matching the current position's Zobrist hash. If multiple moves exist, it typically selects one based on weight.

param position_hash The 64-bit Zobrist hash of the current board position.
param book_path The filesystem path to the Polyglot .bin opening book.
returns Move

No matches found

We couldn't find anything matching ""