Documentation ¶
Index ¶
- Constants
- Variables
- func InitKingAttacks()
- func InitKingSafetyMasks()
- func InitKnightAttacks()
- func InitOccBitCounts()
- func InitPawnAttacks()
- func InitPawnStrucutreMasks()
- func InitSliders()
- func InitSquares()
- func Perft(fen string, depth int) (int64, time.Duration)
- type BBoard
- func BishopAttacksWithBlocker(sq int, blocker BBoard) BBoard
- func BishopRelOcc(sq int) BBoard
- func FindMagicNumber(sq, bitCount int, isBishop bool) BBoard
- func GetBishopAttacks(sq int, occ BBoard) BBoard
- func GetMagicNumber() BBoard
- func GetQueenAttacks(sq int, occ BBoard) BBoard
- func GetRookAttacks(sq int, occ BBoard) BBoard
- func Occupancy(index, count int, attack BBoard) BBoard
- func RookAttacksWithBlocker(sq int, blocker BBoard) BBoard
- func RookRelOcc(sq int) BBoard
- type Board
- func (b *Board) AttackedSquares(side int, occ BBoard) BBoard
- func (b *Board) CompleteCastling(move Move)
- func (b *Board) Copy() *Board
- func (b *Board) ExportFEN() string
- func (b *Board) Flip()
- func (b *Board) GetBitBoard(piece int) *BBoard
- func (b *Board) GetChecksBB(side int) (BBoard, bool)
- func (b *Board) GetGamePhase() int
- func (b *Board) GetPinsBB(side int) map[int]BBoard
- func (b *Board) GetUnmake() func()
- func (b *Board) ImportFEN(fen string) error
- func (b *Board) InsufficentMaterial() bool
- func (b *Board) IsAttacked(sq, side int, occ BBoard) bool
- func (b *Board) IsChecked(side int) bool
- func (b *Board) IsPawnOnly() bool
- func (b *Board) MakeMove(move Move) func()
- func (b *Board) MakeNullMove() func()
- func (b *Board) MoveGenKing() []Move
- func (b *Board) MoveGenLegal() []Move
- func (b *Board) MoveUCI(uciMove string) (func(), bool)
- func (b *Board) PerftDebug(depth int)
- func (b *Board) PieceAtSquare(sq Square) int
- func (b *Board) PlayMovesUCI(uciMoves string) bool
- func (b *Board) Promote(move Move)
- func (b *Board) PseudoCaptureAndQueenPromoGen() []Move
- func (b *Board) PseudoMoveGen() []Move
- func (b *Board) RemoveCaptured(sq int)
- func (b *Board) SeedHash() uint64
- func (b *Board) ZobristCapture(move Move, piece int)
- func (b *Board) ZobristCastling(right CastlingRights)
- func (b *Board) ZobristCastlingRights(right CastlingRights)
- func (b *Board) ZobristEPCapture(move Move)
- func (b *Board) ZobristEnPassant(square Square)
- func (b *Board) ZobristPromotion(move Move)
- func (b *Board) ZobristSideToMove()
- func (b *Board) ZobristSimpleMove(move Move, piece int)
- type CastlingRights
- type Move
- func (m Move) From() Square
- func (m Move) FromTo() (Square, Square)
- func (m Move) IsCapture() bool
- func (m Move) IsCastling() bool
- func (m Move) IsDouble() bool
- func (m Move) IsEnPassant() bool
- func (m Move) Piece() uint8
- func (m Move) Promotion() uint8
- func (m Move) SetPromotion(prom uint8) Move
- func (m Move) String() string
- func (m Move) To() Square
- type PieceSet
- type Square
Constants ¶
const ( IsEnpassant = 1 << 15 IsCapture = 1 << 16 IsCastling = 1 << 17 IsDouble = 1 << 18 PieceMask = 1<<3 - 1 PieceShift = 19 )
const ( P uint8 B N R Q K )
const ( WhiteToMove byte = 'w' BlackToMove byte = 'b' )
const ( WHITE = 0 BLACK = 1 BOTH = 2 PAWNS = 0 BISHOPS = 1 KNIGHTS = 2 ROOKS = 3 QUEENS = 4 KINGS = 5 AFile BBoard = 72340172838076673 BFile BBoard = 144680345676153346 CFile BBoard = 289360691352306692 DFile BBoard = 578721382704613384 EFile BBoard = 1157442765409226768 FFile BBoard = 2314885530818453536 GFile BBoard = 4629771061636907072 HFile BBoard = 9259542123273814144 Rank8 BBoard = 255 Rank7 = Rank8 << 8 Rank6 = Rank7 << 8 Rank5 = Rank6 << 8 Rank4 = Rank5 << 8 Rank3 = Rank4 << 8 Rank2 = Rank3 << 8 Rank1 = Rank2 << 8 F1G1 = Rank1&FFile | Rank1&GFile D1C1 = Rank1&DFile | Rank1&CFile D1B1 = Rank1&DFile | Rank1&CFile | Rank1&BFile F8G8 = Rank8&FFile | Rank8&GFile D8C8 = Rank8&DFile | Rank8&CFile D8B8 = Rank8&DFile | Rank8&CFile | Rank8&BFile )
Useful bitboard constants.
const ( A8 int = iota B8 C8 D8 E8 F8 G8 H8 A7 B7 C7 D7 E7 F7 G7 H7 A6 B6 C6 D6 E6 F6 G6 H6 A5 B5 C5 D5 E5 F5 G5 H5 A4 B4 C4 D4 E4 F4 G4 H4 A3 B3 C3 D3 E3 F3 G3 H3 A2 B2 C2 D2 E2 F2 G2 H2 A1 B1 C1 D1 E1 F1 G1 H1 )
Variables ¶
var ( SquareBitboards [64]BBoard PawnAttacks [2][64]BBoard KnightAttacks [64]BBoard KingAttacks [64]BBoard KingSafetyMask [2][64]BBoard BishopOccBitCount [64]int RookOccBitCount [64]int BishopMagics = [64]BBoard{ 0x20010400808600, 0xa008010410820000, 0x1004440082038008, 0x904040098084800, 0x600c052000520541, 0x4002010420402022, 0x11040104400480, 0x200104104202080, 0x1200210204080080, 0x6c18600204e20682, 0x2202004200e0, 0x100044404810840, 0x400220211108110, 0x20002011009000c, 0xa00200a2084210, 0x202008098011000, 0xc40002004019206, 0x116042040804c500, 0x419002080a80200a, 0x4000844000800, 0x404b080a04800, 0x4608080482012002, 0x44040500a0880841, 0x2002100909050d00, 0x8404004030a400, 0x90709004040080, 0x11444043040d0204, 0x8080100202020, 0x801001181004000, 0x4140822002021000, 0x102089092009006, 0x540a042100540203, 0x50100409482820, 0x8010880900041004, 0x230100500414, 0x200800050810, 0x8294064010040100, 0x9010100220044404, 0x154202022004008e, 0x9420220008401, 0x71080840110401, 0x2000a40420400201, 0x802619048001004, 0x209280a058000500, 0x2004044810100a00, 0xa0208d000804300, 0x638a80d000684, 0x1910401000080, 0x800420210400200, 0x4404410090100, 0x8020808400880000, 0x400081042120c21, 0x4009001022120001, 0x4902220802082000, 0x410841000820290, 0x820020401002440, 0x800420041084000, 0x10818c05a000, 0x301804213d000, 0x800040018208801, 0x1b80000004104405, 0x2500214084184884, 0x1000628801050400, 0x8040229e24002080, } )
var ( BishopAttackMasks [64]BBoard RookAttackMasks [64]BBoard BishopAttacks [64][4096]BBoard RookAttacks [64][4096]BBoard )
var ( PassedPawns [2][64]BBoard IsolatedPawns [64]BBoard DoubledPawns [64]BBoard )
var ( // Castling moves. Used for recognizing castling and moving king during castling. WCastleKing = MoveFromString("e1g1") | IsCastling | KINGS<<PieceShift WCastleQueen = MoveFromString("e1c1") | IsCastling | KINGS<<PieceShift BCastleKing = MoveFromString("e8g8") | IsCastling | KINGS<<PieceShift BCastleQueen = MoveFromString("e8c8") | IsCastling | KINGS<<PieceShift // Complimentary castling moves. Used during castling to reposition rook. WCastleKingRook = MoveFromString("h1f1") WCastleQueenRook = MoveFromString("a1d1") BCastleKingRook = MoveFromString("h8f8") BCastleQueenRook = MoveFromString("a8d8") )
var RookMagics = [64]BBoard{
0x18010a040018000, 0x40002000401001, 0x290010a841e00100, 0x29001000050900a0,
0x4080030400800800, 0x1200040200100801, 0x2200208200040851, 0x220000820425004c,
0x104800740008020, 0x420400020005000, 0x844801000200480, 0x4004808008001000,
0x4009000410080100, 0x3000400020900, 0x4804000810020104, 0x74800641800900,
0x862818014400020, 0x40048020004480, 0x11a1010040200012, 0x20828010000800,
0x848808004020800, 0x4522808004000200, 0x10100020004, 0x400206000092411c,
0x818004444000a000, 0x180a000c0005002, 0xb104100200100, 0x24022202000a4010,
0x100040080080080, 0x2010200080490, 0x180390400221098, 0x410008200010044,
0x310400089800020, 0x8c0804009002902, 0x1004402001001504, 0x105021001000920,
0x40080800801, 0xa02001002000804, 0x108284204005041, 0x8004082002411,
0x2802281c0028001, 0x9044000910020, 0x200010008080, 0x40201001010008,
0x8000080004008080, 0x3010400420080110, 0x414210040008, 0x10348400460001,
0x80002000401040, 0x460200088400080, 0x8201822000100280, 0x600100008008280,
0xc0800800040080, 0x24040080020080, 0x22c11a0108100c00, 0x204008114104200,
0x8800800010290041, 0x401500228206, 0x8002a00011090041, 0x42008100101,
0x283000800100205, 0x2008810010402, 0x490102200880104, 0x800010920940042,
}
Functions ¶
func InitKingSafetyMasks ¶
func InitKingSafetyMasks()
King safety masks ar similar to KingAttacks but do not cover squares behind the king. Only pieces in front of the kind attribute to safety.
func InitOccBitCounts ¶
func InitOccBitCounts()
Calculate and initialize the number of relevant occupancies for Bishops and Rooks for all squares.
func InitPawnStrucutreMasks ¶
func InitPawnStrucutreMasks()
func InitSquares ¶
func InitSquares()
Types ¶
type BBoard ¶
type BBoard uint64
func BishopAttacksWithBlocker ¶
Generate Bishop sliding attacks with a blocker bitboard on the fly. Only used for finding and initializing magic numbers. Too slow to use for movegen.
func BishopRelOcc ¶
Generate bishop relevant occupancy look table Relevant occupancy are the squares where a potential blocking piece can cut a sliding piece from moving beyond it Relevant occupancy does not include border squares as a piece on the edge of the board can not block it moving past it.
func FindMagicNumber ¶
Find magic numbers with a brute force approach for a square.
func GetBishopAttacks ¶
Get bishop attack mask with blocker occupancy.
func GetMagicNumber ¶
func GetMagicNumber() BBoard
Generate a random bitboard with few non-zero bits for Magic Number candidates.
func GetQueenAttacks ¶
Get Queen attacks as a Bishop and Rook superposition.
func GetRookAttacks ¶
Get Rook attack mask with blocker occupancy.
func RookAttacksWithBlocker ¶
Generate Rook sliding attacks with a blocker bitboard on the fly. Only used for finding and initializing magic numbers. Too slow to use for movegen.
func RookRelOcc ¶
Generate rook relevant occupancy. See Bishop equivalent for details.
type Board ¶
type Board struct { Hash uint64 Pieces [2][6]BBoard Occupancy [3]BBoard Side int Phase int EnPassantTarget Square HalfMoveCounter uint8 FullMoveCounter uint8 CastlingRights CastlingRights InCheck bool }
0 empty 1 white pawn 2 wb 3 wk 4 wr 5 wq 6 wk 7 bp 8 bb 9 bk 10 br 11 bq 12 bk.
func (*Board) AttackedSquares ¶
Get a bitboard of all the squares attacked by the opposition.
func (*Board) CompleteCastling ¶
Make the complimentary rook move when castling.
func (*Board) GetBitBoard ¶
Return a pointer to the bitboard of the piece moved.
func (*Board) GetChecksBB ¶
Get checkers and check attack vectors and true if the check is a double check. A zero bitboard indicates no check. Slider piece checks return a bitboard containing squares that are legal destinations which either capture the checker or block its attack. A knight checker returns only the position of the knight to be captured as blocking is impossible unlike sliding pieces. In case of a double check only the king can move and the resulting bitboard can not be used for determining the legality of other piece moves.
func (*Board) GetGamePhase ¶ added in v1.1.0
Determine the game phase as a sliding factor between opening and endgame https://www.chessprogramming.org/Tapered_Eval#Implementation_example
func (*Board) GetPinsBB ¶
Get pinned piece square and pin attack mask which are the only legal destination squares for the pinned piece. Basic assumptions: A piece can only be pinned by one attacker. An attacker that delivers check can not also pin a piece. A knight can never unpin itself. A pinned knight has no legal moves. A bishop can not unpin itself from rook attacks and vice versa.
func (*Board) GetUnmake ¶
func (b *Board) GetUnmake() func()
Generate a function to return the board state the it's current state.
func (*Board) InsufficentMaterial ¶
Determine if there is a draw by insufficient material This determines theoretical possibility of mate. Not KvKNN, which still can be achieved as a 'help mate'.
func (*Board) IsAttacked ¶
Determine if a square is attacked by the opposing side.
func (*Board) IsPawnOnly ¶
Determine if the game only consists of pawns and kings.
func (*Board) MakeMove ¶
Make a legal move in position and update board state - castling rights, en passant, move count, side to move etc. Returns a function to take back the move made.
func (*Board) MakeNullMove ¶
func (b *Board) MakeNullMove() func()
func (*Board) MoveGenKing ¶
Return king moves for the current side to move.
func (*Board) MoveGenLegal ¶
Generate all legal moves for the current side to move.
func (*Board) PerftDebug ¶
func (*Board) PieceAtSquare ¶
Get the piece at square as a collection of values: found, color, piece.
func (*Board) PlayMovesUCI ¶
Play out a line of UCI moves in succession. Returns success.
func (*Board) PseudoCaptureAndQueenPromoGen ¶
func (*Board) PseudoMoveGen ¶
func (*Board) RemoveCaptured ¶
Remove a piece captured by a move from the opposing bitboard.
func (*Board) ZobristCapture ¶
func (*Board) ZobristCastling ¶
func (b *Board) ZobristCastling(right CastlingRights)
Update Zobrist hash with castling move.
func (*Board) ZobristCastlingRights ¶
func (b *Board) ZobristCastlingRights(right CastlingRights)
Update Zobrist hash with castling rights.
func (*Board) ZobristEPCapture ¶
func (*Board) ZobristEnPassant ¶
Update Zobrist hash with En Passant square.
func (*Board) ZobristPromotion ¶
Update Zobrist hash when promoting a piece.
func (*Board) ZobristSideToMove ¶
func (b *Board) ZobristSideToMove()
Update Zobirst hash with flipping side to move.
func (*Board) ZobristSimpleMove ¶
Incrementally update Zborist hash after a move.
type CastlingRights ¶
type CastlingRights byte
type Move ¶
type Move uint32
LSB 0..5 from 6..11 to 12..14 promotion 15 IsEnpassant 16 IsCapture 17 IsCastling 18 IsDouble 19..21 Piece 22..31 unused MSB.