engine

package
v0.0.0-...-ce2a3d9 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// The size of a polyglot entry
	EntryByteLength = 16

	// Each masks helps to extract the correct bits
	// from the move part of a polyglot entry.
	ToFileMask         uint16 = 0x7
	ToRankMask         uint16 = 0x38
	FromFileMask       uint16 = 0x1C0
	FromRankMask       uint16 = 0xE00
	PromotionPieceMask uint16 = 0x7000

	// Each shift works together with a move
	// mask above to extract the correct number
	// from the move part of a polyglot entry
	// by shifting the masked bits to the least
	// significant end of a bitstring.
	ToRankShift         = 3
	FromFileShift       = 6
	FromRankShift       = 9
	PromotionPieceShift = 12

	// Hardcoded indexes into the array of random 64-bit numbers used
	// to create a polyglot hash.
	CastleWKSHash = 768
	CastleWQSHash = 769
	CastleBKSHash = 770
	CastleBQSHash = 771
	EnPassant     = 772
	SideToMove    = 780
)
View Source
const (
	EngineName   = "Blunder 8.5.5"
	EngineAuthor = "Christian Dean"
	EngineEmail  = "[email protected]"


	PerftDepthLimit = 255
	HelpMessage     = `` /* 425-byte string literal not displayed */

)
View Source
const (
	// Constants which map a piece to how much weight it should have on the phase of the game.
	PawnPhase   int16 = 0
	KnightPhase int16 = 1
	BishopPhase int16 = 1
	RookPhase   int16 = 2
	QueenPhase  int16 = 4
	TotalPhase  int16 = PawnPhase*16 + KnightPhase*4 + BishopPhase*4 + RookPhase*4 + QueenPhase*2

	// Constants representing a draw or infinite (checkmate) value.
	Inf int16 = 10000

	// A constant to scale the evaluation score by if the position is considered
	// drawish (e.g. king and queen vs king and queen).
	ScaleFactor int16 = 16

	// A constant representing a draw value.
	Draw int16 = 0
)
View Source
const (
	// Constants represeting the four possible move types.
	Quiet     uint8 = 0
	Attack    uint8 = 1
	Castle    uint8 = 2
	Promotion uint8 = 3

	// Constants representing move flags indicating what kind of promotion
	// is occuring.
	KnightPromotion uint8 = 0
	BishopPromotion uint8 = 1
	RookPromotion   uint8 = 2
	QueenPromotion  uint8 = 3

	// A constant representing a move flag indicating an attack is an en passant
	// attack.
	AttackEP uint8 = 1

	// A constant representing a null flag
	NoFlag uint8 = 0
)
View Source
const (
	// These masks help determine whether or not the squares between
	// the king and it's rooks are clear for castling
	F1_G1, B1_C1_D1 = 0x600000000000000, 0x7000000000000000
	F8_G8, B8_C8_D8 = 0x6, 0x70
)
View Source
const (
	// Constants representing each piece type. The value of the constants
	// are selected so they can be used in Position.Pieces to index the
	// bitboards representing the given piece.
	Pawn   uint8 = 0
	Knight uint8 = 1
	Bishop uint8 = 2
	Rook   uint8 = 3
	Queen  uint8 = 4
	King   uint8 = 5
	NoType uint8 = 6

	// Constants representing each piece color. The value of the constants
	// are selected so they can be used in Position.Pieces and Position.SideBB to
	// index the bitboards representing the given piece of the given color, or
	// the given color.
	Black   uint8 = 0
	White   uint8 = 1
	NoColor uint8 = 2

	// Constants representing the four castling rights. Each constant is set to a number
	// with a single high bit, corresponding to each castling right.
	WhiteKingsideRight  uint8 = 0x8
	WhiteQueensideRight uint8 = 0x4
	BlackKingsideRight  uint8 = 0x2
	BlackQueensideRight uint8 = 0x1

	// Constants mapping each board coordinate to its square
	A1, B1, C1, D1, E1, F1, G1, H1 = 0, 1, 2, 3, 4, 5, 6, 7
	A2, B2, C2, D2, E2, F2, G2, H2 = 8, 9, 10, 11, 12, 13, 14, 15
	A3, B3, C3, D3, E3, F3, G3, H3 = 16, 17, 18, 19, 20, 21, 22, 23
	A4, B4, C4, D4, E4, F4, G4, H4 = 24, 25, 26, 27, 28, 29, 30, 31
	A5, B5, C5, D5, E5, F5, G5, H5 = 32, 33, 34, 35, 36, 37, 38, 39
	A6, B6, C6, D6, E6, F6, G6, H6 = 40, 41, 42, 43, 44, 45, 46, 47
	A7, B7, C7, D7, E7, F7, G7, H7 = 48, 49, 50, 51, 52, 53, 54, 55
	A8, B8, C8, D8, E8, F8, G8, H8 = 56, 57, 58, 59, 60, 61, 62, 63

	// A constant representing no square
	NoSq = 64

	// Common fen strings used in debugging and initalizing the engine.
	FENStartPosition = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0"
	FENKiwiPete      = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1"
)
View Source
const (
	// The maximum depth the engine will attempt to reach.
	MaxDepth = 100

	// A constant representing no move.
	NullMove Move = 0

	// A constant representing the score of the principal variation
	// move from the transposition table.
	PVMoveScore uint16 = 65

	// A constant representing the score offsets of the killer moves.
	FirstKillerMoveScore  uint16 = 10
	SecondKillerMoveScore uint16 = 20

	// A constant representing the maximum number of killers.
	MaxKillers = 2

	// A constant to offset the score of the pv and MVV-LVA move higher
	// than killers and history heuristic moves.
	MvvLvaOffset uint16 = math.MaxUint16 - 256

	// A constant representing the maximum value a history heuristic score
	// is allowed to reach. This ensures history scores stay well below
	// pv moves, captures, and killer moves.
	MaxHistoryScore int32 = int32(MvvLvaOffset - 30)

	// A bonus given to moves scored by history that refuted
	// the previous move played by causing a beta-cutoff
	CounterMoveBonus uint16 = 5

	// A constant representing the maximum game ply,
	// used to initalize the array for holding repetition
	// detection history.
	MaxGamePly = 1024

	// Pruning constants
	NMR_Depth_Limit                 int8  = 2
	FutilityPruningDepthLimit       int8  = 8
	StaticNullMovePruningBaseMargin int16 = 85
	LMRLegalMovesLimit              int   = 4
	LMRDepthLimit                   int8  = 3
	WindowSize                      int16 = 35
	IID_Depth_Reduction             int8  = 2
	IID_Depth_Limit                 int8  = 4
	SingularMoveMargin              int16 = 125
	SingularExtensionDepthLimit     int8  = 4
	SingularMoveExtension           int8  = 1
)
View Source
const (
	Rank1 uint8 = iota
	Rank2
	Rank3
	Rank4
	Rank5
	Rank6
	Rank7
	Rank8
)
View Source
const (
	FileA uint8 = iota
	FileB
	FileC
	FileD
	FileE
	FileF
	FileG
	FileH
)
View Source
const (
	North uint8 = 8
	South uint8 = 8
	East  uint8 = 1
	West  uint8 = 1
)
View Source
const (
	NoValue      int64 = 0
	InfiniteTime int64 = -1
)
View Source
const (
	// Default size of the transposition table, in MB.
	DefaultTTSize = 64

	// The number of buckets to have per transposition table index.
	NumTTBuckets = 2

	// Constant for the size of a transposition table search and perft entry, in bytes,
	// considering memory alignment.
	SearchEntrySize uint64 = 16
	PerftEntrySize  uint64 = 24

	// Constants representing the different flags for a transposition table entry,
	// which determine what kind of entry it is. If the entry has a score from
	// a fail-low node (alpha wasn't raised), it's an alpha entry. If the entry has
	// a score from a fail-high node (a beta cutoff occured), it's a beta entry. And
	// if the entry has an exact score (alpha was raised), it's an exact entry.
	AlphaFlag uint8 = 1
	BetaFlag  uint8 = 2
	ExactFlag uint8 = 3

	// A constant representing the minimum or maximum value that a score from the search
	// must be below or above to be a checkmate score. The score assumes that the engine
	// will not find mate in 100.
	Checkmate = 9000
)
View Source
const (
	// A constant representing the value to use when seeding the random
	// numbers generated for zobrist hashing.
	ZobristSeedValue = 1

	// A constant which represents when there is no ep square. This value indexes
	// into _Zobrist.epFileRand64 to return a 0, which will not affect the zobrist
	// hash.
	NoEPFile = 8
)
View Source
const DefaultBookMoveDelay = 2
View Source
const MaxMoves = 255

Variables

View Source
var BishopMagics [64]Magic
View Source
var BishopMoves [64][512]Bitboard
View Source
var BishopOutPostBonusEG int16 = 14
View Source
var BishopOutPostBonusMG int16 = 10
View Source
var BishopPairBonusEG int16 = 30
View Source
var BishopPairBonusMG int16 = 22
View Source
var CastlingRookSq = map[uint8]RookCastlePositions{
	G1: {H1, F1},
	C1: {A1, D1},
	G8: {H8, F8},
	C8: {A8, D8},
}

An array mapping a castling kings destination square, to the origin and destination square of the appropriate rook to move.

View Source
var CharToPiece = map[byte]Piece{
	'P': {Pawn, White},
	'N': {Knight, White},
	'B': {Bishop, White},
	'R': {Rook, White},
	'Q': {Queen, White},
	'K': {King, White},
	'p': {Pawn, Black},
	'n': {Knight, Black},
	'b': {Bishop, Black},
	'r': {Rook, Black},
	'q': {Queen, Black},
	'k': {King, Black},
}

A constant mapping piece characters to Piece objects.

View Source
var CharToPieceType map[rune]uint8 = map[rune]uint8{
	'N': Knight,
	'B': Bishop,
	'R': Rook,
	'Q': Queen,
	'K': King,
}
View Source
var ClearFile = [8]Bitboard{}
View Source
var ClearRank = [8]Bitboard{}
View Source
var DoubledPawnMasks [2][64]Bitboard
View Source
var DoubledPawnPenatlyEG int16 = 16
View Source
var DoubledPawnPenatlyMG int16 = 1
View Source
var FlipSq [2][64]int = [2][64]int{
	{
		0, 1, 2, 3, 4, 5, 6, 7,
		8, 9, 10, 11, 12, 13, 14, 15,
		16, 17, 18, 19, 20, 21, 22, 23,
		24, 25, 26, 27, 28, 29, 30, 31,
		32, 33, 34, 35, 36, 37, 38, 39,
		40, 41, 42, 43, 44, 45, 46, 47,
		48, 49, 50, 51, 52, 53, 54, 55,
		56, 57, 58, 59, 60, 61, 62, 63,
	},

	{
		56, 57, 58, 59, 60, 61, 62, 63,
		48, 49, 50, 51, 52, 53, 54, 55,
		40, 41, 42, 43, 44, 45, 46, 47,
		32, 33, 34, 35, 36, 37, 38, 39,
		24, 25, 26, 27, 28, 29, 30, 31,
		16, 17, 18, 19, 20, 21, 22, 23,
		8, 9, 10, 11, 12, 13, 14, 15,
		0, 1, 2, 3, 4, 5, 6, 7,
	},
}

Flip white's perspective to black

View Source
var FutilityMargins = [9]int16{
	0,
	100,
	160,
	220,
	280,
	340,
	400,
	460,
	520,
}

Futility margins

View Source
var InnerRingAttackPoints = [5]int16{0, 3, 4, 3, 2}
View Source
var IsolatedPawnMasks [8]Bitboard
View Source
var IsolatedPawnPenatlyEG int16 = 6
View Source
var IsolatedPawnPenatlyMG int16 = 17
View Source
var KingMoves = [64]Bitboard{}
View Source
var KingZones [64]KingZone
View Source
var KnightMoves = [64]Bitboard{}
View Source
var KnightOnOutpostBonusEG int16 = 18
View Source
var KnightOnOutpostBonusMG int16 = 27
View Source
var LMR = [MaxDepth + 1][100]int8{}

Precomputed reductions

View Source
var LateMovePruningMargins = [6]int{0, 8, 12, 16, 20, 24}

Late-move pruning margins

View Source
var MagicSeeds [8]uint64 = [8]uint64{728, 10316, 55013, 32803, 12281, 15100, 16645, 255}

Optimized seeding values to find magic numbers based on file/rank, from Stockfish:

View Source
var MaskAntidiagonal = [15]Bitboard{
	0x1,
	0x102,
	0x10204,
	0x1020408,
	0x102040810,
	0x10204081020,
	0x1020408102040,
	0x102040810204080,
	0x204081020408000,
	0x408102040800000,
	0x810204080000000,
	0x1020408000000000,
	0x2040800000000000,
	0x4080000000000000,
	0x8000000000000000,
}
View Source
var MaskDiagonal = [15]Bitboard{
	0x80,
	0x8040,
	0x804020,
	0x80402010,
	0x8040201008,
	0x804020100804,
	0x80402010080402,
	0x8040201008040201,
	0x4020100804020100,
	0x2010080402010000,
	0x1008040201000000,
	0x804020100000000,
	0x402010000000000,
	0x201000000000000,
	0x100000000000000,
}
View Source
var MaskFile = [8]Bitboard{}
View Source
var MaskRank = [8]Bitboard{}
View Source
var MvvLva [7][6]uint16 = [7][6]uint16{
	{15, 14, 13, 12, 11, 10},
	{25, 24, 23, 22, 21, 20},
	{35, 34, 33, 32, 31, 30},
	{45, 44, 43, 42, 41, 40},
	{55, 54, 53, 52, 51, 50},

	{0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0},
}

An array that maps move scores to attacker and victim piece types for MVV-LVA move ordering: https://www.chessprogramming.org/MVV-LVA.

View Source
var OuterRingAttackPoints = [5]int16{0, 1, 0, 1, 1}
View Source
var OutpostMasks [2][64]Bitboard
View Source
var PSQT_EG [6][64]int16 = [6][64]int16{
	{

		0, 0, 0, 0, 0, 0, 0, 0,
		77, 74, 63, 53, 59, 60, 72, 77,
		17, 11, 11, 11, 11, -6, 14, 8,
		-3, -14, -18, -31, -29, -25, -20, -18,
		-12, -14, -24, -31, -29, -28, -27, -28,
		-22, -20, -25, -20, -21, -24, -34, -34,
		-16, -22, -11, -19, -13, -23, -32, -34,
		0, 0, 0, 0, 0, 0, 0, 0,
	},
	{

		-36, -16, -7, -14, -4, -20, -20, -29,
		-17, 2, -7, 14, 2, -7, -9, -19,
		-13, -7, 14, 12, 4, 6, 0, -13,
		-5, 8, 24, 18, 22, 15, 11, -4,
		-3, 4, 20, 30, 22, 25, 15, -2,
		-7, 1, 3, 19, 10, -2, -4, -4,
		-10, -2, -1, 0, 6, -8, -3, -13,
		-12, -28, -8, 1, -5, -12, -27, -12,
	},
	{

		-9, -5, -9, -5, -2, -4, -5, -8,
		0, 2, 8, -7, 1, 0, -2, -8,
		8, 0, 0, 1, 0, 1, 5, 6,
		0, 7, 7, 8, 3, 5, 2, 6,
		-1, 0, 12, 8, 0, 6, 0, -5,
		0, 0, 3, 6, 8, -1, 0, -1,
		-6, -12, -7, 0, 0, -8, -9, -13,
		-11, 0, -6, 0, -3, -4, -5, -9,
	},
	{

		8, 9, 11, 13, 13, 12, 13, 9,
		3, 5, 1, 0, -1, 0, 6, 2,
		9, 5, 7, 2, 2, 1, 0, 0,
		3, 3, 6, 0, 0, 0, 0, 4,
		5, 4, 9, 0, -3, -2, -6, -2,
		0, 0, -6, -5, -9, -14, -7, -12,
		-2, -5, -1, -7, -9, -11, -13, -1,
		-7, -3, 0, -8, -13, -12, -4, -24,
	},
	{

		-12, 4, 8, 4, 10, 9, 3, 6,
		-17, -7, -1, 7, 3, 6, 1, 0,
		-5, -1, -4, 12, 14, 20, 12, 14,
		-2, 2, 2, 9, 13, 7, 18, 22,
		-9, 3, 1, 15, 5, 10, 12, 10,
		-6, -20, 0, -15, 0, -1, 10, 7,
		-6, -14, -31, -27, -19, -12, -11, -4,
		-12, -22, -19, -30, -8, -13, -6, -15,
	},
	{

		-15, -11, -11, -6, -2, 3, 4, -9,
		-9, 14, 11, 13, 13, 28, 19, 1,
		-1, 18, 19, 15, 16, 35, 34, 4,
		-12, 14, 21, 25, 19, 25, 18, -5,
		-23, -6, 14, 21, 20, 18, 5, -16,
		-21, -6, 5, 13, 15, 9, -2, -12,
		-27, -10, 2, 9, 9, 1, -12, -26,
		-43, -34, -20, -5, -26, -9, -35, -55,
	},
}
View Source
var PSQT_MG = [6][64]int16{
	{

		0, 0, 0, 0, 0, 0, 0, 0,
		45, 52, 42, 43, 28, 34, 19, 9,
		-14, -3, 7, 14, 35, 50, 15, -6,
		-27, -6, -8, 13, 16, 4, -3, -25,
		-32, -28, -7, 5, 7, -1, -15, -30,
		-29, -25, -12, -12, -1, -5, 6, -17,
		-34, -23, -27, -18, -14, 10, 13, -22,
		0, 0, 0, 0, 0, 0, 0, 0,
	},
	{

		-43, -11, -8, -5, 1, -20, -4, -22,
		-31, -22, 19, 7, 5, 13, -8, -11,
		-21, 21, 8, 16, 36, 33, 19, 6,
		-6, 2, 0, 23, 8, 27, 4, 14,
		-3, 10, 12, 8, 16, 10, 19, 1,
		-19, -4, 3, 7, 22, 12, 15, -11,
		-21, -20, -9, 8, 9, 11, -5, 0,
		-19, -13, -20, -14, -2, 3, -11, -8,
	},
	{

		-13, 0, -17, -8, -7, -5, -2, -3,
		-21, 0, -16, -10, 4, 1, -6, -41,
		-23, 6, 10, 8, 8, 26, 0, -10,
		-15, -4, 2, 22, 9, 10, -1, -16,
		0, 10, -2, 15, 17, -7, -1, 13,
		-2, 16, 13, 0, 5, 16, 14, 0,
		8, 11, 12, 3, 11, 23, 27, 3,
		-26, 3, -3, -1, 10, -5, -7, -15,
	},
	{

		3, 1, 0, 7, 7, -1, 0, 0,
		-6, -9, 7, 7, 7, 5, -4, -1,
		-12, 11, 0, 17, -2, 12, 23, -1,
		-17, -9, 4, 0, 3, 15, -1, -2,
		-24, -16, -16, -4, -1, -14, 2, -20,
		-30, -15, -6, -3, 0, 2, 2, -15,
		-25, -6, -6, 5, 8, 6, 8, -46,
		-3, 1, 6, 15, 17, 14, -13, -2,
	},
	{

		-10, 0, 0, 0, 10, 9, 5, 7,
		-19, -35, -5, 2, -9, 7, 1, 15,
		-10, -7, -4, -9, 15, 29, 24, 22,
		-14, -14, -15, -11, -1, -5, 3, -6,
		-8, -20, -8, -5, -4, -2, 2, -2,
		-13, 5, 2, 1, -1, 8, 4, 2,
		-20, 0, 10, 16, 16, 16, -6, 6,
		-3, -1, 7, 19, 5, -10, -9, -17,
	},
	{

		-3, 0, 2, 0, 0, 0, 1, -1,
		1, 4, 0, 7, 4, 2, 3, -2,
		2, 4, 7, 4, 4, 14, 12, 0,
		0, 2, 6, 0, 0, 2, 6, -9,
		-8, 5, 0, -8, -10, -10, -9, -23,
		-3, 5, 1, -8, -12, -12, 8, -24,
		6, 13, 0, -40, -23, -1, 25, 19,
		-28, 29, 17, -53, 2, -25, 34, 15,
	},
}
View Source
var PassedPawnMasks [2][64]Bitboard
View Source
var PassedPawnPSQT_EG = [64]int16{
	0, 0, 0, 0, 0, 0, 0, 0,
	77, 74, 63, 53, 59, 60, 72, 77,
	91, 83, 66, 40, 30, 61, 67, 84,
	55, 52, 42, 35, 30, 34, 56, 52,
	29, 26, 21, 18, 17, 19, 34, 30,
	8, 6, 5, 1, 1, -1, 14, 7,
	2, 3, -4, 0, -2, -1, 7, 6,
	0, 0, 0, 0, 0, 0, 0, 0,
}
View Source
var PassedPawnPSQT_MG = [64]int16{
	0, 0, 0, 0, 0, 0, 0, 0,
	45, 52, 42, 43, 28, 34, 19, 9,
	48, 43, 43, 30, 24, 31, 12, 2,
	28, 17, 13, 10, 10, 19, 6, 1,
	14, 0, -9, -7, -13, -7, 9, 16,
	5, 3, -3, -14, -3, 10, 13, 19,
	8, 9, 2, -8, -3, 8, 16, 9,
	0, 0, 0, 0, 0, 0, 0, 0,
}
View Source
var PawnAttacks = [2][64]Bitboard{}
View Source
var PawnPushes = [2][64]Bitboard{}
View Source
var PieceMobilityEG = [5]int16{0, 2, 3, 2, 6}
View Source
var PieceMobilityMG = [5]int16{0, 5, 3, 3, 0}
View Source
var PieceTypeToChar = map[uint8]rune{
	Pawn:   'i',
	Knight: 'n',
	Bishop: 'b',
	Rook:   'r',
	Queen:  'q',
	King:   'k',
	NoType: '.',
}

A constant mapping piece types to their respective characters.

View Source
var PieceValueEG = [6]int16{106, 244, 268, 478, 886}
View Source
var PieceValueMG = [6]int16{84, 333, 346, 441, 921}
View Source
var PieceValues [7]int16 = [7]int16{
	100,
	300,
	300,
	500,
	900,
	Inf,
	0,
}
View Source
var PossibleEPFiles [65]uint8 = [65]uint8{
	8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8,
	0, 1, 2, 3, 4, 5, 6, 7,
	8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8,
	0, 1, 2, 3, 4, 5, 6, 7,
	8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8,
	8,
}

Precomputing all possible en passant file numbers is much more efficent for Blunder than calculating them on the fly.

View Source
var Random64 [781]uint64 = [781]uint64{}/* 781 elements not displayed */
View Source
var RookMagics [64]Magic
View Source
var RookMoves [64][4096]Bitboard
View Source
var RookOnOpenFileBonusMG int16 = 23
View Source
var RookOrQueenOnSeventhBonusEG int16 = 23

A middlegame equivelent of this bonus is not missing, and one was used at first. But several thousand iterations of the tuner indicated that such a "bonus" was actually quite bad to give in the middlegame.

View Source
var SemiOpenFileNextToKingPenalty int16 = 4
View Source
var Spoilers = [64]uint8{
	0xb, 0xf, 0xf, 0xf, 0x3, 0xf, 0xf, 0x7,
	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
	0xe, 0xf, 0xf, 0xf, 0xc, 0xf, 0xf, 0xd,
}

A 64 element array where each entry, when bitwise ANDed with the castling rights, destorys the correct bit in the castling rights if a move to or from that square would take away castling rights.

View Source
var SquareBB [65]Bitboard

A global constant where each entry represents a square on the chess board, and each entry contains a bitboard with the bit set high at that square. An extra entry is given so that the invalid square constant NoSq can be indexed into the table without the program crashing.

View Source
var TempoBonusMG int16 = 14
View Source
var Zobrist _Zobrist

A constant which will be a singleton of the _Zobrist struct below, since only one instance is ever needed.

Functions

func DividePerft

func DividePerft(pos *Position, depth, divdeAt uint8, TT *TransTable[PerftEntry]) uint64

Explore the move tree up to depth, and return the total number of nodes explored. This function is used to debug move generation and ensure it is working by comparing the results to the known results of other engines

func EvaluatePos

func EvaluatePos(pos *Position) int16

Evaluate a position and give a score, from the perspective of the side to move ( more positive if it's good for the side to move, otherwise more negative).

func FileOf

func FileOf(sq uint8) uint8

Given a board square, return it's file.

func GenPolyglotHash

func GenPolyglotHash(pos *Position) (hash uint64)

Create an initial zobrist hash for a board loaded from a fen string.

func InitBitboards

func InitBitboards()

Initalize the bitboard constants.

func InitEvalBitboards

func InitEvalBitboards()

func InitSearchTables

func InitSearchTables()

func InitTables

func InitTables()

func InitZobrist

func InitZobrist()

func LoadPolyglotFile

func LoadPolyglotFile(path string) (map[uint64][]PolyglotEntry, error)

Parse a polyglot file and create a map of PolyglotEntry's from it. Each zobrist hash for an entry maps to the moves and weight of the entry.

func Min

func Min[Int constraints.Integer](a, b Int) Int

Get the minimum between two integers.

func Perft

func Perft(pos *Position, depth uint8, TT *TransTable[PerftEntry]) uint64

Same as divide perft but doesn't print subnode count for each move, only the final total.

func RankOf

func RankOf(sq uint8) uint8

Given a board square, return it's rank.

func RunCommLoop

func RunCommLoop()

Types

type Bitboard

type Bitboard uint64

A type representing a bitboard, which is a unsigned 64-bit number. Blunder's bitboard representation has the most significant bit being A1 and the least signficanrt bit being H8.

const EmptyBB Bitboard = 0x0
const FullBB Bitboard = 0xffffffffffffffff

A constant representing a bitboard with every square set and every square empty.

func GenBishopMoves

func GenBishopMoves(sq uint8, blockers Bitboard) Bitboard

Generate rook moves.

func GenRookMoves

func GenRookMoves(sq uint8, blockers Bitboard) Bitboard

Generate rook moves.

func (Bitboard) BitSet

func (bb Bitboard) BitSet(sq uint8) bool

Test whether the bit of the given bitbord at the given position is set.

func (*Bitboard) ClearBit

func (bitboard *Bitboard) ClearBit(sq uint8)

Clear the bit at given square.

func (Bitboard) CountBits

func (bitboard Bitboard) CountBits() int

Count the bits in a given bitboard using the SWAR-popcount algorithm for 64-bit integers.

func (Bitboard) Msb

func (bitboard Bitboard) Msb() uint8

Get the position of the MSB of the given bitboard.

func (*Bitboard) PopBit

func (bitboard *Bitboard) PopBit() uint8

Get the position of the MSB of the given bitboard, and clear the MSB.

func (*Bitboard) SetBit

func (bitboard *Bitboard) SetBit(sq uint8)

Set the bit at given square.

func (Bitboard) String

func (bitboard Bitboard) String() (bitboardAsString string)

Return a string representation of the given bitboard

type Eval

type Eval struct {
	MGScores [2]int16
	EGScores [2]int16

	KingZones        [2]KingZone
	KingAttackPoints [2]uint16
	KingAttackers    [2]uint8
}

type KingZone

type KingZone struct {
	OuterRing Bitboard
	InnerRing Bitboard
}

type Magic

type Magic struct {
	MagicNo     uint64
	BlockerMask Bitboard
	Shift       uint8
}

type Move

type Move uint32

func ConvertSANToLAN

func ConvertSANToLAN(pos *Position, moveStr string) Move

Convert a move in short algebraic notation, to the long algebraic notation used by the UCI protocol.

func NewMove

func NewMove(from, to, moveType, flag uint8) Move

Create a new move. The first 6 bits are the from square, the next 6 bits are the to square, the next two represent the move type, the next two are reserved for any speical flags needed to give full information concering the move, and the last 16-bits are used for scoring a move for move-ordering in the search phase.

func (*Move) AddScore

func (move *Move) AddScore(score uint16)

Add a score to the move for move ordering.

func (Move) Equal

func (move Move) Equal(m Move) bool

Test if two moves are equal.

func (Move) Flag

func (move Move) Flag() uint8

Get the flag of the move.

func (Move) FromSq

func (move Move) FromSq() uint8

Get the from square of the move.

func (Move) MoveType

func (move Move) MoveType() uint8

Get the type of the move.

func (Move) Score

func (move Move) Score() uint16

Get the score of a move.

func (Move) String

func (move Move) String() string

A helper function to extract the info from a move represented as 32-bits, and display it.

func (Move) ToSq

func (move Move) ToSq() uint8

Get the to square of the move.

type MoveList

type MoveList struct {
	Moves [MaxMoves]Move
	Count uint8
}

func (*MoveList) AddMove

func (moveList *MoveList) AddMove(move Move)

type PVLine

type PVLine struct {
	Moves []Move
}

A struct representing a principal variation line.

func (*PVLine) Clear

func (pvLine *PVLine) Clear()

Clear the principal variation line.

func (*PVLine) GetPVMove

func (pvLine *PVLine) GetPVMove() Move

Get the best move from the principal variation line.

func (PVLine) String

func (pvLine PVLine) String() string

Convert the principal variation line to a string.

func (*PVLine) Update

func (pvLine *PVLine) Update(move Move, newPVLine PVLine)

Update the principal variation line with a new best move, and a new line of best play after the best move.

type PerftEntry

type PerftEntry struct {
	Hash  uint64
	Nodes uint64
	Depth uint8
}

A struct for a transposition table entry used in perft.

func (*PerftEntry) Get

func (entry *PerftEntry) Get(hash uint64, depth uint8) (nodeCount uint64, ok bool)

func (PerftEntry) GetAge

func (entry PerftEntry) GetAge() uint8

func (PerftEntry) GetDepth

func (entry PerftEntry) GetDepth() uint8

func (PerftEntry) GetHash

func (entry PerftEntry) GetHash() uint64

func (*PerftEntry) Set

func (entry *PerftEntry) Set(hash uint64, depth uint8, nodes uint64)

type Piece

type Piece struct {
	Type  uint8
	Color uint8
}

A struct representing a piece

type PolyglotEntry

type PolyglotEntry struct {
	Hash   uint64
	Move   string
	Weight uint16
}

Each polyglot book is composed of a series of 16-byte entries. Each of these entries contains a key, which is the hash of the position after the current moves have been made, the moves made, the weight those moves are given (i.e. how good they are), and a learn field, which, as far as I can tell, is usually ignored and set to zero by polyglot book generators, so it's not included here. The key element of the entry is the mapping key to a PolyglotEntry.

type Position

type Position struct {
	Pieces         [2][6]Bitboard
	Sides          [2]Bitboard
	Squares        [64]Piece
	Hash           uint64
	CastlingRights uint8
	SideToMove     uint8
	EPSq           uint8
	Ply            uint16
	Rule50         uint8

	StatePly uint8

	MGScores [2]int16
	EGScores [2]int16
	Phase    int16
	// contains filtered or unexported fields
}

A struct reprenting Blunder's core internal position representation.

func (*Position) DoMove

func (pos *Position) DoMove(move Move) (isValid bool)

Given a move, create a copy of the current position with the move applied, and return whether or not the new position is valid.

func (*Position) DoNullMove

func (pos *Position) DoNullMove()

func (Position) GenFEN

func (pos Position) GenFEN() string

Generate the FEN string represention of the current board.

func (*Position) InCheck

func (pos *Position) InCheck() bool

Determine if the side to move is in check.

func (*Position) LoadFEN

func (pos *Position) LoadFEN(FEN string)

Setup the position using a fen string.

func (*Position) MoveIsPseduoLegal

func (pos *Position) MoveIsPseduoLegal(move Move) bool

Determine if a move is pseduo-legally valid.

func (*Position) NoMajorsOrMiniors

func (pos *Position) NoMajorsOrMiniors() bool

Determine if the current position has no majors or miniors left.

func (*Position) See

func (pos *Position) See(move Move) int16

Peform a static exchange evaluation on target square of the move given, and return a score of the move from the perspective of the side to move.

func (Position) String

func (pos Position) String() (boardStr string)

Return a string representation of the board.

func (*Position) UndoMove

func (pos *Position) UndoMove(move Move)

func (*Position) UndoNullMove

func (pos *Position) UndoNullMove()

type PseduoRandomGenerator

type PseduoRandomGenerator struct {
	// contains filtered or unexported fields
}

An implementation of a xorshift pseudo-random number generator for 64 bit numbers, based on the implementation by Stockfish.

func (*PseduoRandomGenerator) Random64

func (prng *PseduoRandomGenerator) Random64() uint64

Generator a random 64 bit number.

func (*PseduoRandomGenerator) Seed

func (prng *PseduoRandomGenerator) Seed(seed uint64)

Seed the generator.

func (*PseduoRandomGenerator) SparseRandom64

func (prng *PseduoRandomGenerator) SparseRandom64() uint64

Generate a random 64 bit number with few bits. This method is useful in finding magic numbers faster for generating slider attacks.

type RookCastlePositions

type RookCastlePositions struct {
	FromSq uint8
	ToSq   uint8
}

A struct representing the from and to squares of a given castling rook.

type Search struct {
	Pos   Position
	TT    TransTable[SearchEntry]
	Timer TimeManager
	// contains filtered or unexported fields
}

A struct that holds state needed during the search phase. The search routines are thus implemented as methods of this struct.

func (*Search) AddHistory

func (search *Search) AddHistory(hash uint64)

Add a zobrist hash to the history.

func (*Search) ClearCounterMoves

func (search *Search) ClearCounterMoves()

Clear the counter move table.

func (*Search) ClearHistoryTable

func (search *Search) ClearHistoryTable()

Clear the values in the history table.

func (*Search) ClearKillers

func (search *Search) ClearKillers()

Clear the killer moves table.

func (*Search) Qsearch

func (search *Search) Qsearch(alpha, beta int16, maxPly uint8, pvLine *PVLine, ply uint8) int16

Onece we reach a depth of zero in the main negamax search, instead of returning a static evaluation right away, continue to search deeper using a special form of negamax until the position is quiet (i.e there are no winning tatical captures). Doing this is known as quiescence search, and it makes the static evaluation much more accurate.

func (*Search) RemoveHistory

func (search *Search) RemoveHistory()

Remove a zobrist hash from the history.

func (*Search) Reset

func (search *Search) Reset()

Reset the necessary internals of the engine. Normally used when the "ucinewgame" command is sent.

func (*Search) Search

func (search *Search) Search() Move

The main search function for Blunder, implemented as an interative deepening loop.

func (*Search) Setup

func (search *Search) Setup(FEN string)

Setup the necessary internals of the engine when given a new FEN string.

type SearchEntry

type SearchEntry struct {
	Hash       uint64
	Depth      uint8
	Score      int16
	Best       Move
	FlagAndAge uint8
}

A struct for a transposition table entry used in the search.

func (*SearchEntry) Get

func (entry *SearchEntry) Get(hash uint64, ply, depth uint8, alpha, beta int16, best *Move) (int16, bool)

func (SearchEntry) GetAge

func (entry SearchEntry) GetAge() uint8

func (SearchEntry) GetDepth

func (entry SearchEntry) GetDepth() uint8

func (SearchEntry) GetFlag

func (entry SearchEntry) GetFlag() uint8

func (SearchEntry) GetHash

func (entry SearchEntry) GetHash() uint64

func (*SearchEntry) Set

func (entry *SearchEntry) Set(hash uint64, score int16, best Move, ply, depth, flag, age uint8)

func (*SearchEntry) SetAge

func (entry *SearchEntry) SetAge(age uint8)

func (*SearchEntry) SetFlag

func (entry *SearchEntry) SetFlag(flag uint8)

type State

type State struct {
	Hash           uint64
	CastlingRights uint8
	EPSq           uint8
	Rule50         uint8
	Captured       Piece
	Moved          Piece
}

A struct representing position state that is irreversible (cannot be undone in UnmakeMove). A position state object is used each time a move is made, and then popped off of a stack once a move needs to be unmade.

type TimeManager

type TimeManager struct {
	// Fields for UCI go command arguments
	TimeLeft     int64
	Increment    int64
	MoveTime     int64
	MovesToGo    int16
	MaxNodeCount uint64
	MaxDepth     uint8

	// Fields to calculate when the search should be stopped.
	Stop        bool
	TimeForMove int64
	// contains filtered or unexported fields
}

A struct which holds data for a timer for Blunder's time mangement.

func (*TimeManager) Check

func (tm *TimeManager) Check()

Check if the time we alloted for picking this move has expired.

func (*TimeManager) Setup

func (tm *TimeManager) Setup(timeLeft, increment, moveTime int64,
	movesToGo int16, maxDepth uint8, maxNodeCount uint64)

Setup the interals of the timer given the "go" command arguments.

func (*TimeManager) Start

func (tm *TimeManager) Start(gamePly uint16)

Start the timer, setting up the internal state.

func (*TimeManager) Update

func (tm *TimeManager) Update(newTimeForMove int64)

Update the alloted search time.

type TransTable

type TransTable[Entry interface {
	SearchEntry | PerftEntry
	GetHash() uint64
	GetAge() uint8
	GetDepth() uint8
}] struct {
	// contains filtered or unexported fields
}

A struct for a transposition table.

func (*TransTable[Entry]) Clear

func (tt *TransTable[Entry]) Clear()

Clear the transposition table

func (*TransTable[Entry]) Probe

func (tt *TransTable[Entry]) Probe(hash uint64) *Entry

Get an entry from the table to use it.

func (*TransTable[Entry]) Resize

func (tt *TransTable[Entry]) Resize(sizeInMB uint64, entrySize uint64)

Resize the transposition table given what the size should be in MB.

func (*TransTable[Entry]) Store

func (tt *TransTable[Entry]) Store(hash uint64, depth uint8, currAge uint8) *Entry

Get an entry from the table to store in it.

func (*TransTable[Entry]) Unitialize

func (tt *TransTable[Entry]) Unitialize()

Unitialize the memory used by the transposition table

type UCIInterface

type UCIInterface struct {
	Search      Search
	OpeningBook map[uint64][]PolyglotEntry

	OptionUseBook       bool
	OptionBookPath      string
	OptionBookMoveDelay int
}

func (*UCIInterface) Reset

func (inter *UCIInterface) Reset()

func (*UCIInterface) UCILoop

func (inter *UCIInterface) UCILoop()

Jump to

Keyboard shortcuts

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