A chess library that provides fast move generation, position manipulation, and support for common chess formats like FEN and PGN. The library uses bitboards for efficient board representation and move generation.
Overview
The library is organized into several core modules:
Core Types and Board Representation
- types: Fundamental chess types (Square, Color, Piece)
- bitboard: Efficient bitboard operations for fast computation
- position: Chess position representation and manipulation
Move Generation and Validation
Game Management and Formats
- game: Complete chess game representation
- pgn: PGN (Portable Game Notation) reading and writing
- strchess: String parsing utilities (FEN, SAN, UCI)
Quick Start
Creating and working with positions:
Example:
import src/nimchess # Create starting position from FEN let pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".toPosition # Display the position echo pos # Shows board with Unicode pieces # Generate all legal moves for move in pos.legalMoves: echo move.toSAN(pos) # Show move in algebraic notation # Make a move let newPos = pos.doMove("e4".toMove(pos)) echo newPos.fen() # Get FEN string of new positionWorking with games and PGN:
Example:
import src/nimchess let games = readPgnFile("tests/testdata/pgns.pgn") for game in games: echo game.headers.getOrDefault("Event", "?") echo "Result: ", game.resultWorking with bitboards:
Example:
import src/nimchess let position = "1rbn1rk1/p2p2q1/1p2p1pp/2b5/2P1NP2/P3P3/1P2BNP1/1KQR3R b - - 4 21".toPosition bishopAttack = position.attacksFrom(bishop, c5) attackedPawns = position[white, pawn] and bishopAttack echo attackedPawns for attackedPawnSquare in attackedPawns: echo attackedPawnSquare
Exports
-
down, isSet, file, &=, shr, |=, toSquare, not, and, countSetBits, mirrorVertically, xor, mask3x3, $, homeRank, up, isPassedMask, items, right, left, shl, Bitboard, empty, or, toBitboard, ==, attackMask, up, mask5x5, mirrorHorizontally, rank, attackMaskPawnQuiet, attackMaskPawnCapture, promoted, isChess960, zobristKeysAreOk, ==, opposite, [], newSquare, $, doMove, CastlingSide, boardString, toBitboard, mirrorHorizontally, isEnPassantCapture, mirrorVertically, noMove, addMove, $, []=, Game, []=, fileNumber, up, attackMaskPawnCapture, isEdge, [], [], ColoredPiece, shr, moved, isLowerEdge, setZobristKeys, right, colorAt, removePiece, Square, doNullMove, attackMaskPawnQuiet, Move, addMove, and, enPassantTargetSquare, isSet, inCheck, ==, or, mirrorVertically, seventyFiveMoveRule, isNoMove, mirrorHorizontally, fiftyMoveRule, down, isUpperEdge, attackers, homeRank, left, [], attacksFrom, attackMask, notation, up, mask5x5, castlingSide, up, mirrorHorizontally, rank, occupancy, |=, ==, toSquare, right, source, fivefoldRepetition, ~, target, $, captured, squareDistance, calculateZobristKeys, Bitboard, empty, Piece, addPiece, down, pieceAt, coloredPieceAt, [], positions, ^=, file, Position, isLegal, isLeftEdge, currentPosition, mask3x3, [], mirrorVertically, xor, squareManhattanDistance, currentFullmoveNumber, left, toColoredPiece, isCapture, repetitionCount, movePiece, enemy, Color, isPseudoLegal, isRightEdge, addColoredPiece, not, &=, isCastling, attacksFrom, kingSquare, countSetBits, isAttacked, newMove, shl, ZobristKey, items, isTactical, isPassedMask, notation, up, hasRepetition, newGame, rankNumber, promoted, isChess960, zobristKeysAreOk, ==, [], newSquare, doMove, castlingSide, boardString, toBitboard, ==, mirrorHorizontally, isEnPassantCapture, mirrorVertically, noMove, []=, []=, fileNumber, up, [], or, isEdge, Bitboard, [], attackMaskPawnCapture, ColoredPiece, shr, moved, isLowerEdge, setZobristKeys, $, right, colorAt, isPassedMask, isRightEdge, Square, doNullMove, captured, attackMaskPawnQuiet, and, enPassantTargetSquare, isSet, opposite, inCheck, $, [], isNoMove, mirrorHorizontally, down, isUpperEdge, attackers, homeRank, left, [], attacksFrom, attackMask, notation, mask5x5, CastlingSide, up, mirrorHorizontally, rank, occupancy, |=, ==, toSquare, right, source, ~, target, $, squareDistance, calculateZobristKeys, empty, Piece, addPiece, down, pieceAt, coloredPieceAt, mirrorVertically, ^=, Position, file, isLegal, isLeftEdge, mask3x3, removePiece, [], xor, squareManhattanDistance, currentFullmoveNumber, left, toColoredPiece, isCapture, movePiece, enemy, Color, isPseudoLegal, addColoredPiece, Move, not, &=, isCastling, attacksFrom, kingSquare, countSetBits, up, isAttacked, newMove, shl, ZobristKey, items, isTactical, mirrorVertically, notation, up, rankNumber, promoted, isChess960, zobristKeysAreOk, ==, opposite, [], newSquare, $, doMove, CastlingSide, boardString, toBitboard, mirrorHorizontally, isEnPassantCapture, mirrorVertically, noMove, $, []=, []=, fileNumber, up, attackMaskPawnCapture, isEdge, [], [], ColoredPiece, shr, moved, isLowerEdge, setZobristKeys, right, colorAt, removePiece, Square, doNullMove, attackMaskPawnQuiet, Move, and, enPassantTargetSquare, isSet, inCheck, ==, or, mirrorVertically, isNoMove, mirrorHorizontally, down, isUpperEdge, attackers, homeRank, left, generateCaptures, [], attacksFrom, attackMask, notation, up, mask5x5, castlingSide, up, mirrorHorizontally, rank, occupancy, |=, ==, generateMoves, toSquare, right, source, ~, target, $, captured, squareDistance, calculateZobristKeys, Bitboard, empty, Piece, legalMoves, addPiece, down, pieceAt, coloredPieceAt, [], ^=, file, Position, isLegal, isLeftEdge, mask3x3, [], mirrorVertically, xor, squareManhattanDistance, currentFullmoveNumber, left, toColoredPiece, isCapture, movePiece, enemy, Color, isPseudoLegal, isRightEdge, addColoredPiece, not, &=, generateQuiets, isCastling, attacksFrom, kingSquare, countSetBits, isMate, isAttacked, newMove, shl, isStalemate, ZobristKey, items, isTactical, isPassedMask, notation, up, rankNumber, []=, isChess960, noMove, zobristKeysAreOk, boardString, toBitboard, ==, $, attackMaskPawnCapture, setZobristKeys, readPgnFromStreamIter, shr, and, readPgnFromString, colorAt, empty, Square, doNullMove, enemy, isSet, inCheck, $, or, addMove, isNoMove, isUpperEdge, left, [], attackMask, up, CastlingSide, up, mirrorHorizontally, occupancy, readPgnFileIter, |=, ==, Move, right, source, $, captured, squareDistance, calculateZobristKeys, [], addPiece, down, pieceAt, file, currentPosition, squareManhattanDistance, left, toColoredPiece, Color, isPseudoLegal, &=, isCastling, fivefoldRepetition, toPgnString, isLegal, isTactical, mirrorVertically, repetitionCount, up, hasRepetition, rankNumber, promoted, opposite, newSquare, isLowerEdge, doMove, castlingSide, mirrorHorizontally, [], mirrorVertically, readPgnFromStream, Game, fileNumber, up, isEnPassantCapture, [], isEdge, removePiece, [], ColoredPiece, moved, right, [], seventyFiveMoveRule, attackMaskPawnQuiet, enPassantTargetSquare, Bitboard, mirrorHorizontally, down, attackers, homeRank, attacksFrom, countSetBits, notation, mask5x5, rank, toSquare, ~, target, readPgnFile, addMove, Piece, fiftyMoveRule, toPgnString, coloredPieceAt, positions, ^=, Position, ==, isLeftEdge, newMove, mirrorVertically, xor, currentFullmoveNumber, isCapture, []=, movePiece, isRightEdge, addColoredPiece, not, attacksFrom, kingSquare, isAttacked, mask3x3, shl, ZobristKey, items, isPassedMask, notation, newGame, down, isChess960, ==, setZobristKeys, zobristKeysAreOk, ==, isUpperEdge, [], attackers, newSquare, homeRank, $, left, boardString, toBitboard, mirrorHorizontally, removePiece, attacksFrom, countSetBits, coloredPieceAt, attackMask, opposite, notation, up, mask5x5, fileNumber, CastlingSide, up, mirrorHorizontally, rank, occupancy, |=, toSquare, right, mirrorVertically, inCheck, ~, $, squareDistance, calculateZobristKeys, Bitboard, empty, Piece, addPiece, down, pieceAt, up, mirrorVertically, [], ZobristKey, isEdge, ^=, [], attackMaskPawnCapture, file, ColoredPiece, mirrorHorizontally, shr, []=, isLowerEdge, Position, mask3x3, right, mirrorVertically, colorAt, [], xor, squareManhattanDistance, currentFullmoveNumber, left, [], toColoredPiece, Square, movePiece, enemy, isRightEdge, addColoredPiece, attackMaskPawnQuiet, []=, not, &=, and, isSet, attacksFrom, kingSquare, isAttacked, isLeftEdge, shl, or, items, isPassedMask, [], rankNumber, notation, up, Color, toUCI, promoted, isChess960, zobristKeysAreOk, ==, opposite, [], newSquare, isLowerEdge, toMoveFromUCI, doMove, CastlingSide, boardString, toBitboard, mirrorHorizontally, toSAN, rank, isEnPassantCapture, mirrorVertically, noMove, debugString, []=, fileNumber, up, [], or, isEdge, [], attackMaskPawnCapture, ColoredPiece, setZobristKeys, shr, moved, $, right, colorAt, toPosition, isPassedMask, doMove, isRightEdge, Square, $, doNullMove, isCapture, enemy, notation, Move, and, squareDistance, isSet, inCheck, removePiece, isNoMove, fen, mirrorHorizontally, down, isUpperEdge, ==, attackers, homeRank, left, mirrorVertically, attacksFrom, attackMask, notation, up, mask5x5, castlingSide, mirrorHorizontally, occupancy, |=, ==, toSquare, right, source, ~, target, $, captured, enPassantTargetSquare, calculateZobristKeys, Bitboard, empty, Piece, addPiece, down, pieceAt, toMove, coloredPieceAt, [], ^=, file, Position, isLeftEdge, mask3x3, mirrorVertically, [], xor, squareManhattanDistance, currentFullmoveNumber, left, toColoredPiece, movePiece, attackMaskPawnQuiet, Color, isPseudoLegal, addColoredPiece, not, classicalStartPos, &=, isCastling, attacksFrom, kingSquare, countSetBits, up, isAttacked, newMove, $, shl, ZobristKey, items, isLegal, isTactical, [], []=, notation, up, rankNumber, down, ==, opposite, isUpperEdge, isLowerEdge, ColoredPiece, newSquare, squareManhattanDistance, isEdge, left, toColoredPiece, isRightEdge, rankNumber, Square, notation, mirrorHorizontally, fileNumber, up, mirrorVertically, $, squareDistance, isLeftEdge, right, ZobristKey, Piece, boardString, ^=, up, notation, Color