Documentation ¶
Overview ¶
Package board contain chess board representation and utilities.
Index ¶
- Constants
- Variables
- func FormatMoves(list []Move, fn func(Move) string) string
- func IsSameDiagonal(a, b Square) bool
- func IsSameRankOrFile(a, b Square) bool
- func PrintMoves(list []Move) string
- func SortByPriority(moves []Move, fn MovePriorityFn)
- type Bitboard
- func Attackboard(bb RotatedBitboard, sq Square, piece Piece) Bitboard
- func BishopAttackboard(bb RotatedBitboard, sq Square) Bitboard
- func BitFile(f File) Bitboard
- func BitMask(sq Square) Bitboard
- func BitRank(r Rank) Bitboard
- func KingAttackboard(sq Square) Bitboard
- func KnightAttackboard(sq Square) Bitboard
- func PawnCaptureboard(c Color, pawns Bitboard) Bitboard
- func PawnJumpRank(c Color) Bitboard
- func PawnMoveboard(all Bitboard, c Color, pawns Bitboard) Bitboard
- func PawnPromotionRank(c Color) Bitboard
- func QueenAttackboard(bb RotatedBitboard, sq Square) Bitboard
- func RookAttackboard(bb RotatedBitboard, sq Square) Bitboard
- type Board
- func (b *Board) Adjudicate(result Result)
- func (b *Board) AdjudicateNoLegalMoves() Result
- func (b *Board) Fork() *Board
- func (b *Board) FullMoves() int
- func (b *Board) HasCastled(c Color) bool
- func (b *Board) HasMoved(limit int) Bitboard
- func (b *Board) Hash() ZobristHash
- func (b *Board) LastMove() (Move, bool)
- func (b *Board) NoProgress() int
- func (b *Board) Ply() int
- func (b *Board) PopMove() (Move, bool)
- func (b *Board) Position() *Position
- func (b *Board) PushMove(m Move) bool
- func (b *Board) Result() Result
- func (b *Board) SecondToLastMove() (Move, bool)
- func (b *Board) String() string
- func (b *Board) Turn() Color
- type Castling
- type Color
- type File
- type Move
- func (m Move) CastlingRightsLost() Castling
- func (m Move) CastlingRookMove() (Square, Square, bool)
- func (m Move) EnPassantCapture() (Square, bool)
- func (m Move) EnPassantTarget() (Square, bool)
- func (m Move) Equals(o Move) bool
- func (m Move) IsCapture() bool
- func (m Move) IsCaptureOrEnPassant() bool
- func (m Move) IsCastle() bool
- func (m Move) IsInvalid() bool
- func (m Move) IsNotUnderPromotion() bool
- func (m Move) IsPromotion() bool
- func (m Move) IsUnderPromotion() bool
- func (m Move) String() string
- type MoveList
- type MovePredicateFn
- type MovePriority
- type MovePriorityFn
- type MoveType
- type Outcome
- type Piece
- type Placement
- type Position
- func (p *Position) All() Bitboard
- func (p *Position) Castling() Castling
- func (p *Position) Color(c Color) Bitboard
- func (p *Position) EnPassant() (Square, bool)
- func (p *Position) HasInsufficientMaterial() bool
- func (p *Position) IsAttacked(c Color, sq Square) bool
- func (p *Position) IsAttackedBy(c Color, sq Square, list []Piece) bool
- func (p *Position) IsCheckMate(c Color) bool
- func (p *Position) IsChecked(c Color) bool
- func (p *Position) IsDefended(c Color, sq Square) bool
- func (p *Position) IsDefendedBy(c Color, sq Square, list []Piece) bool
- func (p *Position) IsEmpty(sq Square) bool
- func (p *Position) KingSquare(c Color) Square
- func (p *Position) LegalMoves(turn Color) []Move
- func (p *Position) Move(m Move) (*Position, bool)
- func (p *Position) Piece(c Color, piece Piece) Bitboard
- func (p *Position) PieceSquares(c Color, piece Piece) []Square
- func (p *Position) PseudoLegalMoves(turn Color) []Move
- func (p *Position) Rotated() RotatedBitboard
- func (p *Position) Square(sq Square) (Color, Piece, bool)
- func (p *Position) String() string
- type Rank
- type Reason
- type Result
- type RotatedBitboard
- type Square
- type ZobristHash
- type ZobristTable
Constants ¶
const ( NoCastlingRights = Castling(0) WhiteCastlingRights = WhiteKingSideCastle | WhiteQueenSideCastle BlackCastlingRights = BlackKingSideCastle | BlackQueenSideCastle FullCastingRights = WhiteCastlingRights | BlackCastlingRights ZeroCastling = NoCastlingRights NumCastling = FullCastingRights + 1 )
Variables ¶
var ( AllPieces = []Piece{King, Queen, Rook, Knight, Bishop, Pawn} KingQueen = []Piece{King, Queen} KingQueenRookKnightBishop = []Piece{King, Queen, Rook, Knight, Bishop} QueenRookBishop = []Piece{Queen, Rook, Bishop} QueenRookKnightBishop = []Piece{Queen, Rook, Knight, Bishop} QueenRookKnightBishopPawn = []Piece{Queen, Rook, Knight, Bishop, Pawn} )
Functions ¶
func FormatMoves ¶
FormatMoves formats a list of moves.
func IsSameDiagonal ¶
IsSameDiagonal returns true if the squares are on the same diagonal.
func IsSameRankOrFile ¶
IsSameRankOrFile returns true if the squares are on the same rank or same file.
func SortByPriority ¶ added in v0.90.1
func SortByPriority(moves []Move, fn MovePriorityFn)
SortByPriority sorts the moves by priority, preserving order for same priority.
Types ¶
type Bitboard ¶
type Bitboard uint64
Bitboard is a bit-wise representation of the chess board. Each bit represents the appearance of some piece on that square. (bit 63 = A8 and bit 0 = H1). It relies on CPU-support for certain operations, such as popcount and bitscan.
const (
EmptyBitboard Bitboard = 0
)
func Attackboard ¶
func Attackboard(bb RotatedBitboard, sq Square, piece Piece) Bitboard
Attackboard returns all potential moves/attacks for an officer (= non-Pawn) at the given square.
func BishopAttackboard ¶
func BishopAttackboard(bb RotatedBitboard, sq Square) Bitboard
BishopAttackboard returns all potential moves/attacks for a Bishop at the given square.
func KingAttackboard ¶
KingAttackboard returns all potential moves/attacks for a King at the given square.
func KnightAttackboard ¶
KnightAttackboard returns all potential moves/attacks for a Knight at the given square.
func PawnCaptureboard ¶
PawnCaptureboard returns all potential pawn captures for the given color.
func PawnJumpRank ¶
PawnJumpRank returns the mask of the target rank for jump moves for the given color, i.e., Rank4 for White or Rank5 for Black.
func PawnMoveboard ¶
PawnMoveboard returns all potential pawn sigle-step moves for the given color.
func PawnPromotionRank ¶
PawnPromotionRank returns the mask of the promotion rank for the given color, i.e., Rank8 for White or Rank1 for Black.
func QueenAttackboard ¶
func QueenAttackboard(bb RotatedBitboard, sq Square) Bitboard
QueenAttackboard returns all potential moves/attacks for a Queen at the given square. Convenience function.
func RookAttackboard ¶
func RookAttackboard(bb RotatedBitboard, sq Square) Bitboard
RookAttackboard returns all potential moves/attacks for a Rook at the given square.
func (Bitboard) LastPopSquare ¶
LastPopSquare returns the index of the least-significant 1. Returns 64 if zero.
type Board ¶
type Board struct {
// contains filtered or unexported fields
}
Board represents a chess board, metadata and history of positions to correctly handle game results, notably various draw conditions. Not thread-safe.
func NewBoard ¶
func NewBoard(zt *ZobristTable, pos *Position, turn Color, noprogress, fullmoves int) *Board
func (*Board) Adjudicate ¶
Adjudicate the position as given.
func (*Board) AdjudicateNoLegalMoves ¶
AdjudicateNoLegalMoves adjudicates the position assuming no legal moves exist. The result is then either Mate or Stalemate.
func (*Board) Fork ¶
Fork branches off a new board, sharing the node history for past positions. If forked, the shared history should not be mutated (via PopMove) as the forward moves in node might then become stale.
func (*Board) FullMoves ¶
FullMoves returns the number of full moves. May be larger than game history suggests for FromPosition games.
func (*Board) HasCastled ¶
HasCastled returns true iff the color has castled.
func (*Board) Hash ¶
func (b *Board) Hash() ZobristHash
Hash returns the Zobrist hashcode for the current position.
func (*Board) NoProgress ¶
NoProgress returns the ply count since last irreversible move, i.e, pawn move, castling or capture. Used solely to track the 50 move draw rule.
func (*Board) Ply ¶
Ply returns the number of half-moves since the beginning of the game. It is equal to teh number of positions in the game history.
func (*Board) SecondToLastMove ¶
SecondToLastMove returns the second-to-last move, if any.
type Castling ¶
type Castling uint8
Castling represents the set of castling rights. 4 bits.
func CastlingRights ¶
CastlingRights returns the castling rights for the given color.
type File ¶
type File uint8
File represents a chess board file from FileH=0, ..FileA=7. The numbering is reversed to match Square. 3bits.
type Move ¶
type Move struct { Type MoveType From, To Square Piece Piece // moved piece Promotion Piece // desired piece for promotion, if any. Capture Piece // captured piece, if any. Not set if EnPassant. }
Move represents a not-necessarily legal move along with contextual metadata. 64bits.
func FindMoves ¶ added in v0.90.1
func FindMoves(moves []Move, fn MovePredicateFn) []Move
FindMoves returns moves that satisfy a predicate from a list of moves.
func ParseMove ¶
ParseMove parses a move in pure algebraic coordinate notation, such as "a2a4" or "a7a8q". The parsed move does not contain contextual information like castling or en passant.
func (Move) CastlingRightsLost ¶
CastlingRightsLost returns the castling rights that are definitely not present after this move. If king moves, rights are lost. Ditto if rook moves or is captured.
func (Move) CastlingRookMove ¶
CastlingRookMove returns the implicit rook move (from, to), if a KingSideCastle or QueenSideCastle move.
func (Move) EnPassantCapture ¶
EnPassantCapture return the e.p capture square, if a EnPassant move. For d4*e3 e.p, it turns e4.
func (Move) EnPassantTarget ¶
EnPassantTarget return the e.p target square, if a Jump move. For e2-e4, it turns e3.
func (Move) IsCapture ¶
IsCapture returns true iff the move is a Capture or CapturePromotion. Convenience function.
func (Move) IsCaptureOrEnPassant ¶ added in v0.90.1
IsCaptureOrEnPassant returns true iff the move is a Capture, CapturePromotion or EnPassant. Convenience function.
func (Move) IsCastle ¶
IsCastle returns true iff the move is a KingSideCastle or QueenSideCastle. Convenience function.
func (Move) IsNotUnderPromotion ¶ added in v0.90.1
IsNotUnderPromotion returns false if under-promotion. Convenience function for move selection.
func (Move) IsPromotion ¶
IsPromotion returns true iff the move is a Promotion or CapturePromotion. Convenience function.
func (Move) IsUnderPromotion ¶ added in v0.90.1
IsUnderPromotion returns true iff the move is a Promotion, but not to a Queen. Convenience function.
type MoveList ¶ added in v0.90.1
type MoveList struct {
// contains filtered or unexported fields
}
MoveList is move priority queue for move ordering.
func NewMoveList ¶ added in v0.90.1
func NewMoveList(moves []Move, fn MovePriorityFn) *MoveList
NewMoveList returns a new move list with the given priorities.
type MovePredicateFn ¶ added in v0.90.1
MovePredicateFn is a move predicate.
type MovePriority ¶ added in v0.90.1
type MovePriority int16
MovePriority represents the move order priority.
type MovePriorityFn ¶ added in v0.90.1
type MovePriorityFn func(move Move) MovePriority
MovePriorityFn assigns a priority to moves
func First ¶ added in v0.90.1
func First(first Move, fn MovePriorityFn) MovePriorityFn
First puts the given move first. Otherwise uses the given function.
type MoveType ¶
type MoveType uint8
MoveType indicates the type of move. The no-progress counter is reset with any non-Normal move.
type Outcome ¶
type Outcome uint8
Outcome represents the result of a game, if any. Result include a special Unknown option to better support the lazy movegen approach, where we only discover the true result when no legal move exists. 3 bits.
type Piece ¶
type Piece uint8
Piece represents a chess piece (King, Pawn, etc) with no color. Zero indicates "No Piece". 3 bits.
func ParsePiece ¶
type Position ¶
type Position struct {
// contains filtered or unexported fields
}
Position represents a board position suitable for move generation. It includes castling and en passant, but not game metadata to determine various Draw conditions.
func NewPosition ¶
func (*Position) EnPassant ¶
EnPassant return the target en passant square, if previous move was a Jump. For example, after e2e4, the en passant target square is e3 whether or not black has pawns on d4 or f4.
func (*Position) HasInsufficientMaterial ¶
HasInsufficientMaterial returns true iff there is not sufficient material for either side to win. The cases are: K v K, KN v K, KB v KB (or KBB v K) w/ Bishops on same square color. Assumes 2 kings.
func (*Position) IsAttacked ¶
IsAttacked returns true iff the square is attacked by the opposing color. Does not include en passant.
func (*Position) IsAttackedBy ¶ added in v0.91.1
IsAttackedBy returns true iff the square is attacked by the given pieces of the opposing color. Does not include en passant.
func (*Position) IsCheckMate ¶
IsCheckMate returns true iff the color is checkmate. Convenient for IsChecked && len(LegalMoves)==0.
func (*Position) IsChecked ¶
IsChecked returns true iff the color is in check. Convenient for IsAttacked(King).
func (*Position) IsDefended ¶ added in v0.90.1
IsDefended returns true iff the square is defended by the color.
func (*Position) IsDefendedBy ¶ added in v0.91.1
IsDefendedBy returns true iff the square is defended by pieces of the color.
func (*Position) KingSquare ¶ added in v0.90.1
KingSquare returns the square for a given color. Must be valid and unique.
func (*Position) LegalMoves ¶
LegalMoves returns a list of all legal moves. Convenience function.
func (*Position) Move ¶
Move attempts to make a pseudo-legal move. The attempted move is assumed to be pseudo-legal and generated from the position. Returns false if not legal.
func (*Position) PieceSquares ¶ added in v0.90.1
PieceSquares returns the squares for a given color/piece.
func (*Position) PseudoLegalMoves ¶
PseudoLegalMoves returns a list of all pseudo-legal moves. The move may not respect either side being in check, which must be validated subsequently.
func (*Position) Rotated ¶
func (p *Position) Rotated() RotatedBitboard
Rotated returns the rotated bitboard.
type Rank ¶
type Rank uint8
Rank represents a chess board rank from Rank1=0, ..Rank8=7. 3bits.
func PromotionRank ¶ added in v0.90.1
PromotionRank returns the promotion rank of the given color.
type Reason ¶
type Reason string
Reason is the reason for a terminal result.
const ( Checkmate Reason = "Checkmate" Resigned Reason = "Opponent Resigned" TimedOut Reason = "Opponent lost on time" Stalemate Reason = "Stalemate" Repetition3 Reason = "3-Fold Repetition" // can be claimed, but does not have to be Repetition5 Reason = "5-Fold Repetition" NoProgress Reason = "No progress" InsufficientMaterial Reason = "Insufficient Material" Agreement Reason = "Agreement" )
type Result ¶
Result represents the result of a game, if any, with reason.
func (Result) IsTerminal ¶
type RotatedBitboard ¶
type RotatedBitboard struct {
// contains filtered or unexported fields
}
RotatedBitboard represents the piece-agnostic population of the board as so-called "rotated bitboards". It is designed to map files/diagonals into adjacent memory cells. It is conceptually simpler to view the transformations as rotations, but we are free to "shuffle" the files/diagonals as we please: the 'rot90' is really a flip. In the diagonal case we have to hold additional information about the length and offset of the desired diagonal, since that information is not constant.
func NewRotatedBitboard ¶
func NewRotatedBitboard(bb Bitboard) RotatedBitboard
func (RotatedBitboard) Mask ¶
func (r RotatedBitboard) Mask() Bitboard
Mask returns the bitboard mask (in normal orientation).
func (RotatedBitboard) String ¶
func (r RotatedBitboard) String() string
func (RotatedBitboard) Xor ¶
func (r RotatedBitboard) Xor(sq Square) RotatedBitboard
Xor returns the rotated bitboard xor the square mask.
type Square ¶
type Square uint8
Square represents a square on the board, ordered H1=0, G1=1 .., A8=63. This numbering matches a 64-bit interpretation as a bitboard:
A8 = 63, B8 = 62, C8 = 61, D8 = 60, E8 = 59, F8 = 58, G8 = 57, H8 = 56, A7 = 55, B7 = 54, C7 = 53, D7 = 52, E7 = 51, F7 = 50, G7 = 49, H7 = 48, A6 = 47, B6 = 46, C6 = 45, D6 = 44, E6 = 43, F6 = 42, G6 = 41, H6 = 40, A5 = 39, B5 = 38, C5 = 37, D5 = 36, E5 = 35, F5 = 34, G5 = 33, H5 = 32, A4 = 31, B4 = 30, C4 = 29, D4 = 28, E4 = 27, F4 = 26, G4 = 25, H4 = 24, A3 = 23, B3 = 22, C3 = 21, D3 = 20, E3 = 19, F3 = 18, G3 = 17, H3 = 16, A2 = 15, B2 = 14, C2 = 13, D2 = 12, E2 = 11, F2 = 10, G2 = 9, H2 = 8, A1 = 7, B1 = 6, C1 = 5, D1 = 4, E1 = 3, F1 = 2, G1 = 1, H1 = 0
A square is a bit-index into the bitboard layout. 6 bits.
Iteration helpers to enable "for i := ZeroSquare; i<NumSquares; i++".
func ParseSquare ¶
func ParseSquareStr ¶
type ZobristHash ¶
type ZobristHash uint64
ZobristHash is a position hash based on piece-squares. It is intended for 3-fold repetition draw detection and hashes "identical" positions under that rule to the same hash value.
See also: https://research.cs.wisc.edu/techreports/1970/TR88.pdf.
type ZobristTable ¶
type ZobristTable struct {
// contains filtered or unexported fields
}
ZobristTable is a pseudo-randomized table for computing a position hash.
func NewZobristTable ¶
func NewZobristTable(seed int64) *ZobristTable
func (*ZobristTable) Hash ¶
func (z *ZobristTable) Hash(pos *Position, turn Color) ZobristHash
Hash computes the zobrist hash for the given position.
func (*ZobristTable) Move ¶
func (z *ZobristTable) Move(h ZobristHash, pos *Position, m Move) ZobristHash
Move computes a hash for the position after the (legal) move incrementally. Cheaper than computing it for the new position directly.