board

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2023 License: GPL-3.0 Imports: 6 Imported by: 2

Documentation

Index

Constants

View Source
const (
	IsEnpassant = 1 << 15
	IsCapture   = 1 << 16
	IsCastling  = 1 << 17
	IsDouble    = 1 << 18
	PieceMask   = 1<<3 - 1
	PieceShift  = 19
)
View Source
const (
	P uint8
	B
	N
	R
	Q
	K
)
View Source
const (
	WhiteToMove byte = 'w'
	BlackToMove byte = 'b'
)
View Source
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.

View Source
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

View Source
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,
	}
)
View Source
var (
	BishopAttackMasks [64]BBoard
	RookAttackMasks   [64]BBoard
	BishopAttacks     [64][4096]BBoard
	RookAttacks       [64][4096]BBoard
)
View Source
var (
	PassedPawns   [2][64]BBoard
	IsolatedPawns [64]BBoard
	DoubledPawns  [64]BBoard
)
View Source
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")
)
View Source
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 InitKingAttacks

func InitKingAttacks()

Initialize King move lookup table.

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 InitKnightAttacks

func InitKnightAttacks()

Initialize Knight attack lookup table.

func InitOccBitCounts

func InitOccBitCounts()

Calculate and initialize the number of relevant occupancies for Bishops and Rooks for all squares.

func InitPawnAttacks

func InitPawnAttacks()

Initialize pawn attack lookup table.

func InitPawnStrucutreMasks

func InitPawnStrucutreMasks()

func InitSliders

func InitSliders()

Initialize sliding piece lookup tables with magic numbers.

func InitSquares

func InitSquares()

func Perft

func Perft(fen string, depth int) (int64, time.Duration)

Types

type BBoard

type BBoard uint64
const (
	MajorDiag BBoard = 0x8142241818244281
	MinorDiag BBoard = 0x42a55a3c3c5aa542
)

func BishopAttacksWithBlocker

func BishopAttacksWithBlocker(sq int, blocker BBoard) BBoard

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

func BishopRelOcc(sq int) BBoard

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

func FindMagicNumber(sq, bitCount int, isBishop bool) BBoard

Find magic numbers with a brute force approach for a square.

func GetBishopAttacks

func GetBishopAttacks(sq int, occ BBoard) BBoard

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

func GetQueenAttacks(sq int, occ BBoard) BBoard

Get Queen attacks as a Bishop and Rook superposition.

func GetRookAttacks

func GetRookAttacks(sq int, occ BBoard) BBoard

Get Rook attack mask with blocker occupancy.

func Occupancy

func Occupancy(index, count int, attack BBoard) BBoard

Generate occupancy bitboards for a given relevant occupancy bitboard.

func RookAttacksWithBlocker

func RookAttacksWithBlocker(sq int, blocker BBoard) BBoard

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

func RookRelOcc(sq int) BBoard

Generate rook relevant occupancy. See Bishop equivalent for details.

func (*BBoard) Clear

func (bb *BBoard) Clear(sq int)

Set a bit to zero at position.

func (BBoard) Count

func (bb BBoard) Count() int

Return population count (number of 1's).

func (BBoard) Flip added in v1.3.0

func (bb BBoard) Flip() BBoard

func (*BBoard) Get

func (bb *BBoard) Get(sq int) BBoard

Get the bit at position.

func (BBoard) LS1B

func (bb BBoard) LS1B() int

Get the position of the Least Significant.

func (*BBoard) PopLS1B

func (bb *BBoard) PopLS1B() int

func (*BBoard) Set

func (bb *BBoard) Set(sq int)

Set a bit to one at position.

func (BBoard) String

func (bb BBoard) String() string

Get a human readable string represantiation of a bitboard.

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 NewBoard

func NewBoard(position string) *Board

func (*Board) AttackedSquares

func (b *Board) AttackedSquares(side int, occ BBoard) BBoard

Get a bitboard of all the squares attacked by the opposition.

func (*Board) CompleteCastling

func (b *Board) CompleteCastling(move Move)

Make the complimentary rook move when castling.

func (*Board) Copy

func (b *Board) Copy() *Board

func (*Board) ExportFEN

func (b *Board) ExportFEN() string

func (*Board) Flip added in v1.3.0

func (b *Board) Flip()

func (*Board) GetBitBoard

func (b *Board) GetBitBoard(piece int) *BBoard

Return a pointer to the bitboard of the piece moved.

func (*Board) GetChecksBB

func (b *Board) GetChecksBB(side int) (BBoard, bool)

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

func (b *Board) GetGamePhase() int

Determine the game phase as a sliding factor between opening and endgame https://www.chessprogramming.org/Tapered_Eval#Implementation_example

func (*Board) GetPinsBB

func (b *Board) GetPinsBB(side int) map[int]BBoard

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) ImportFEN

func (b *Board) ImportFEN(fen string) error

func (*Board) InsufficentMaterial

func (b *Board) InsufficentMaterial() bool

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

func (b *Board) IsAttacked(sq, side int, occ BBoard) bool

Determine if a square is attacked by the opposing side.

func (*Board) IsChecked

func (b *Board) IsChecked(side int) bool

Determine if the king for the given side is in check.

func (*Board) IsPawnOnly

func (b *Board) IsPawnOnly() bool

Determine if the game only consists of pawns and kings.

func (*Board) MakeMove

func (b *Board) MakeMove(move Move) func()

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

func (b *Board) MoveGenKing() []Move

Return king moves for the current side to move.

func (*Board) MoveGenLegal

func (b *Board) MoveGenLegal() []Move

Generate all legal moves for the current side to move.

func (*Board) MoveUCI

func (b *Board) MoveUCI(uciMove string) (func(), bool)

Attempt to play a UCI move in position. Returns unmake closure and ok.

func (*Board) PerftDebug

func (b *Board) PerftDebug(depth int)

func (*Board) PieceAtSquare

func (b *Board) PieceAtSquare(sq Square) int

Get the piece at square as a collection of values: found, color, piece.

func (*Board) PlayMovesUCI

func (b *Board) PlayMovesUCI(uciMoves string) bool

Play out a line of UCI moves in succession. Returns success.

func (*Board) Promote

func (b *Board) Promote(move Move)

Replace a pawn on the 8th/1st rank with the promotion piece.

func (*Board) PseudoCaptureAndQueenPromoGen

func (b *Board) PseudoCaptureAndQueenPromoGen() []Move

func (*Board) PseudoMoveGen

func (b *Board) PseudoMoveGen() []Move

func (*Board) RemoveCaptured

func (b *Board) RemoveCaptured(sq int)

Remove a piece captured by a move from the opposing bitboard.

func (*Board) SeedHash

func (b *Board) SeedHash() uint64

Calculate Zborist hash of the position.

func (*Board) ZobristCapture

func (b *Board) ZobristCapture(move Move, piece int)

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 (b *Board) ZobristEPCapture(move Move)

func (*Board) ZobristEnPassant

func (b *Board) ZobristEnPassant(square Square)

Update Zobrist hash with En Passant square.

func (*Board) ZobristPromotion

func (b *Board) ZobristPromotion(move Move)

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

func (b *Board) ZobristSimpleMove(move Move, piece int)

Incrementally update Zborist hash after a move.

type CastlingRights

type CastlingRights byte
const (
	WOO CastlingRights = 1 << iota
	WOOO
	BOO
	BOOO
	CASTLING_ALL = WOO | WOOO | BOO | BOOO
)

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.

func MoveFromString

func MoveFromString(s string) Move

func (Move) From

func (m Move) From() Square

func (Move) FromTo

func (m Move) FromTo() (Square, Square)

func (Move) IsCapture

func (m Move) IsCapture() bool

func (Move) IsCastling

func (m Move) IsCastling() bool

func (Move) IsDouble

func (m Move) IsDouble() bool

func (Move) IsEnPassant

func (m Move) IsEnPassant() bool

func (Move) Piece

func (m Move) Piece() uint8

func (Move) Promotion

func (m Move) Promotion() uint8

func (Move) SetPromotion

func (m Move) SetPromotion(prom uint8) Move

func (Move) String

func (m Move) String() string

func (Move) To

func (m Move) To() Square

type PieceSet

type PieceSet struct {
	Kings   BBoard
	Pawns   BBoard
	Knights BBoard
	Bishops BBoard
	Rooks   BBoard
	Queens  BBoard
}

type Square

type Square int

0..7 a8 to h8 0..63 to a8 to h1 mapping.

func SquareFromString

func SquareFromString(s string) Square

func (Square) String

func (s Square) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL