Documentation
¶
Overview ¶
Package chess is a self-contained chess engine: board state, pieces, move generation, legality checking, FEN parsing, make/unmake, and game-result detection.
It is the project's only public, reusable surface — an external program can depend on it to represent a position and enumerate legal moves without pulling in any of gambit's agents, GUI or game-loop code.
Squares are indexed 0..63 with A1=0 and H8=63. Colors, pieces and moves are compact value types so move lists are cheap to copy and sort during search.
Index ¶
- Constants
- func Perft(b *Board, depth int) uint64
- type Board
- func (b *Board) ApplyMove(m Move) *Board
- func (b *Board) CastleRights() CastleRights
- func (b *Board) Clone() *Board
- func (b *Board) Each(fn func(s Square, p Piece))
- func (b *Board) EnPassantSquare() Square
- func (b *Board) FEN() string
- func (b *Board) FullMoveNumber() int
- func (b *Board) GenerateMoves(dst []Move) []Move
- func (b *Board) GeneratePseudoLegal(dst []Move) []Move
- func (b *Board) HalfMoveClock() int
- func (b *Board) Hash() uint64
- func (b *Board) InCheck() bool
- func (b *Board) IsCheckmate() bool
- func (b *Board) IsInsufficientMaterial() bool
- func (b *Board) IsSquareAttacked(s Square, by Color) bool
- func (b *Board) IsStalemate() bool
- func (b *Board) KingSquare(c Color) Square
- func (b *Board) LegalMoves() []Move
- func (b *Board) MakeMove(m Move) Undo
- func (b *Board) PieceAt(s Square) Piece
- func (b *Board) SideToMove() Color
- func (b *Board) Status() (Result, DrawReason)
- func (b *Board) UnmakeMove(m Move, u Undo)
- type CastleRights
- type Color
- type DrawReason
- type Move
- type MoveFlag
- type Piece
- type PieceType
- type Result
- type Square
- type Undo
Constants ¶
const StartingFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
StartingFEN is the FEN of the standard initial position.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Board ¶
type Board struct {
// contains filtered or unexported fields
}
Board is a single chess position using an 8x8 mailbox representation. It is deliberately representation-agnostic at the API level so the internals could be swapped for bitboards later without breaking callers.
Board.MakeMove and Board.UnmakeMove mutate the receiver in place and are meant to be paired for fast search; Board.ApplyMove instead returns a new board and leaves the receiver unchanged, for callers that want an immutable step.
func NewStartingBoard ¶
func NewStartingBoard() *Board
NewStartingBoard returns a board in the standard initial position.
func (*Board) ApplyMove ¶
ApplyMove returns a new board with the move applied, leaving the receiver unchanged. This clone-based variant suits search strategies (e.g. beam search) that keep many independent positions alive at once.
func (*Board) CastleRights ¶
func (b *Board) CastleRights() CastleRights
CastleRights returns the current castling rights.
func (*Board) Each ¶
Each calls fn for every square on the board in index order. Useful for rendering without exposing the internal array.
func (*Board) EnPassantSquare ¶
EnPassantSquare returns the current en-passant target, or NoSquare.
func (*Board) FullMoveNumber ¶
FullMoveNumber returns the full-move counter.
func (*Board) GenerateMoves ¶
GenerateMoves appends all fully-legal moves for the side to move to dst and returns the extended slice. The pseudo-legal moves are generated into dst and filtered in place, so hot search loops that pass a reused buffer avoid a per-call allocation.
func (*Board) GeneratePseudoLegal ¶
GeneratePseudoLegal appends all pseudo-legal moves (legal except that they may leave the mover's king in check) to dst and returns the extended slice.
func (*Board) HalfMoveClock ¶
HalfMoveClock returns the halfmove clock (for the fifty-move rule).
func (*Board) Hash ¶
Hash returns a Zobrist hash of the position, suitable for repetition detection. Positions that are equal for repetition purposes (same pieces, side to move, castling rights and en-passant possibility) hash equally.
func (*Board) IsCheckmate ¶
IsCheckmate reports whether the side to move is checkmated.
func (*Board) IsInsufficientMaterial ¶
IsInsufficientMaterial reports whether neither side has enough material to force checkmate (K vs K, K+minor vs K, and K+B vs K+B with same-colored bishops are treated as insufficient).
func (*Board) IsSquareAttacked ¶
IsSquareAttacked reports whether square s is attacked by any piece of the given color.
func (*Board) IsStalemate ¶
IsStalemate reports whether the side to move is stalemated.
func (*Board) KingSquare ¶
KingSquare returns the square of the given color's king.
func (*Board) LegalMoves ¶
LegalMoves returns all fully-legal moves for the side to move. It allocates a fresh slice; hot search loops should prefer GenerateMoves with a reused buffer.
func (*Board) MakeMove ¶
MakeMove applies a move to the board and returns an Undo that reverses it. The move must be legal (or at least pseudo-legal); behavior is undefined otherwise.
func (*Board) Status ¶
func (b *Board) Status() (Result, DrawReason)
Status returns the outcome derivable from the current position alone (checkmate, stalemate, insufficient material). Threefold repetition and the fifty-move rule depend on game history and are handled by callers; the fifty-move clock is exposed via HalfMoveClock.
func (*Board) UnmakeMove ¶
UnmakeMove reverses a MakeMove given the Undo it returned.
type CastleRights ¶
type CastleRights uint8
CastleRights is a bitset of the four castling possibilities.
const ( WhiteKingside CastleRights = 1 << iota WhiteQueenside BlackKingside BlackQueenside )
Castling-rights bits.
func (CastleRights) Has ¶
func (c CastleRights) Has(r CastleRights) bool
Has reports whether all of the given rights are present.
type Color ¶
type Color uint8
Color identifies which side a piece belongs to or which side is to move.
type DrawReason ¶
type DrawReason uint8
DrawReason explains why a game was drawn.
const ( NotDraw DrawReason = iota Stalemate FiftyMoveRule ThreefoldRepetition InsufficientMaterial )
Draw reasons.
func (DrawReason) String ¶
func (d DrawReason) String() string
String returns a human-readable draw reason.
type Move ¶
type Move uint16
Move packs a from-square, to-square and flag into a single uint16: bits 0..5 = from, bits 6..11 = to, bits 12..15 = flag. This keeps move lists cheap to copy and sort during search.
const NoMove Move = 0
NoMove is the zero value, distinguishable because a real move never has equal from/to squares.
func (Move) IsPromotion ¶
IsPromotion reports whether the move promotes a pawn.
type Piece ¶
type Piece uint8
Piece encodes a colored piece in a single byte. The low three bits hold the PieceType and the next bit holds the Color. NoPiece (zero) is an empty square.
const NoPiece Piece = 0
NoPiece is the empty-square sentinel.
type Square ¶
type Square uint8
Square is a board square index in the range 0..63, where A1=0, B1=1, ..., H8=63. NoSquare (64) is the "no such square" sentinel.
const NoSquare Square = 64
NoSquare marks the absence of a square (e.g. no en-passant target).
func NewSquare ¶
NewSquare builds a square from zero-based file (0=a..7=h) and rank (0=rank 1..7=rank 8).
func ParseSquare ¶
ParseSquare parses an algebraic square such as "e4".