engine

package
v0.0.0-...-305eda8 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 13 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
	En_Passant    = 772
	SideToMove    = 780
)
View Source
const (
	IsolatedPawnPenatlyMG int = 17
	IsolatedPawnPenatlyEG int = 6

	DoubledPawnPenatlyMG int = 1
	DoubledPawnPenatlyEG int = 16

	KnightOnOutpostBonusMG int = 27
	KnightOnOutpostBonusEG int = 18

	BishopOutPostBonusMG int = 10
	BishopOutPostBonusEG int = 14

	RookOrQueenOnSeventhBonusEG int = 23

	RookOnOpenFileBonusMG int = 23

	BishopPairBonusMG int = 22
	BishopPairBonusEG int = 30

	SemiOpenFileNextToKingPenalty int = 4

	TempoBonusMG int = 14

	DrawishScaleFactor int = 16
)
View Source
const (
	TIME_LIMIT      time.Duration = 2 * time.Second // Time in sec
	CHECKMATE_VALUE int           = 1000000
	MATE_CUTOFF     int           = CHECKMATE_VALUE / 2
	MAX_DEPTH       int           = 100
	TIMER_CHECK     uint64        = (1 << 10) - 1
)
View Source
const (
	MvvLvaOffset          int = 10000 - 256
	PVMoveScore           int = 65
	FirstKillerMoveScore  int = 10
	SecondKillerMoveScore int = 20
)
View Source
const (
	Window int = 12

	StaticNullMovePruningBaseMargin int = 85
	NMR_Depth_Limit                 int = 2
	FutilityPruningDepthLimit       int = 8
	IID_Depth_Limit                 int = 4
	IID_Depth_Reduction             int = 2
)
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
)
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 DEBUG bool = false

Variables

View Source
var CHESS_OPENINGS map[string][]string = map[string][]string{
	"Start Position":   {},
	"Sicilian Defense": {"e4", "c5"},
	"Italian Game":     {"e4", "e5", "Nf3", "Nc6", "Bc4"},
}
View Source
var ClearFile = [8]chess.Bitboard{}
View Source
var ClearRank = [8]chess.Bitboard{}
View Source
var DoubledPawnMasks [2][64]chess.Bitboard
View Source
var FLIP = [2][64]int{
	{
		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,
	},
	{
		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,
	},
}
View Source
var FutilityMargins = [9]int{
	0,
	100,
	160,
	220,
	280,
	340,
	400,
	460,
	520,
}
View Source
var InnerRingAttackPoints = []int{0, 0, 2, 3, 4, 3}
View Source
var IsolatedPawnMasks [8]chess.Bitboard
View Source
var KingAttackPoints [2]int
View Source
var KingAttackers [2]int
View Source
var KingMoves = [64]chess.Bitboard{}
View Source
var KingZones [2]KingZone
View Source
var KingZonesMasks [64]KingZone
View Source
var KnightMoves = [64]chess.Bitboard{}
View Source
var LateMovePruningMargins = [6]int{
	0,
	8,
	12,
	16,
	20,
	24,
}
View Source
var MaskAntidiagonal = [15]chess.Bitboard{
	0x1,
	0x102,
	0x10204,
	0x1020408,
	0x102040810,
	0x10204081020,
	0x1020408102040,
	0x102040810204080,
	0x204081020408000,
	0x408102040800000,
	0x810204080000000,
	0x1020408000000000,
	0x2040800000000000,
	0x4080000000000000,
	0x8000000000000000,
}
View Source
var MaskDiagonal = [15]chess.Bitboard{
	0x80,
	0x8040,
	0x804020,
	0x80402010,
	0x8040201008,
	0x804020100804,
	0x80402010080402,
	0x8040201008040201,
	0x4020100804020100,
	0x2010080402010000,
	0x1008040201000000,
	0x804020100000000,
	0x402010000000000,
	0x201000000000000,
	0x100000000000000,
}
View Source
var MaskFile = [8]chess.Bitboard{}
View Source
var MaskRank = [8]chess.Bitboard{}
View Source
var Mobility_EG = [7]int{0, 0, 6, 2, 3, 2, 0}
View Source
var Mobility_MG = [7]int{0, 0, 0, 3, 3, 5, 0}
View Source
var OuterRingAttackPoints = []int{0, 0, 1, 1, 0, 1}
View Source
var OutpostMasks [2][64]chess.Bitboard
View Source
var PST_EG = [7][64]int{
	{
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
	},
	{
		-1, 4, -2, 2, 8, 2, 2, 0,
		-1, 14, 10, 8, 12, 14, 21, -3,
		3, 33, 25, 18, 22, 29, 26, 3,
		5, 19, 21, 25, 39, 25, 26, -1,
		-8, 10, 19, 18, 18, 11, 3, -20,
		-8, -11, 1, 4, 8, 0, -10, -24,
		-12, -19, -4, -5, -1, -7, -19, -25,
		-20, -41, -24, -28, -58, -20, -37, -70,
	},
	{
		0, 6, 4, 3, 3, 4, 5, 1,
		-13, 10, 11, 11, 5, 8, 5, -3,
		-6, 7, 9, 11, 15, 22, 14, 4,
		-6, -4, 10, 21, 18, 17, 13, -2,
		-10, 0, 4, 25, 0, -2, 4, -1,
		-12, -10, 0, 3, -8, 2, -12, 0,
		-10, -12, -22, -20, -31, -35, -12, -6,
		-10, -6, -32, -25, -29, -23, -11, -8,
	},
	{
		7, 15, 22, 15, 21, 9, 10, 12,
		5, 20, 19, 25, 30, 10, 15, 15,
		15, 14, 19, 15, 13, 14, 11, 2,
		0, 7, 22, 10, 5, 5, 3, 1,
		-5, -5, 8, -1, -1, 0, -9, -13,
		-13, -16, -11, -17, -9, -22, -7, -26,
		-21, -20, -13, -16, -28, -26, -33, -13,
		-17, -10, -3, -1, -14, -9, -13, -31,
	},
	{
		-8, -5, 7, 6, 0, -2, -9, 0,
		-11, 1, 6, 9, -2, 1, 0, -14,
		6, 12, 7, 0, 11, 17, 17, 17,
		-11, 17, 7, 14, 28, 8, 9, 5,
		-6, 12, 25, 13, 23, 19, -2, -26,
		-17, 8, 9, 18, 17, 1, -6, -9,
		-17, -24, -9, 2, 5, -17, -10, -18,
		-15, -25, -37, -9, -19, -12, -8, -14,
	},
	{
		-22, -1, 6, 2, 0, -4, -6, -3,
		-9, 2, 7, 12, 16, 2, -3, 0,
		4, 4, 27, 24, 26, 10, 17, 0,
		12, 27, 31, 36, 37, 18, 27, -5,
		-14, 2, 31, 34, 36, 30, 3, -12,
		-32, -1, 12, 22, 17, 0, -22, -17,
		-32, -20, -20, -12, -11, -9, -18, -13,
		-19, -67, -28, -18, -37, -34, -45, -18,
	},
	{
		0, 0, 0, 0, 0, 0, 0, 0,
		83, 78, 59, 64, 68, 43, 51, 64,
		44, 35, 23, 10, 20, 19, 50, 21,
		0, -2, -13, -26, -24, -30, -9, -9,
		-16, -13, -33, -40, -34, -32, -22, -30,
		-26, -14, -34, -24, -28, -32, -25, -35,
		-17, -9, -19, -9, -13, -21, -20, -33,
		0, 0, 0, 0, 0, 0, 0, 0,
	},
}
View Source
var PST_MG = [7][64]int{
	{
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0,
	},
	{
		0, 1, 0, 0, 1, 0, 0, 0,
		0, 2, 2, 2, 1, 2, 4, 0,
		0, 8, 7, 4, 2, 6, 6, 0,
		1, 4, 4, 5, 5, 4, 6, -2,
		-5, 2, 2, 6, 0, 2, -2, -8,
		-9, 1, 13, -11, -19, -9, -11, -15,
		3, 2, -13, -25, -26, -22, 10, 2,
		3, 42, 13, -53, -1, -33, 27, 29,
	},
	{
		-10, -1, 5, 6, 1, 4, 6, -4,
		0, -14, 2, 1, 10, 18, -4, 4,
		-9, 1, 3, 18, 31, 29, 31, 27,
		-9, -1, 3, -1, 4, 10, 6, 5,
		-14, -9, -11, -10, -12, -4, 0, 3,
		-10, -11, -7, -14, -4, -5, 3, -2,
		-28, -15, -6, -4, -3, 3, -9, -2,
		-9, -21, -19, -12, -7, -32, -4, -30,
	},
	{
		15, 18, 15, 22, 10, 6, 3, 11,
		11, -1, 15, 28, 19, 14, 6, 16,
		-2, 15, 0, 25, 34, 31, 21, 5,
		-5, -7, -15, 18, 1, -7, -1, 5,
		-32, -28, -30, -15, -29, -26, -4, -33,
		-48, -21, -29, -25, -33, -28, -20, -17,
		-47, -37, -28, -26, -31, -37, -24, -41,
		-42, -35, -29, -25, -25, -39, -34, -35,
	},
	{
		-8, -6, 0, -8, -7, -5, -4, -3,
		-10, -7, -2, 4, 2, 10, -17, -8,
		-6, 21, 6, 29, 26, 20, 33, 15,
		-7, -10, 9, 26, 5, 26, -4, -5,
		-5, -2, -3, 2, 3, -3, 0, 0,
		-12, 2, -3, -5, -6, -9, -2, -4,
		-5, -10, 2, -18, -15, -7, -4, -17,
		-22, -2, -21, -32, -28, -24, -16, -14,
	},
	{
		-38, 0, 5, 5, 0, -5, -3, -3,
		-6, 13, 17, 10, 27, 25, 1, 10,
		-1, 17, 36, 45, 57, 33, 37, 0,
		0, -1, 3, 24, 7, 31, 3, 19,
		-22, 6, -1, -7, 1, 2, 17, -10,
		-29, -16, -12, 2, 0, -7, 3, -15,
		-35, -30, -16, -17, -14, -13, -33, -25,
		-32, -30, -42, -40, -27, -28, -25, -34,
	},
	{
		0, 0, 0, 0, 0, 0, 0, 0,
		30, 41, 55, 56, 44, 25, 15, 15,
		-8, 1, 24, 47, 48, 31, 36, 18,
		-26, -15, -16, -5, 7, 6, 13, -8,
		-38, -23, -23, -16, -11, -10, -2, -16,
		-44, -26, -33, -34, -18, -11, 3, -19,
		-43, -30, -34, -43, -26, -10, 0, -28,
		0, 0, 0, 0, 0, 0, 0, 0,
	},
}
View Source
var PVM_EG = [7]int{0, 20000, 807, 484, 294, 277, 139}
View Source
var PVM_MG = [7]int{0, 20000, 787, 311, 214, 192, 96}

piece value map

View Source
var PassedPawnMasks [2][64]chess.Bitboard
View Source
var PassedPawn_EG = [64]int{
	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 PassedPawn_MG = [64]int{
	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]chess.Bitboard{}
View Source
var PawnPushes = [2][64]chess.Bitboard{}
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 TotalPhase int = phases[chess.Pawn]*16 +
	phases[chess.Knight]*4 +
	phases[chess.Bishop]*4 +
	phases[chess.Rook]*4 +
	phases[chess.Queen]*2
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 FileOf

func FileOf(square uint8) uint8

func GenPolyglotHash

func GenPolyglotHash(pos *chess.Position) (hash uint64)

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

func InitEvalBitboards

func InitEvalBitboards()

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 MVV_LVA

func MVV_LVA(move *chess.Move, board *chess.Board) int

func Max

func Max(x, y int) int

func Min

func Min(x, y int) int

func RankOf

func RankOf(square uint8) uint8

func RunEngine

func RunEngine()

Types

type Engine

type Engine struct {
	EngineClass
	// contains filtered or unexported fields
}

func (*Engine) Add_Zobrist_History

func (e *Engine) Add_Zobrist_History(hash uint64)

adds to zobrist history, which is used for draw detection

func (*Engine) Is_Draw_By_Repetition

func (e *Engine) Is_Draw_By_Repetition(hash uint64) bool

func (*Engine) Remove_Zobrist_History

func (e *Engine) Remove_Zobrist_History()

decrements ply counter, which means history will be overwritten

type EngineClass

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

type EngineCounters

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

type EngineUpgrades

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

type KingZone

type KingZone struct {
	OuterRing chess.Bitboard
	InnerRing chess.Bitboard
}

type PVLine

type PVLine struct {
	Moves []*chess.Move
}

func (PVLine) String

func (pvLine PVLine) String() string

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 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 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 SearchEntry

type SearchEntry struct {
	Hash       uint64
	Depth      int
	Score      int
	Best       chess.Move
	FlagAndAge uint8
}

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

func (*SearchEntry) Get

func (entry *SearchEntry) Get(hash uint64, ply int, depth int, alpha int, beta int) (int, bool, *chess.Move)

func (SearchEntry) GetAge

func (entry SearchEntry) GetAge() uint8

func (SearchEntry) GetDepth

func (entry SearchEntry) GetDepth() int

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 int, best *chess.Move, ply int, depth int, flag, age uint8)

func (*SearchEntry) SetAge

func (entry *SearchEntry) SetAge(age uint8)

func (*SearchEntry) SetFlag

func (entry *SearchEntry) SetFlag(flag uint8)

type TimeManager

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

	TimeForMove int64
	// contains filtered or unexported fields
}

func (*TimeManager) CheckIfTimeIsUp

func (tm *TimeManager) CheckIfTimeIsUp()

func (*TimeManager) ForceStop

func (tm *TimeManager) ForceStop()

func (*TimeManager) IsStopped

func (tm *TimeManager) IsStopped() bool

func (*TimeManager) Setup

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

func (*TimeManager) Start

func (tm *TimeManager) Start()

type TransTable

type TransTable[Entry interface {
	SearchEntry | PerftEntry
	GetHash() uint64
	GetAge() uint8
	GetDepth() int
}] 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 int, 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 UCIEngine

type UCIEngine struct {
	OpeningBook map[uint64][]PolyglotEntry

	OptionUseBook       bool
	OptionBookPath      string
	OptionBookMoveDelay int
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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